gr.Interface class is a high-level abstraction in Gradio that allows you to quickly create a demo for any Python function by specifying the input types and output types.
Basic structure
TheInterface class is initialized with three required parameters:
fn: the function to wrap a user interface (UI) aroundinputs: which Gradio component(s) to use for the input. The number of components should match the number of arguments in your functionoutputs: which Gradio component(s) to use for the output. The number of components should match the number of return values from your function
Gradio components
Gradio includes more than 30 pre-built components (as well as many community-built custom components) that can be used as inputs or outputs in your demo. These components correspond to common data types in machine learning and data science:gr.Image- designed to handle input or output imagesgr.Label- displays classification labels and probabilitiesgr.LinePlot- displays line plotsgr.Textbox- handles text input/outputgr.Slider- numeric slider input
Component attributes
You can customize component appearance and behavior using component attributes. Instead of using string shortcuts like"text", you can instantiate the actual component class:
Multiple inputs and outputs
You can create interfaces with multiple inputs and outputs. Each component in theinputs list corresponds to one parameter of the function in order, and each component in the outputs list corresponds to one return value:
Working with images
When using theImage component as input, your function receives a NumPy array with shape (height, width, 3), where the last dimension represents RGB values:
type= keyword argument. For example, to receive a file path instead of a NumPy array:
Descriptive content
You can add descriptive content to help users understand your interface using these parameters:title
title
Accepts text and displays it at the top of the interface. Also becomes the page title in the browser.
description
description
Accepts text, markdown, or HTML and places it right under the title.
article
article
Accepts text, markdown, or HTML and places it below the interface. Can also be an HTTP(S) link to a remote file.
Component labels
Every component has alabel attribute that modifies the label text at the top of the component:
info parameter provides additional information below the label for form elements.
Additional inputs within an accordion
If your prediction function takes many inputs, you can hide some within a collapsed accordion to avoid cluttering the UI:Loading from pipelines
You can automatically create an Interface from Hugging Face transformers pipelines:Next steps
Examples
Learn how to add example inputs to your Interface
Interface types
Explore the 4 different types of interfaces
Reactive interfaces
Create live and streaming interfaces
Flagging
Allow users to flag interesting outputs