Skip to main content
Interface is Gradio’s main high-level class that allows you to create a web-based GUI/demo around a machine learning model (or any Python function) in a few lines of code.

Constructor

gr.Interface(
    fn,
    inputs,
    outputs,
    examples=None,
    cache_examples=None,
    cache_mode=None,
    examples_per_page=10,
    example_labels=None,
    preload_example=0,
    live=False,
    title=None,
    description=None,
    article=None,
    flagging_mode=None,
    flagging_options=None,
    flagging_dir=".gradio/flagged",
    flagging_callback=None,
    analytics_enabled=None,
    batch=False,
    max_batch_size=4,
    api_visibility="public",
    api_name=None,
    api_description=None,
    allow_duplication=False,
    concurrency_limit="default",
    additional_inputs=None,
    additional_inputs_accordion=None,
    submit_btn="Submit",
    stop_btn="Stop",
    clear_btn="Clear",
    delete_cache=None,
    show_progress="full",
    fill_width=False,
    time_limit=30,
    stream_every=0.5,
    deep_link=None,
    validator=None
)

Parameters

fn
Callable
required
The function to wrap an interface around. Often a machine learning model’s prediction function. Each parameter of the function corresponds to one input component, and the function should return a single value or a tuple of values, with each element in the tuple corresponding to one output component.
inputs
str | Component | Sequence[str | Component] | None
required
A single Gradio component, or list of Gradio components. Components can either be passed as instantiated objects, or referred to by their string shortcuts. The number of input components should match the number of parameters in fn. If set to None, then only the output components will be displayed.
outputs
str | Component | Sequence[str | Component] | None
required
A single Gradio component, or list of Gradio components. Components can either be passed as instantiated objects, or referred to by their string shortcuts. The number of output components should match the number of values returned by fn. If set to None, then only the input components will be displayed.
examples
list[Any] | list[list[Any]] | str | None
default:"None"
Sample inputs for the function; if provided, appear below the UI components and can be clicked to populate the interface. Should be nested list, in which the outer list consists of samples and each inner list consists of an input corresponding to each input component. A string path to a directory of examples can also be provided.
cache_examples
bool | None
default:"None"
If True, caches examples in the server for fast runtime in examples. If “lazy”, then examples are cached (for all users of the app) after their first use. Note that examples are cached separately from Gradio’s queue() so certain features will not be displayed in Gradio’s UI for cached examples.
cache_mode
Literal['eager', 'lazy'] | None
default:"None"
If “lazy”, examples are cached after their first use. If “eager”, all examples are cached at app launch.
examples_per_page
int
default:"10"
If examples are provided, how many to display per page.
example_labels
list[str] | None
default:"None"
A list of labels for each example. If provided, the length of this list should be the same as the number of examples, and these labels will be used in the UI instead of rendering the example values.
preload_example
int | Literal[False]
default:"0"
If an integer is provided, the example at that index in the examples list will be preloaded when the Gradio app is first loaded. If False, no example will be preloaded.
live
bool
default:"False"
Whether the interface should automatically rerun if any of the inputs change.
title
str | I18nData | None
default:"None"
A title for the interface; if provided, appears above the input and output components in large font. Also used as the tab title when opened in a browser window.
description
str | None
default:"None"
A description for the interface; if provided, appears above the input and output components and beneath the title. Accepts Markdown and HTML content.
article
str | None
default:"None"
An expanded article explaining the interface; if provided, appears below the input and output components. Accepts Markdown and HTML content.
flagging_mode
Literal['never', 'auto', 'manual'] | None
default:"None"
One of “never”, “auto”, or “manual”. If “never” or “auto”, users will not see a button to flag an input and output. If “manual”, users will see a button to flag. If “auto”, every input the user submits will be automatically flagged.
flagging_options
list[str] | list[tuple[str, str]] | None
default:"None"
If provided, allows user to select from the list of options when flagging.
flagging_dir
str
default:"'.gradio/flagged'"
Path to the directory where flagged data is stored.
flagging_callback
FlaggingCallback | None
default:"None"
Either None or an instance of a subclass of FlaggingCallback which will be called when a sample is flagged.
analytics_enabled
bool | None
default:"None"
Whether to allow basic telemetry. If None, will use GRADIO_ANALYTICS_ENABLED environment variable if defined, or default to True.
batch
bool
default:"False"
If True, then the function should process a batch of inputs.
max_batch_size
int
default:"4"
The maximum number of inputs to batch together if this is called from the queue.
api_visibility
Literal['public', 'private', 'undocumented']
default:"'public'"
Controls the visibility of the prediction endpoint. Can be “public” (shown in API docs and callable), “private” (hidden from API docs and not callable), or “undocumented” (hidden from API docs but callable).
api_name
str | None
default:"None"
Defines how the prediction endpoint appears in the API docs. Can be a string or None.
api_description
str | None | Literal[False]
default:"None"
Description of the API endpoint. Can be a string, None, or False.
allow_duplication
bool
default:"False"
If True, then will show a ‘Duplicate Spaces’ button on Hugging Face Spaces.
concurrency_limit
int | None | Literal['default']
default:"'default'"
If set, this is the maximum number of this event that can be running simultaneously.
additional_inputs
str | Component | Sequence[str | Component] | None
default:"None"
A single Gradio component, or list of Gradio components. These components will be rendered in an accordion below the main input components.
submit_btn
str | Button
default:"'Submit'"
The button to use for submitting inputs.
stop_btn
str | Button
default:"'Stop'"
The button to use for stopping the interface.
clear_btn
str | Button | None
default:"'Clear'"
The button to use for clearing the inputs.
delete_cache
tuple[int, int] | None
default:"None"
A tuple corresponding [frequency, age] both expressed in number of seconds.
show_progress
Literal['full', 'minimal', 'hidden']
default:"'full'"
How to show the progress animation while event is running.
fill_width
bool
default:"False"
Whether to horizontally expand to fill container fully.
time_limit
int | None
default:"30"
The time limit for the stream to run. Default is 30 seconds.
stream_every
float
default:"0.5"
The latency (in seconds) at which stream chunks are sent to the backend.
A string or gr.DeepLinkButton object that creates a unique URL you can use to share your app.
validator
Callable | None
default:"None"
A function that takes in the inputs and can optionally return a gr.validate() object for each input.

Class Methods

from_pipeline

gr.Interface.from_pipeline(pipeline, **kwargs)
Class method that constructs an Interface from a Hugging Face transformers.Pipeline or diffusers.DiffusionPipeline object. The input and output components are automatically determined from the pipeline.
pipeline
Pipeline | DiffusionPipeline
required
The pipeline object to use.
returns
Interface
A Gradio Interface object from the given Pipeline.

Example

import gradio as gr

def image_classifier(inp):
    return {'cat': 0.3, 'dog': 0.7}

demo = gr.Interface(fn=image_classifier, inputs="image", outputs="label")
demo.launch()

Build docs developers (and LLMs) love