Swiftorial Logo
Home
Swift Lessons
AI Tools
Learn More
Career
Resources

Python Advanced - Model Deployment with FastAPI

Deploying machine learning models using FastAPI in Python

FastAPI is a modern, fast (high-performance), web framework for building APIs with Python 3.7+ based on standard Python type hints. It is an excellent choice for deploying machine learning models due to its speed, ease of use, and automatic interactive API documentation. This tutorial explores how to deploy machine learning models using FastAPI in Python.

Key Points:

  • FastAPI is a high-performance web framework for building APIs with Python.
  • It is ideal for deploying machine learning models due to its speed and ease of use.
  • FastAPI automatically generates interactive API documentation.

Setting Up FastAPI

First, you need to install FastAPI and an ASGI server like uvicorn:


# Install FastAPI and uvicorn
pip install fastapi uvicorn
            

Once installed, you can create a basic FastAPI application:


# Import FastAPI
from fastapi import FastAPI

# Create a FastAPI instance
app = FastAPI()

# Define a root endpoint
@app.get("/")
def read_root():
    return {"message": "Welcome to the FastAPI model deployment!"}
            

You can run this application using uvicorn:


# Run the FastAPI application
uvicorn main:app --reload
            

This command runs the FastAPI application with hot-reloading, making it easier to develop and test.

Loading a Machine Learning Model

To deploy a machine learning model, you need to load the model in your FastAPI application. This example demonstrates loading a model using scikit-learn:


# Import necessary libraries
from fastapi import FastAPI
from pydantic import BaseModel
import joblib

# Create a FastAPI instance
app = FastAPI()

# Load the machine learning model
model = joblib.load("model.joblib")

# Define the input data model
class InputData(BaseModel):
    feature1: float
    feature2: float
    feature3: float
    feature4: float

# Define a prediction endpoint
@app.post("/predict")
def predict(data: InputData):
    # Convert input data to a format suitable for prediction
    input_data = [[data.feature1, data.feature2, data.feature3, data.feature4]]
    # Make a prediction
    prediction = model.predict(input_data)
    # Return the prediction result
    return {"prediction": prediction[0]}
            

In this example, the model is loaded using joblib, and an endpoint /predict is defined to handle prediction requests. The InputData class defines the structure of the input data using Pydantic for data validation.

Testing the API

With the FastAPI application running, you can test the API using an HTTP client like Postman or the interactive API documentation provided by FastAPI:

  • Open your web browser and navigate to http://127.0.0.1:8000/docs.
  • Use the interactive API documentation to send a POST request to the /predict endpoint with the required input data.
  • Check the prediction result returned by the API.

The interactive API documentation allows you to test your endpoints easily without using an external tool.

Handling Model Retraining

In a production environment, you may need to retrain your model periodically. You can create an endpoint to handle model retraining:


@app.post("/retrain")
def retrain_model(new_data: list[InputData]):
    # Process new data and retrain the model
    # (This is just a placeholder, actual retraining code will depend on your model)
    global model
    model = train_new_model(new_data)
    # Save the retrained model
    joblib.dump(model, "model.joblib")
    return {"message": "Model retrained successfully"}
            

In this example, the /retrain endpoint accepts new training data, retrains the model, and saves the updated model. You need to implement the actual retraining logic based on your specific use case.

Summary

In this tutorial, you learned how to deploy machine learning models using FastAPI in Python. FastAPI is a high-performance web framework that simplifies the process of building and deploying APIs. You explored setting up FastAPI, loading a machine learning model, defining prediction endpoints, testing the API, and handling model retraining. FastAPI's ease of use and automatic interactive API documentation make it an excellent choice for deploying machine learning models.