{
"cells": [
{
"cell_type": "markdown",
"id": "63ab391a",
"metadata": {},
"source": [
"# Intro to MLOps using ZenML\n",
"\n",
"## 🌍 Overview\n",
"\n",
"This repository is a minimalistic MLOps project intended as a starting point to learn how to put ML workflows in production. It features: \n",
"\n",
"Follow along this notebook to understand how you can use ZenML to productionalize your ML workflows!\n",
"\n",
""
]
},
{
"cell_type": "markdown",
"id": "8c28b474",
"metadata": {},
"source": [
"# ⌚ Step 1: (Feature engineering) + Training pipeline"
]
},
{
"cell_type": "markdown",
"id": "8e5a76e6-8655-47d5-ab61-015b2d69d720",
"metadata": {},
"source": [
"Lets run the feature engineering pipeline\n",
"\n",
""
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "942a20f9-244b-4761-933e-55989a7377d6",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\u001b[1;35mInitiating a new run for the pipeline: \u001b[0m\u001b[1;36mfeature_engineering\u001b[1;35m.\u001b[0m\n",
"\u001b[1;35mReusing registered version: \u001b[0m\u001b[1;36m(version: 28)\u001b[1;35m.\u001b[0m\n",
"\u001b[1;35mNew model version \u001b[0m\u001b[1;36m7\u001b[1;35m was created.\u001b[0m\n",
"\u001b[1;35mExecuting a new run.\u001b[0m\n",
"\u001b[1;35mUsing user: \u001b[0m\u001b[1;36mhamza@zenml.io\u001b[1;35m\u001b[0m\n",
"\u001b[1;35mUsing stack: \u001b[0m\u001b[1;36mlocal-sagemaker-step-operator-stack\u001b[1;35m\u001b[0m\n",
"\u001b[1;35m model_registry: \u001b[0m\u001b[1;36mmlflow\u001b[1;35m\u001b[0m\n",
"\u001b[1;35m step_operator: \u001b[0m\u001b[1;36msagemaker-eu\u001b[1;35m\u001b[0m\n",
"\u001b[1;35m experiment_tracker: \u001b[0m\u001b[1;36mmlflow\u001b[1;35m\u001b[0m\n",
"\u001b[1;35m container_registry: \u001b[0m\u001b[1;36maws-eu\u001b[1;35m\u001b[0m\n",
"\u001b[1;35m orchestrator: \u001b[0m\u001b[1;36mdefault\u001b[1;35m\u001b[0m\n",
"\u001b[1;35m image_builder: \u001b[0m\u001b[1;36mlocal\u001b[1;35m\u001b[0m\n",
"\u001b[1;35m artifact_store: \u001b[0m\u001b[1;36ms3-zenfiles\u001b[1;35m\u001b[0m\n",
"\u001b[33mCould not import GCP service connector: No module named 'google.api_core'.\u001b[0m\n",
"\u001b[33mCould not import Azure service connector: No module named 'azure.identity'.\u001b[0m\n",
"\u001b[1;35mStep \u001b[0m\u001b[1;36mdata_loader\u001b[1;35m has started.\u001b[0m\n",
"\u001b[1;35mDataset with 541 records loaded!\u001b[0m\n",
"\u001b[1;35mStep \u001b[0m\u001b[1;36mdata_loader\u001b[1;35m has finished in \u001b[0m\u001b[1;36m7.510s\u001b[1;35m.\u001b[0m\n",
"\u001b[1;35mStep \u001b[0m\u001b[1;36mdata_splitter\u001b[1;35m has started.\u001b[0m\n",
"\u001b[1;35mStep \u001b[0m\u001b[1;36mdata_splitter\u001b[1;35m has finished in \u001b[0m\u001b[1;36m11.852s\u001b[1;35m.\u001b[0m\n",
"\u001b[1;35mStep \u001b[0m\u001b[1;36mdata_preprocessor\u001b[1;35m has started.\u001b[0m\n",
"\u001b[1;35mStep \u001b[0m\u001b[1;36mdata_preprocessor\u001b[1;35m has finished in \u001b[0m\u001b[1;36m16.644s\u001b[1;35m.\u001b[0m\n",
"\u001b[1;35mRun \u001b[0m\u001b[1;36mfeature_engineering-2024_01_04-14_23_46_664507\u001b[1;35m has finished in \u001b[0m\u001b[1;36m55.055s\u001b[1;35m.\u001b[0m\n",
"\u001b[1;35mDashboard URL: https://1cf18d95-zenml.cloudinfra.zenml.io/workspaces/default/pipelines/c8c78176-d287-4fa1-ab35-a90f35670aa4/runs/29cb4996-65a5-449a-a53a-f140e971d97c/dag\u001b[0m\n",
"\u001b[1;35mFeature Engineering pipeline finished successfully!\u001b[0m\n",
"\u001b[1;35mThe latest feature engineering pipeline produced the following artifacts: \n",
"\n",
"1. Train Dataset - Name: dataset_trn, Version Name: 191 \n",
"2. Test Dataset: Name: dataset_tst, Version Name: 189\u001b[0m\n"
]
}
],
"source": [
"!python run.py --feature-pipeline"
]
},