The Interface class is Gradio’s main high-level abstraction for creating web-based UIs around Python functions. It provides a simple way to wrap any function with input and output components, making it ideal for quick demos and prototypes.
What is an Interface?
An Interface allows you to create a web-based GUI around a machine learning model (or any Python function) in just a few lines of code. You specify three main parameters:
- The function to wrap (
fn)
- The input components (
inputs)
- The output components (
outputs)
Gradio automatically generates the UI, handles user interactions, and displays results.
Basic usage
Here’s the simplest possible Interface:
import gradio as gr
def greet(name):
return "Hello " + name + "!"
demo = gr.Interface(fn=greet, inputs="textbox", outputs="textbox")
demo.launch()
This creates a complete web interface with:
- An input textbox for entering a name
- A submit button
- An output textbox showing the greeting
Using component shortcuts
You can specify components using string shortcuts (like "textbox", "image", "label") or instantiate them explicitly:
import gradio as gr
def image_classifier(img):
return {'cat': 0.3, 'dog': 0.7}
demo = gr.Interface(
fn=image_classifier,
inputs="image",
outputs="label"
)
demo.launch()
For more control, use component objects:
import gradio as gr
demo = gr.Interface(
fn=image_classifier,
inputs=gr.Image(type="pil"),
outputs=gr.Label(num_top_classes=3)
)
You can pass lists of components for multiple inputs or outputs:
import gradio as gr
def process_data(text, temperature, enable_feature):
result = f"Text: {text}, Temp: {temperature}, Feature: {enable_feature}"
return result, temperature * 2
demo = gr.Interface(
fn=process_data,
inputs=[
gr.Textbox(label="Input Text"),
gr.Slider(0, 100, label="Temperature"),
gr.Checkbox(label="Enable Feature")
],
outputs=[
gr.Textbox(label="Result"),
gr.Number(label="Double Temperature")
]
)
The number of function parameters must match the number of input components, and the number of return values must match the number of output components.
Key parameters
Title and description
Add metadata to make your Interface more informative:
demo = gr.Interface(
fn=greet,
inputs="textbox",
outputs="textbox",
title="Greeting Generator",
description="Enter your name to receive a personalized greeting."
)
Examples
Provide example inputs to help users get started:
demo = gr.Interface(
fn=greet,
inputs="textbox",
outputs="textbox",
examples=[["Alice"], ["Bob"], ["Charlie"]]
)
Use additional_inputs to add optional parameters that appear in a collapsible accordion:
demo = gr.Interface(
fn=process_text,
inputs="textbox",
outputs="textbox",
additional_inputs=[
gr.Slider(0, 100, value=50, label="Max Length"),
gr.Dropdown(["formal", "casual"], label="Style")
],
additional_inputs_accordion="Advanced Options"
)
Live mode
Set live=True to automatically update outputs when inputs change (no submit button needed):
demo = gr.Interface(
fn=lambda x: x.upper(),
inputs="textbox",
outputs="textbox",
live=True
)
Working with state
For stateful applications, you can use the special "state" component:
import gradio as gr
def increment(count, state):
state = state + 1
return f"Count: {state}", state
demo = gr.Interface(
fn=increment,
inputs=["textbox", "state"],
outputs=["textbox", "state"]
)
When using state, there must be exactly one state input and one state output.
Loading from pipelines
The Interface.from_pipeline() class method creates an Interface from Hugging Face transformers pipelines:
import gradio as gr
from transformers import pipeline
pipe = pipeline("image-classification")
demo = gr.Interface.from_pipeline(pipe)
demo.launch()
Gradio automatically determines the appropriate input and output components based on the pipeline type.
API endpoints
Every Interface automatically generates API endpoints. Control their visibility:
demo = gr.Interface(
fn=greet,
inputs="textbox",
outputs="textbox",
api_name="greet_user", # Custom endpoint name
api_visibility="public" # "public", "private", or "undocumented"
)
Comparison with Blocks
Interface is ideal for:
- Quick prototypes and demos
- Single-function applications
- Simple input-output workflows
Use Blocks instead when you need:
- Custom layouts and multi-page apps
- Multiple interconnected functions
- Complex event handling and data flows
See also
- Blocks - For more complex layouts and workflows
- ChatInterface - Specialized interface for chatbots
- Components - Available input/output components
- Events - Understanding event listeners