How to Deploy a Machine Learning Model: A Step-by-Step Guide
Deploying a machine learning model means making it accessible to real users or systems. A model that remains in a Jupyter notebook provides no value. This tutorial covers a simple, robust deployment path: save your model, expose it via an API, containerize it, and push it to the cloud.
While platforms like SageMaker offer managed deployment, a portable approach using FastAPI and Docker gives you full control and works across all major cloud providers. Let’s walk through each step.
1. Save Your Trained Model
Use Python’s joblib library to serialize your model and preprocessing objects. This checkpoint ensures you can reload the exact same model later.
- Train your model and ensure it performs well on validation data.
- Save the model object and any scalers/encoders using
joblib.dump(). - Keep track of the feature columns and dependencies.
2. Build a REST API
FastAPI is a modern, fast framework for building APIs. Create a main.py file that loads the saved model and exposes a /predict endpoint.
- Define a Pydantic request body to validate incoming data.
- Load the model once at startup to avoid slow predictions.
- Return predictions as clean JSON responses.
3. Containerize with Docker
Docker bundles your code, model, and dependencies into a single image. This guarantees consistency between development and production environments.
- Write a
Dockerfilebased on a slim Python image. - Copy your application files and install dependencies from
requirements.txt. - Expose the port and run
uvicorn main:appas the entry command.
4. Deploy to Your Chosen Platform
Once your image is built, push it to a registry like Docker Hub and deploy it anywhere:
- Use AWS App Runner or Google Cloud Run for serverless simplicity.
- Use Azure Container Instances for a quick start.
- Or use Hugging Face Spaces for the fastest community-facing prototype.
Deployment is a critical skill for any ML engineer. By following these steps—saving, serving, containerizing, and deploying—you turn a static model into a live product that users can query in real time. Start with a local demo, then scale to the cloud as demand grows.