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:
- Create a
Procfile
in your project root with the following content:
web: uvicorn main:app --host 0.0.0.0 --port $PORT
- Create a
requirements.txt
file in your project root with the list of required packages:
pip freeze > requirements.txt
- Initialize a git repository and commit your code:
git init
git add .
git commit -m "Initial commit"
- Create a Heroku app and deploy your code:
heroku login
heroku create
git push heroku master
- 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.
Can you make a video on making a fast api for Pytesseract for OCR . It would be helpful
thanks for guidance..i'll apply your approach
Asynchronous service gateway interface…
Can someone tell me where is he loading the model , is he loading it on the local machine ?
Gate name? Please explain
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
very good session .greet like as boos …
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
Does it work the same for Mac os
Sir please continue uploading fastapi videos.
I don't know what you mean by deploy, but I dont think deploy means running it on localhost.
Can i create these api' sin vs code?
Really thanks!!
In detailed explanation. Thank you sir.
Can we deploy two different models at a time?
Very nice content but i think you should speak slowly for most easier understanding
very very helpful
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?
How to deploy this model as html. As u deploy model using flask.
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?