Spaces:
Runtime error
Runtime error
File size: 17,622 Bytes
551af5c 7f1aa39 551af5c ec48b04 551af5c ec48b04 551af5c 7f1aa39 551af5c 7f1aa39 551af5c ec48b04 551af5c ec48b04 551af5c ec48b04 551af5c 7f1aa39 ec48b04 551af5c 7f1aa39 551af5c ec48b04 551af5c ec48b04 551af5c ec48b04 551af5c ec48b04 551af5c ec48b04 551af5c ec48b04 551af5c ec48b04 551af5c |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 |
{
"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",
"<img src=\"_assets/pipeline_overview.png\" width=\"50%\" alt=\"Pipelines Overview\">"
]
},
{
"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",
"<img src=\"_assets/feature_engineering_pipeline.png\" width=\"50%\" alt=\"Training pipeline\">"
]
},
{
"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",
"<img src=\"_assets/training_pipeline.png\" width=\"50%\" alt=\"Training pipeline\">"
]
},
{
"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",
"<img src=\"_assets/inference_pipeline.png\" width=\"45%\" alt=\"Inference pipeline\">"
]
},
{
"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": [
"<img src=\"_assets/deployment_pipeline.png\" width=\"45%\" alt=\"Deployment pipeline\">"
]
},
{
"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
}
|