Deploy a Gradio App

This guide explains how to deploy a basic Gradio (opens in a new tab) application to Koyeb using:

  1. Git-driven deployment to automatically build and deploy a new version of your application each time a change is detected on your branch.
  2. Pre-built containers you can deploy from any public or private registry.

To successfully follow this documentation, you will need to have a Koyeb account (opens in a new tab). You can optionally install the Koyeb CLI if you prefer to follow this guide without leaving the terminal.

You can deploy and preview the Gradio application from this guide by clicking the Deploy to Koyeb button below.

Deploy to Koyeb (opens in a new tab)

You can consult the repository on GitHub (opens in a new tab) to find out more about the example application that this guide uses.

Create the Gradio app

We will start by creating a basic Gradio application. You will need a recent version of Python (opens in a new tab) as well as pip, the Python packaging tool, installed on your local machine. Additionally, you may wish to install Docker (opens in a new tab) if you are planning on building and testing a container image for the application locally.

Create a new project

To get started, create a new directory for your project files and navigate into it:

mkdir example-gradio
cd example-gradio

Inside the new directory, create a virtual environment for the new project and activate it by typing:

python3 -m venv venv
source venv/bin/activate

Your prompt will likely change to reflect that the virtual environment is now active.

Install the package

Now we can install Gradio by typing:

pip install gradio

Save your project dependencies to a requirements.txt file by typing:

pip freeze > requirements.txt

Create a basic Gradio application

Now that the dependencies are installed, you can create a basic Gradio application.

Create a file called app.py with the following contents:

app.py
import gradio as gr
import os
 
 
def greet(name, intensity):
    return(f"Hello, {name or 'world'}" + "!" * int(intensity))
 
demo = gr.Interface(
        fn=greet,
        inputs=["text", "slider"],
        outputs=["text"]
        )
 
port = int(os.environ.get('PORT', 7860))
 
demo.launch(server_name="0.0.0.0", server_port=port)

This application sets up a basic web page with some input fields and an output field that renders results. The web server will run on the port specified by the PORT environment variable, using port 7860 as a fallback value.

Test the application

Test the application by typing:

python app.py

Navigate to http://localhost:7860 in your web browser to see the application's web page.

Create a Dockerfile for the project (Optional)

We can build and run our Gradio project on Koyeb using the native Python buildpack, but we can also optionally build from a Dockerfile for more control. To make it possible to build a container image for our application, we just need to create the Dockerfile. We'll also define a .dockerignore file to tell the builder what files to skip when creating the image.

First, define a .dockerignore file in your main project directory. Inside, paste the following contents:

.dockerignore
.git
.gitignore
Dockerfile
.dockerignore
venv
flagged

This file tells Docker to not include Git files, the Docker files themselves, the Python virtual environment, or the flagged directory that is generated by interface interactions. This helps ensure that the image we build is not bloated and that the build completes faster.

Next, create a new file called Dockerfile within the main project directory. Inside, paste the following contents:

Dockerfile
FROM python:slim
 
WORKDIR /app
COPY . .
RUN pip install -r requirements.txt
 
CMD ["python", "app.py"]

This Dockerfile is based on the slim version of the python image (opens in a new tab). The Dockerfile copies all of the project files to the image's filesystem and then installs the dependencies with pip.

If you have Docker installed locally, you can build and test the image on your computer and optionally upload it to a registry. You can deploy container images from any container registry to Koyeb.

We can also build the Dockerfile directly from the repository when we deploy, which is useful as a way of automatically deploying when changes occur. We will demonstrate this method as one of the options in this guide.

Push the project to GitHub

In the project directory, initialize a new git repository by running the following command:

git init

Create a new GitHub repository (opens in a new tab) for your Gradio project if you haven't already done so.

Pull down GitHub's default .gitignore file for Python to help avoid accidentally committing unnecessary and unwanted files:

curl -L https://raw.githubusercontent.com/github/gitignore/master/Python.gitignore -o .gitignore

Append an entry for the flagged directory that is created by user interactions by typing:

echo "flagged" > .gitingore

When you are ready, run the following commands to commit and push changes to the repository. Remember to replace the values of <YOUR_GITHUB_USERNAME> and <YOUR_REPOSITORY_NAME> with your own information:

git add :/
git commit -m "Initial commit"
git remote add origin git@github.com:<YOUR_GITHUB_USERNAME>/<YOUR_REPOSITORY_NAME>.git
git branch -M main
git push -u origin main

Make sure to replace <YOUR_GITHUB_USERNAME>/<YOUR_REPOSITORY_NAME> with your GitHub username and repository name.

Deploy to Koyeb using git-driven deployment

Once the repository is pushed to GitHub, you can deploy the Gradio application to Koyeb. Any changes in the deployed branch of your codebase will automatically trigger a redeploy on Koyeb, ensuring that your application is always up-to-date.

To deploy the Gradio app on Koyeb using the control panel (opens in a new tab), follow the steps below:

  1. Click Create Service on the Overview tab of the Koyeb control panel and choose Web service.
  2. Select GitHub as the deployment option.
  3. Choose the GitHub repository containing your application code. Alternatively, you can enter our public Gradio example repository (opens in a new tab) into the Public GitHub repository at the bottom of the page: https://github.com/koyeb/example-gradio.
  4. Select your preferred Instance size and region.
  5. Choose the Builder for your project. We can use either a Dockerfile or buildpack for this repository.
  6. If you are using the Buildpack builder, click the Override toggle associated with the Run command field and enter python app.py.
  7. Choose a name for your Service, for example example-gradio.
  8. Click the Deploy button.

A Koyeb App and Service will be created. Your application will be built and deployed to Koyeb. Once the build has finished, you will be able to access your application running on Koyeb by clicking the URL ending with .koyeb.app.

Deploy to Koyeb from a container registry

If you chose to build a container image for the Gradio application, you can optionally deploy the application from a container registry instead of from GitHub.

To deploy a pre-built Gradio container image on Koyeb using the control panel (opens in a new tab), follow the steps below:

  1. Click Create Service on the Overview tab of the Koyeb control panel and then select Web service.
  2. Select Docker as the deployment option.
  3. Choose the container image and tag from your registry and click Next to continue.
  4. Choose a name for your Service, for example example-gradio.
  5. Click the Deploy button.

A Koyeb App and Service will be created. The container image will be pulled from the container registry and a container will be deployed from it. Once the initialization is complete, you will be able to access your application running on Koyeb by clicking the URL ending with .koyeb.app.