Deploying a machine learning model in FastAPI

If you aspire to work in the field of machine learning, you might have to deploy your machine learning model in production. In this blog post you will learn how to deploy a simple linear regression model in FastAPI. FastAPI is a modern web framework to deploy your application in Python.

Getting Started

Let us get started by installing the libraries needed to build our application.

We will create a virtual environment for development purpose. Using a virtual environment gives us the flexibility to separate dependencies for different Python projects.

Go to a directory where you would like to create this project and in a separate terminal execute the following command

python3 -m venv fastapi-env

Once you have the environment setup, we can activate the environment by executing the following command

source fastapi-env/bin/activate

Now your environment is activated, and we can begin by installing the necessary dependencies for our project.

Let us begin by creating a requirements.txt file which will include all the libraries that we would be using in our project.

We would be needing the FastAPI library and the sklearn library for running our regression model.

Open a new file, name it requirements.txt and insert the following lines:

# requirements.txt
fastapi
uvicorn
sklearn

You can now install the libraries by executing the following command

pip3 install -r requirements.txt

Building a Linear Regression Model

We will be using a trained linear regression model to predict the quantitative measure of disease prediction. You can use the following link to train a linear regression model. Once you have the model trained you can save the model using the joblib library

from joblib import dump, load
dump(regr , 'model.joblib')

We save the model in the current directory and give it a name–in our case we have given the model a name, model.joblib.

Serving Your Model

Now we begin by understanding how FastAPI works and how we can implement a simple API to serve a machine learning request.

We begin by importing the libraries

from fastapi import FastAPI
from joblib import load
import numpy as np

We then load our model and declare an instance of the FastAPI class. We store this in a variable called app.

app = FastAPI()
model = load('model.joblib')

We then implement a function for our index route. Whenever a user or a client tries to access the index route, the function root() is called and a β€œHello World” message is sent back.

@app.get("/")
async def root():
   return {"message": "Hello World"}

You can run your app by using the uvicorn library which is an asynchronous server which spins up your app.

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

Once your app is running we can go to localhost:8000 and see the message β€œHello World” displayed

We will now implement the predict function and since we need to send a json file with our data we will define it as post request using the @app.post decorator function.

@app.post("/predict")
def predict(data_diabetes:float):
   data = np.array([[data_diabetes]])
   prediction = model.predict(data)
   return {
       'prediction': prediction[0],
   }

As you can see in the above code snippet, first the data is transformed into a NumPy array since our model expects an array of shape 1×1. We can these use this transformed vector value to call the predict function of the model, which will return the prediction or the quantitative metric of progression of diabetes.

Let us see the entire code in action

from fastapi import FastAPI
from joblib import load
import numpy as np
app = FastAPI()

model = load('model.joblib')



@app.get("/")
async def root():
   return {"message": "Hello World"}


@app.post("/predict")
def predict(data_diabetes:float):
   data = np.array([[data_diabetes]])
   prediction = model.predict(data)
   return {
       'prediction': prediction[0],
   }


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

Calling Your Endpoint

You can call this endpoint using a client library in Python or using a simple curl command to test the functionality of our application.

curl -X POST "http://localhost:8000/predict?data_diabetes=0.07786339" -H  "accept: application/json" -d ""

You get the following response back

{"prediction":225.97324232953468}

The model outputted a value of 225.97324 and this was sent back as a response to our client.

Conclusion

In this blog post you learned how to deploy a simple machine learning model in FastAPI. FastAPI is a powerful web framework to deploy and build scalable Python applications.