Original Source Here
Creating API 💻
In this section, we will build an API to deploy our trained hotdog detector model into production.
│ ├── __init__.py
│ ├── json_models
│ │ ├── __init__.py
│ │ └── request.py
│ ├── main.py
│ ├── models
│ │ ├── __init__.py
│ │ └── model.pkl
│ └── utils.py
│ └── HotDogOrNot.ipynb
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.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.
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.
This file that resides within the
json_models directory contains a
Pydantic 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
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
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.
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.
Trending AI/ML Article Identified & Digested via Granola by Ramsey Elbasheer; a Machine-Driven RSS Bot