Learn how to deploy an API for Spam classification.
AI Deploy is covered by OVHcloud Public Cloud Special Conditions.
The use case is the Spam Ham Collection Dataset.
The objective of this tutorial is to show how it is possible to create, deploy, and call an API with AI Deploy.
To do this, we will use FastAPI, a web framework for developing RESTful APIs in Python. You will also learn how to build and use a custom Docker image for a FastAPI API.
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
- You also should have followed the tutorial AI Notebooks - Tutorial - Build your spam classifier. Find the code on the GitHub repository. You will be able to understand the creation of the spam classification model.
Instructions
You are going to follow different steps to build your FastAPI app.
- More information about FastAPI capabilities can be found here.
- A direct link to the full code can be found here.
Here we will mainly discuss how to write the model.py
and app.py
codes, the requirements.txt
file, and the Dockerfile
. If you want to see the whole code, please refer to the GitHub repository.
Create the API
Two Python files are created to define the model and build the API.
Define the model.py file
This Python file is dedicated to building and defining the Logistic Regression model. You can find the full code here.
You can find all the information on the method used in this notebook. You will be able to understand the process and the different steps to follow to build a spam classifier based on logistic regression.
First, we have to load the Spam Ham Collection Dataset.
Then, we create the function to split the dataset for training and test.
The last function allows us to build the model.
By calling the different functions as follows, you will be able to get a classification result (spam or not) as well as a confidence score.
Write the app.py file
Initialize an instance of FastAPI.
Define the data format.
Process the message sent by the user.
Define the GET
method.
Create the POST
method.
The classify_message
function allows the user to send a message.
It will then call the model and return the result of the classification.
All the functions defined above are in the app.py
Python file.
You can find the code on the GitHub repository.
Write the requirements.txt file for the API
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
.
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 the python:3.8
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:
Set the listening port of the container:
Define the entrypoint and the default launching command to start the application:
Give correct access rights to an 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 fastapi-spam-classification:latest.
Test it locally (optional)
Launch the following Docker command to launch your application locally on your computer:
The -p 8000:8000
argument indicates that you want to execute a port redirection from the port 8000 of your local machine into the port 8000 of the Docker container. The port 8000 is the default port used by FastAPI 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:8000
.
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:
Log in 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 FastAPI application:
--default-http-port 8000
indicates that the port to reach on the app URL is the 8000
.
--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.
Interact with the deployed API through the dashboard
By clicking on the link of your AI Deploy app, you will arrive at the following page.
How to interact with your API?
You can add /docs
at the end of the URL of your app.
In our example, the URL is as follows: https://faf8ea3b-7095-4352-8e22-28f1f4107c77.app.us-east-va.ai.cloud.ovh.us/docs
.
It provides a complete dashboard for interacting with the API!
To be able to send a message for classification, select /spam_detection_path
in the green box. Click on Try it out
and type the message of your choice in the dedicated zone.
To get the result of the prediction, click on the Execute
button.
Congratulations! You have obtained the result of the prediction with the label and the confidence score.
Go further
- You can imagine deploying an AI model with another tool: Gradio. Read this tutorial.
- Another way to create an AI Deploy app is to use Streamlit! Follow this tutorial.
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.