All Course > Data Science With Fastapi > Machine Learning Model Deployment With Fastapi Nov 13, 2024

Deploy Machine Learning Models with FastAPI: A Step-by-Step Guide

In the previous lesson, we explored how to handle data with FastAPI and Pandas. We learned how to process, clean, and serve data using FastAPI endpoints, which is a crucial step before deploying machine learning models. Now, we’ll take it a step further and dive into deploying machine learning models with FastAPI. This lesson will teach you how to integrate trained models, create API endpoints for inference, and handle model input and output effectively.

Deploying a Sentiment Analysis Model

I recently worked on a project where I had to deploy a sentiment analysis model using FastAPI. The model, which was trained on customer reviews, needed to predict whether a review was positive or negative. I faced challenges in loading the model, creating an API endpoint, and ensuring the input/output formats were correct. By following the steps below, I was able to deploy the model successfully and make it accessible via a REST API.

Loading Machine Learning Models in FastAPI

The first step is to load your trained machine learning model into the FastAPI application. Most models are saved in formats like .pkl, .joblib, or .h5. For this example, let’s assume we have a sentiment analysis model saved as sentiment_model.pkl.

To load the model, we use Python’s joblib library, which is efficient for serializing and deserializing models. Here’s how you can do it:

import joblib  
from fastapi import FastAPI  

app = FastAPI()  

# Load the trained model  
model = joblib.load("sentiment_model.pkl")  

This code loads the model when the FastAPI app starts. Make sure the model file is in the same directory as your script or provide the correct path.

Creating an API Endpoint for ML Inference

Once the model is loaded, the next step is to create an API endpoint that accepts input data, processes it, and returns the model’s prediction. FastAPI makes this easy with its intuitive routing system.

Let’s create a /predict endpoint that takes a review as input and returns the sentiment prediction:

from pydantic import BaseModel  

class Review(BaseModel):  
    text: str  

@app.post("/predict")  
def predict_sentiment(review: Review):  
    # Preprocess the input text (if needed)  
    processed_text = preprocess_text(review.text)  

    # Get the model's prediction  
    prediction = model.predict([processed_text])  

    # Return the result  
    return {"sentiment": prediction[0]}  

In this example, Review is a Pydantic model that defines the input schema. The /predict endpoint accepts a JSON payload with a text field, processes it, and returns the sentiment prediction.

Handling Model Input and Output

Handling input and output is a critical part of deploying machine learning models. The input data must be preprocessed to match the format the model expects. For instance, if your model was trained on tokenized text, you’ll need to tokenize the input before passing it to the model.

Similarly, the output should be formatted in a way that’s easy for clients to understand. In our sentiment analysis example, the output is a simple JSON object with a sentiment field.

Here’s an example of preprocessing the input text:

def preprocess_text(text):  
# Example: Convert text to lowercase and remove punctuation  
text = text.lower()  
text = "".join([char for char in text if char.isalnum() or char == " "])  
return text  

This function ensures the input text is cleaned and formatted correctly before being passed to the model.

Testing the API

After setting up the API, it’s important to test it to ensure everything works as expected. You can use tools like Postman or FastAPI’s built-in Swagger UI to test the /predict endpoint.

Here’s an example of how to test the API using Python’s requests library:

import requests  

data = {"text": "This product is amazing!"}  
response = requests.post("http://127.0.0.1:8000/predict", json=data)  
print(response.json())  

This script sends a POST request to the /predict endpoint and prints the response, which should include the predicted sentiment.

Conclusion

In this tutorial, we covered the essential steps for deploying machine learning models with FastAPI. We learned how to load a trained model, create an API endpoint for inference, and handle input and output effectively. By following these steps, you can deploy your own machine learning models and make them accessible via a REST API.

In the next lesson, we’ll explore how to use FastAPI with Scikit-Learn and TensorFlow models, which will expand your knowledge of integrating different types of machine learning models.

Comments

There are no comments yet.

Write a comment

You can use the Markdown syntax to format your comment.