Hello everyone 👋,
In this quick post I’ll show you how to deploy a FastAI model using FastAPI.
It’s a follow-up to my previous post from here and it is partially based on this FastAI lecture:
Exporting the Model
Before we create the API, we need to export the model as a pickle file.
Add learn.export("<path>/cat_or_dog.pkl")
with the full absolute export path at the end of the script:
|
|
Deployment with FastAPI
Let’s create a new project and install the required dependencies:
|
|
Now create a main.py
Python file that we’ll use to define our FastAPI app. Note that this is just a quick tutorial,
it’s not meant to be a full introduction to FastAPI, you can find more details about it here.
The FastAPI handler is a simple post endpoint that accepts a file and returns a JSON response with the prediction, in case the file is not an image, it will return a 400 response.
The is_cat
function is required for importing the model, if it’s not defined the model will not be loaded correctly.
|
|
That’s it, you can now run the app using python main.py
and test it using curl:
|
|
You should get a response similar to this:
|
|
I get around 70ms response time on my machine.
Conclusion
I’ve showcased a simple way to deploy a FastAI model using FastAPI, by exporting it to a pickle file and then loading it in the API.
Another more advanced way to deploy the model would be to use the ONNX Runtime. Other ways to deploy the model are also showcased by Jeremy in the video above.
Thank you for reading! 📚🫶
If you want to be notified when I post new content, please subscribe to my newsletter 📰.