Course Content
Model Deployment
Deploying ML models to production environments
Introduction
After training your machine learning model, the next step is to deploy it so others can use it in real-world applications. Deployment involves serving your model as an API so it can receive input data and return predictions.
Why Deploy a Model?
β
Allow users and applications to use your model easily.
β
Integrate machine learning into production systems.
β
Automate predictions for workflows and products.
Tools for Deployment
Popular options include:
- FastAPI (lightweight, Python-based, easy to use)
- Flask
- Docker (for containerization)
- Cloud services (AWS, GCP, Azure)
In this tutorial, weβll use FastAPI for simplicity.
Example: Deploying a Scikit-Learn Model with FastAPI
1οΈβ£ Install Dependencies
In your project environment, install FastAPI and Uvicorn:
pip install fastapi uvicorn scikit-learn pandas
2οΈβ£ Train and Save Your Model
First, train and save your model using scikit-learn:
# train_model.py
import pandas as pd
from sklearn.linear_model import LinearRegression
import joblib
data = {
'Hours': [1, 2, 3, 4, 5],
'Scores': [10, 20, 30, 40, 50]
}
df = pd.DataFrame(data)
X = df[['Hours']]
y = df['Scores']
model = LinearRegression()
model.fit(X, y)
# Save the model
joblib.dump(model, 'linear_model.pkl')
Run this script to save linear_model.pkl
.
3οΈβ£ Create the FastAPI App
# app.py
from fastapi import FastAPI
from pydantic import BaseModel
import joblib
app = FastAPI()
# Load the trained model
model = joblib.load('linear_model.pkl')
# Request schema
class InputData(BaseModel):
Hours: float
@app.post("/predict")
def predict(data: InputData):
prediction = model.predict([[data.Hours]])
return {"prediction": prediction[0]}
4οΈβ£ Run the FastAPI App
Run the server using Uvicorn:
uvicorn app:app --reload
Your API will be available at http://127.0.0.1:8000
.
5οΈβ£ Test Your API
You can test your deployment using:
- The interactive docs at
http://127.0.0.1:8000/docs
- Sending a POST request using curl or Postman:
curl -X POST "http://127.0.0.1:8000/predict" -H "Content-Type: application/json" -d '{"Hours": 6}'
Conclusion
π You have successfully:
β
Trained and saved a machine learning model.
β
Created a FastAPI application to serve your model.
β
Sent requests to get predictions from your deployed model.
Whatβs Next?
- Explore Docker to containerize your FastAPI app for easier deployment.
- Deploy to cloud platforms (AWS, GCP, Azure) for public access.
- Learn about model monitoring to track performance in production.
Join our SuperML Community to share your deployment projects and get feedback!