ML Engineer · Step 6 of 8 · 9 min
Shipping and monitoring models
You can wrap a model in an API, package it, and know what to monitor once real users depend on it.
From notebook to service
A notebook is a sketch. To ship, move your code into plain Python files: one to prepare data, one to train, one to predict. Save the trained model to a file, along with the exact preprocessing steps. A scikit-learn Pipeline keeps both together, so training and serving cannot drift apart.
Then put it behind an API. FastAPI is a popular choice: a few lines turn a predict function into a web endpoint that takes JSON and returns a prediction. Not everything needs to be live. A nightly batch job that writes scores to a table is often simpler and enough.
Package it so it runs anywhere
Docker puts your code, the model file and the exact library versions into a container. If it runs on your laptop, it runs the same on a cloud server. Pin your versions; a silent library update can change predictions.
MLOps is the name for the habits that make this repeatable: code and data in version control, automated tests, a pipeline that retrains and checks a model before it replaces the old one, and a record of which model version is live.
Models decay. Monitor them.
The world changes after you train. Prices rise, users behave differently, a new product launches. When the input data shifts, that is data drift. When the link between inputs and the right answer changes, that is concept drift. Both make a good model slowly go wrong, with no error message.
So watch three things: the system (speed, errors, cost), the inputs (do they still look like the training data?) and the outcomes (once true answers arrive, is accuracy holding?). Set alerts, and plan how and when you retrain.
Release carefully
Do not swap models all at once. Run the new one in shadow mode, where it predicts but nobody sees the result, or send it a small share of traffic first. Keep the old version ready so you can roll back in minutes.
Try it
Write your first model API
- 1.Ask a chatbot for the code in the prompt below.
- 2.Run it in a free GitHub Codespace or on your own machine with Python installed.
- 3.Open the /docs page FastAPI creates and send a test prediction from your browser.
Show me a minimal FastAPI app that loads a scikit-learn model saved with joblib and exposes a POST /predict endpoint taking JSON features and returning a prediction. Include a short training script that saves the model, a requirements.txt with pinned versions, and a simple Dockerfile. Explain each file in one sentence.