Implementing an efficient prediction endpoint
Now that we have a way to save and load our machine learning models, it's time to use them in a FastAPI project. As you'll see, the implementation shouldn't be too much of a surprise if you've followed this book. The main part of the implementation is the class dependency, which will take care of loading the model and making predictions. If you need a refresher on class dependencies, check out Chapter 5, Dependency Injections in FastAPI.
Let's go! Our example will be based on the newgroups model we dumped in the previous section. We'll start by showing you how to implement the class dependency, which will take care of loading and making predictions:
chapter13_prediction_endpoint.py
class PredictionInput(BaseModel): text: str class PredictionOutput(BaseModel): category: str class NewsgroupsModel: model: Optional[Pipeline] ...