Chapter 9: Machine Learning Deployment
Deploy ML models using APIs, Flask, Streamlit and cloud platforms. Publish real-world AI solutions for business and public users.
Model
API
Cloud
AI App
9.1 Chapter Overview
Training a Machine Learning model is only one part of the AI development journey. A model inside Jupyter Notebook cannot help real users unless it is deployed. Machine Learning deployment means making a trained model available through an application, API, dashboard, cloud service or business system.
Deployment allows users to enter new data and receive predictions in real time. For example, a student performance model can be deployed as a web application where trainers enter attendance and marks to predict whether a student is likely to pass.
9.2 Machine Learning Deployment Lifecycle
| Stage | Purpose | Example |
|---|---|---|
| Model Training | Build model using historical data. | Train student pass predictor. |
| Model Evaluation | Check model accuracy and reliability. | Accuracy, confusion matrix, F1 score. |
| Model Saving | Store trained model for reuse. | student_model.pkl |
| Application Development | Create interface or API. | Flask API or Streamlit dashboard. |
| Testing | Check predictions with sample inputs. | POST JSON request. |
| Deployment | Publish to server or cloud. | Render, Railway, AWS, Azure. |
| Monitoring | Track performance and errors. | Logs, model drift, feedback. |
9.3 Deployment Architecture
Input
or API
Model
Output
9.4 Saving Trained Machine Learning Models
After training a model, we save it so that we do not need to retrain it every time the application runs. Common tools for saving models are Pickle and Joblib.
| Tool | Use | Suitable For |
|---|---|---|
| Pickle | General Python object saving. | Simple models and objects. |
| Joblib | Efficient model saving. | Scikit-learn models and large arrays. |
Complete Example: Train and Save Student Model
import pandas as pd
from sklearn.linear_model import LogisticRegression
import joblib
# Create training dataset.
data = {
"Attendance": [90, 80, 45, 55, 95, 35, 85, 60],
"Marks": [85, 78, 40, 50, 90, 30, 82, 58],
"Pass": [1, 1, 0, 0, 1, 0, 1, 0]
}
# Convert dictionary to DataFrame.
df = pd.DataFrame(data)
# Select input features.
X = df[["Attendance", "Marks"]]
# Select target label.
y = df["Pass"]
# Create model.
model = LogisticRegression()
# Train model.
model.fit(X, y)
# Save trained model to file.
joblib.dump(model, "student_model.pkl")
print("Model saved successfully.")Line-by-Line Explanation
| Code | Explanation |
|---|---|
| import joblib | Imports the library used to save and load models. |
| X = df[["Attendance", "Marks"]] | Selects input features. |
| y = df["Pass"] | Selects output target label. |
| model.fit(X, y) | Trains the model. |
| joblib.dump(...) | Saves the trained model as a .pkl file. |
9.5 Loading a Saved Model
import joblib
# Load saved model from file.
model = joblib.load("student_model.pkl")
# Make prediction using new input.
prediction = model.predict([[90, 85]])
print("Prediction:", prediction[0])Prediction: 1
Meaning: 1 usually represents Pass and 0 represents Fail.
9.6 What is an API?
API stands for Application Programming Interface. In ML deployment, an API allows other applications to send data to the model and receive predictions.
| Component | Role |
|---|---|
| Client | Website, mobile app or dashboard sending request. |
| API Endpoint | URL where request is sent. |
| Request | Input data sent to API. |
| Model | Predicts result using input data. |
| Response | Prediction returned to client. |
JSON Request Example
{
"attendance": 90,
"marks": 85
}JSON Response Example
{
"prediction": 1,
"result": "Pass"
}9.7 Interactive API Prediction Simulator
This simulator shows how a prediction API receives input and returns a response.
9.8 Flask for ML Deployment
Flask is a lightweight Python web framework. It is commonly used to create APIs for Machine Learning models.
Basic Flask Application
from flask import Flask
app = Flask(__name__)
@app.route("/")
def home():
return "PDTC AI API Running"
if __name__ == "__main__":
app.run(debug=True)| Code | Explanation |
|---|---|
| Flask(__name__) | Creates a Flask application. |
| @app.route("/") | Defines the home endpoint. |
| home() | Function that runs when endpoint is opened. |
| app.run(debug=True) | Starts the development server. |
9.9 Flask Prediction API
from flask import Flask, request, jsonify
import joblib
app = Flask(__name__)
model = joblib.load("student_model.pkl")
@app.route("/predict", methods=["POST"])
def predict():
data = request.json
attendance = data["attendance"]
marks = data["marks"]
prediction = model.predict([[attendance, marks]])
if int(prediction[0]) == 1:
result = "Pass"
else:
result = "Fail"
return jsonify({
"prediction": int(prediction[0]),
"result": result
})
if __name__ == "__main__":
app.run(debug=True)9.10 Flask Project Folder Structure
student_prediction_api/ │ ├── app.py ├── student_model.pkl ├── requirements.txt ├── templates/ │ └── index.html ├── static/ │ └── style.css └── README.md
| File / Folder | Purpose |
|---|---|
| app.py | Main Flask application. |
| student_model.pkl | Saved ML model. |
| requirements.txt | List of required Python libraries. |
| templates | HTML pages. |
| static | CSS, JavaScript and images. |
9.11 requirements.txt
The requirements file tells the cloud server which libraries to install.
flask scikit-learn pandas joblib gunicorn
9.12 Streamlit for ML Dashboards
Streamlit is a Python framework for quickly building interactive data apps and ML dashboards. It is easier than Flask for beginners because it automatically creates a web interface from Python code.
Streamlit Prediction App
import streamlit as st
import joblib
model = joblib.load("student_model.pkl")
st.title("Student Pass Prediction App")
attendance = st.slider("Attendance Percentage", 0, 100, 80)
marks = st.slider("Marks", 0, 100, 75)
if st.button("Predict"):
prediction = model.predict([[attendance, marks]])
if int(prediction[0]) == 1:
st.success("Prediction: Pass")
else:
st.error("Prediction: Fail")9.13 Flask vs Streamlit
| Feature | Flask | Streamlit |
|---|---|---|
| Main Purpose | APIs and web applications | Data apps and dashboards |
| Frontend Control | High control with HTML/CSS | Limited but very fast |
| Beginner Friendly | Medium | High |
| Best For | Production APIs | Prototypes and ML dashboards |
| Deployment | Cloud server, Render, Railway | Streamlit Community Cloud, cloud servers |
9.14 FastAPI Introduction
FastAPI is a modern Python framework for building high-performance APIs. It is often used in professional ML deployment because it is fast, supports validation and provides automatic API documentation.
from fastapi import FastAPI
import joblib
app = FastAPI()
model = joblib.load("student_model.pkl")
@app.get("/")
def home():
return {"message": "PDTC FastAPI ML App Running"}
@app.post("/predict")
def predict(attendance: float, marks: float):
prediction = model.predict([[attendance, marks]])
return {"prediction": int(prediction[0])}9.15 Cloud Deployment Platforms
Cloud deployment publishes your AI application online so users can access it through a public URL.
| Platform | Use | Beginner Notes |
|---|---|---|
| Render | Deploy Flask and web apps | Beginner friendly |
| Railway | Deploy APIs and databases | Simple deployment workflow |
| Streamlit Community Cloud | Deploy Streamlit apps | Very suitable for dashboards |
| AWS | Enterprise cloud services | Powerful but more complex |
| Google Cloud | Cloud apps and AI services | Good for AI integration |
| Microsoft Azure | Enterprise AI deployment | Popular with organizations |
9.16 Deployment Workflow on Render
- Create a Flask project with app.py.
- Create requirements.txt.
- Push project to GitHub.
- Create a new Web Service on Render.
- Connect GitHub repository.
- Set build command: pip install -r requirements.txt.
- Set start command: gunicorn app:app.
- Deploy and test public URL.
gunicorn app:app
9.17 Docker for ML Deployment
Docker packages an application and its dependencies into a container. This helps ensure that the app runs the same way on different machines and cloud servers.
Simple Dockerfile
FROM python:3.10 WORKDIR /app COPY . . RUN pip install -r requirements.txt CMD ["gunicorn", "app:app"]
| Line | Meaning |
|---|---|
| FROM python:3.10 | Uses Python 3.10 as base image. |
| WORKDIR /app | Sets working folder. |
| COPY . . | Copies project files into container. |
| RUN pip install... | Installs required libraries. |
| CMD | Starts the application. |
9.18 Model Monitoring
After deployment, models must be monitored. A model that performs well today may become weak later because real-world data changes.
| Monitoring Area | Meaning |
|---|---|
| Prediction Logs | Record inputs and outputs. |
| Error Logs | Track API errors and failures. |
| Latency | Measure response time. |
| Model Drift | Detect changes in data patterns. |
| Accuracy Feedback | Compare predictions with actual outcomes. |
9.19 Security Considerations
- Validate all user inputs before sending them to the model.
- Do not expose secret keys in code.
- Use HTTPS for secure communication.
- Limit API request rates to prevent abuse.
- Protect model files from unauthorized access.
- Log activity but avoid storing sensitive personal data unnecessarily.
9.20 Deployment Best Practices
| Best Practice | Reason |
|---|---|
| Use virtual environment | Keeps dependencies organized. |
| Use requirements.txt | Ensures correct packages are installed. |
| Test locally before deployment | Reduces cloud errors. |
| Use clear API responses | Makes integration easier. |
| Monitor logs | Helps detect issues. |
| Version models | Allows rollback to earlier model. |
9.21 Complete Capstone Project: Student Prediction AI App
Students should build and deploy a complete AI application with the following requirements:
- Train a Logistic Regression model using attendance and marks.
- Save the model as student_model.pkl.
- Create a Flask API with /predict endpoint.
- Create a Streamlit dashboard for user input.
- Test predictions with sample data.
- Deploy the project to Render, Railway or Streamlit Cloud.
- Prepare a short deployment report with screenshots.
Training
Model
or Streamlit
URL
9.22 Hands-On Activities
Activity 1: Save a Model
Train a simple ML model and save it using joblib.
Activity 2: Load and Predict
Load the saved model and test one prediction.
Activity 3: Flask API
Create a /predict endpoint that accepts attendance and marks as JSON.
Activity 4: Streamlit App
Create sliders for attendance and marks and display prediction result.
Mini Project
Deploy a Student Pass Prediction app and submit the public URL with screenshots.
9.23 Interactive Final Assessment Quiz
Each correct answer gives +1 mark. Each wrong answer gives -0.5 mark.
1. Machine Learning deployment makes a trained model available for real-world use.
2. Which library can save Scikit-learn models?
3. An API can receive input data and return model predictions.
4. Flask is used to build web applications and APIs in Python.
5. Streamlit is useful for creating interactive ML dashboards.
6. Which file lists required Python libraries for deployment?
7. Cloud deployment publishes an AI app online.
8. Model monitoring is important after deployment.
9. Model drift means real-world data patterns may change over time.
10. Docker helps package applications and dependencies into containers.
Your Score: 0
9.24 Chapter Summary
In this chapter, learners studied Machine Learning deployment using saved models, APIs, Flask, Streamlit, FastAPI, cloud platforms, Docker, monitoring, security and best practices. Learners also explored how to publish real-world AI solutions through web applications and cloud platforms.