This tutorial explains how to build a containerized sentiment analysis API using Hugging Face, FastAPI, and Docker.
According to various reports (e.g. Hardvard Business Review), many AI projects fail. I suspect that one of the barriers to AI project success is the technical steps from building the model to making it widely available to others in the organization.
So how do you make your model easy to use? One way is to wrap this in an API and containerize it, allowing you to expose your model to any server with Docker installed. That’s exactly what we’ll do in this tutorial.
Let’s take a sentiment analysis model from Hugging Face (chosen at random to have an easy model to demonstrate, for example), create an API endpoint that exposes the model using FastAPI, and then containerize the sentiment analysis app like this: Docker. I will provide code examples and explanations throughout.
The tutorial code has been tested on Linux and should also work on Windows.
We will use Hugging Face’s Pipeline class. transformers
library. If you’re unfamiliar with pipelines, see Hugging Face’s tutorial for an introduction to pipelines.
Pipelines make it very easy to work with models like sentiment models. Check out Hugging Face’s sentiment analysis tutorial for a thorough introduction to the concept.
You can instantiate a pipe using several different constructor arguments. One way is to pass the task type.
from transformers import pipelinepipe = pipeline(task="sentiment-analysis")
This will use Hugging Face’s default model for the given task.
Another way is to pass a model argument that specifies the model to use. You don’t…