ML - Deploy Machine Learning Models Using FastAPI

Dorian Machado
4 min readMar 13, 2023

--

Imagine your machine learning model being used by others team members/customers as an API without a Jupyter Notebook 📕. Well this is where FastAPI jumps in action to save the day.

Photo by AltumCode on Unsplash

Exposing your machine learning model as API with the help of FastAPI could be so simple that it will blow your mind 🤯and remember all the sources and dataset will be available in GitHub.

For this tutorial we’ll need a ML model persisted, but don’t worry 😉 you can check my other post about how to persist a ML model and use the .joblib file

Hands-on 🚀

First thing first, let’s create our python virtual environment to isolate all we need

If you don’t know what a python virtual environment is, go and check this amazing post 😃

Let’s create it and activate

python3 -m venv venv
source venv/bin/activate

Install the necessary packages

pip install uvicorn fastapi joblib

The Schema CarUser 🚗

We’ll be pydantic for the data validation layer of our API, this library will help to prevent the user provide wrong parameters in the api call.

Create the file CarUser.py with the following content:

from pydantic import BaseModel

class CarUser(BaseModel):
age: int
gender: int

The App 🤓

Let’s write some python code for the app.py file, this will be the main file for our API

import uvicorn
from fastapi import FastAPI
import joblib
from CarUser import CarUser

app = FastAPI()
joblib_in = open("car-recommender.joblib","rb")
model=joblib.load(joblib_in)


@app.get('/')
def index():
return {'message': 'Cars Recommender ML API'}

@app.post('/car/predict')
def predict_car_type(data:CarUser):
data = data.dict()
age=data['age']
gender=data['gender']

prediction = model.predict([[age, gender]])

return {
'prediction': prediction[0]
}

if __name__ == '__main__':
uvicorn.run(app, host='127.0.0.1', port=8000)

Breakdown the Code 🔍

The data validation with pydantic is being imported here:

from CarUser import CarUser

Do you remember our beloved ML model called car-recommender.joblib ? well now is being read and loaded as shown bellow

joblib_in = open("car-recommender.joblib","rb")
model=joblib.load(joblib_in)

Will response all the petitions to the root/index of the API with the message “Cars Recommender ML API” in JSON format

@app.get('/')
def index():
return {'message': 'Cars Recommender ML API'}

Whit this section we’re indicating the API endpoint where the user need to execute the petition https://api.com/car/predict using the HTTP method POST

@app.post('/car/predict')

This simple but powerful function is in charge of receive the parameters in the API call, use the already loaded ML model to perform the prediction and last but not least return the result of the prediction.

def predict_car_type(data:CarUser):
data = data.dict()
age=data['age']
gender=data['gender']

prediction = model.predict([[age, gender]])

return {
'prediction': prediction[0]
}

Time to spin-up the API 🔥

For start the API execute the following command in the terminal

uvicorn app:app

The output should looks like this one, indicating the API is up&running on port 8000

If you go to http://127.0.0.1:8000 in your favorite web browser you’ll see something like this

And also if go to https://127.0.0.1:8000/docs you’ll see something amazing

FastAPI generated all the API doc 📚 for us 🎉

Now let’s execute an API call asking for a prediction, let’s ask what type of car prefers a 22 years old woman.

Boom 💥 we have our magic prediction 😱 under the section called “Response Body”

Conclusions

This is a fast and easy way to serve a ML model as API. Remember that for production environments you should take in account some important item such as Auth, hight availability, throttle, concurrency and other important topics to make your API the best ever.

Also surely you noticed how easy and straightforward is to configure FastAPI and the awesome feature to auto-magicly ✨ generate the documentation for us 🤯

Bonus Track 📼

You can use services like heroku.com or fly.io to deploy your beautiful and powerful ML model as API and share with the world 🌎

The starting plan for Heroku is 5 USD / month but for Fly the initial plan is free.

--

--

Dorian Machado
Dorian Machado

Written by Dorian Machado

Serveless Evangelist / Data Engineer / AWS

No responses yet