Introduction
After training the model but not putting it into use, the value remains in the notebook. This article helps you package the model into a simple service using FastAPI and Docker, enough for internal demo or as a foundation for real deployment.
Lesson objectives
- Serialize the model properly.
- Create an API that receives input and returns prediction.
- Package the service using Docker.
Minimum required output
- Saved model file, for example with joblib.
- File app FastAPI.
- File requirements or equivalent.
- Dockerfile to run the service stably.
Proposed procedure
- Train model and save artifact.
- Define input or output schema.
- Write endpoint /predict.
- Test locally with sample request.
- Containerize using Docker.
FastAPI framework code
from fastapi import FastAPI
from pydantic import BaseModel
import joblib
import pandas as pd
Things to think about when going out into the real environment
- Logging requests and responses at an appropriate level.
- Manage model versions.
- Validation of input more carefully.
- Timeout, retry and monitoring.
Common mistakes
- Save the model but forget the preprocessing pipeline.
- API Schema does not match training data.
- Test with beautiful data, do not test error input.
Practice exercises
- Package the churn or housing model into an API.
- Create 3 sample requests: valid, missing fields, wrong data type.
- Write a short README describing how to run locally using Docker.
Completion criteria
- There is a predict API that can run locally.
- Docker build and run successfully.
- Schema input is clear enough for others to call the API.
Practice step by step (advanced)
- Standardize input/output schema with Pydantic.
- Package model + preprocessing into versioned artifact.
- Write endpoint predict and health check.
- Add basic logging and clear error handling.
- Build Docker image and run smoke test using curl.
Artifact should be submitted
- API source code and Dockerfile can be run.
- Example request/response for 3 situations.
- Minimum local deployment README.
Self-test questions
- Why is it necessary to version the model artifact?
- If the input schema changes, how will backward compatibility handle it?
- Which runtime metrics need to be monitored right from the start?