
{
"cell_type": "markdown",
"id": "87909827",
"metadata": {},
"source": [
"Lets run the training pipeline\n",
"\n",
""
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "fccf1bd9",
"metadata": {
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\u001b[1;35mInitiating a new run for the pipeline: \u001b[0m\u001b[1;36mbreast_cancer_training\u001b[1;35m.\u001b[0m\n",
"\u001b[1;35mReusing registered version: \u001b[0m\u001b[1;36m(version: 6)\u001b[1;35m.\u001b[0m\n",
"\u001b[1;35mNew model version \u001b[0m\u001b[1;36m8\u001b[1;35m was created.\u001b[0m\n",
"\u001b[1;35mExecuting a new run.\u001b[0m\n",
"\u001b[1;35mUsing user: \u001b[0m\u001b[1;36mhamza@zenml.io\u001b[1;35m\u001b[0m\n",
"\u001b[1;35mUsing stack: \u001b[0m\u001b[1;36mlocal-sagemaker-step-operator-stack\u001b[1;35m\u001b[0m\n",
"\u001b[1;35m model_registry: \u001b[0m\u001b[1;36mmlflow\u001b[1;35m\u001b[0m\n",
"\u001b[1;35m step_operator: \u001b[0m\u001b[1;36msagemaker-eu\u001b[1;35m\u001b[0m\n",
"\u001b[1;35m experiment_tracker: \u001b[0m\u001b[1;36mmlflow\u001b[1;35m\u001b[0m\n",
"\u001b[1;35m container_registry: \u001b[0m\u001b[1;36maws-eu\u001b[1;35m\u001b[0m\n",
"\u001b[1;35m orchestrator: \u001b[0m\u001b[1;36mdefault\u001b[1;35m\u001b[0m\n",
"\u001b[1;35m image_builder: \u001b[0m\u001b[1;36mlocal\u001b[1;35m\u001b[0m\n",
"\u001b[1;35m artifact_store: \u001b[0m\u001b[1;36ms3-zenfiles\u001b[1;35m\u001b[0m\n",
"\u001b[33mCould not import GCP service connector: No module named 'google.api_core'.\u001b[0m\n",
"\u001b[33mCould not import Azure service connector: No module named 'azure.identity'.\u001b[0m\n",
"\u001b[1;35mStep \u001b[0m\u001b[1;36mdata_loader\u001b[1;35m has started.\u001b[0m\n",
"\u001b[1;35mDataset with 541 records loaded!\u001b[0m\n",
"\u001b[1;35mStep \u001b[0m\u001b[1;36mdata_loader\u001b[1;35m has finished in \u001b[0m\u001b[1;36m7.566s\u001b[1;35m.\u001b[0m\n",
"\u001b[1;35mStep \u001b[0m\u001b[1;36mdata_splitter\u001b[1;35m has started.\u001b[0m\n",
"\u001b[1;35mStep \u001b[0m\u001b[1;36mdata_splitter\u001b[1;35m has finished in \u001b[0m\u001b[1;36m12.308s\u001b[1;35m.\u001b[0m\n",
"\u001b[1;35mStep \u001b[0m\u001b[1;36mdata_preprocessor\u001b[1;35m has started.\u001b[0m\n",
"\u001b[1;35mStep \u001b[0m\u001b[1;36mdata_preprocessor\u001b[1;35m has finished in \u001b[0m\u001b[1;36m18.092s\u001b[1;35m.\u001b[0m\n",
"\u001b[1;35mCaching \u001b[0m\u001b[1;36mdisabled\u001b[1;35m explicitly for \u001b[0m\u001b[1;36mmodel_trainer\u001b[1;35m.\u001b[0m\n",
"\u001b[1;35mStep \u001b[0m\u001b[1;36mmodel_trainer\u001b[1;35m has started.\u001b[0m\n",
"\u001b[1;35mTraining model DecisionTreeClassifier()...\u001b[0m\n",
"/home/htahir1/.virtualenvs/demo_stack_showcase/lib/python3.8/site-packages/_distutils_hack/__init__.py:33: UserWarning: Setuptools is replacing distutils.\n",
" warnings.warn(\"Setuptools is replacing distutils.\")\n",
"/home/htahir1/.virtualenvs/demo_stack_showcase/lib/python3.8/site-packages/zenml/integrations/mlflow/experiment_trackers/mlflow_experiment_tracker.py:245: FutureWarning: ``mlflow.gluon.autolog`` is deprecated since 2.5.0. This method will be removed in a future release.\n",
" module.autolog(disable=True)\n",
"\u001b[33mFailed to disable MLflow autologging for the following frameworks: ['tensorflow'].\u001b[0m\n",
"\u001b[1;35mStep \u001b[0m\u001b[1;36mmodel_trainer\u001b[1;35m has finished in \u001b[0m\u001b[1;36m15.244s\u001b[1;35m.\u001b[0m\n",
"\u001b[1;35mCaching \u001b[0m\u001b[1;36mdisabled\u001b[1;35m explicitly for \u001b[0m\u001b[1;36mmodel_evaluator\u001b[1;35m.\u001b[0m\n",
"\u001b[1;35mStep \u001b[0m\u001b[1;36mmodel_evaluator\u001b[1;35m has started.\u001b[0m\n",
"\u001b[33mYour artifact was materialized under Python version 'unknown' but you are currently using '3.8.10'. This might cause unexpected behavior since pickle is not reproducible across Python versions. Attempting to load anyway...\u001b[0m\n",
"\u001b[1;35mTrain accuracy=100.00%\u001b[0m\n",
"\u001b[1;35mTest accuracy=92.66%\u001b[0m\n",
"/home/htahir1/.virtualenvs/demo_stack_showcase/lib/python3.8/site-packages/zenml/integrations/mlflow/experiment_trackers/mlflow_experiment_tracker.py:245: FutureWarning: ``mlflow.gluon.autolog`` is deprecated since 2.5.0. This method will be removed in a future release.\n",
" module.autolog(disable=True)\n",
"\u001b[33mFailed to disable MLflow autologging for the following frameworks: ['tensorflow'].\u001b[0m\n",
"\u001b[1;35mImplicitly linking artifact \u001b[0m\u001b[1;36moutput\u001b[1;35m to model \u001b[0m\u001b[1;36mbreast_cancer_classifier\u001b[1;35m version \u001b[0m\u001b[1;36m8\u001b[1;35m.\u001b[0m\n",
"\u001b[1;35mStep \u001b[0m\u001b[1;36mmodel_evaluator\u001b[1;35m has finished in \u001b[0m\u001b[1;36m15.865s\u001b[1;35m.\u001b[0m\n",
"\u001b[1;35mStep \u001b[0m\u001b[1;36mmodel_promoter\u001b[1;35m has started.\u001b[0m\n",
"\u001b[1;35mModel promoted to production!\u001b[0m\n",
"\u001b[1;35mImplicitly linking artifact \u001b[0m\u001b[1;36moutput\u001b[1;35m to model \u001b[0m\u001b[1;36mbreast_cancer_classifier\u001b[1;35m version \u001b[0m\u001b[1;36m8\u001b[1;35m.\u001b[0m\n",
"\u001b[1;35mStep \u001b[0m\u001b[1;36mmodel_promoter\u001b[1;35m has finished in \u001b[0m\u001b[1;36m8.292s\u001b[1;35m.\u001b[0m\n",
"\u001b[1;35mRun \u001b[0m\u001b[1;36mbreast_cancer_training-2024_01_04-14_30_21_437288\u001b[1;35m has finished in \u001b[0m\u001b[1;36m1m51s\u001b[1;35m.\u001b[0m\n",
"\u001b[1;35mDashboard URL: https://1cf18d95-zenml.cloudinfra.zenml.io/workspaces/default/pipelines/bb529fac-e51f-44c0-a2c5-c1a8930f6d44/runs/23510d3a-dbb8-4ebd-904a-6a4ddf8105c5/dag\u001b[0m\n",
"\u001b[1;35mTraining pipeline finished successfully!\u001b[0m\n"
]
}
],
"source": [
"!python run.py --training-pipeline"
]
},