
Gradio is a Python library that allows you to create a web interface using any Machine Learning (ML) model of your choice. When intergrated the model is directly deployed as a web application to suite your needs and scale the application depending on your user requirements. This article explains how to deploy Gradio on a Ubuntu 22.04 server and securely expose the application for public access using a domain name.
Before you begin:
Install the required model dependency packages.
Below is what each of the above packages does:
realesrgan: Handles the model background restoration process.gfpgan: Performs the face restoration processes.basicsr: Imports RRDBNET which is a deep neural network architecture used in super-resolution algorithms and it's utilized by the GFPGAN model.gradio: Creates the model web interface.The model implementation in this article uses the pandas library that requires jinja2 version 3.1.2 or above. Verify the installed version.
Output:
If the available version is below 3.1, upgrade the jinja2 package.
Create a new Project directory gradio-webapp in a system-wide location such as /opt/ to store the application files.
Grant your user group ownership permissions to the directory.
Set the directory permissions to 775 to allow the user to write and execute files in the directory.
Create a new Gradio web application that uses an open-source ML model such as GFPGAN used in this section. The model allows you to restore and improve images by uploading a source image that is enhanced with two output copies. Follow the steps below to create the Gradio web application powered by GFPGAN to expose on the internal server port 8080.
Switch to your project directory.
Using a text editor such as nano, create a new Python application file.
Add the following code to the file.
Save and close the file.
Below is what the above application functions do:
enhance_image(): Stores the model checkpoints and architecture information along with face enhancement model parameters. The model is defined within this parameter because gradio requires a defined function to mark what it should execute in the interface.rrdbnet: Defines the image super-resolution parameters.bg_upsampler: Defines the background enhancement parameters.restorer: Defines the face restoration parameters.interface: Defines how the interface should look.interface.launch(): Specifies the launch command of the interface to create a web app.Run the application to verify that the model starts correctly.
When successful, your output should look like the one below:
To create a public link, set share=True in launch().
Press Control + C to stop the application process.
To start the Gradio web application as a system service and enable start, stop, status operations to verify the application processes, set up a new Systemd service. Follow the steps below to create a new system service and test the Gradio web application processes.
Create a new Systemd service file.
Add the following contents to the file. Replace example-user with your actual user account to run the service.
Save and close the file.
Below is what the above Systemd declarations represent:
Description: A brief summary of the service functionality.ExecStart: Specifies how the service should execute. In the above file, the Python3 interpreter is executed to start the app.py in your Gradio directory.WorkingDirectory: The active directory where your Python file is located.Restart: Defines the service restart behavior. When set to always the automatically restart it in case of a failure.User: Specifies the user to run the service as.Restart the Systemd daemon to save changes.
Enable the service to start at boot time.
Start the Gradio service.
Verify the service is running correctly without any error.
Output:
To safely expose the Gradio web application port 8080 to the Internet, configure Nginx as a reverse proxy to forward and handle connection requests from the HTTP port 80. To securely handle incoming traffic, later configure Nginx to handle HTTPS requests with a valid SSL certificate. In this section, create the necessary Nginx configurations and test access to the Gradio web interface.
Create a new Nginx virtual host configuration file in the sites-available directory.
Add the following contents to the file.
Save and close the file.
Link the configuration file to the sites-enabled directory to activate the configuration file.
Test the Nginx configuration for errors.
Output:
Restart Nginx to apply changes.
Verify that the default UFW firewall is active on your Ubuntu server.
Output:
To accept incoming HTTP connections, allow the HTTP port 80 through the firewall.
Allow the HTTPS port 443.
Reload the firewall rules to save changes.
To securely encrypt your server traffic, generate valid SSL certificates using the Certbot Let's Encrypt Client. When generated, you Gradio web application domain name is protected with HTTPS traffic and a valid SSL certificate. Follow the steps below to install the Certbot client and generate SSL certificates using your Nginx configuration.
Install Certbot.
Generate a new SSL certificate using your Nginx configurations.
Verify that the Certificate auto-renews upon expiry.
If the above command doesn't throw an error, Certbot auto-renews your SSL certificate after every 90 days.
Using a web browser such as Chrome, visit your Gradio web application domain name.
Upload a sample image in the input_image section and verify that the application uses the Vultr GPU Server resources to process the enhanced image output.
You have deployed a Gradio web application on your Ubuntu server and secured it for public access using your domain name. You can modify the application processes with more functionalities to match your user needs. For more information, visit the Gradio web interface documentation. To explore more GFPGAN model functionalites, explore how to perform AI face restoration using GFPGAN on a Vultr Cloud GPU Server.
0 Comments
Be the first to comment and share your perspective with the community.