100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
ML Ops & Data Science in Production
55 minadvanced

Package Model as FastAPI + Docker Service

What You'll Build

In this exercise you will take the XGBoost model promoted to 'Staging' in lesson 31 and wrap it in a production-ready web service. You will create a FastAPI application with /health and /predict endpoints, define Pydantic schemas for cricket player input validation, write a Dockerfile that bundles the application and its dependencies into a portable container, add a docker-compose.yml for convenient local orchestration, build and run the container, and test both endpoints using curl and the httpx async client. By the end, anyone with Docker installed can reproduce the same running service from a single docker compose up command.

Analogy🏏Cricket
🏏 Think of it like cricket: Before the IPL auction, a franchise's analytics team evaluates every player across dozens of trial matches — tracking Rohit Sharma's strike rate in power plays, Virat Kohli's average against pace, MS Dhoni's finishing rate in the death overs, Shubman Gill's consistency across pitches, and Jasprit Bumrah's economy in the middle overs. Each trial is recorded in a shared logbook so the selectors can compare and pick the best combination. MLflow is exactly that shared logbook for your ML experiments — every training run is a trial match, every metric is a scorecard entry, and the Model Registry is the final squad announcement. Keep the auction framing in mind throughout the exercise, because it fixes the discipline the steps teach: a franchise never signs a player off one good highlight reel, and you never register a model off one lucky run — you log every trial, compare them on identical conditions, and promote only with the full scorecard in front of you.

Prerequisites

You need Docker Engine 24+ and Docker Compose v2 installed. The MLflow tracking server from lesson 31 must still be running on localhost:5000 with the ipl-strike-rate-predictor model in Staging. Alternatively, for offline development, export the model artefact with mlflow artifacts download and reference it via a local path. Python 3.11+ and the packages fastapi, uvicorn, pydantic, mlflow, xgboost, httpx, and pytest should be available in a virtual environment for running pre-container tests.

Analogy🏏Cricket
🏏 Think of it like cricket: the kit check before a net session. Just as a batter arriving for practice needs pads, gloves, and a bat they already know how to use — but doesn't need to have faced the new bowling machine before, because today's session is exactly where they'll learn it — this exercise expects you to arrive comfortable with Python, basic scikit-learn (fit, predict, train_test_split), and pandas, while MLflow itself is taught from scratch. Just as the coach insists on a properly prepared practice pitch — a clean, dedicated strip rather than the match square — you need a clean virtual environment or Conda environment where packages can be installed freely. And just as the only outside help needed is the equipment delivery van arriving once before practice, network access is required only for the initial pip install; after that everything runs locally, whether your 'net' is a laptop, a Docker container, or a cloud notebook. The payoff: checking your kit now means the session ahead is pure skill-building, with no stoppages for missing gear.

Setup

python
# Project structure to create before coding
"""
ipl-performance-predictor/
 api/
    __init__.py
    main.py          # FastAPI application
    schemas.py       # Pydantic input/output models
 Dockerfile
 docker-compose.yml
 requirements.txt
"""

import os
os.makedirs("api", exist_ok=True)
open("api/__init__.py", "w").close()
print("Project skeleton created.")

# requirements.txt — exact pins for reproducible builds
requirements = """fastapi==0.111.0
uvicorn[standard]==0.29.0
pydantic==2.7.1
mlflow==2.13.0
xgboost==2.0.3
numpy==1.26.4
httpx==0.27.0
pytest==8.2.0
pytest-asyncio==0.23.7
"""

with open("requirements.txt", "w") as f:
    f.write(requirements)
print("requirements.txt written.")
Lesson 33 of 35
0% complete