
{
"cell_type": "markdown",
"id": "d6306f14",
"metadata": {},
"source": [
"# 🫅 Step 2: The inference pipeline"
]
},
{
"cell_type": "markdown",
"id": "b51f3108",
"metadata": {},
"source": [
"The batch inference pipeline simply takes the model marked as `production` and runs inference on it\n",
"with `live data`. The critical step here is the `inference_predict` step, where we load the model in memory\n",
"and generate predictions:\n",
"\n",
""
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "9918a8a1-c569-494f-aa40-cb7bd3aaea07",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\u001b[1;35m\u001b[0m\u001b[1;36mversion\u001b[1;35m \u001b[0m\u001b[1;36mproduction\u001b[1;35m matches one of the possible \u001b[0m\u001b[1;36mModelStages\u001b[1;35m and will be fetched using stage.\u001b[0m\n",
"\u001b[33mUsing an external artifact as step input currently invalidates caching for the step and all downstream steps. Future releases will introduce hashing of artifacts which will improve this behavior.\u001b[0m\n",
"\u001b[1;35mInitiating a new run for the pipeline: \u001b[0m\u001b[1;36minference\u001b[1;35m.\u001b[0m\n",
"\u001b[1;35mReusing registered version: \u001b[0m\u001b[1;36m(version: 9)\u001b[1;35m.\u001b[0m\n",
"\u001b[1;35mExecuting a new run.\u001b[0m\n",
"\u001b[1;35mUsing user: \u001b[0m\u001b[1;36mhamza@zenml.io\u001b[1;35m\u001b[0m\n",
"\u001b[1;35mUsing stack: \u001b[0m\u001b[1;36mlocal-sagemaker-step-operator-stack\u001b[1;35m\u001b[0m\n",
"\u001b[1;35m model_registry: \u001b[0m\u001b[1;36mmlflow\u001b[1;35m\u001b[0m\n",
"\u001b[1;35m step_operator: \u001b[0m\u001b[1;36msagemaker-eu\u001b[1;35m\u001b[0m\n",
"\u001b[1;35m experiment_tracker: \u001b[0m\u001b[1;36mmlflow\u001b[1;35m\u001b[0m\n",
"\u001b[1;35m container_registry: \u001b[0m\u001b[1;36maws-eu\u001b[1;35m\u001b[0m\n",
"\u001b[1;35m orchestrator: \u001b[0m\u001b[1;36mdefault\u001b[1;35m\u001b[0m\n",
"\u001b[1;35m image_builder: \u001b[0m\u001b[1;36mlocal\u001b[1;35m\u001b[0m\n",
"\u001b[1;35m artifact_store: \u001b[0m\u001b[1;36ms3-zenfiles\u001b[1;35m\u001b[0m\n",
"\u001b[33mCould not import GCP service connector: No module named 'google.api_core'.\u001b[0m\n",
"\u001b[33mCould not import Azure service connector: No module named 'azure.identity'.\u001b[0m\n",
"\u001b[1;35mUsing cached version of \u001b[0m\u001b[1;36mdata_loader\u001b[1;35m.\u001b[0m\n",
"\u001b[1;35mStep \u001b[0m\u001b[1;36mdata_loader\u001b[1;35m has started.\u001b[0m\n",
"\u001b[1;35mStep \u001b[0m\u001b[1;36minference_preprocessor\u001b[1;35m has started.\u001b[0m\n",
"\u001b[33mYour artifact was materialized under Python version 'unknown' but you are currently using '3.8.10'. This might cause unexpected behavior since pickle is not reproducible across Python versions. Attempting to load anyway...\u001b[0m\n",
"\u001b[1;35mStep \u001b[0m\u001b[1;36minference_preprocessor\u001b[1;35m has finished in \u001b[0m\u001b[1;36m8.990s\u001b[1;35m.\u001b[0m\n",
"\u001b[1;35mStep \u001b[0m\u001b[1;36minference_predict\u001b[1;35m has started.\u001b[0m\n",
"\u001b[33mYou specified both an ID as well as a version of the artifact_versions. Ignoring the version and fetching the artifact_versions by ID.\u001b[0m\n",
"\u001b[33mYour artifact was materialized under Python version 'unknown' but you are currently using '3.8.10'. This might cause unexpected behavior since pickle is not reproducible across Python versions. Attempting to load anyway...\u001b[0m\n",
"\u001b[1;35mStep \u001b[0m\u001b[1;36minference_predict\u001b[1;35m has finished in \u001b[0m\u001b[1;36m16.236s\u001b[1;35m.\u001b[0m\n",
"\u001b[1;35mRun \u001b[0m\u001b[1;36minference-2024_01_04-14_44_48_944106\u001b[1;35m has finished in \u001b[0m\u001b[1;36m42.637s\u001b[1;35m.\u001b[0m\n",
"\u001b[1;35mDashboard URL: https://1cf18d95-zenml.cloudinfra.zenml.io/workspaces/default/pipelines/4ac825a0-d89c-4109-91ef-f077327cfe4f/runs/3fab91f4-7c52-497b-9aa4-a491d1f0a60b/dag\u001b[0m\n",
"\u001b[1;35mInference pipeline finished successfully!\u001b[0m\n"
]
}
],
"source": [
"!python run.py --inference-pipeline"
]
},