Skip to content

Commit

Permalink
Updated all syntax to 0.55.0
Browse files Browse the repository at this point in the history
  • Loading branch information
htahir1 committed Jan 24, 2024
1 parent fb052fb commit 8ff87a5
Show file tree
Hide file tree
Showing 20 changed files with 85 additions and 99 deletions.
2 changes: 1 addition & 1 deletion customer-satisfaction/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ settings:
- mlflow

# configuration of the Model Control Plane
model_version:
model:
name: Customer_Satisfaction_Predictor
license: Apache 2.0
description: Predictor of Customer Satisfaction.
Expand Down
2 changes: 1 addition & 1 deletion customer-satisfaction/pipelines/deployment_pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from zenml.integrations.mlflow.steps import mlflow_model_deployer_step

from zenml import pipeline, ModelVersion
from zenml import pipeline

from pipelines.training_pipeline import customer_satisfaction_training_pipeline
from steps import predictor
Expand Down
2 changes: 1 addition & 1 deletion customer-satisfaction/steps/evaluation.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def evaluation(
mlflow.log_metric("rmse", rmse)

# Also add the metrics to the Model within the ZenML Model Control Plane
artifact = get_step_context().model_version.get_artifact("sklearn_regressor")
artifact = get_step_context().model.get_artifact("sklearn_regressor")

log_artifact_metadata(
metadata={
Expand Down
6 changes: 3 additions & 3 deletions customer-satisfaction/steps/model_loader.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from sklearn.base import RegressorMixin
from zenml import step, ModelVersion
from zenml import step, Model
from zenml.client import Client


Expand All @@ -12,9 +12,9 @@ def model_loader(
Args:
model_name: Name of the Model to load
"""
model_version = ModelVersion(
model = Model(
name=model_name,
version="production"
)
model_artifact: RegressorMixin = model_version.load_artifact("sklearn_regressor")
model_artifact: RegressorMixin = model.load_artifact("sklearn_regressor")
return model_artifact
22 changes: 11 additions & 11 deletions customer-satisfaction/steps/model_promoter.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from zenml import get_step_context, step, ModelVersion
from zenml import get_step_context, step, Model
from zenml.logger import get_logger

logger = get_logger(__name__)
Expand All @@ -21,37 +21,37 @@ def model_promoter(
Returns:
Whether the model was promoted or not.
"""
# Get the current model_version produced by the current pipeline
model_version = get_step_context().model_version
# Get the current model produced by the current pipeline
zenml_model = get_step_context().model

# Get the previous model version at the production stage
previous_production_model = ModelVersion(
name=model_version.name,
previous_production_model = Model(
name=zenml_model.name,
version="production"
)

try:
# In case there already is a model version at the correct stage
previous_production_model_version_mse = float(
previous_production_model_mse = float(
previous_production_model.get_artifact("sklearn_regressor").run_metadata["metrics"].value["mse"]
)
except RuntimeError:
# In case no model version has been promoted before,
# default to a threshold value well above the new mse
previous_production_model_version_mse = mse + 1000
previous_production_model_mse = mse + 1000

if mse > previous_production_model_version_mse:
if mse > previous_production_model_mse:
logger.info(
f"Model mean-squared error {mse:.2f} is higher than"
f" the mse of the previous production model "
f"{previous_production_model_version_mse:.2f} ! "
f"{previous_production_model_mse:.2f} ! "
f"Not promoting model."
)
is_promoted = False
else:
logger.info(f"Model promoted to {stage}!")
is_promoted = True
model_version = get_step_context().model_version
model_version.set_stage(stage, force=True)
zenml_model = get_step_context().model
zenml_model.set_stage(stage, force=True)

return is_promoted
43 changes: 19 additions & 24 deletions huggingface-sagemaker/run.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"id": "51690802-31a7-4e6d-9f88-e6457c6c4a96",
"metadata": {},
"source": [
"# Huggingface Model to Sagemaker Endpoint: Automating MLOps with ZenML\r\n",
"# Huggingface Model to Sagemaker Endpoint: Automating MLOps with ZenML\n",
"Deploying Huggingface models to AWS Sagemaker endpoints typically only requires a few lines of code. However, there's a growing demand to not just deploy, but to seamlessly automate the entire flow from training to production with comprehensive lineage tracking. ZenML adeptly fills this niche, providing an end-to-end MLOps solution for Huggingface users wishing to deploy to Sagemaker. Below, we’ll walk through the architecture that ZenML employs to bring a Huggingface model into production with AWS Sagemaker. Of course all of this can be adapted to not just Sagemaker, but any other model deployment service like GCP Vertex or Azure ML Platform.\n",
"\n",
"This blog post showcases one way of using ZenML pipelines to achieve this:\n",
Expand All @@ -30,16 +30,13 @@
"import numpy as np\n",
"from datasets import DatasetDict, load_dataset\n",
"from typing_extensions import Annotated\n",
"from zenml import step\n",
"from zenml import step, pipeline, Model\n",
"from zenml.logger import get_logger\n",
"\n",
"import os\n",
"from typing import Optional\n",
"from datetime import datetime as dt\n",
"\n",
"from zenml import pipeline\n",
"from zenml.model import ModelConfig\n",
"\n",
"from steps import (\n",
" data_loader,\n",
" notify_on_failure,\n",
Expand Down Expand Up @@ -68,9 +65,9 @@
"metadata": {},
"source": [
"# 🍳Breaking it down\n",
"\r\n",
"\r\n",
"\r\n"
"\n",
"\n",
"\n"
]
},
{
Expand Down Expand Up @@ -250,12 +247,10 @@
"\n",
"# This executes all steps in the pipeline in the correct order using the orchestrator\n",
"# stack component that is configured in your active ZenML stack.\n",
"model_config = ModelConfig(\n",
"zenml_model = Model(\n",
" name=zenml_model_name,\n",
" license=\"Apache 2.0\",\n",
" description=\"Show case Model Control Plane.\",\n",
" create_new_model_version=True,\n",
" delete_new_version_on_failure=True,\n",
" tags=[\"sentiment_analysis\", \"huggingface\"],\n",
")\n",
"\n",
Expand All @@ -265,7 +260,7 @@
" pipeline_args[\"enable_cache\"] = False\n",
"\n",
"# Execute Feature Engineering Pipeline\n",
"pipeline_args[\"model_config\"] = model_config\n",
"pipeline_args[\"model\"] = zenml_model\n",
"pipeline_args[\"config_path\"] = os.path.join(\"configs\", \"feature_engineering_config.yaml\")\n",
"run_args_feature = {\n",
" \"max_seq_length\": max_seq_length,\n",
Expand Down Expand Up @@ -299,7 +294,7 @@
"id": "78ab8771-4421-4975-a3d5-12892a56b805",
"metadata": {},
"source": [
"## 💪 Step 2: Train the model with Huggingface Hub as the model registry\r\n",
"## 💪 Step 2: Train the model with Huggingface Hub as the model registry\n",
" "
]
},
Expand Down Expand Up @@ -342,7 +337,7 @@
"# run_args_train[\"tokenizer_artifact_id\"] = latest_run.steps['tokenizer_loader'].output.id\n",
"\n",
"# Configure the model\n",
"pipeline_args[\"model_config\"] = model_config\n",
"pipeline_args[\"model\"] = zenml_model\n",
"\n",
"pipeline_args[\n",
" \"run_name\"\n",
Expand Down Expand Up @@ -400,7 +395,7 @@
"id": "be79f454-a45d-4f5f-aa93-330d52069124",
"metadata": {},
"source": [
"## 🫅 Step 3: Promote the model to production\r\n"
"## 🫅 Step 3: Promote the model to production\n"
]
},
{
Expand Down Expand Up @@ -431,10 +426,10 @@
"outputs": [],
"source": [
"run_args_promoting = {}\n",
"model_config = ModelConfig(name=zenml_model_name)\n",
"zenml_model = Model(name=zenml_model_name)\n",
"pipeline_args[\"config_path\"] = os.path.join(\"configs\", \"promoting_config.yaml\")\n",
"\n",
"pipeline_args[\"model_config\"] = model_config\n",
"pipeline_args[\"model\"] = zenml_model\n",
"\n",
"pipeline_args[\n",
" \"run_name\"\n",
Expand All @@ -458,7 +453,7 @@
"id": "6efc4968-35fd-42e3-ba62-d8e1557aa0d6",
"metadata": {},
"source": [
"## 💯 Step 4: Deploy the model to AWS Sagemaker Endpoints\r\n"
"## 💯 Step 4: Deploy the model to AWS Sagemaker Endpoints\n"
]
},
{
Expand Down Expand Up @@ -491,11 +486,11 @@
"pipeline_args[\"config_path\"] = os.path.join(\"configs\", \"deploying_config.yaml\")\n",
"\n",
"# Deploying pipeline has new ZenML model config\n",
"model_config = ModelConfig(\n",
"zenml_model = Model(\n",
" name=zenml_model_name,\n",
" version=ModelStages.PRODUCTION,\n",
")\n",
"pipeline_args[\"model_config\"] = model_config\n",
"pipeline_args[\"model\"] = zenml_model\n",
"pipeline_args[\"enable_cache\"] = False\n",
"run_args_deploying = {}\n",
"pipeline_args[\n",
Expand All @@ -520,10 +515,10 @@
"id": "594ee4fc-f102-4b99-bdc3-2f1670c87679",
"metadata": {},
"source": [
"ZenML builds upon the straightforward deployment capability of Huggingface models to AWS Sagemaker, and transforms it into a sophisticated, repeatable, and transparent MLOps workflow. It takes charge of the intricate steps necessary for modern ML systems, ensuring that software engineering leads can focus on iteration and innovation rather than operational intricacies.\r\n",
"\r\n",
"To delve deeper into each stage, refer to the comprehensive guide on GitHub[: zenml-io/zenml-huggingface-sagemak](https://github.com/zenml-io/zenml-huggingface-sagemaker)er. Additionally[, this YouTube playli](https://www.youtube.com/watch?v=Q1EH2H8Akgo&list=PLhNrLW_IWplw6dBbmGcL828-atJMu3CwF)st provides a detailed visual walkthrough of the entire pipeline: Huggingface to Sagemaker ZenML tutorial.\r\n",
"\r\n",
"ZenML builds upon the straightforward deployment capability of Huggingface models to AWS Sagemaker, and transforms it into a sophisticated, repeatable, and transparent MLOps workflow. It takes charge of the intricate steps necessary for modern ML systems, ensuring that software engineering leads can focus on iteration and innovation rather than operational intricacies.\n",
"\n",
"To delve deeper into each stage, refer to the comprehensive guide on GitHub[: zenml-io/zenml-huggingface-sagemak](https://github.com/zenml-io/zenml-huggingface-sagemaker)er. Additionally[, this YouTube playli](https://www.youtube.com/watch?v=Q1EH2H8Akgo&list=PLhNrLW_IWplw6dBbmGcL828-atJMu3CwF)st provides a detailed visual walkthrough of the entire pipeline: Huggingface to Sagemaker ZenML tutorial.\n",
"\n",
"Interested in standardizing your MLOps workflows? ZenML Cloud is now available to all - get a managed ZenML server with important features such as RBAC and pipeline trigge[rs. Book a ](https://zenml.io/book-a-demo)demo with us now to learn how you can create your own MLOps pipelines today."
]
}
Expand Down
18 changes: 8 additions & 10 deletions huggingface-sagemaker/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
from zenml.client import Client
from zenml.enums import ModelStages
from zenml.logger import get_logger
from zenml.model.model_version import ModelVersion
from zenml import Model

from pipelines import (
sentinment_analysis_deploy_pipeline,
Expand Down Expand Up @@ -200,12 +200,10 @@ def main(
os.path.dirname(os.path.realpath(__file__)),
"configs",
)
model_version = ModelVersion(
zenml_model = Model(
name=zenml_model_name,
license="Apache 2.0",
description="Show case Model Control Plane.",
create_new_model_version=True,
delete_new_version_on_failure=True,
tags=["sentiment_analysis", "huggingface"],
)

Expand All @@ -216,7 +214,7 @@ def main(

# Execute Feature Engineering Pipeline
if feature_pipeline:
pipeline_args["model_version"] = model_version
pipeline_args["model"] = zenml_model
pipeline_args["config_path"] = os.path.join(config_folder, "feature_engineering_config.yaml")
run_args_feature = {
"max_seq_length": max_seq_length,
Expand Down Expand Up @@ -259,7 +257,7 @@ def main(
run_args_train["dataset_artifact_id"] = tokenized_dataset_artifact.id
run_args_train["tokenizer_artifact_id"] = tokenized_tokenizer_artifact.id

pipeline_args["model_version"] = model_version
pipeline_args["model"] = zenml_model

pipeline_args[
"run_name"
Expand All @@ -274,13 +272,13 @@ def main(
if promoting_pipeline:
run_args_promoting = {}
# Promoting pipeline always check latest version
model_version = ModelVersion(
zenml_model = Model(
name=zenml_model_name,
version=ModelStages.LATEST,
)
pipeline_args["config_path"] = os.path.join(config_folder, "promoting_config.yaml")

pipeline_args["model_version"] = model_version
pipeline_args["model"] = zenml_model

pipeline_args[
"run_name"
Expand All @@ -294,11 +292,11 @@ def main(
pipeline_args["config_path"] = os.path.join(config_folder, "deploying_config.yaml")

# Deploying pipeline has new ZenML model config
model_version = ModelVersion(
zenml_model = Model(
name=zenml_model_name,
version=ModelStages.PRODUCTION,
)
pipeline_args["model_version"] = model_version
pipeline_args["model"] = zenml_model
pipeline_args["enable_cache"] = False
run_args_deploying = {}
pipeline_args[
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def deploy_hf_to_sagemaker(
# Otherwise, use the provided values.
if repo_id is None or revision is None:
context = get_step_context()
mv = context.model_version
mv = context.model
deployment_metadata = mv.get_data_artifact(name="huggingface_url").run_metadata
repo_id = deployment_metadata["repo_id"].value
revision = deployment_metadata["revision"].value
Expand Down
6 changes: 3 additions & 3 deletions huggingface-sagemaker/steps/deploying/save_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ def save_model_to_deploy():
f" Loading latest version of the model for stage {pipeline_extra['target_env']}..."
)
# Get the current model version
latest_version = get_step_context().model_version
current_zenml_model = get_step_context().model

# Load model and tokenizer from Model Control Plane
model = latest_version.load_artifact(name="model")
tokenizer = latest_version.load_artifact(name="tokenizer")
model = current_zenml_model.load_artifact(name="model")
tokenizer = current_zenml_model.load_artifact(name="tokenizer")
# Save the model and tokenizer locally
model_path = "./gradio/" # replace with the actual path
tokenizer_path = "./gradio/" # replace with the actual path
Expand Down
18 changes: 7 additions & 11 deletions huggingface-sagemaker/steps/promotion/promote_get_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,6 @@

logger = get_logger(__name__)

model_registry = Client().active_stack.model_registry


@step
def promote_get_metrics() -> (
Tuple[
Expand All @@ -54,24 +51,23 @@ def promote_get_metrics() -> (
zenml_client = Client()

# Get current model version metric in current run
model_version = get_step_context().model_version
current_version = model_version._get_model_version()
current_metrics = current_version.get_model_artifact("model").run_metadata["metrics"].value
current_zenml_model = get_step_context().model
current_metrics = current_zenml_model.get_model_artifact("model").run_metadata["metrics"].value
logger.info(f"Current model version metrics are {current_metrics}")

# Get latest saved model version metric in target environment
try:
latest_version = zenml_client.get_model_version(
model_name_or_id=model_version.name,
latest_zenml_model = zenml_client.get_model_version(
model_name_or_id=current_zenml_model.name,
model_version_name_or_number_or_id=ModelStages(
pipeline_extra["target_env"]
),
)
except KeyError:
latest_version = None
if latest_version:
latest_zenml_model = None
if latest_zenml_model:
latest_metrics = (
latest_version.get_model_artifact("model").run_metadata["metrics"].value
latest_zenml_model.get_model_artifact("model").run_metadata["metrics"].value
)
logger.info(f"Current model version metrics are {latest_metrics}")
else:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,8 @@ def promote_metric_compare_promoter(
should_promote = False

if should_promote:
model_version = get_step_context().model_version
model_version = model_version._get_model_version()
model_version.set_stage(pipeline_extra["target_env"], force=True)
zenml_model = get_step_context().model
zenml_model.set_stage(pipeline_extra["target_env"], force=True)

logger.info(
f"Promoted current model version to {pipeline_extra['target_env']} environment"
Expand Down
Loading

0 comments on commit 8ff87a5

Please sign in to comment.