⚡ Core Mechanics of `gr.Interface`
The `gr.Interface` class is the quickest way to create a full web UI around any Python function. It takes three core arguments: a function (`fn`), inputs (`inputs`), and outputs (`outputs`). Gradio automatically inspects your function signature and component types to render an interactive web interface with zero frontend code required.
1. Building a Simple Text Processing Interface
Create a basic web app that accepts a string input, transforms it (e.g., capitalization and character counting), and returns structured text output.
Python (`app.py`)
import gradio as gr
def process_text(text, uppercase):
result = text.upper() if uppercase else text
char_count = len(text)
return f"Processed Text: {result}", f"Total Characters: {char_count}"
demo = gr.Interface(
fn=process_text,
inputs=[
gr.Textbox(label="Enter Your Text", placeholder="Type something here..."),
gr.Checkbox(label="Convert to Uppercase", value=True)
],
outputs=[
gr.Textbox(label="Output Result"),
gr.Textbox(label="Character Statistics")
],
title="Text Analyzer Pro",
description="A simple Gradio interface demonstrating multi-input and multi-output bindings."
)
if __name__ == "__main__":
demo.launch()
Running on local URL: http://127.0.0.1:7860
To create a public link, set `share=True` in `launch()`.
2. Numeric Inputs & Slider Configurations
Use interactive sliders and numeric components to compute mathematical operations and visualize outputs dynamically.
Python (`math_app.py`)
import gradio as gr
import numpy as np
import matplotlib.pyplot as plt
def generate_sine_wave(frequency, amplitude):
x = np.linspace(0, 10, 500)
y = amplitude * np.sin(frequency * x)
fig, ax = plt.subplots(figsize=(6, 4))
ax.plot(x, y, color='#f37626', linewidth=3)
ax.set_title(f"Sine Wave (Freq: {frequency}, Amp: {amplitude})")
ax.grid(True)
return fig
demo = gr.Interface(
fn=generate_sine_wave,
inputs=[
gr.Slider(minimum=1, maximum=10, value=3, step=0.5, label="Frequency"),
gr.Slider(minimum=0.5, maximum=5, value=1, step=0.5, label="Amplitude")
],
outputs=gr.Plot(label="Generated Sine Wave Plot"),
title="Dynamic Sine Wave Generator",
description="Adjust frequency and amplitude sliders to instantly update matplotlib plot outputs."
)
if __name__ == "__main__":
demo.launch()
Interactive math plot successfully bound to UI sliders. Real-time rendering enabled.
3. Multi-Modal Inputs: Handling Image Uploads
Integrate `gr.Image` component to accept user image uploads, apply image processing filters (e.g., grayscale conversion), and return modified images.
Python (`image_app.py`)
import gradio as gr
import numpy as np
def sepia_filter(input_image):
# Convert RGB image to Sepia tone using matrix multiplication
sepia_matrix = np.array([
[0.393, 0.769, 0.189],
[0.349, 0.686, 0.168],
[0.272, 0.534, 0.131]
])
sepia_img = input_image.dot(sepia_matrix.T)
sepia_img = np.clip(sepia_img, 0, 255).astype(np.uint8)
return sepia_img
demo = gr.Interface(
fn=sepia_filter,
inputs=gr.Image(label="Upload Input Image"),
outputs=gr.Image(label="Processed Sepia Image"),
title="Sepia Photo Filter Studio",
description="Upload any photo to instantly apply custom numpy matrix filters via Gradio."
)
if __name__ == "__main__":
demo.launch()
Image component bound successfully. Supports drag-and-drop file uploads and webcam captures.
4. Configuring Launch Parameters (`launch()`)
Customize server behavior, network ports, authentication credentials, and public share links using `demo.launch()` arguments.
Python (Launch Configuration)
# Advanced launch configuration options
demo.launch(
server_name="0.0.0.0", # Expose server to local network / container
server_port=7860, # Custom port binding
share=False, # Generate public *.gradio.live tunnel link (True/False)
auth=("admin", "secret"), # Optional HTTP Basic Authentication credentials
show_error=True # Display traceback in UI if exception occurs
)
Server started with HTTP Basic Authentication enabled. Secure remote access configured.