AI Deploy - Tutoriel - Construire & utiliser une image Streamlit (EN)

Comment construire et utiliser votre propre image Docker contenant une application Streamlit

Last updated 3rd November, 2022.

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.

Objective

Streamlit is a python framework that turns scripts into shareable web application.

The purpose of this tutorial is to provide a concrete example on how to build and - On the use a custom Docker image for a Streamlit applications.

Requirements

Instructions

Write a simple Streamlit application

Create a simple python file with name simple_app.py.

Inside that file, import your required modules:

import streamlit as st
import pandas as pd

Display all information you want on your Streamlit application:

st.title('My first app')
st.write("Here's our first attempt at using data to create a table:")
st.write(pd.DataFrame({
    'first column': [1, 2, 3, 4],
    'second column': [10, 20, 30, 40]
}))
  • More information about Streamlit capabilities can be found here
  • Direct link to the full python file can be found here here

Write the Dockerfile for your application

Your Dockerfile should start with the the FROM instruction indicating the parent image to use. In our case we choose to start from a classic python image.

FROM python:3.8

Install your needed python module using a pip install ... command. In our case we only need these 2 modules:

  • streamlit
  • pandas
RUN pip install streamlit pandas

Install your application inside your image. In our case, we just copy our python file inside the /opt directory.

COPY simple_app.py /opt/simple_app.py

Define your default launching command to start the application:

CMD [ "streamlit" , "run" , "/opt/simple_app.py", "--server.address=0.0.0.0" ]

In order to access the app from the outside world, don't forget to add the --server.address=0.0.0.0 instruction on your streamlit run ... command. By doing this you indicate to the process that it have to bind on all network interfaces and not only the localhost.

Create the home directory of the ovhcloud user (42420:42420) and give it correct access rights:

RUN mkdir /workspace && chown -R 42420:42420 /workspace
ENV HOME /workspace
WORKDIR /workspace

This last step is mandatory because streamit needs to be able to write inside the HOME directory of the owner of the process in order to work properly.

  • More information about Dockerfiles can be found here
  • Direct link to the full Dockerfile can be found here here

Build the docker image from the dockerfile

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

docker build . -t streamlit-example:latest

The dot . argument indicates that your build context (place of the Dockerfile and other needed files) is the current directory.

The -t argument allow 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 streamlit-example:latest.

Test it locally (optional)

Launch the following docker command to launch your application locally on your computer:

docker run --rm -it -p 8501:8501 --user=42420:42420 streamlit-example:latest

The -p 8501:8501 argument indicates that you want to execute a port rediction from the port 8501 of your local machine into the port 8501 of the docker container. The port 8501 is the default port used by streamlit applications.

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

Push the image into the shared registry

The shared registry of AI Deploy should only be use 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

Login on the shared registry with your usual Openstack credentials

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

Push the compiled image into the shared registry:

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

Launch the AI Deploy app

The following command starts a new app running your Streamlit application:

ovhai app run --default-http-port 8501 --cpu 1 <shared-registry-address>/streamlit-example:latest

--default-http-port 8501 indicates that the port to reach on the app URL is the 8501.

--cpu 1 indicates that we only request 1 CPU for that app.

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

Once the AI Deploy app is running you can access your Streamlit application directly from the app's URL.

image

Go further

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

Feedback

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


Cette documentation vous a-t-elle été utile ?

N’hésitez pas à nous proposer des suggestions d’amélioration afin de faire évoluer cette documentation.

Images, contenu, structure… N’hésitez pas à nous dire pourquoi afin de la faire évoluer ensemble !

Vos demandes d’assistance ne seront pas traitées par ce formulaire. Pour cela, utilisez le formulaire "Créer un ticket" .

Merci beaucoup pour votre aide ! Vos retours seront étudiés au plus vite par nos équipes..


Ces guides pourraient également vous intéresser...

OVHcloud Community

Accedez à votre espace communautaire. Posez des questions, recherchez des informations, publiez du contenu et interagissez avec d’autres membres d'OVHcloud Community.

Echanger sur OVHcloud Community

Conformément à la Directive 2006/112/CE modifiée, à partir du 01/01/2015, les prix TTC sont susceptibles de varier selon le pays de résidence du client
(par défaut les prix TTC affichés incluent la TVA française en vigueur).