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.

APIsFlaskStreamlitCloudAI Solutions
Save
Model
Build
API
Deploy
Cloud
Publish
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.

Learning Outcome: By the end of this chapter, learners should be able to save trained ML models, create prediction APIs, build Flask and Streamlit applications, deploy AI solutions to cloud platforms, and understand monitoring, security and maintenance.
1Train Model
2Save Model
3Create API
4Build App
5Deploy Cloud

9.2 Machine Learning Deployment Lifecycle

StagePurposeExample
Model TrainingBuild model using historical data.Train student pass predictor.
Model EvaluationCheck model accuracy and reliability.Accuracy, confusion matrix, F1 score.
Model SavingStore trained model for reuse.student_model.pkl
Application DevelopmentCreate interface or API.Flask API or Streamlit dashboard.
TestingCheck predictions with sample inputs.POST JSON request.
DeploymentPublish to server or cloud.Render, Railway, AWS, Azure.
MonitoringTrack performance and errors.Logs, model drift, feedback.

9.3 Deployment Architecture

User
Input
Web App
or API
Saved ML
Model
Prediction
Output
Simple Meaning: The user sends data, the application passes it to the model, the model predicts, and the application returns the result.

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.

ToolUseSuitable For
PickleGeneral Python object saving.Simple models and objects.
JoblibEfficient 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

CodeExplanation
import joblibImports 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])
Expected Output:
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.

ComponentRole
ClientWebsite, mobile app or dashboard sending request.
API EndpointURL where request is sent.
RequestInput data sent to API.
ModelPredicts result using input data.
ResponsePrediction 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.

Click "Send API Request" to see JSON 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)
CodeExplanation
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)
Explanation: The API receives JSON data, extracts attendance and marks, sends them to the saved ML model, and returns the prediction as JSON.

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 / FolderPurpose
app.pyMain Flask application.
student_model.pklSaved ML model.
requirements.txtList of required Python libraries.
templatesHTML pages.
staticCSS, JavaScript and images.

9.11 requirements.txt

The requirements file tells the cloud server which libraries to install.

flask
scikit-learn
pandas
joblib
gunicorn
Note: gunicorn is commonly used to run Flask applications in production deployment.

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")
Student Pass Prediction App
Attendance Percentage: 80
Marks: 75
Predict
Prediction: Pass

9.13 Flask vs Streamlit

FeatureFlaskStreamlit
Main PurposeAPIs and web applicationsData apps and dashboards
Frontend ControlHigh control with HTML/CSSLimited but very fast
Beginner FriendlyMediumHigh
Best ForProduction APIsPrototypes and ML dashboards
DeploymentCloud server, Render, RailwayStreamlit 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.

PlatformUseBeginner Notes
RenderDeploy Flask and web appsBeginner friendly
RailwayDeploy APIs and databasesSimple deployment workflow
Streamlit Community CloudDeploy Streamlit appsVery suitable for dashboards
AWSEnterprise cloud servicesPowerful but more complex
Google CloudCloud apps and AI servicesGood for AI integration
Microsoft AzureEnterprise AI deploymentPopular with organizations

9.16 Deployment Workflow on Render

  1. Create a Flask project with app.py.
  2. Create requirements.txt.
  3. Push project to GitHub.
  4. Create a new Web Service on Render.
  5. Connect GitHub repository.
  6. Set build command: pip install -r requirements.txt.
  7. Set start command: gunicorn app:app.
  8. Deploy and test public URL.
gunicorn app:app
Meaning: The first app refers to app.py. The second app refers to the Flask app object inside the file.

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"]
LineMeaning
FROM python:3.10Uses Python 3.10 as base image.
WORKDIR /appSets working folder.
COPY . .Copies project files into container.
RUN pip install...Installs required libraries.
CMDStarts 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 AreaMeaning
Prediction LogsRecord inputs and outputs.
Error LogsTrack API errors and failures.
LatencyMeasure response time.
Model DriftDetect changes in data patterns.
Accuracy FeedbackCompare predictions with actual outcomes.
Model Drift Example: A student prediction model trained on old learning patterns may become less accurate if exam format, teaching method or student behavior changes.

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 PracticeReason
Use virtual environmentKeeps dependencies organized.
Use requirements.txtEnsures correct packages are installed.
Test locally before deploymentReduces cloud errors.
Use clear API responsesMakes integration easier.
Monitor logsHelps detect issues.
Version modelsAllows 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:

  1. Train a Logistic Regression model using attendance and marks.
  2. Save the model as student_model.pkl.
  3. Create a Flask API with /predict endpoint.
  4. Create a Streamlit dashboard for user input.
  5. Test predictions with sample data.
  6. Deploy the project to Render, Railway or Streamlit Cloud.
  7. Prepare a short deployment report with screenshots.
Dataset
Model
Training
Saved
Model
Flask API
or Streamlit
Cloud
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.

Remember: A Machine Learning model becomes valuable when real users can access it, test it and use it for decision-making. Deployment turns AI learning into real-world AI solutions.