Model Deployment

Model Deployment Chapter 2 – Building REST APIs for ML | Flask and FastAPI

Building REST APIs for Machine Learning Using Flask and FastAPI

Once a machine learning model is trained and serialized, it must be exposed
to users or applications for predictions. The most common way to do this
is by creating REST APIs that accept input data and return model predictions.

Flask and FastAPI are two popular Python frameworks used to build lightweight,
scalable APIs for deploying machine learning models in production.

⭐ What is a REST API?

A REST (Representational State Transfer) API allows applications to communicate
with each other over HTTP. In machine learning systems, REST APIs receive input
data, pass it to a model, and return predictions in formats such as JSON.

📌 Why Use REST APIs for ML Deployment

  • Model can be accessed from any platform
  • Supports real-time predictions
  • Easy integration with web and mobile apps
  • Scalable and production-ready

⭐ Deploying ML Models Using Flask

Flask is a lightweight Python web framework that is easy to learn and ideal
for simple ML deployments and prototypes.

📌 Basic Flask API Structure


from flask import Flask, request, jsonify
import joblib

app = Flask(__name__)
model = joblib.load("model.joblib")

@app.route("/predict", methods=["POST"])
def predict():
    data = request.json["input"]
    prediction = model.predict([data])
    return jsonify({"prediction": prediction.tolist()})

if __name__ == "__main__":
    app.run(debug=True)

📌 Advantages of Flask

  • Simple and lightweight
  • Easy to debug
  • Good for small-scale deployments

⭐ Deploying ML Models Using FastAPI

FastAPI is a modern, high-performance web framework designed for building APIs
quickly. It provides automatic validation, documentation, and better performance
than traditional frameworks.

📌 FastAPI Example


from fastapi import FastAPI
import joblib

app = FastAPI()
model = joblib.load("model.joblib")

@app.post("/predict")
def predict(input: list):
    prediction = model.predict([input])
    return {"prediction": prediction.tolist()}

📌 Key Features of FastAPI

  • High performance (built on Starlette)
  • Automatic API documentation (Swagger UI)
  • Built-in data validation
  • Async support

📌 Flask vs FastAPI

  • Flask: Simple, flexible, beginner-friendly
  • FastAPI: Faster, modern, production-focused

📌 Handling Requests and Responses

  • JSON-based input and output
  • HTTP methods (GET, POST)
  • Status codes and error handling

📌 Real-Life Applications

  • Recommendation engines
  • Fraud detection APIs
  • Image and text prediction services
  • AI-powered web applications

📌 Project Title

Machine Learning Model Deployment Using REST APIs

📌 Project Description

In this project, you will deploy a trained machine learning model using Flask
or FastAPI. The API will accept user input, perform predictions using the
serialized model, and return results in JSON format, simulating a real-world
production system.

📌 Summary

REST APIs are the backbone of production machine learning systems. Flask and
FastAPI enable developers to expose models as scalable services. Mastering
API-based deployment prepares you for containerization and cloud deployment,
which are covered in the next chapters.

Leave a Reply

Your email address will not be published. Required fields are marked *