Web Apps with Python - Gradio

Web Apps with Python - Gradio

(Image by storyset on Freepik)

Let's have a look at the Python library Gradio, a great way to build user interfaces to demo your machine learning or data science apps.

I'll also show you how to build and deploy them completely in the cloud!

(Check out my other posts Web Apps with Python - Dash and Web Apps with Python - Streamlit)

Intro

According to its creators, "Gradio is the fastest way to demo your machine learning model with a friendly web interface so that anyone can use it, anywhere!". Gradio was acquired in December 2021 by Hugging Face, a data science community and platform.

Availability

Gradio is free to install and use. In a cloud notebook such as Google Colab, use the magic command '!pip install gradio' in the first cell.

Example Code

A variety of examples can be found on the Gradio site. There are also community-built Gradio demos on Hugging Face Spaces. If you want more learning resources, this course by Hugging Face introduces Gradio in Chapter 9.

Limitations

Gradio is a good framework to build simple app interfaces for speech-to-text, image recognition, sentiment analysis, etc. But it has limitations on the look, the layout, and the charting options. Therefore, it does not appear to be a good framework to build complex dashboards, elaborate charts, or multi-page apps. Dash and Streamlit are much better suited for those cases.

Build in the Cloud

It is very easy to create and test Gradio web apps in the cloud -- both notebook environments and IDEs!

I built a simple Gradio interface that accepts text input and uses the Python library TextBlob to perform sentiment analysis:

GOOGLE COLAB

Here is the link to my notebook. Gradio works very well in Google Colab and offers you two ways to view the app interface:

  1. as a notebook cell output, and

  2. via a public URL that can be shared with others. The public URL looks something like 'xxxxx.gradio.app' and expires after 72 hours.

REPLIT

Here is the link to my Replit project.

Replit is an online IDE for Python and many other programming languages.

Create a new Python repl in Replit, paste the Python script in 'main.py' and hit the 'Run' button at the top. Replit takes care of the rest, including installing the required Python libraries.

Note that in Replit you have to specify the following in the Python script so that it generates the shareable public URL:

app.launch(share = True)

A word of caution: I am using the free tier of Replit, which has limited CPU and RAM. The first time I ran the script for this app, it almost maxed out the computing resources when the Python packages were being configured. This didn't happen on subsequent runs, though.

Deployment

The 'xxxxx.gradio.app' URL is available for a maximum of 72 hours, but you have the option to host your app permanently for free on Hugging Face Spaces!

I will summarise the steps below:

  • Create an account at (huggingface.co/spaces)

  • Hit 'Create a new Space'.

  • Choose the name, the type of licence (I left it blank), the SDK (I chose Gradio), and level of privacy (I chose public).

  • Hit 'Create'.

  • Follow the link to create the 'app.py' file in the browser. This will have your Python script for the app. You could copy and paste the code from the Replit 'main.py' file for convenience.

  • Add the optional description (these steps are similar to creating a repo in GitHub) and hit 'Commit new file'.

  • Go back to the 'Files and versions' tab at the top of the page, select the option to add a file, and set up the 'requirements.txt' file -- I specified 'textblob', 'pandas' and 'gradio'. Commit this to the same branch.

  • Now, next to the name of your Space, you will see a status update that says 'Building'.

  • This status changed to 'Runtime Error' for me, because I had 'share=True' in the app launch command in my Python code. This is not supported by Spaces.

  • I removed 'share=True' from my code, and committed it again. The status went back to 'Building'.

  • When the status changes to 'Running', you can move to the 'app' tab at the top and you'll see your app!

Here is the link to the app I created.

Conclusion

Gradio is a convenient way to build interfaces to demo your speech-processing, image-processing, NLP, or Machine Learning apps. The apps are easy to build and deploy in the cloud. There have been new developments such as Gradio Blocks for greater flexibility over the app layout, but it does not yet match the ability of Dash or Streamlit for building elaborate dashboards or multipage apps.