Skip to content

Chapter 15: MLOps & Model Deployment

Take a model from notebook to production—package with joblib, serve with FastAPI, containerize, build a model registry, design CI/CD with eval gates, and monitor drift, latency, and errors in production.


Metadata

Field Value
Track Practitioner
Time 8 hours
Prerequisites Chapters 1–14

Learning Objectives

  • Package ML models for production: serialize sklearn pipelines, freeze deps, define typed I/O
  • Serve models behind an HTTP API with FastAPI (/predict, /health, /version, batching)
  • Containerize and reason about deployments: Dockerfile layers, image size, health/readiness probes
  • Track experiments and manage a model registry with stages and promotion gates
  • Design CI/CD for ML: lint → test → train → eval → register → deploy with quality gates
  • Monitor models in production: PSI / KS drift, latency, errors, A/B tests, canary, rollback

What's Included

Notebooks

Notebook Description
01_packaging_serving.ipynb Lifecycle, joblib, Pydantic, FastAPI app + TestClient, Dockerfile, health checks
02_pipelines_cicd.ipynb sklearn Pipeline, reproducibility, tracking, registry, GitHub Actions, the data/code/model triplet
03_advanced_mlops.ipynb Drift (PSI / KS), Evidently sketch, A/B and canary, observability, scaling, capstone

Scripts

  • config.py — Chapter config, registry paths, optional-integration flags
  • deployment.py — FastAPI service factory, Pydantic schemas, batching helpers
  • registry.py — File-backed model registry with stage transitions (None / Staging / Production / Archived)
  • monitoring.py — Drift (PSI, KS), latency percentiles, structured JSON logs

Exercises

  • Problem Set 1 (notebook) — Package a model with joblib, write a Pydantic schema, build /predict, write a Dockerfile, batch predictions, add /version
  • Problem Set 2 (notebook) — Detect drift via PSI, implement a canary splitter, write a CI YAML with eval gates, build a tiny registry, structured logging middleware, rollback policy
  • Solutions — In exercises/solutions/ (notebooks and solutions.py for CI)

Diagrams (Mermaid)

  • mlops_lifecycle.mermaid, deployment_architecture.mermaid, monitoring_pipeline.mermaid

Read Online

  • 15.1 Introduction — Lifecycle, joblib, Pydantic, FastAPI, Dockerfile, health checks
  • 15.2 Intermediate — Pipelines, reproducibility, tracking, registry, GitHub Actions CI
  • 15.3 Advanced — Drift detection, A/B & canary, observability, scaling, capstone

Or try the code in the Playground.

How to Use This Chapter

Quick Start

Follow these steps to get coding in minutes.

1. Clone and install dependencies

git clone https://github.com/luigipascal/berta-chapters.git
cd berta-chapters
pip install -r requirements.txt

2. Navigate to the chapter

cd chapters/chapter-15-mlops-and-model-deployment
pip install -r requirements.txt

3. (Optional) Install MLflow / Evidently / Prometheus

pip install mlflow evidently prometheus-client bentoml
# All notebooks fall back to local implementations if these are missing.

4. Launch Jupyter

jupyter notebook notebooks/01_packaging_serving.ipynb

GitHub Folder

All chapter materials live in: chapters/chapter-15-mlops-and-model-deployment/


Created by Luigi Pascal Rondanini | Generated by Berta AI