This tutorial explains how to create a containerized sentiment analysis API using Hugging Face, FastAPI, and Docker.
Many ai projects fail, according to various reports (e.g. ai-projects-on-track” rel=”noopener ugc nofollow” target=”_blank”>Harvard Business Review). I speculate that part of the barrier to successful ai project is the technical step from having built a model to making it widely available to others in your organization.
So how do you make your model easily available for consumption? One way is to wrap it in an API and wrap it so that your model can be exposed on any server with Docker installed. And that is exactly what we will do in this tutorial.
We'll take a Hugging Face sentiment analysis model (an arbitrary choice just to have a model that's easy to show as an example), write an API endpoint that exposes the model using FastAPI, and then package our sentiment analysis app with Docker . I will provide code examples and explanations throughout.
The tutorial code has been tested on Linux and should work on Windows as well.
We will use Hugging Face's Pipeline class transformers
library. See Hugging Face Tutorial for an introduction to Pipeline if you are not familiar with it.
Pipelining makes it easier to use models such as sentiment models. Check out Hugging Face sentiment analysis tutorial for a complete introduction to the concept.
You can instantiate the pipe with several different constructor arguments. One way is to pass a task type:
from transformers import pipelinepipe = pipeline(task="sentiment-analysis")
This will use the default Hugging Face model for the given task.
Another way is to pass the model argument specifying which model you want to use. You do not…