This tutorial explains how to build a containerized sentiment analysis API using Hugging Face, FastAPI and Docker

Photo by Joshua Hoehne on Unsplash

Many AI projects fail, according to various reports (eg. Hardvard Business Review). I speculate that part of the barrier to AI project success is the technical step from having built a model to making it widely available for 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 containerize it so that your model can be exposed on any server with Docker installed. And that’s exactly what we’ll do in this tutorial.

We will take a sentiment analysis model from Hugging Face (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 we’ll containerize our sentiment analysis app with Docker. I’ll provide code examples and explanations all the way.

The tutorial code has been tested on Linux, and should work on Windows too.

We will use the Pipeline class from Hugging Face’s transformers library. See Hugging Face’s tutorial for an introduction to the Pipeline if you’re unfamiliar with it.

The pipeline makes it very easy to use models such as sentiment models. Check out Hugging Face’s sentiment analysis tutorial for a thorough introduction to the concept.

You can instantiate the pipe with several different constructor arguments. One way is to pass in a type of task:

from transformers import pipeline

pipe = pipeline(task="sentiment-analysis")

This will use Hugging Face’s default model for the provided task.

Another way is to pass the model argument specifying which model you want to use. You don’t…

Leave a Reply