
Gradio is a Python package that allows developers to create customizable and interactive interfaces for Machine Learning models. Deployed as a web application, Gradio allows you to build a production-ready application on top of your machine learning model and avail to Internet users using your Server IP address or domain name.
This article explains how to build machine learning web applications using Gradio. You will create a Gradio interface, integrate a working machine learning model into the web application interface, and implement additional interface features using a Vultr Cloud Server.
Before you begin:
Deploy a Ubuntu A100 Cloud GPU server.
Create a non-root user with sudo privileges. For example gradio_user
Switch to the sudo user account
To build a new interface, install Gradio on the server, and set up your application as described in the steps below.
Using the Python pip package manager, install the Gradio package
Using a text editor such as Nano, create a new Python file app.py
Add the following code to the file
Save and close the file
Below is what the application code does:
import: Imports the installed Gradio packagedef test(name): Defines a function test() with one parameter name which consists of a name given as an input to the function by the userinterface: Defines the function interface which includes:gr.Interface: A Gradio library class used to define and configure user interfacesfn = test: Passes the defined function test(). The function can consist of the machine learning model, takes inputs and produces outputsinputs: Specifies the input type the user adds to the test() function. It's set to gr.Textbox() which means the input data is textoutputs="text": Specifies the type of output returned by the test() function. It's set to gr.Textbox() which means the output is text datainterface.launch(): Specifies the Gradio interface launch platforms with the following variables:server_name: Specifies Gradio listening interface. When set to 0.0.0.0, Gradio accepts incoming connections from all Server IP Addresses. Set the value to 127.0.0.1 when using a reverse proxy such as Nginxserver_port: Set the Gradio port 8080To accept incoming connections to the Gradio interface, allow the listening port 8080 through the UFW firewall
Restart the firewall to save changes
Using Python, run the Gradio application file
When successful, your output should look like the one below:
Using a web browser such as Firefox, visit your public Server IP Address on port 8080
Verify that the Gradio web application loads successfully. Then, enter your name in the name field and click Submit to view the output. In your terminal session, press Ctrl + C to stop the application.
To leverage the power of your Gradio web application, build and integrate the GFPGAN model to perform image restoration tasks with the ability to upscale any low-resolution image as described below.
Using pip, install the latest PyTorch package
Install GFPGAN and the model dependency packages
Create a new Python application file model.py
Add the following code to the file
Save and close the file
Below is what the above code does:
import: Imports all the model-specific librariesdef enhance_image(): Stores the model checkpoints and enables the following functions:restorer: Stores the image enhancement model parametersbg_upsampler: Handles background enhancement model parametersinput_image: Handles image preprocessingarch: When set to clean, it uses the clean version without the StyleGAN2-specific compiled CUDA extensionsmodel_name, gfpgan_checkpoint, realersgan_checkpoint: Consists of the URL that points to the checkpointsbg_upsampler: Enables background enhancementrestorer: Allows face enhancementinput_image: Converts the input data type array to numpy.uint8restorer.enhance(input_image): Stores three input types, a cropped image, restored face, and the restored imagereturn: Returns the two array variable as Gradio outputinterface: Specifies the variable that stores the gr.Interface() function and its parameters including:fn: Sets the function name Gradio needs to process. In this case, enhance_imageinputs: Specifies that the input type is an image.outputs: Specifies that the output type is an image. Two gr.Image() functions return two images, a restored face and the restored image.live: Specifies that when an input image loads, it immediately processes the output without the need to click a Submit button.title: Sets the interface headingdescription: Sets the interface description that appears below the titleinterface.launch(server_name="0.0.0.0", server_port=8080): Specifies the compilation of the interface and sets the launch codeRun the Python application file
In a new web browser window, access the Gradio web application interface
Verify that you can upload an input image and two restored images display in your output.
The example web application in this article uses the image Gradio type to generate the interface. To customize your application, you can implement any of the following supported Gradio input and output functions:
gr.Textbox()gr.Audio()gr.Video()gr.Image()For example, to implement Text input and output type, apply the function to your respective interface as below:
To implement more than one function in a single interface, set your interface to the following
Depending on your model requirements, you can change the inputs and outputs types. Before implementing a function, verify if it's available for either input or output. For more input and output types, visit the Gradio Interface documentation.
In this article, you installed Gradio and created a sample image restoration application on a Vultr Cloud GPU server. You can customize your Gradio interface with more models, and server the web application using your domain name by integrating a reverse proxy such as Nginx for public access. To implement login functionality, visit the Gradio login button documentation page.
0 Comments
Be the first to comment and share your perspective with the community.