By hitjethva and Anish Singh Walia
The author selected Girst Who Code to receive a donation as part of the Write for DOnations program.
Gradio is an open-source Python library used to create machine learning (ML) and deep learning (DL) web applications. It offers a user-friendly interface that allows developers to build and deploy interactive and customizable interfaces for their machine learning models quickly, without extensive knowledge of web development.
The primary goal of Gradio is to bridge the gap between machine learning models and end-users by providing an easy-to-use interface for creating web applications. It enables users to interact with ML models in a more intuitive and accessible manner, making the deployment and usage of machine learning applications more widespread.
Gradio provides pre-built UI components for simplifying the creation of ML web applications and other common tasks such as input forms, sliders, image displays, and text boxes. Users can easily set input and output components for their machine learning models, allowing Gradio to handle other aspects such as installation and hosting of applications. This approach allows Machine learning practitioners to focus on the model itself and speed up the development and deployment process by simplifying the complexity of web development.
This tutorial will walk you through building Machine Learning web applications using Gradio on Ubuntu.
Before starting this guide, you should have:
An Ubuntu Cloud GPU Server with at least 1/7 GPU, 10 GB VRAM, 2 vCPUs, and 10 GB Memory.
A root user or a user with sudo privileges. Follow our initial server setup guide for guidance.
Python and Pip package manager installed, following Steps 1 of How To Install Python 3 and Set Up a Programming Environment on Ubuntu.
A domain name configured to point to your server. You can purchase one on Namecheap or get one for free on Freenom. You can learn how to point domains to DigitalOcean by following the relevant documentation on domains and DNS.
You can use the PIP package manager to install Gradio with the required model dependency packages.
Here’s an explanation of the above-mentioned packages:
RealESRGAN is an image super-resolution model that aims to enhance the resolution and quality of images.
GFPGAN, or Generative Facial Prior GAN, is likely a GAN-based model that generates facial images.
BasicSR is a library or framework for Super-Resolution tasks in computer vision. It likely provides tools, models, and utilities for implementing and experimenting with various super-resolution algorithms.
Gradio creates user interfaces for machine learning models.
Next, verify the Jinja2
version to check the compatibility.
You will see the following output.
As you can see, the Jinja2 version is 3.0.3. However, this article uses the Pandas library that requires Jinja2 version 3.1.2 or above. Let’s upgrade the jinja2 package with the latest version.
Next, install other dependencies for OpenGL support on Linux systems.
This command will download and install the necessary OpenGL libraries on your system.
You can follow these basic steps to create a new Gradio web application. This your_domain assumes you have a trained machine-learning model you want to deploy using Gradio.
First, create a directory for your web application.
Next, change the permission and ownership of this directory:
Next, navigate to the application directory and create an app.py file for your Gradio application.
First, add the following code to import the required libraries.
Here is the explanation:
gr: Gradio library for creating web interfaces for machine learning models.
GFPGANer: Class for GFPGAN model.
RRDBNet: Class for the RRDBNet architecture.
RealESRGANer: Class for the RealESRGAN model.
numpy: Library for numerical operations.
cv2: OpenCV library for computer vision.
requests: Library for making HTTP requests.
Next, define the model configuration and image enhancement function:
This function (enhance_image)
takes an input image and enhances it using the GFPGAN and RealESRGAN models. It configures the models, loads checkpoints, and performs the enhancement.
Next, define the Gradio interface configuration:
Here is the explanation:
fn: The function (enhance_image) to be used for processing inputs.
inputs: The input component (in this case, an image).
outputs: The output components (two images: restored_faces and restored_img).
live: Enables live updates in the Gradio interface.
title: Title for the Gradio interface.
description: Description for the Gradio interface.
Finally, add the following line to launch the Gradio Interface.
Save and close the file, then run the application to verify the model.
If everything is fine, you will see the following output.
Press the CTRL + C
to stop the application process.
The systemd
service file is used to define how your Gradio application should be managed as a service by the system.
Use the nano editor to create a gradio.service
file.
Add the following configuration:
Save the changes and close the text editor. Then, reload the systemd
configuration:
Finally, start the Gradio service and enable it to start on boot.
Check the status of your Gradio service to ensure it’s running without errors:
This command will display the current status and any error messages if the service is not running correctly.
Now your Gradio application should be running as a systemd
service, and it will start automatically on system boot.
You can implement the Nginx as a reverse proxy to enhance the overall performance, security, and scalability of your Gradio web application. It allows you to take advantage of Nginx’s capabilities while Gradio focuses on serving interactive machine-learning models.
First, install the Nginx web server packages.
Next, create a new Nginx configuration file for your Gradio application.
Add the following configuration, adjusting the placeholders:
Replace gradio.your_domain.com
with your domain name or server’s IP address. Change the proxy_pass address to the address where Gradio is running. If Gradio runs on the same server as Nginx and uses the default Gradio port (usually 8080
), you can leave it as is.
Before restarting Nginx, it’s a good idea to test the configuration to ensure no syntax errors:
If everything is fine, you will see the following output.
Next, restart Nginx to apply the changes:
Now, Nginx is configured as a reverse proxy for your Gradio application. When users access your domain or IP, Nginx forwards the requests to the Gradio server running on the specified address and port.
Securing a Gradio web application with Let’s Encrypt SSL encrypts the data exchanged between the user’s browser and the Gradio web application. This ensures that sensitive information, such as user inputs or any confidential data, remains secure during transmission. By incorporating Let’s Encrypt SSL into your Gradio web application, you not only enhance security but also contribute to a safer and more trustworthy online environment for your users.
First, install Certbot, the client for Let’s Encrypt, on your server.
Next, run the certbot
command to obtain an SSL certificate for your domain. Replace your_domain.com with your actual domain.
This will download the Let’s Encrypt SSL and automatically update your Nginx configuration to use the obtained SSL certificate.
Let’s Encrypt certificates are typically valid for 90 days. Add a cron job to run the renewal command to automate the renewal process.
Add the following line to run the renewal twice a day:
Save the changes and exit the editor.
Your Gradio web application should be accessible over HTTPS with a valid Let’s Encrypt SSL certificate. You can open your web browser and access your Gradio web application using the URL https://gradio.your_domain.com
You can upload any image in the input_image section and verify that the application uses the GPU Server resources to process the enhanced image output.
In this tutorial, we explored the key steps involved in setting up a Gradio web application. We covered the installation of Gradio and demonstrated how to integrate it with popular machine learning models such as GFPGAN and RealESRGAN. We even discussed enhancing security and performance by configuring Nginx as a reverse proxy with Let’s Encrypt SSL.
By leveraging Gradio, users can effortlessly upload images, input data, and visualize model outputs through a dynamic web interface. The platform’s flexibility enables integration with various machine learning frameworks and models, making it a versatile tool for various applications. For more information, visit the official Gradio web interface documentation.
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!