Build Gradio Docker Image and Deploy Publicly in 3 Minutes
open source
Aug 10, 2023

Build Gradio Docker Image and Deploy Publicly in 3 Minutes

Ce Gao
Co-founder

Gradio is a Python library that allows you to quickly create customizable UI components around your machine learning models. In this blog post, we'll show you how to build a Gradio Docker image and deploy it publicly in 3 minutes.

The deployment will give you a public URL (similar to xx.modelz.live) for your model that anyone can use without installing anything on their computer.

After that, you can share your model with others and get feedback on it!

Why build a Gradio Docker image, instead of share=True?

Gradio is a great tool for building machine learning models, but it can be difficult to deploy them. You need to set up a server, install dependencies, and configure everything just right. This process is time-consuming and error-prone. With Docker, you can easily package your model into a container that contains all the necessary dependencies and configuration files. This makes it easy to deploy your model on any machine with Docker installed.

First, create a Gradio app

It's easy to create a Gradio app. You can follow the official tutorial to create a Gradio app. Here is an example:

import gradio as gr

def greet(name):
    return "Hello " + name + "!"

demo = gr.Interface(fn=greet, inputs="text", outputs="text")
    
demo.launch()

Build a Gradio Docker image

After creating a Gradio app, you can build a Gradio Docker image. We provide a Dockerfile for CPU deployment and a Dockerfile.gpu for GPU. You can use them directly.

The Dockerfile is very simple:

FROM python:3.9

ARG GRADIO_SERVER_PORT=7860
ENV GRADIO_SERVER_PORT=${GRADIO_SERVER_PORT}

WORKDIR /workspace

ADD requirements.txt main.py /workspace/

RUN pip install -r /workspace/requirements.txt

CMD ["python", "/workspace/main.py"]

You just need to update the requirements.txt and main.py to your own. Then you could run the command to push it to the Docker Hub:

docker build -t docker.io/USER/IMAGE .
docker push docker.io/USER/IMAGE

Deploy to any cloud provider

After building the Gradio Docker image, you can deploy it. You could pick up a VM from any cloud provider, such as AWS, GCP, Azure, etc. Then you could run the command to deploy it:

$ pip install openmodelz
$ mdz start <your public ip>
$ mdz deploy --image docker.io/USER/IMAGE --port 7860

Then you will get a public URL for your model via mdz list, such as xx.modelz.live. OpenModelZ takes care of the rest for you!

Conclusion

In this blog post, we showed you how to build a Gradio Docker image and deploy it publicly in 3 minutes. We hope that this will help you get started with Gradio and make your models more accessible to others. If you have any questions or feedback, please let us know in our Discord server!