FastAPI — Create and Deploy Hot Dog Detector

https://miro.medium.com/max/1200/0*MazZ79L6XAj-Kjf-

Original Source Here

Creating API 💻

In this section, we will build an API to deploy our trained hotdog detector model into production.

Project Structure

.
├── LICENSE
├── README.md
├── app
│ ├── __init__.py
│ ├── json_models
│ │ ├── __init__.py
│ │ └── request.py
│ ├── main.py
│ ├── models
│ │ ├── __init__.py
│ │ └── model.pkl
│ └── utils.py
├── notebooks
│ └── HotDogOrNot.ipynb
└── requirements.txt

Most of the relevant code resides in the appdirectory. Notice that we have created a directory called models which stores our trained model file.

Before we begin, make sure to install all the requirements by running the following command from the parent directory:

$ pip3 install -r requirements.txt

Let’s have a look at our code.

utils.py

Our utils.pyfile contains three functions. Each of the three functions serves a different purpose:

read_image is quite self-explanatory. This function helps to read the image data as bytes from the request and transform it into an appropriate format in order to be fed into the model for predictions.

is_hotdog is a bit more nuanced function. This function is actually used by our fastai DataLoader for appropriately serving data to our model.

Although our model was stored in a .pkl file after training, it does not store the is_hotdog function as a whole.

Instead, it stores a reference to the function in the file. Hence, we write the function within our utils file so that when the model is loaded, it is able to successfully search for the is_hotdog function and will be able to operate as intended.

For more information about this and ImageDataLoaders, you can read the official Fastai docs and this forum thread.

Finally, our predict_hotdog function loads our model which will utilize the is_hotdog function and serve predictions. The function returns a dictionary which will serve as the response for our API.

response.py

This file that resides within the json_models directory contains aPydantic class for the response model of our API.

In short, Pydantic is a fantastic library that provides data validations using python type annotations at runtime.

This library is especially useful for building APIs and has superb interoperability with FastAPI.

As we can see, the attributes of our Response class have the same names as the keys of our predict_hotdog function.

main.py

Finally, this is the file that our application will run in production.

First, we create a FastAPI class object. Then, we decorate our predict function using this class object. @app.post indicates that our request will be a post request followed by the path for our API /predict/ .

We also have a parameter called response_model here which accepts our Pydantic model as input. This parameter is to validate our response at runtime.

Finally, our predict function has a query parameter called myfile of type UploadFile which will be used to read our image file in the API.

This variable is passed into the read_image function we saw before which will convert our image data in bytes to a PILImage object which we can easily use to feed our FastAI model.

Finally, the image is passed into our predict_hotdog function which serves predictions and returns a dictionary object as a response.

AI/ML

Trending AI/ML Article Identified & Digested via Granola by Ramsey Elbasheer; a Machine-Driven RSS Bot

%d bloggers like this: