Learn how to deploy a web service for sentiment analysis on text using Hugging Face pre-trained models. To do this, you will use Flask, an open-source micro framework for web development in Python. You will also learn how to build and use a custom Docker image for a Flask application.
AI Deploy is covered by OVHcloud Public Cloud Special Conditions.
Overview of the app:
For more information about Hugging Face, please visit the Hugging Face website.
Requirements
- Access to the OVHcloud Control Panel
- An AI Deploy project created inside a Public Cloud project in your OVHcloud account
- A user for AI Deploy
- Docker installed on your local computer
- Some knowledge about building an image and Dockerfile
We also suggest that you conduct some tests to determine which Hugging Face model is best suited for your use case. Find examples on our GitHub repository.
Instructions
First, the tree structure of your folder should be as follows:
Find more information about the Flask application here to get ready to use it.
Write the Flask application
Create a Python file named app.py
.
Inside that file, import your required modules:
Create Flask app:
Load Hugging Face models:
Write the inference function:
Define the GET method:
Define the POST method:
Start your app:
Write the requirements.txt file for the application
The requirements.txt
file will allow us to write all the modules needed to make our application work. This file will be useful when writing the Dockerfile
.
Here we will mainly discuss how to write the app.py
code, the requirements.txt
file and the Dockerfile
. If you want to see the whole codebase and download the required contents for the static
and templates
directories, please refer to the GitHub repository.
Write the Dockerfile for the application
Your Dockerfile
should start with the FROM
instruction indicating the parent image to use. In our case we choose to start from a Python image:
Create the home directory and add your files to it:
Install the requirements.txt
file which contains your needed Python modules using a pip install ...
command:
Define your default launching command to start the application:
Give correct access rights to ovhcloud user (42420:42420
):
Build the Docker image from the Dockerfile
From the directory containing your Dockerfile, run one of the following commands to build your application image:
-
The first command builds the image using your system’s default architecture. This may work if your machine already uses the
linux/amd64
architecture, which is required to run containers with our AI products. However, on systems with a different architecture (e.g.ARM64
onApple Silicon
), the resulting image will not be compatible and cannot be deployed. -
The second command explicitly targets the
linux/AMD64
architecture to ensure compatibility with our AI services. This requiresbuildx
, which is not installed by default. If you haven’t usedbuildx
before, you can install it by running:docker buildx install
The dot argument .
indicates that your build context (place of the Dockerfile and other needed files) is the current directory.
The -t
argument allows you to choose the identifier to give to your image. Usually, image identifiers are composed of a name and a version tag <name>:<version>
. For this example, we chose sentiment_analysis_app:latest.
Test it locally (optional)
Launch the following Docker command to launch your application locally on your computer:
The -p 5000:5000
argument indicates that you want to execute a port redirection from the port 5000 of your local machine into the port 5000 of the Docker container. The port 5000 is the default port used by Flask applications.
NOTE: Don't forget the --user=42420:42420
argument if you want to simulate the exact same behavior that will occur on AI Deploy apps. It executes the Docker container as the specific OVHcloud user (user 42420:42420).
Once started, your application should be available on http://localhost:5000
.
Push the image into the shared registry
NOTE: The shared registry should only be used for testing purposes. Please consider creating and attaching your own registry. More information about this can be found here. The images pushed to this registry are for AI Tools workloads only, and will not be accessible for external uses.
Find the address of your shared registry by launching this command:
Login on the shared registry with your usual AI Platform user credentials:
Push the compiled image into the shared registry:
Launch the AI Deploy app
The following command starts a new app running your Flask application:
--default-http-port 5000
indicates that the port to reach on the app URL is the 5000
.
--cpu 4
indicates that we request 4 CPUs for that app.
Consider adding the --unsecure-http
attribute if you want your application to be reachable without any authentication.
Go further
- Discover another tool to deploy easily AI models: Gradio. Refer to this documentation.
For more information and tutorials, please see our other AI & Machine Learning support guides or explore the guides for other OVHcloud products and services.
If you need training or technical assistance to implement our solutions, contact your sales representative or click on this link to get a quote and ask our Professional Services experts for a custom analysis of your project.