After a machine learning model is developed and evaluated, the final step is to make it usable. While the Hugging Face pipeline offers a simple programmatic interface, sharing a model often requires a more user-friendly, interactive format. A tool like Gradio is highly beneficial here, enabling the building of simple web applications directly from Python code.
Gradio is a Python library designed to quickly build and share web applications for machine learning models. Its main advantage is simplicity. You can create a functional UI with just a few lines of code, allowing colleagues or users to interact with your model through a web browser without needing to run any code themselves.
A Gradio application is built around three main components:
These three pieces are wrapped together in a gradio.Interface object, which automatically generates and launches the web application.
To get started, you will need to install Gradio, along with the transformers and torch libraries if you are following along with the Hugging Face pipeline example from the previous section.
pip install gradio transformers torch
Let's build a web-based interface for an ASR model. We will use a pre-trained Whisper model from Hugging Face for this demonstration, as it provides a high-quality, ready-to-use transcription engine.
First, we need to load our ASR pipeline and create a Python function that will perform the transcription. This function will serve as the core logic for our Gradio app. It must accept the arguments provided by the input components and return values that the output components can display.
import gradio as gr
from transformers import pipeline
# Load the ASR pipeline
# Using a smaller model like whisper-tiny.en for faster inference
asr_pipeline = pipeline("automatic-speech-recognition", model="openai/whisper-tiny.en")
def transcribe_audio(audio):
"""
A function to transcribe audio using the Hugging Face ASR pipeline.
Gradio will provide the audio input as a NumPy array and sample rate.
"""
if audio is None:
return "No audio file provided. Please upload or record audio."
# The pipeline returns a dictionary, we extract the transcribed text
result = asr_pipeline(audio)
return result["text"]
In this function, transcribe_audio, the audio argument will be supplied by Gradio's audio input component. The function then passes this audio to our asr_pipeline and returns the resulting text transcription.
Next, we define the user interface using gradio.Interface. We connect our transcribe_audio function to user-friendly input and output components.
For our input, gradio.Audio is the perfect fit. We can configure it to accept both uploaded files and direct microphone recordings. For the output, a simple gradio.Textbox will suffice to display the transcribed text.
# Define the Gradio interface
demo = gr.Interface(
fn=transcribe_audio,
inputs=gr.Audio(sources=["upload", "microphone"], type="filepath"),
outputs=gr.Textbox(label="Transcription"),
title="Simple ASR System",
description="Upload an audio file or record audio from your microphone to transcribe speech to text. The model is based on OpenAI's Whisper-tiny."
)
Notice the inputs argument. We've specified gr.Audio and configured sources to allow both file uploads and microphone input, providing flexibility for the user. The type="filepath" argument tells Gradio to save the input audio to a temporary file and pass the path to our function, which is a common and reliable way to handle audio data.
The flow of data from the user to the model and back is illustrated in the diagram below.
The user interacts with the Gradio audio component. Gradio passes the audio data to our Python function, which uses the Hugging Face pipeline to perform transcription. The resulting text is returned to the Gradio interface and displayed to the user.
The final step is to launch the web server. This is done by calling the launch() method on our Interface object.
# To launch the application
if __name__ == "__main__":
demo.launch()
When you run this Python script, Gradio will output a local URL (usually http://127.0.0.1:7860). Opening this URL in your web browser will reveal your fully functional speech-to-text application.
One of Gradio's most useful features is its ability to create a temporary, shareable public link for your application. This is ideal for demonstrations or getting feedback from collaborators who are not on your local network.
To enable this, simply set the share argument to True in the launch() method:
# Launch the app with a public link
demo.launch(share=True)
Gradio will generate a public URL (e.g., https://xxxx.gradio.app) that remains active for 72 hours. Be mindful that this makes your application publicly accessible, so do not use it for applications that process sensitive data.
You have now seen how to wrap a sophisticated ASR model into an interactive web application with minimal effort. Gradio is an excellent tool for creating demos, testing models interactively, and sharing results. However, for applications requiring low latency and continuous processing, such as live captioning, a different architecture is needed. The next section will introduce the challenges and approaches for building such real-time streaming systems.
Was this section helpful?
© 2026 ApX Machine LearningEngineered with