Un projet de bout en bout simple à mettre en œuvre avec HuggingFace

Main cover. End-to-end project generation with Hugging Face, FastAPI, and Docker.

Using_hugging-_face_hub_for_a_data_science_project_Ferrer_1 Un projet de bout en bout simple à mettre en œuvre avec HuggingFace NEWS
Image by author

Imagine leveraging a Hugging Face model to determine the sentiment of reviews. Traditionally, the first step would be to create such a model and ensure it works correctly. However, today’s pre-trained models allow us to prepare extensive language models (LLMs) with minimal effort.

Once we have this model ready for use, our primary goal is to enable colleagues within a company to use this model without needing to download or implement it from scratch.

To achieve this, we would create an endpoint API, allowing users to call and use the model independently. This is what we call an end-to-end project, built from start to finish.

Today, we will deploy a simple model using Hugging Face, FastAPI, and Docker, demonstrating how to achieve this goal efficiently.

Step 1: Choosing Our Hugging Face Model

The first thing to do is to choose a Hugging Face model that fits our needs. 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

Next, we need to import the pipeline command from the transformers library.

from transformers import pipeline

Using the pipeline command, we can easily generate a model that determines the sentiment of a given text. We can do this using two different approaches: by defining the task as « sentiment-analysis » or by specifying the model, as shown in the following code snippet.

# 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 using the task-based approach is not recommended as it limits our control over the specific model used.

In my case, I chose the « distilbert-base-uncased-finetuned-sst-2-english » model, but you are free to browse the Hugging Face Hub and choose any model that suits your needs. You can find a simple guide on Hugging Face in the following article.

pipe = pipeline(model="distilbert/distilbert-base-uncased-finetuned-sst-2-english")

Now that our pipeline model is defined, we can send a simple prompt to retrieve our result. For example, for the following command:

print(pipe("This tutorial is great!"))

We would get [{‘label’: ‘POSITIVE’, ‘score’: 0.9998689889907837}]

Suppose we prefer our users to get a natural language sentence regarding this classification. We can implement a 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 by repeating the same experiment, we would get:

The message « This tutorial is great! » input is POSITIVE with a score of 0.9997909665107727

We now have a functional model, and we can proceed to define our API.

Step 2: Writing 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 our inputs are of the desired type.

The following code will generate a functional 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}"

Here is what happens step-by-step in the code:

  1. Importing necessary libraries: The code starts by importing FastAPI and Pydantic, ensuring that the data we receive and send is properly structured.
  2. Loading the model: Next, we load a pre-trained sentiment analysis model, as we did in the first step.
  3. Setting up the FastAPI application: app = FastAPI() initializes our FastAPI application, making it ready to handle requests.
  4. Defining the request model: Using Pydantic, a RequestModel class is defined. This class specifies that we expect a string input, ensuring that our API only accepts data in the correct format.
  5. Creating the endpoint: The @app.post("/sentiment") decorator tells FastAPI that this function should be triggered when a POST request is made to the /sentiment endpoint. The get_response function takes in a RequestModel object, which contains the text we want to analyze.
  6. Processing the request: Inside the get_response function, the text from the request is extracted and passed to the model (pipe(prompt)). The model returns a response with the sentiment label (like « POSITIVE » or « NEGATIVE ») and a score indicating the confidence of the prediction.
  7. Returning the response: Finally, the function returns a formatted string that includes the input text, the sentiment label, and the confidence score, providing a clear and concise result to the user.

If we run the code, the API will be available on our local host, as observed in the image below.

Using_hugging-_face_hub_for_a_data_science_project_Ferrer_2 Un projet de bout en bout simple à mettre en œuvre avec HuggingFace NEWS Using_hugging-_face_hub_for_a_data_science_project_Ferrer_2 Un projet de bout en bout simple à mettre en œuvre avec HuggingFace NEWS
Screenshot of the local host endpoint with FastAPI

In simple terms, this code sets up a web service to which you can send a piece of text, 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 containerize our application so 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 the dependencies needed for the application.

For example, you can install Python and all required packages in the container so it can run anywhere without needing to install such 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 specifying what the Docker image should contain.

If Docker is not installed on your system, you can download it from the Docker website. Here is the Dockerfile we will use for this project, named 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"]

Next, we need to run the following command in the terminal to create the Docker image.

docker build -t sentiment-app .

And then to run, we have two options:

  1. Using our terminal with commands.

    docker run -p 8000:8000 --name name_of_container sentiment-hf

  2. Using Docker Hub. We can easily access Docker Hub and click the Run button on the image.

    Using_hugging-_face_hub_for_a_data_science_project_Ferrer_3 Un projet de bout en bout simple à mettre en œuvre avec HuggingFace NEWS Using_hugging-_face_hub_for_a_data_science_project_Ferrer_3 Un projet de bout en bout simple à mettre en œuvre avec HuggingFace NEWS
    Screenshot of Docker Hub

And that’s it! We now have a functional sentiment classification model that can run anywhere and be executed using an API.

In Summary

  • Model Selection and Setup: 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, enabling 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 all my code in the following GitHub repository.

Josep Ferrer is an analytical engineer from Barcelona. He holds a degree 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 boom in this field.

Source