Sentiment Analysis (Part 3): Deploying an App to Hugging Face using Docker.
1. Introduction
In today’s world, sentiment analysis has become an essential tool for businesses to gauge customer satisfaction and understand consumer behavior. In this article, we will explore how to deploy a sentiment analysis app to Hugging Face using Docker. We will discuss the architecture of the app, how it uses a pre-trained sentiment analysis model to classify the sentiment of text input, and the benefits of using Hugging Face and Docker for deployment. By the end of this article, you will have a clear understanding of how to deploy any app to Hugging Face using Docker.
2. Overview of the Sentiment Analysis App
2.1 The Architecture of the App
The sentiment analysis app has two main components: a user interface built with Gradio, and a back-end server. The interface allows users to input text and receive a sentiment score indicating positive, negative, or neutral sentiment. The back-end server uses a finetuned pre-trained sentiment analysis model based on DistilBERT to process the text input and classify the sentiment. The app provides a simple and intuitive way for users to perform sentiment analysis on COVID-19 vaccines tweets.
2.2 How the App works.
The app uses a pre-trained sentiment analysis model that is fine-tuned on a dataset of tweets related to COVID-19. The model is based on the DistilBERT architecture and is hosted on the Hugging Face platform. The app’s user interface is built using Gradio, a Python library that allows for the creation of customizable interfaces for machine learning models.
When a user inputs a text or tweet:
- The input is preprocessed using a custom function.
- The preprocessed input is then passed through the pre-trained model.
- The output is a set of scores for each of the three sentiment labels: negative, neutral, and positive.
The highest score is considered as the predicted sentiment of the input text or tweet. The user can use this sentiment analysis to gauge the overall sentiment of a given set of tweets related to COVID-19.
3. Dockerizing the App
3.1 Importance of Docker in Software Development
Docker has become an essential tool for software deployment in today’s world. Docker allows developers to package their applications into containers, which can be deployed on any platform or operating system. Containers are lightweight and efficient, making them an ideal solution for deploying applications at scale.
3.2 Creating A Dockerfile
# read the doc: https://huggingface.co/docs/hub/spaces-sdks-docker
# you will also find guides on how best to write your Dockerfile
FROM python:3.9
WORKDIR /code
COPY ./requirements.txt /code/requirements.txt
RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
# Set up a new user named "user" with user ID 1000
RUN useradd -m -u 1000 user
# Switch to the "user" user
USER user
# Set home to the user's home directory
ENV HOME=/home/user \
PATH=/home/user/.local/bin:$PATH
# Set the working directory to the user's home directory
WORKDIR $HOME/app
# Copy the current directory contents into the container at $HOME/app setting the owner to the user
COPY --chown=user . $HOME/app
CMD ["python", "app.py"]
Here’s an explanation of each step in the Dockerfile:
FROM python:3.9
- This line sets the base image for your Docker image to the official Python 3.9 image.WORKDIR /code
- This line sets the working directory of your Docker container to/code
.COPY ./requirements.txt /code/requirements.txt
- This line copies therequirements.txt
file from your local machine into the Docker container at/code/requirements.txt
.RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
- This line runs thepip
package manager inside the Docker container to install the Python dependencies specified inrequirements.txt
.RUN useradd -m -u 1000 user
- This line creates a new user nameduser
with a user ID of1000
.USER user
- This line switches the user context in the Docker container touser
.ENV HOME=/home/user PATH=/home/user/.local/bin:$PATH
- This line sets environment variables for the Docker container to configure thePATH
andHOME
directory for theuser
user.WORKDIR $HOME/app
- This line sets the working directory of the Docker container to$HOME/app
.COPY --chown=user . $HOME/app
- This line copies the contents of your local machine's current directory into the Docker container at$HOME/app
, setting the owner to theuser
user.CMD ["python", "app.py"]
- This line specifies the default command to run inside the Docker container when it starts, which is to run theapp.py
file using thepython
interpreter.
3.3 Deploying the App to Hugging Face Using Docker
To deploy your app on hugging face using Docker, follow the following steps:
Step 1: Go to Hugging Face Homepage
Kindly visit https://huggingface.co/ Navigate to spaces and click on create a new space.
Step 2: Create the spaces.
To deploy your app using Docker, select the framework and give your space a name, then click on “Create Space”. Hugging Face will automatically generate a README.md file for you.
Step 3: Clone the repository
Hugging Face spaces function like GitHub repositories and can be managed using Git commands.
To initialize a new repository in a directory, navigate to the directory in the command line and run:
git clone https://huggingface.co/spaces/bright1/MyFirstDockerApp
In this case your command will be:
git clone https://huggingface.co/spaces/<username>/<spaces’s name>
After cloning, the repository will be available locally in your base directory.
Step 4: Add your files
Add your app.py files, requirements.txt file, and Dockerfile to the local repository you just cloned. The Dockerfile will contain instructions to install the libraries and dependencies specified in the requirements.txt file and run your app.py file.
Step 5: Commit all changes and push changes to your remote repository (Spaces)
git add *
git commit -m 'Add a commit message'
git push
Once done pushing the files, the files should appear in your hugging face spaces.
Hugging face starts building the app in the spaces. Once the app is built successfully in the app section of the Spaces, the status changes from “building” to “running”.
Below is how my final app looks like. Kindly find the app here