Api 2
Api 2
Save
This tutorial follows on the previous tutorial: Serve your first model with
Scikit-Learn + Flask + Docker: 👇
https://engineering.rappi.com/using-fastapi-to-deploy-machine-learning-models-cd5ed7219ea 1/14
5/7/23, 1:33 PM Using FastAPI to deploy Machine Learning models | by Carl W. Handlin | Rappi Tech
https://engineering.rappi.com/using-fastapi-to-deploy-machine-learning-models-cd5ed7219ea 2/14
5/7/23, 1:33 PM Using FastAPI to deploy Machine Learning models | by Carl W. Handlin | Rappi Tech
In this tutorial we will create a Docker image for deploying a model using
FastAPI as part of the stack, you can find the full code and documentation
here: 👇
https://engineering.rappi.com/using-fastapi-to-deploy-machine-learning-models-cd5ed7219ea 3/14
5/7/23, 1:33 PM Using FastAPI to deploy Machine Learning models | by Carl W. Handlin | Rappi Tech
*If you are using Python 3, then you should already have the virtualenv
module from the standard library installed.
Next up, create a new virtual environment with the name venv:
$ virtualenv venv
It’s also helpful to specify the Python version used to create the environment
by using the option -p and the Python path (This is helpful later when
building the Docker image to match the same Python version).
$ source venv/bin/activate
Commands are a bit different if you are working with Windows, however, this is a
really great guide to get going with virtual environments in Windows:
https://medium.com/co-learning-lounge/create-virtual-environment-python-
windows-2021-d947c3a3ca78
Python packages
Once we have the virtual environment activated we need to install the
software dependencies (site-packages), a list of the Python packages and
their versions are listed in the file requirements.txt:
https://engineering.rappi.com/using-fastapi-to-deploy-machine-learning-models-cd5ed7219ea 4/14
5/7/23, 1:33 PM Using FastAPI to deploy Machine Learning models | by Carl W. Handlin | Rappi Tech
1 anyio==3.5.0
2 asgiref==3.5.0
3 click==8.1.2
4 cycler==0.11.0
5 fastapi==0.75.2
6 fonttools==4.32.0
7 h11==0.13.0
8 idna==3.3
9 joblib==1.1.0
10 kiwisolver==1.4.2
11 matplotlib==3.5.1
12 numpy==1.22.3
13 packaging==21.3
14 pandas==1.4.2
15 Pillow==9.1.0
16 pydantic==1.9.0
17 pyparsing==3.0.8
18 python-dateutil==2.8.2
19 pytz==2022.1
20 scikit-learn==1.0.2
21 scipy==1.8.0
22 six==1.16.0
23 sklearn==0.0
24 sniffio==1.2.0
25 starlette==0.17.1
26 threadpoolctl==3.1.0
27 typing extensions==4.2.0
After the installation is done, we can check we have correctly installed all
the packages in our virtual environment:
$ pip freeze
https://engineering.rappi.com/using-fastapi-to-deploy-machine-learning-models-cd5ed7219ea 5/14
5/7/23, 1:33 PM Using FastAPI to deploy Machine Learning models | by Carl W. Handlin | Rappi Tech
We should see the same list in the file requirements.txt, this means that now
we are ready to train our model.
(root) sklearn_fastapi_docker/
├── code/
│ └── train.py
├── data/
│ └── breast_cancer.csv
├── model/
│ └── model_binary.dat.gz
├── ms/
│ ├── __init__.py
│ └── functions.py
├── tests/
│ └── example_calls.txt
├── .gitignore
├── Dockerfile
├── LICENSE
├── README.md
├── main.py
└── requirements.txt
In the folder code/, we will find the main script for training our model
train.py,
Open in apphere we basically follow the standard data science process for access
Get unlimited
training a model: loading the data, preprocessing, training and finally
saving the model inside the folder model/.
$ python code/train.py
Web application
Now we are ready to create our FastAPI application!
https://engineering.rappi.com/using-fastapi-to-deploy-machine-learning-models-cd5ed7219ea 6/14
5/7/23, 1:33 PM Using FastAPI to deploy Machine Learning models | by Carl W. Handlin | Rappi Tech
We will start by creating the web application using FastAPI following the
script in main.py:
197 3
https://engineering.rappi.com/using-fastapi-to-deploy-machine-learning-models-cd5ed7219ea 7/14
5/7/23, 1:33 PM Using FastAPI to deploy Machine Learning models | by Carl W. Handlin | Rappi Tech
1 # Local imports
2 import datetime
3
4 # Third party imports
5 from pydantic import BaseModel, Field
6
7 from ms import app
8 from ms.functions import get_model_response
9
10
11 model_name = "Breast Cancer Wisconsin (Diagnostic)"
12 version = "v1.0.0"
13
14
15 # Input for data validation
16 class Input(BaseModel):
17 concavity_mean: float = Field(..., gt=0)
18 concave_points_mean: float = Field(..., gt=0)
19 perimeter_se: float = Field(..., gt=0)
20 area_se: float = Field(..., gt=0)
21 texture_worst: float = Field(..., gt=0)
22 area_worst: float = Field(..., gt=0)
23
24 class Config:
25 schema_extra = {
26 "concavity_mean": 0.3001,
27 "concave_points_mean": 0.1471,
28 "perimeter_se": 8.589,
29 "area_se": 153.4,
30 "texture_worst": 17.33,
31 "area_worst": 2019.0,
32 }
33
34
35 # Ouput for data validation
36 class Output(BaseModel):
37 label: str
38 prediction: int
39
40
41 @app.get('/info')
42 async def model_info():
43 """Return model information, version, how to call"""
44 return {
https://engineering.rappi.com/using-fastapi-to-deploy-machine-learning-models-cd5ed7219ea 8/14
5/7/23, 1:33 PM Using FastAPI to deploy Machine Learning models | by Carl W. Handlin | Rappi Tech
44 return {
Here
45
we create the routes for the endpoints of our application, an /info route
"name": model_name,
with
46 the GET method version
"version": to retrieve the information from the model and a
47
/predict }
route with the POST method to receive input data, run the data
48
through the pipeline and produce a prediction for the client. The file
49
ms/functions.py
50 contains the auxiliary functions in helping run the model:
@app.get('/health')
51 async def service_health():
52 """Return service health"""
1 import pandas as pd
53 return {
2 from ms import model
54 "ok"
3
55 }
4
56
5 def predict(X, model):
57
6 prediction = model.predict(X)[0]
7 return prediction
8
9
10 def get_model_response(input):
11 X = pd.json_normalize(input.__dict__)
12 prediction = predict(X, model)
13 if prediction == 1:
14 label = "M"
15 else:
16 label = "B"
17 return {
18 'label': label,
19 'prediction': int(prediction)
20 }
https://engineering.rappi.com/using-fastapi-to-deploy-machine-learning-models-cd5ed7219ea 9/14
5/7/23, 1:33 PM Using FastAPI to deploy Machine Learning models | by Carl W. Handlin | Rappi Tech
$ uvicorn main:app
This command will run the development server locally and listen to port
8000 where we can test our application! The folder /tests contain some
example calls to test that our application is up and running:
{
"name": "Breast Cancer Wisconsin (Diagnostic)",
"version": "v1.0.0"
}
https://engineering.rappi.com/using-fastapi-to-deploy-machine-learning-models-cd5ed7219ea 10/14
5/7/23, 1:33 PM Using FastAPI to deploy Machine Learning models | by Carl W. Handlin | Rappi Tech
Container
Finally, we can create our container, following the Dockerfile we give the
instructions to create an image with everything we need to run the
application we just created.
https://engineering.rappi.com/using-fastapi-to-deploy-machine-learning-models-cd5ed7219ea 11/14
5/7/23, 1:33 PM Using FastAPI to deploy Machine Learning models | by Carl W. Handlin | Rappi Tech
1 # Base image
2 FROM python:3.10
3
4 # Set working directory
5 WORKDIR /app
6
7 # Copy files
8 COPY main.py /app
9 COPY requirements.txt /app
10 COPY model /app/model
11 COPY ms /app/ms
12
13 # Install dependencies
14 RUN pip install -r requirements.txt
15
16 # Run the application
17 EXPOSE 8000
18 CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]
The process should take a couple of minutes, but in the end, we are going to
have a Docker image with the name sklearn_fastapi_docker which will serve
as the base for our application.
https://engineering.rappi.com/using-fastapi-to-deploy-machine-learning-models-cd5ed7219ea 12/14
5/7/23, 1:33 PM Using FastAPI to deploy Machine Learning models | by Carl W. Handlin | Rappi Tech
This will launch the application at localhost where we will be able to test it
on the port 8000, we can use another example call to test it with a single
observation:
{
"label": "M",
"prediction": 1
}
I must add that working with FastAPI feels very easy for creating this type of
applications for machine learning models. Data validation and
documentation provide two benefits of working with the tool without the
need for additional components. Give it a try!
References
[1] Christopher Samiullah, 2021 “FastAPI vs Flask — The Complete Guide”
https://christophergs.com/python/2021/06/16/python-flask-fastapi/
https://engineering.rappi.com/using-fastapi-to-deploy-machine-learning-models-cd5ed7219ea 13/14
5/7/23, 1:33 PM Using FastAPI to deploy Machine Learning models | by Carl W. Handlin | Rappi Tech
https://engineering.rappi.com/using-fastapi-to-deploy-machine-learning-models-cd5ed7219ea 14/14