Deploy trained models through containerized FastAPI endpoints. Implement statistical validation checks to monitor incoming data stream distributions for drift.
The diagram below displays the containerized MLOps architecture. The serialized model is deployed inside a FastAPI Docker container, exposing prediction endpoints. Incoming request inputs are monitored by a drift detection script to compare distributions against training data baseline metrics.
2. Part 1: Step-by-Step Action Items & Key Execution Steps
STEP 1
Activate Python Virtual Environment
Point the terminal execution environment to the course conda sandbox environment.
$ conda activate ds_ai_ml
This targets active python libraries to the isolated virtual sandbox.
STEP 2
Create Project Folders inside Linux VM
Create a dedicated folder for the project files inside your guest VM home folder directory.
$ mkdir -p ~/Projects/mlops_pipeline && cd ~/Projects/mlops_pipeline
This sets up the working directory layout for the MLOps code files.
STEP 3
Install FastAPI and Uvicorn via Pip
Install the required API serving packages inside the active conda session.
This returns the prediction response JSON containing prediction label classes.
STEP 8
Run the Data Drift Monitor
Execute the drift detection script to identify distribution shifts in production features.
$ python monitor_drift.py
This runs statistical Kolmogorov-Smirnov tests and prints drift flags to the console.
3. MLOps Execution Flow
The flowchart below outlines the MLOps pipeline execution flow. It details the steps from training and serializing the model to exposing endpoints via FastAPI and containerizing the app with Docker.
4. Part 2: Complete Deliverable Assets & Production Templates
To run the MLOps serving and monitoring setup, we need the FastAPI script, Dockerfile config, and drift detection script. Below is a line-by-line explanation of the code, followed by the combined template files.
Step-by-Step Code Construction
Lines 1 - 8
Import API and schema modules
Include system packages, FastAPI endpoints, Pydantic schemas, and model loading utilities in the script.
from fastapi import FastAPI
from pydantic import BaseModel
import joblib
import pandas as pd
import numpy as np
These imports pull core FastAPI classes, Pydantic model configurations, and joblib model deserializers.
Lines 9 - 18
Define Input Data Schema
Define a Pydantic model class to validate the structured request JSON payloads submitted by clients.
class PredictionInput(BaseModel):
age: float
salary: float
city: str
This sets up schema verification rules to enforce correct input types for age, salary, and city features.
Lines 19 - 30
FastAPI Endpoints
Expose `/predict` POST endpoints and `/health` GET check endpoints in the FastAPI app class instance.