How To Deploy Machine Learning Model As Microservices
How To Deploy Machine Learning Model As Microservices
As of today, FastAPI is the most popular web framework for building microservices with
python 3.6+ versions. By deploying machine learning models as microservice-based
architecture, we make code components re-usable, highly maintained, ease of testing, and of-
course the quick response time. FastAPI is built over ASGI (Asynchronous Server Gateway
Interface) instead of flask’s WSGI (Web Server Gateway Interface). This is the reason it is
faster as compared to flask-based APIs.
It has a data validation system that can detect any invalid data type at the runtime and
returns the reason for bad inputs to the user in the JSON format only which frees developers
from managing this exception explicitly.
In this post, the objective is to explain the machine learning model deployment as
microservices with the help of FastAPI. So we will focus on that part, not on the model
training.
complete source code is also available in github repository. You will get the repository link at
the end of the post.
Step 1. Make your model for which you want to create the API ready
To create API for prediction we need the model ready so I have written few lines of code that
train the model and save it as LRClassifier.pkl file in the local disk. I have not focused on
exploratory data analysis, pre-processing or feature engineering part as that is out of the
scope for this article.
1/7
Logistic Regression python code snippet
Install Libraries:
2/7
Here main is the name of file where you are writing the code. you can give any name
but same you have to use while executing in the command in place of main.
When you need to send data from a client (let’s say, a browser) to your API, you send it
as a .
A body is data sent by the client to your API. A body is the data your API sends to the
client.
Your API almost always has to send a body. But clients don’t necessarily need to send
bodies all the time.
To declare a body, you use models with all their power and benefits.
Then you declare your data model as a class that inherits from BaseModel .
Use standard Python types for all the attributes.
In our case we want to predict the Iris Species so will create a data model as class with
four parameters which are the dimensions of the species.
Now create an end point also known as route named “predict”
Add a parameter of type data model we created which is “IrisSpecies”.
Now we can post data as json and it will be accepted in iris variable.
Next, we will load the already saved model in a variable loaded_model.
Now perform the prediction the same way we do in machine learning and return the
results.
now you can run the app and see the beautiful User Interface (UI) created by FastAPI
which uses Swagger now known as openAPI as backend for designing the
documentation and UI.
Full code is given below you can simply copy and paste and it will work if you have
followed the above steps properly.
3/7
Executing the APP:
Now if you can see the nice UI created by typing the url: 127.0.0.0:8000/docs
Below you see the API end point is created as POST request.
4/7
Click on the end point and it will expand as below.
Now click on Try it out and paste the dimensions to get the prediction.
5/7
Now you see that it has predicted it as Iris-setosa with 99% accuracy.
So this was all about the API creation using the FastAPI.
FastAPI also provides nice documentation which gets created automatically. just type in the
browser 127.0.0.0:8000/redoc
6/7
That’s it for this article. Hope you enjoyed reading. Share your thoughts about your
experience with FastAPI. Also, you can ask if you get any questions during implementation
using the comments.
7/7