Image by author
Imagine leveraging a Hugging Face model to determine review sentiment. Traditionally, the first step would involve building such a model and ensuring that it works correctly.
However, current pre-trained models allow us to have these large language models (LLMs) ready with minimal effort.
Once we have this model ready to use, our main goal is to allow colleagues within a company to use this model without needing to download it or implement it from scratch.
To do this, we would create an endpoint API that allows users to call and use the model independently. This is what we call a start-to-finish project, built from start to finish.
Today we will implement a simple model using Hugging Face, FastAPI and Docker, demonstrating how to achieve this goal efficiently.
Step 1: Choose our HuggingFace model
The first thing we must do is choose a Hugging Face model that suits our needs. To do so, we can easily install Hugging Face in our environment using the following command:
pip install transformers
# remember to work with transformers we need either tensorflow or pytorch installed as well
pip install torch
pip install tensorflow
Now we need to import the pipeline command from the transformer library.
from transformers import pipeline
Then, using the pipeline command we can easily generate a model that defines the sentiment of a given text. We can do this using two different approaches: defining the “sentiment analysis” task or defining the model, as you can see in the following code.
# Defining directly the task we want to implement.
pipe = pipeline(task="sentiment-analysis")
# Defining the model we choose.
pipe = pipeline(model="model-to-be-used")
It is important to note that it is not recommended to use the task-based approach as it limits our control over the specific model that is used.
In my case, I chose “distilbert-base-uncased-fine tuned-sst-2-english” but you can explore Hugging Face Hub and choose any model that suits your needs. You can find a simple guide to face hugging in the next article.
pipe = pipeline(model="distilbert/distilbert-base-uncased-finetuned-sst-2-english")
Now that we have our pipeline model defined, just sending a simple message will get us our result. For example, for the following command:
print(pipe("This tutorial is great!"))
We would get ({'label': 'POSITIVE', 'score': 0.9998689889907837})
Let's imagine that we prefer that our users get a natural language phrase about this classification. We can implement some simple Python code that also does this:
def generate_response(prompt:str):
response = pipe("This is a great tutorial!")
label = response(0)("label")
score = response(0)("score")
return f"The '{prompt}' input is {label} with a score of {score}"
print(generate_response("This tutorial is great!"))
And repeating the same experiment we would obtain:
The message 'This tutorial is great!' the entry is POSITIVE with a score of 0.9997909665107727
We now have a working model and can proceed to define our API.
Step 2: Write the API endpoint for the model with FastAPI
To define our API we will use FastAPI. It is a Python framework for creating high-performance web APIs. First, install the FastAPI library using the pip command and import it into our environment. Additionally, we will use the pydantic library to ensure that our inputs are of the desired type.
The following code will generate a working API that our colleagues can use directly.
from fastapi import FastAPI
from pydantic import BaseModel
from transformers import pipeline
# You can check any other model in the Hugging Face Hub
pipe = pipeline(model="distilbert/distilbert-base-uncased-finetuned-sst-2-english")
# We define the app
app = FastAPI()
# We define that we expect our input to be a string
class RequestModel(BaseModel):
input: str
# Now we define that we accept post requests
@app.post("/sentiment")
def get_response(request: RequestModel):
prompt = request.input
response = pipe(prompt)
label = response(0)("label")
score = response(0)("score")
return f"The '{prompt}' input is {label} with a score of {score}"
This is what happens step by step in the code:
- Importing required libraries: The code begins by importing FastAPI and Pydantic, ensuring that the data we receive and send is structured correctly.
- Loading the model: We then load a pre-trained sentiment analysis model, as we already did in the first step.
- FastAPI Application Settings:
app = FastAPI()
initializes our FastAPI application, preparing it to handle requests. - Request Model Definition: Using Pydantic, a RequestModel class is defined. This class specifies that we expect an input string, ensuring that our API only accepts data in the correct format.
- Creation of the endpoint: the
@app.post("/sentiment")
The decorator tells FastAPI that this feature should be triggered when a POST request is made to the /sentiment endpoint. The get_response function takes as input a RequestModel object, which contains the text we want to parse. - Application Processing: Within the
get_response
function, the request text is extracted and passed to the model(pipe(prompt))
. The model returns a response with a sentiment label (such as “POSITIVE” or “NEGATIVE”) and a score indicating the confidence of the prediction. - Return response: Finally, the function returns a formatted string that includes the input text, sentiment label, and confidence score, providing a clear and concise result for the user.
If we run the code, the API will be available on our local host, as can be seen in the image below.
Screenshot of localhost endpoint with FastAPI
In a nutshell, this code sets up a simple web service that you can send a snippet of text to and it will respond with a sentiment analysis of that text, leveraging the powerful capabilities of the Hugging Face model via FastAPI. .
Next, we need to contain our application so that it can run anywhere, not just on our local computer. This will ensure greater portability and ease of deployment.
Step 3: Use Docker to run our model
Containerization involves placing your application in a container. A Docker container runs an instance of a Docker image, which includes its own operating system and all dependencies necessary for the application.
For example, you can install Python and all the necessary packages inside the container, so that it can run everywhere without needing to install those libraries.
To run our sentiment analysis application in a Docker container, we first need to create a Docker image. This process involves writing a Dockerfile, which acts as a recipe that specifies what the Docker image should contain.
If Docker is not installed on your system, you can download it from Docker website. Here is the Dockerfile we will use for this project, called Dockerfile in the repository.
# Use an official Python runtime as a parent image
FROM python:3.10-slim
# Set the working directory in the container
WORKDIR /sentiment
# Copy the requirements.txt file into the root
COPY requirements.txt .
# Copy the current directory contents into the container at /app as well
COPY ./app ./app
# Install any needed packages specified in requirements.txt
RUN pip install -r requirements.txt
# Make port 8000 available to the world outside this container
EXPOSE 8000
# Run main.py when the container launches, as it is contained under the app folder, we define app.main
CMD ("uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000")
Then we just need to run the following command in the terminal to create the docker image.
docker build -t sentiment-app .
And then to execute we have two options:
- Using our terminal with commands.
docker run -p 8000:8000 --name name_of_cointainer sentiment-hf
- Using the attachable hub. We can easily go to the Docker Hub and click the Run button on the image.
Screenshot from Dockerhub
And that's all! We now have a working sentiment classification model that can run anywhere and can be run via an API.
Soon
- Model selection and configuration: Choose and configure a pre-trained Hugging Face model for sentiment analysis, ensuring it meets your needs.
- API Development with FastAPI – Create an API endpoint using FastAPI, allowing easy interaction with the sentiment analysis model.
- Containerization with Docker: Containerize the application using Docker to ensure portability and seamless deployment across different environments.
You can check my complete code. in the following GitHub repository.
Joseph Ferrer He is an analytical engineer from Barcelona. He graduated in physical engineering and currently works in the field of data science applied to human mobility. He is a part-time content creator focused on data science and technology. Josep writes about all things ai, covering the application of the ongoing explosion in this field.