Machine LearningLesson 19 of 22

MLOps and Deployment

Training is half the job. Deployment is how users actually get predictions. MLOps covers saving models, serving them, and keeping them healthy in production.

Training is half the job. Deployment is how users actually get predictions. MLOps covers saving models, serving them, and keeping them healthy in production.

You will save a trained pipeline with joblib, wrap it in a FastAPI endpoint, and test it locally. Streamlit lets you build a demo UI in pure Python. Docker makes it portable.

The biggest production pitfall is not the model but the data: distributions drift, and accuracy drops. Monitoring beats retraining blindly.

Save and serve

Save the whole pipeline, not just the model, so preprocessing is included. Serve with FastAPI.

PYTHON
import joblib
joblib.dump(pipe, "model.joblib")
# FastAPI: from fastapi import FastAPI; app = FastAPI()
# @app.post("/predict")
# def predict(x: dict): return {"pred": pipe.predict([x["text"]])[0]}

Docker and Streamlit

Dockerfile copies code and installs deps. Streamlit turns a script into a web app.

PYTHON
# Dockerfile
# FROM python:3.10
# COPY requirements.txt .
# RUN pip install -r requirements.txt
# CMD ["uvicorn", "app:app", "--host", "0.0.0.0"]

Syntax

PYTHON
import joblib
joblib.dump(pipe, "model.joblib")
pipe = joblib.load("model.joblib")
pipe.predict([["new data"]])

Pickle security trap

Never load pickle/joblib from untrusted sources — it can execute code. GitHub security advisory GHSA-... flags this as top MLOps vulnerability. Use joblib only for your own files.

PYTHON
# Never: joblib.load("untrusted.pkl")
# Only load your own model.joblib

Good to know

  • Save pipelines, not just models.
  • FastAPI and Streamlit are quickest to demo.
  • Monitor drift, not just accuracy.

Keep building your ML models

Copy the code, run it locally with pip install scikit-learn pandas, and continue to the next lesson.

→ Next: Project: House Price Predictor