FastAPI Transformer model serving
There are many web frameworks we can use for serving. Sanic, Flask, and FastAPI are just some examples. However, FastAPI has recently gained a lot of attention because of its speed and reliability. In this section, we will use FastAPI and learn how to build a service according to its documentation. We will also use pydantic
to define our data classes. Let’s begin!
- Before we start, we must install
pydantic
and FastAPI:$ pip install pydantic $ pip install fastapi
- The next step is to make the data model for decorating the input of the API using
pydantic
. But before forming the data model, we must know what our model is and identify its input.We are going to use a question-answering (QA) model for this. As you know from Chapter 6, the input is in the form of a question and a context.
- By using the following data model, you can make the QA data model:
from pydantic import BaseModel class QADataModel(BaseModel): ...