Skip to content

Latest commit

 

History

History
125 lines (96 loc) · 4.44 KB

File metadata and controls

125 lines (96 loc) · 4.44 KB

FastAPI demo

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.

Contents

Install FastAPI

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]"

Creating a simple API

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;
  • /predict will 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.

Start the server

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 models

In detail:

  • uvicorn src.api.api:app is the location of app (src directory > api directory > api.py script > app object);
  • --reload makes the server reload every time we update;
  • --reload-dir app makes it only reload on updates to the api/ directory;
  • --reload-dir models makes it also reload on updates to the models/ directory;

Try the API

We can now test that the application is working. These are some of the possibilities:

  • Visit localhost:5000

  • Use curl

    curl -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.

Access the API documentation

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.

Try some requests

To try an API request, click on the "Try it out" button and click execute.

For example:

Positive review

{
  "reviews": [
    {
      "review": "This is a great film. I loved it!"
    }
  ]
}

Positive and negative reivews

{
  "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!"
    }
  ]
}

Test the API

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.