
{
"cell_type": "markdown",
"id": "36140d24-a280-48eb-bb03-5e03280e128c",
"metadata": {},
"source": [
"## Step 3: Deploying the pipeline to Huggingface"
]
},
{
"cell_type": "markdown",
"id": "13bd8087-2ab0-4f9d-8bff-6266a05eb6e7",
"metadata": {},
"source": [
""
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "8000849c-1ce8-4900-846e-3ef1873561f8",
"metadata": {},
"outputs": [],
"source": [
"!python run.py --deployment-pipeline"
]
},
{
"cell_type": "markdown",
"id": "594ee4fc-f102-4b99-bdc3-2f1670c87679",
"metadata": {},
"source": [
"## Congratulations!\n",
"\n",
"You're a legit MLOps engineer now! You trained two models, evaluated them against\n",
"a test set, registered the best one with the ZenML model control plane,\n",
"and served some predictions. You also learned how to iterate on your models and\n",
"data by using some of the ZenML utility abstractions. You saw how to view your\n",
"artifacts and models via the client as well as the ZenML Dashboard.\n",
"\n",
"## Further exploration\n",
"\n",
"This was just the tip of the iceberg of what ZenML can do; check out the [**docs**](https://docs.zenml.io/) to learn more\n",
"about the capabilities of ZenML. For example, you might want to:\n",
"\n",
"- [Deploy ZenML](https://docs.zenml.io/user-guide/production-guide/connect-deployed-zenml) to collaborate with your colleagues.\n",
"- Run the same pipeline on a [cloud MLOps stack in production](https://docs.zenml.io/user-guide/production-guide/cloud-stack).\n",
"- Track your metrics in an experiment tracker like [MLflow](https://docs.zenml.io/stacks-and-components/component-guide/experiment-trackers/mlflow).\n",
"\n",
"## What next?\n",
"\n",
"* If you have questions or feedback... join our [**Slack Community**](https://zenml.io/slack) and become part of the ZenML family!\n",
"* If you want to quickly get started with ZenML, check out the [ZenML Cloud](https://zenml.io/cloud)."
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.10"
}
},
"nbformat": 4,
"nbformat_minor": 5
}