In this demo we will see the main features of FastAPI to create an API for a simple machine learning project.
The scrips used in this demo are based on the SE4AI2021Course_FastAPI-demo GitHub project.
The first step is to install FastAPI and Uvicorn, which is a fast ASGI server (it can run asynchronous code in a single process) to launch our application. To do this, we can run the following command:
uv add fastapi "uvicorn[standard]"We can now create a simple API. To do this, we create a new file called api.py in the src/api
directory. In this file, we will create a FastAPI application and two endpoints:
/will be the root endpoint, which will return a welcome message;/predictwill return the prediction using the model specified in the path parameter.
Since the /predict endpoint receives a payload specifying the review, we will create a Pydantic
class called PredictRequest to represent our payload. We will use this class to validate that the input of the users is correct. This class will be located in the
schemas.py file.
Use the following command to start the server:
uvicorn src.api.api:app \
--host 0.0.0.0 \
--port 5000 \
--reload \
--reload-dir src/api \
--reload-dir modelsIn detail:
uvicorn src.api.api:appis the location of app (srcdirectory >apidirectory >api.pyscript >appobject);--reloadmakes the server reload every time we update;--reload-dir appmakes it only reload on updates to theapi/directory;--reload-dir modelsmakes it also reload on updates to themodels/directory;
We can now test that the application is working. These are some of the possibilities:
-
Visit localhost:5000
-
Use
curlcurl -X GET http://localhost:5000/
-
Access the API programmatically, e.g.:
import json import requests response = requests.get("http://localhost:5000/") print(json.loads(response.text))
-
Use an external tool like Postman, which lets you execute and manage tests that can be saved and shared with others.
You can access the Swagger UI in http://localhost:5000/docs for documentation
endpoint and select one of the models. The documentation generated via Redoc is
accessible at the /redoc endpoint.
API User Interface in localhost:5000/docs endpoint.
To try an API request, click on the "Try it out" button and click execute.
For example:
{
"reviews": [
{
"review": "This is a great film. I loved it!"
}
]
}{
"reviews": [
{
"review": "This is a great film. I loved it!"
},
{
"review": "It was so hard not to fall asleep during this movie. It was too boring!"
}
]
}We can now test the API using Pytest. To do this, we create a new file called test_api.py in the tests/ directory.
Here we will create a fixture called client that will be used to test the API. We will also create a second fixture called payload that will be used to test the /predict/tabular/{type} endpoint. Since our endpoints expect the payload in JSON format we must build the payload return value according to the same format. If you have correctly implemented your API, the /docs endpoint will show you an example of the payload expected by each of your endpoints.
Finally, we will create a test for each endpoint. To do this, we will use the client fixture to make requests to the API and check the response.
