π Model Deployment
π Introduction
Once a machine learning model has been trained and tested, the next step is to deploy it so others can use it β for example, via a web app or an API.
Model deployment is the process of making your model available in a production environment for real-world use.
Common ways to deploy models include:
- Building APIs using Flask or FastAPI
- Creating interactive web apps with Streamlit
- Saving/loading models using joblib or pickle
π Building APIs with Flask or FastAPI
1. Flask (Lightweight Web Framework)
Flask allows you to expose your model through a RESTful API that can receive input and return predictions.
Example: Model Deployment with Flask
from flask import Flask, request, jsonify
import joblib
# Initialize Flask app
app = Flask(__name__)
# Load trained model
model = joblib.load("model.pkl")
# Define route for predictions
@app.route("/predict", methods=["POST"])
def predict():
data = request.get_json() # Input JSON data
prediction = model.predict([data["features"]]).tolist()
return jsonify({"prediction": prediction})
if __name__ == "__main__":
app.run(debug=True)
To test the API:
Send a POST request using curl or Postman:
curl -X POST -H "Content-Type: application/json" -d '{"features": [5.1, 3.5, 1.4, 0.2]}' http://127.0.0.1:5000/predict
2. FastAPI (Modern, Faster Alternative)
FastAPI is a high-performance framework for building APIs quickly. It automatically generates interactive API documentation (Swagger UI).
Example: Model Deployment with FastAPI
from fastapi import FastAPI
from pydantic import BaseModel
import joblib
# Define input schema
class InputData(BaseModel):
features: list
# Initialize FastAPI app
app = FastAPI()
# Load model
model = joblib.load("model.pkl")
# Define route
@app.post("/predict")
def predict(data: InputData):
prediction = model.predict([data.features]).tolist()
return {"prediction": prediction}
Run the app:
uvicorn app:app --reload
Access automatic documentation:
- Swagger UI: http://127.0.0.1:8000/docs
- ReDoc: http://127.0.0.1:8000/redoc
π₯οΈ Deploying Models with Streamlit
Concept
Streamlit is a Python framework that allows you to turn models and data scripts into interactive web apps β all with minimal code.
Itβs ideal for data demos, dashboards, or lightweight deployments.
Example: Streamlit App
import streamlit as st
import joblib
import numpy as np
# Load trained model
model = joblib.load("model.pkl")
st.title("πΈ Iris Flower Classifier")
# Input features
sepal_length = st.number_input("Sepal Length", 0.0, 10.0, 5.1)
sepal_width = st.number_input("Sepal Width", 0.0, 10.0, 3.5)
petal_length = st.number_input("Petal Length", 0.0, 10.0, 1.4)
petal_width = st.number_input("Petal Width", 0.0, 10.0, 0.2)
if st.button("Predict"):
features = np.array([[sepal_length, sepal_width, petal_length, petal_width]])
prediction = model.predict(features)
st.success(f"Predicted Class: {prediction[0]}")
Run the app:
streamlit run app.py
Features of Streamlit:
- Instant UI for model input/output
- Real-time updates
- Easy integration with machine learning models
πΎ Saving and Loading Models with Joblib
Concept
To deploy a model, you must save it after training so it can be loaded later without retraining.
Libraries like joblib or pickle allow you to serialize and deserialize (save/load) models efficiently.
Example: Saving a Model
from sklearn.ensemble import RandomForestClassifier
from sklearn.datasets import load_iris
import joblib
# Train a model
X, y = load_iris(return_X_y=True)
model = RandomForestClassifier()
model.fit(X, y)
# Save model to file
joblib.dump(model, "model.pkl")
print("Model saved successfully!")
Example: Loading a Model
import joblib
# Load saved model
model = joblib.load("model.pkl")
print("Model loaded successfully!")
# Make predictions
pred = model.predict([[5.1, 3.5, 1.4, 0.2]])
print("Prediction:", pred)
Advantages of joblib:
- Optimized for large numpy arrays
- Fast and efficient for model storage
- Easy to integrate with scikit-learn and deployment workflows
βοΈ Deployment Options
After building APIs or Streamlit apps, models can be deployed to cloud or hosting platforms such as:
- AWS Elastic Beanstalk / Lambda
- Google Cloud Run / App Engine
- Heroku (simple and free tier for small apps)
- Streamlit Cloud (for sharing Streamlit apps easily)
π§ Summary
| Concept | Description | Tools |
|---|---|---|
| Flask / FastAPI | Create APIs for model inference | Flask, FastAPI |
| Streamlit | Build interactive web apps for ML models | Streamlit |
| Model Saving/Loading | Persist models for deployment | joblib, pickle |
| Cloud Deployment | Host models online for users | AWS, GCP, Heroku |
Model deployment bridges the gap between data science and real-world applications.
By mastering Flask, FastAPI, Streamlit, and joblib, you can transform a trained model into a usable, scalable product accessible to everyone.