Deploying Machine Learning Models with FastAPI: Utilizing ML Models as APIs

Posted by


In this tutorial, we will walk through the process of deploying a machine learning model using FastAPI, which is a modern web framework for building APIs with Python.

Step 1: Install required packages
First, you need to install FastAPI and other required packages. You can do this using pip:

pip install fastapi uvicorn pandas scikit-learn

Step 2: Create a machine learning model
For the purpose of this tutorial, we will create a simple machine learning model using scikit-learn. You can use any machine learning model of your choice. Here is an example of creating a simple linear regression model:

from sklearn.linear_model import LinearRegression
import pandas as pd

# Create sample data
data = {'X': [1, 2, 3, 4, 5],
        'Y': [2, 4, 6, 8, 10]}
df = pd.DataFrame(data)

# Create and train the model
model = LinearRegression()
model.fit(df[['X']], df['Y'])

Step 3: Create a FastAPI app
Now, you need to create a FastAPI app that will serve as the API for your machine learning model. Here is an example of a FastAPI app that makes predictions using the model we created in the previous step:

from fastapi import FastAPI
from pydantic import BaseModel

class InputData(BaseModel):
    X: int

app = FastAPI()

@app.post("/predict/")
async def predict(data: InputData):
    prediction = model.predict([[data.X]])
    return {"prediction": prediction[0]}

Step 4: Run the FastAPI app
You can run the FastAPI app using uvicorn:

uvicorn main:app --reload

Now your FastAPI app is running and you can make predictions using the /predict/ endpoint. You can test the API using tools like Postman or curl:

curl -X POST http://localhost:8000/predict/ -d '{"X": 6}'

This will return a JSON response with the prediction from the machine learning model.

Step 5: Deploy the model
To deploy your machine learning model, you can use a platform like Heroku or AWS. Here is a brief overview of deploying your FastAPI app to Heroku:

  1. Create a Procfile in your project root with the following content:
web: uvicorn main:app --host 0.0.0.0 --port $PORT
  1. Create a requirements.txt file in your project root with the list of required packages:
pip freeze > requirements.txt
  1. Initialize a git repository and commit your code:
git init
git add .
git commit -m "Initial commit"
  1. Create a Heroku app and deploy your code:
heroku login
heroku create
git push heroku master
  1. Your machine learning model is now deployed as an API and you can access it using the URL provided by Heroku.

That’s it! You have successfully deployed your machine learning model using FastAPI. You can now integrate this API into your applications or share it with others for further use.

0 0 votes
Article Rating
29 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@Krish_Melodies
2 months ago

Can you make a video on making a fast api for Pytesseract for OCR . It would be helpful

@SauravdasDas
2 months ago

thanks for guidance..i'll apply your approach

@narasa12
2 months ago

Asynchronous service gateway interface…

@creator6561
2 months ago

Can someone tell me where is he loading the model , is he loading it on the local machine ?

@zaibalizaibali
2 months ago

Gate name? Please explain

@GameDevForLife
2 months ago

good video just one question what to do when you have large json file with same data how to talk with API when you have a lot of client

@mdmoinuddinkamal3412
2 months ago

very good session .greet like as boos …

@hariprasath7050
2 months ago

Hi Sir you did great explain but you said at end of video you will continue to upload deploy deep learning model and and return HTML file continuation not posted till please make this playlist remaining video to finish

@rajav4144
2 months ago

Does it work the same for Mac os

@shivalingachalere2783
2 months ago

Sir please continue uploading fastapi videos.

@abhicasm9237
2 months ago

I don't know what you mean by deploy, but I dont think deploy means running it on localhost.

@AdityaYadav-qf4qm
2 months ago

Can i create these api' sin vs code?

@atharvagangshettiwar8920
2 months ago

Really thanks!!

@kavithamangalagiri2495
2 months ago

In detailed explanation. Thank you sir.

@tripurarajavarapu2126
2 months ago

Can we deploy two different models at a time?

@jcatlantis
2 months ago

Very nice content but i think you should speak slowly for most easier understanding

@ankitmehra16
2 months ago

very very helpful

@bidemiaduke5160
2 months ago

Thank you Krish for all your tutorials. Please, can someone help me. I made machine learning models in its own environment in anaconda and I want to deploy them using flask but I cant get either pickle or joblib to work in anaconda environment. Does anyone know what I'm doing wrong please?

@jayvirsinhchhasatiya
2 months ago

How to deploy this model as html. As u deploy model using flask.

@ShashankGurnalkar
2 months ago

Thank you for the session.
Question: how to work with pydantic when we need to handle exceptions? For example right now you are taking 4 features, but what if any feature is missing in the input?