Chapter-2Introduction
In Chapter-1, you saw how easy it is to build a basic Gradio app. But the real power of Gradio comes from its rich variety of input and output components. Whether you're working with images, text, audio, video, or files — Gradio provides ready-made UI elements to make your models interactive with just a few lines of code.
In this article, we’ll explore:
-
All major
inputs
andoutputs
-
Parameter tuning for customization
-
How to chain multiple components/functions together
Textbox
Use When:
You want the user to input plain text (sentences, prompts, numbers as strings, etc.)
Key Parameters:
-
label
: Display name
-
lines
: Height of textbox (default = 1)
-
placeholder
: Hint inside the box
-
value
: Default value
Example:import gradio as gr
def reverse(text): return text[::-1]
gr.Interface(fn=reverse, inputs=gr.Textbox(label="Enter text"), outputs="text").launch()
label
: Display name
lines
: Height of textbox (default = 1)
placeholder
: Hint inside the box
value
: Default value
Use When:
You're building an image classifier, object detector, drawing interface, etc.
Key Parameters:
-
type
: "numpy"
(for models), "pil"
(PIL Image), or "file"
(file path)
-
image_mode
: "RGB"
, "L"
(grayscale), etc.
-
shape
: Resized output shape
-
tool
: "editor"
(free drawing), "select"
(crop), or "sketch"
Exampledef shape(image): return image.shape
gr.Interface(fn=shape, inputs=gr.Image(type="numpy"), outputs="text").launch()
type
: "numpy"
(for models), "pil"
(PIL Image), or "file"
(file path)
image_mode
: "RGB"
, "L"
(grayscale), etc.
shape
: Resized output shape
tool
: "editor"
(free drawing), "select"
(crop), or "sketch"
Audio
Use When:
You're working with speech-to-text, audio classification, or waveform manipulation.
Key Parameters:
-
type
: "numpy"
(tuple of sample rate and array) or "file"
(file path)
-
label
, source
: Upload or microphone
-
interactive
: Record live audio
Exampledef audio_length(audio): sr, data = audio duration = len(data) / sr return f"{duration:.2f} seconds"
gr.Interface(fn=audio_length, inputs=gr.Audio(type="numpy"), outputs="text").launch()
type
: "numpy"
(tuple of sample rate and array) or "file"
(file path)
label
, source
: Upload or microphone
interactive
: Record live audio
Video
Use When:
You're building action recognition, frame prediction, or captioning tools.
Key Parameters:
-
type
: "numpy"
(for video array) or "file"
(path)
-
format
: Accepts .mp4
, .webm
, etc.
type
: "numpy"
(for video array) or "file"
(path)
format
: Accepts .mp4
, .webm
, etc.
Example:
def video_echo(video): return video
gr.Interface(fn=video_echo, inputs=gr.Video(), outputs="video").launch()
File
Use When:
You want users to upload any document — CSVs, PDFs, images, etc.
Key Parameters:
-
file_types
: Restrict file types like[".csv", ".pdf"]
-
type
:"file"
(returns path) or"binary"
Slider
Use When:
You want a numeric input within a specific range.
Key Parameters:
-
minimum
,maximum
,step
-
value
(default) -
label
Checkbox
Use When:
You want a binary yes/no or true/false input.
Example:
def check_status(checked): return "Enabled" if checked else "Disabled"
gr.Interface(fn=check_status, inputs=gr.Checkbox(label="Enable feature"),
outputs="text").launch()
Radio Buttons
Use When:
You want the user to select one from multiple options.
Key Parameters:
-
choices
: List of options
choices
: List of options
Example:
def favorite_color(choice): return f"Nice choice: {choice}"
gr.Interface(fn=favorite_color, inputs=gr.Radio(["Red", "Green", "Blue"]),
outputs="text").launch()
Dropdown
Use When:
You want a collapsible list of options (can also allow multiple selections).
Key Parameters:
-
choices
, multiselect
, value
choices
, multiselect
, value
Example:
import gradio as grdef color_summary(color): return f"You selected: {color}"
gr.Interface(fn=color_summary, inputs=gr.Dropdown(choices=["Red", "Green", "Blue"]),
outputs="text").launch()
Dataframe
Use When:
You want users to input/edit tabular data like CSVs.
Key Parameters:
-
headers
, datatype
, row_count
, col_count
headers
, datatype
, row_count
, col_count
Comments
Post a Comment