AI Deploy - Tutorial - Deploy a Gradio app for sketch recognition

How to build and use a custom Docker image containing a Gradio application

Last updated 31st January, 2023.

AI Deploy is in beta. During the beta-testing phase, the infrastructure’s availability and data longevity are not guaranteed. Please do not use this service for applications that are in production, as this phase is not complete.

AI Deploy is covered by OVHcloud Public Cloud Special Conditions.


The purpose of this tutorial is to deploy an application for sketch recognition using a trained model.

The use case is handwritten digits recognition, based on the MNIST dataset.

In order to do this, you will use Gradio, an open-source Python library which 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:




You are going to follow different steps to build your Gradio application.

  • More information about Gradio capabilities can be found here.
  • Direct link to the full Python file can be found here here.

Here we will mainly discuss on how to write the 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

Inside that file, import your required modules.

import gradio as gr
import tensorflow as tf
import cv2

Define the elements that make up the AI Deploy app: title, header and references.

title = "Welcome on your first sketch recognition app!"

head = (
  "<img src='file/mnist-classes.png' width=400>"
  "The robot was trained to classify numbers (from 0 to 9). To test it, write your number in the space provided."

ref = "Find the whole code [here]("

Specify the input images size and the classes names.

img_size = 28

labels = ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"]

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.

model = tf.keras.models.load_model("model/sketch_recognition_numbers_model.h5")

Create the function that recognizes the written number.

def predict(img):

  img = cv2.resize(img, (img_size, img_size))
  img = img.reshape(1, img_size, img_size, 1)

  preds = model.predict(img)[0]

  return {label: float(pred) for label, pred in zip(labels, preds)}

label = gr.outputs.Label(num_top_classes=3)

Launch the Gradio interface.

interface = gr.Interface(fn=predict, inputs="sketchpad", outputs=label, title=title, description=head, article=ref)
interface.launch(server_name="", server_port=8080)

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:

FROM python:3.7

Create the home directory and add your files to it:

WORKDIR /workspace
ADD . /workspace

Install your needed Python modules using a pip install ... command with the requirements.txt file which contains all modules:

RUN pip install -r requirements.txt

Give correct access rights to the ovhcloud user (42420:42420):

RUN chown -R 42420:42420 /workspace
ENV HOME=/workspace

Define your default launching command to start the application:

CMD [ "python3" , "/workspace/" ]

Build the Docker image from the Dockerfile

Launch the following command from the Dockerfile directory to build your application image:

docker build . -t gradio_app:latest

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.

Please make sure that the docker image you will push in order to run containers using AI products respects the linux/AMD64 target architecture. You could, for instance, build your image using buildx as follows:

docker buildx build --platform linux/amd64 ...

Push the image into the shared registry

The shared registry of AI Deploy should only be used for testing purposes. Please consider attaching your own Docker registry. More information about this can be found here.

Find the address of your shared registry by launching this command:

ovhai registry list

Log in on the shared registry with your usual OpenStack credentials:

docker login -u <user> -p <password> <shared-registry-address>

Push the created image into the shared registry:

docker tag gradio_app:latest <shared-registry-address>/gradio_app:latest
docker push <shared-registry-address>/gradio_app:latest

Launch the AI Deploy app

The following command starts a new AI Deploy app running your Gradio application:

ovhai app run \
      --cpu 1 \
      --volume <my_saved_model>@<region>/:/workspace/model:RO \

--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.

Consider adding the --unsecure-http attribute if you want your application to be reachable without any authentication.

Go further

  • You can imagine deploying an AI model with an other tool: Flask. Refer to this tutorial.
  • Do you want to use Streamlit to create a audio classification app? Here it is.


Please send us your questions, feedback and suggestions to improve the service:

Czy ten przewodnik był pomocny?

Zachęcamy do przesyłania sugestii, które pomogą nam ulepszyć naszą dokumentację.

Obrazy, zawartość, struktura - podziel się swoim pomysłem, my dołożymy wszelkich starań, aby wprowadzić ulepszenia.

Zgłoszenie przesłane za pomocą tego formularza nie zostanie obsłużone. Skorzystaj z formularza "Utwórz zgłoszenie" .

Dziękujemy. Twoja opinia jest dla nas bardzo cenna.

Inne przewodniki, które mogą Cię zainteresować...

OVHcloud Community

Dostęp do OVHcloud Community Przesyłaj pytania, zdobywaj informacje, publikuj treści i kontaktuj się z innymi użytkownikami OVHcloud Community.

Porozmawiaj ze społecznością OVHcloud

Zgodnie z Dyrektywą 2006/112/WE po zmianach, od dnia 1 stycznia 2015 r., ceny brutto mogą różnić się w zależności od kraju zameldowania klienta
(ceny brutto wyświetlane domyślnie zawierają stawkę podatku VAT na terenie Polski).