Learn how to deploy an application for sketch recognition using a trained model.
AI Deploy is covered by OVHcloud Public Cloud Special Conditions.
The use case is handwritten digit recognition, based on the MNIST dataset.
To do this, you will use Gradio, an open-source Python library that is a quick way to expose and use Machine Learning models. You will also learn how to build and use a custom Docker image for a Gradio application.
Overview of the app:
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 image and Dockerfile
- You also should have followed the steps of the Image Classification with MNIST dataset notebook from the GitHub repository. You will be able to train and save your model. To launch this notebook and run it, please refer to this documentation.
Instructions
You are going to follow different steps to build your Gradio application.
- More information about Gradio's capabilities can be found here.
- A direct link to the full Python file can be found here.
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 code, please refer to the GitHub repository.
Write the Gradio application
Create a Python file named app.py
.
Inside that file, import your required modules.
Define the elements that make up the AI Deploy app: title, header, and references.
Specify the input image's size and the class names.
Load the previously trained model for handwritten digits classification.
To learn more about how you can save a model with TensorFlow, please refer to the part "Save and export the model for future inference" of the notebook.
Here you will use your trained model, then save it in an Object Storage container. Click here to learn more about Object Storage.
Create the function that recognizes the written number.
Launch the Gradio interface.
Write the requirements.txt file for the application
The requirements.txt
file will allow us to get 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.7
image:
Create the home directory and add your files to it:
Install your needed Python modules using a pip install ...
command with the requirements.txt
file which contains all modules:
Give correct access rights to the ovhcloud user (42420:42420
):
Define your default launching command to start the application:
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 gradio_app:latest.
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 created image into the shared registry:
Launch the AI Deploy app
The following command starts a new AI Deploy app running your Gradio application:
--cpu 1
indicates that we request 1 CPU for that AI Deploy app.
If you want, you can also launch this AI Deploy app with one or more GPUs.
To launch your Gradio app, you need to attach 1 volume to this AI Deploy app. It contains the model that you trained before in part "Save and export the model for future inference" of the notebook.
--volume <my_saved_model>@<region>/:/workspace/saved_model:RO
is the volume attached for using your pretrained model. This volume is read-only (RO
) because you just need to use the model and not make any changes to this Object Storage container.
If you want your AI Deploy app to be accessible without the need to authenticate, specify it as follows.
--unsecure-http
attribute if you want your application to be reachable without any authentication.
Go further
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.