Deploying a Machine Learning Model as an API Using FastAPI in Python

Posted by


In this tutorial, we will walk through the steps of deploying a machine learning model as an API using FastAPI in Python. FastAPI is a modern, fast (high-performance), web framework for building APIs with Python 3.6+ based on standard Python type hints.

Before we begin, make sure you have the following prerequisites:

  1. Basic knowledge of Python programming.
  2. Understanding of machine learning concepts and how to build and train a model.
  3. Familiarity with creating and working with APIs.

Let’s get started:

Step 1: Build and train your machine learning model
First, you need to build and train your machine learning model using any library/framework of your choice (e.g. Scikit-learn, TensorFlow, PyTorch). For the purpose of this tutorial, we will use a simple example of building a linear regression model using Scikit-learn.

Here is an example code snippet for building and training a linear regression model:

from sklearn.linear_model import LinearRegression
import pandas as pd

# Load data
data = pd.read_csv('data.csv')
X = data[['feature1', 'feature2']]
y = data['target']

# Initialize and train the model
model = LinearRegression()
model.fit(X, y)

Step 2: Create an API using FastAPI
Next, we will create an API using FastAPI to deploy our machine learning model.

First, install FastAPI and Uvicorn (a lightning-fast ASGI server) using pip:

pip install fastapi uvicorn

Next, create a new Python file (e.g. app.py) and import FastAPI:

from fastapi import FastAPI
import numpy as np

app = FastAPI()

Step 3: Define API endpoints
Next, define the API endpoints that will serve as the interface for interacting with the model.

For example, we can create a /predict endpoint that takes input features as JSON data and returns the prediction from the model:

@app.get("/predict")
def predict(feature1: float, feature2: float):
    features = np.array([[feature1, feature2]])
    prediction = model.predict(features)
    return {"prediction": prediction[0]}

Step 4: Run the API server
Finally, run the API server using Uvicorn:

uvicorn app:app --reload

Now, you can access the API endpoint at http://127.0.0.1:8000/predict and send a POST request with input data to get predictions from the machine learning model.

That’s it! You have successfully deployed a machine learning model as an API using FastAPI in Python. You can customize and extend the API as needed to support more endpoints and functionality.

I hope this tutorial was helpful in guiding you through deploying your machine learning model as an API. Happy coding!

0 0 votes
Article Rating

Leave a Reply

26 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@sangeethag8228
13 days ago

Hi everyone,

I encountered the same issue where the model consistently predicted non-diabetic cases the same way. I resolved it by making the following adjustment, which worked for me:

### Data Standardization:

In our training code, we used `StandardScaler` to standardize the data. When deploying the model with FastAPI, it's crucial to ensure that the input data is standardized in the same way before making predictions.

To fix this, I added the `StandardScaler` transformation to our FastAPI code, using the same scaler that was fitted during training. Here’s what the `api.py` file looks like:

“`python

import requests

url = 'http://127.0.0.1:8000/diabetes_prediction'

input_data_for_model = {

'pregnancies': 4,

'Glucose': 110,

'BloodPressure': 92,

'SkinThickness': 0,

'Insulin': 0,

'BMI': 37.6,

'DiabetesPedigreeFunction': 0.191,

'Age': 30

}

response = requests.post(url, json=input_data_for_model)

print(response.json())

“`

And here’s the `ml.py` code:

“`python

import json

from fastapi import FastAPI

from pydantic import BaseModel

import pickle

import numpy as np

app = FastAPI()

class ModelInput(BaseModel):

pregnancies: int

Glucose: int

BloodPressure: int

SkinThickness: int

Insulin: int

BMI: float

DiabetesPedigreeFunction: float

Age: int

# Load the model and the scaler

diabetes_model = pickle.load(open('diabetes_model.sav', 'rb'))

scaler = pickle.load(open('scaler.sav', 'rb'))

@app.post('/diabetes_prediction')

def diabetes_pred(input_parameters: ModelInput):

input_data = np.array([

input_parameters.pregnancies,

input_parameters.Glucose,

input_parameters.BloodPressure,

input_parameters.SkinThickness,

input_parameters.Insulin,

input_parameters.BMI,

input_parameters.DiabetesPedigreeFunction,

input_parameters.Age

])

input_data_reshaped = input_data.reshape(1, -1)

std_data = scaler.transform(input_data_reshaped)

prediction = diabetes_model.predict(std_data)

if prediction[0] == 0:

return {'prediction': 'The person is not diabetic'}

else:

return {'prediction': 'The person is diabetic'}

“`

Additionally, I made a small change when saving the model:

“`python

import pickle

# Save the model

filename = 'diabetes_model.sav'

pickle.dump(classifier, open(filename, 'wb'))

# Save the scaler

scaler_filename = 'scaler.sav'

pickle.dump(scaler, open(scaler_filename, 'wb'))

“`

I hope this helps you all!

@mayumayuri470
13 days ago

Anna can you do project on Genetic Disorder Prediction plz…

@bishnudas1187
13 days ago

True GEM, MAN //\

@Karthik.D.project
13 days ago

sir will this code will generate API even if our model is not able to run in python shell it is only able to run on google collab because of large training data and hardware restrictions

@growingfire
13 days ago

Nice implementation

@growingfire
13 days ago

I'm getting 'Internal server error' , pls help

@unconnu8481
13 days ago

great job, thanks

@innadragota696
13 days ago

You are great teacher, thanks a million) Very rare decent corse!

@majdsami9325
13 days ago

What if the model is cnn how i can do it ?

@ayushpratapsinghranag5551
13 days ago

Hello Sir
I'm facing this problem
raise PydanticUserError(

pydantic.errors.PydanticUserError: A non-annotated attribute was detected: `diabetes_model = SVC(kernel='linear')`. All model fields require a type annotation; if `diabetes_model` is not meant to be a field, you may be able to resolve this error by annotating it as a `ClassVar` or updating `model_config['ignored_types']`.

@channadissanayaka6450
13 days ago

Thank you.This is a greate explanation.!!One of the best video of this category….

@naveenk.m704
13 days ago

Thank you for the informative content bro 👍, it was very helpfull.

@rmasineni
13 days ago

Siddhardhan, you have nicely explained the topic. Thank you.

@lawrenceowusu2017
13 days ago

Amazing video. Well done

@nimeshnethsara6318
13 days ago

best ml tutorial i have ever seen .thank you brother.

@sk49609
13 days ago

sir I want a deploy model for spam mail can you help me please
I tried by watching this video but it is throwing the error please help me
I've to present it next week

@Tiger-Tippu
13 days ago

Why we need to pass as json why not dictionary only

@pallavipal3608
13 days ago

Sir on testing it I am getting "self.probA_, self.probB_, svm_type=svm_type, kernel=kernel,
AttributeError: 'SVC' object has no attribute 'probA_' " error. How I can resolve that?

@chiamakaeboagu6660
13 days ago

Amazing video.Thank you

@goodomen1886
13 days ago

I have a problem
I couldn't install pickle5

26
0
Would love your thoughts, please comment.x
()
x