Creating an Interface

gradio.Interface(self, fn, inputs, outputs, verbose=False, examples=None, live=False, capture_session=False, interpretation=None, title=None, description=None, thumbnail=None, allow_screenshot=True, allow_flagging=True, flagging_dir="flagged")

Interfaces are created with Gradio using the `gradio.Interface()` function.

Parameters

  • fn (Callable) - the function to wrap an interface around.
  • inputs (Union[str, List[Union[str, InputComponent]]]) - a single Gradio input component, or list of Gradio input 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.
  • outputs (Union[str, List[Union[str, OutputComponent]]]) - a single Gradio output component, or list of Gradio output 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.
  • verbose (bool) - whether to print detailed information during launch.
  • examples (List[List[Any]]) - sample inputs for the function; if provided, appears below the UI components and can be used 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.
  • live (bool) - whether the interface should automatically reload on change.
  • capture_session (bool) - if True, captures the default graph and session (needed for Tensorflow 1.x)
  • interpretation (Union[Callable, str]) - function that provides interpretation explaining prediction output. Pass "default" to use built-in interpreter.
  • title (str) - a title for the interface; if provided, appears above the input and output components.
  • description (str) - a description for the interface; if provided, appears above the input and output components.
  • thumbnail (str) - path to image or src to use as display picture for models listed in gradio.app/hub
  • allow_screenshot (bool) - if False, users will not see a button to take a screenshot of the interface.
  • allow_flagging (bool) - if False, users will not see a button to flag an input and output.
  • flagging_dir (str) - what to name the dir where flagged data is stored.

gradio.Interface.launch(self, inline=None, inbrowser=None, share=False, debug=False)

Generates the UI for the interface.

Parameters

  • inline (bool) - whether to display in the interface inline on python notebooks.
  • inbrowser (bool) - whether to automatically launch the interface in a new tab on the default browser.
  • share (bool) - whether to create a publicly shareable link from your computer for the interface.
  • debug (bool) - if True, and the interface was launched from Google Colab, prints the errors in the cell output.

Input Components

gradio.inputs.Textbox(self, lines=1, placeholder=None, default=None, numeric=False, type="str", label=None)

Component creates a textbox for user to enter input. Provides a string (or number is `type` is "float") as an argument to the wrapped function.

Input type: str

Parameters

  • lines (int) - number of line rows to provide in textarea.
  • placeholder (str) - placeholder hint to provide behind textarea.
  • default (str) - default text to provide in textarea.
  • numeric (bool) - DEPRECATED. Whether the input should be parsed as a number instead of a string.
  • type (str) - Type of value to be returned by component. "str" returns a string, "number" returns a float value.
  • label (str) - component name in interface.

String Shortcuts

  • "text" - Uses defaults implementation.
  • "textbox" - Sets lines=7
  • "number" - Sets type=number

Interpretation

This interface supports interpretation. Accepts a list of 2-length tuples. The first element of the tuples is a string representing a string of characters. The second element represents the corresponding interpretation score of the string, from -1 to 1.

Demos

diff_texts.py: Link to Code
generate_tone.py: Link to Code
text_analysis.py: Link to Code

gradio.inputs.Slider(self, minimum=0, maximum=100, step=None, default=None, label=None)

Component creates a slider that ranges from `minimum` to `maximum`. Provides a number as an argument to the wrapped function.

Input type: float

Parameters

  • minimum (float) - minimum value for slider.
  • maximum (float) - maximum value for slider.
  • step (float) - increment between slider values.
  • default (float) - default value.
  • label (str) - component name in interface.

String Shortcuts

  • "slider" - Uses defaults implementation.

Demos

generate_tone.py: Link to Code
sentence_builder.py: Link to Code
stock_forecast.py: Link to Code

gradio.inputs.Checkbox(self, label=None)

Component creates a checkbox that can be set to `True` or `False`. Provides a boolean as an argument to the wrapped function.

Input type: bool

Parameters

  • label (str) - component name in interface.

String Shortcuts

  • "checkbox" - Uses defaults implementation.

Demos

sentence_builder.py: Link to Code
stock_forecast.py: Link to Code

gradio.inputs.CheckboxGroup(self, choices, type="value", label=None)

Component creates a set of checkboxes of which a subset can be selected. Provides a list of strings representing the selected choices as an argument to the wrapped function.

Input type: Union[List[str], List[int]]

Parameters

  • choices (List[str]) - list of options to select from.
  • type (str) - Type of value to be returned by component. "value" returns the list of strings of the choices selected, "index" returns the list of indicies of the choices selected.
  • label (str) - component name in interface.

String Shortcuts

None.

Demos

sentence_builder.py: Link to Code
stock_forecast.py: Link to Code

gradio.inputs.Radio(self, choices, type="value", label=None)

Component creates a set of radio buttons of which only one can be selected. Provides string representing selected choice as an argument to the wrapped function.

Input type: Union[str, int]

Parameters

  • choices (List[str]) - list of options to select from.
  • type (str) - Type of value to be returned by component. "value" returns the string of the choice selected, "index" returns the index of the choice selected.
  • label (str) - component name in interface.

String Shortcuts

None.

Demos

sentence_builder.py: Link to Code
stock_forecast.py: Link to Code

gradio.inputs.Dropdown(self, choices, type="value", label=None)

Component creates a dropdown of which only one can be selected. Provides string representing selected choice as an argument to the wrapped function.

Input type: Union[str, int]

Parameters

  • choices (List[str]) - list of options to select from.
  • type (str) - Type of value to be returned by component. "value" returns the string of the choice selected, "index" returns the index of the choice selected.
  • label (str) - component name in interface.

String Shortcuts

None.

Demos

filter_records.py: Link to Code
generate_tone.py: Link to Code
sentence_builder.py: Link to Code
stock_forecast.py: Link to Code

gradio.inputs.Image(self, shape=None, image_mode="RGB", invert_colors=False, source="upload", tool="editor", type="numpy", label=None)

Component creates an image upload box with editing capabilities.

Input type: Union[numpy.array, PIL.Image, str]

Parameters

  • shape (Tuple[int, int]) - shape to crop and resize image to; if None, matches input image size.
  • image_mode (str) - "RGB" if color, or "L" if black and white.
  • invert_colors (bool) - whether to invert the image as a preprocessing step.
  • source (str) - Source of image. "upload" creates a box where user can drop an image file, "webcam" allows user to take snapshot from their webcam, "canvas" defaults to a white image that can be edited and drawn upon with tools.
  • tool (str) - Tools used for editing. "editor" allows a full screen editor, "select" provides a cropping and zoom tool.
  • type (str) - Type of value to be returned by component. "numpy" returns a numpy array with shape (width, height, 3) and values from 0 to 255, "pil" returns a PIL image object, "file" returns a temporary file object whose path can be retrieved by file_obj.name.
  • label (str) - component name in interface.

String Shortcuts

  • "image" - Uses defaults implementation.
  • "webcam" - Sets source=webcam
  • "sketchpad" - Sets image_mode=L, source=canvas, shape=[28, 28], invert_colors=True

Interpretation

This interface supports interpretation. Accepts a 2D float array, where each value represents the interpretation score of the corresponding pixel in the input image, with a value ranging from -1 to 1.

Demos

digit_classifier.py: Link to Code
image_mod.py: Link to Code
webcam.py: Link to Code

gradio.inputs.Audio(self, source="upload", type="numpy", label=None)

Component accepts audio input files.

Input type: Union[Tuple[int, numpy.array], str, numpy.array]

Parameters

  • source (str) - Source of audio. "upload" creates a box where user can drop an audio file, "microphone" creates a microphone input.
  • type (str) - Type of value to be returned by component. "numpy" returns a 2-set tuple with an integer sample_rate and the data numpy.array of shape (samples, 2), "file" returns a temporary file object whose path can be retrieved by file_obj.name, "mfcc" returns the mfcc coefficients of the input audio.
  • label (str) - component name in interface.

String Shortcuts

  • "audio" - Uses defaults implementation.
  • "microphone" - Sets source=microphone

Demos

reverse_audio.py: Link to Code
spectogram.py: Link to Code

gradio.inputs.File(self, type="file", label=None)

Component accepts generic file uploads.

Input type: Union[str, bytes]

Parameters

  • type (str) - Type of value to be returned by component. "file" returns a temporary file object whose path can be retrieved by file_obj.name, "binary" returns an bytes object.
  • label (str) - component name in interface.

String Shortcuts

  • "file" - Uses defaults implementation.

Demos

zip_to_json.py: Link to Code
zip_two_files.py: Link to Code

gradio.inputs.Dataframe(self, headers=None, row_count=3, col_count=3, datatype="str", type="pandas", label=None)

Component accepts 2D input through a spreadsheet interface.

Input type: Union[pandas.DataFrame, numpy.array, List[Union[str, float]], List[List[Union[str, float]]]]

Parameters

  • headers (List[str]) - Header names to dataframe.
  • row_count (int) - Limit number of rows for input.
  • col_count (int) - Limit number of columns for input. If equal to 1, return data will be one-dimensional. Ignored if `headers` is provided.
  • datatype (Union[str, List[str]]) - Datatype of values in sheet. Can be provided per column as a list of strings, or for the entire sheet as a single string. Valid datatypes are "str", "number", "bool", and "date".
  • type (str) - Type of value to be returned by component. "pandas" for pandas dataframe, "numpy" for numpy array, or "array" for a Python array.
  • label (str) - component name in interface.

String Shortcuts

  • "dataframe" - Sets type=pandas
  • "numpy" - Sets type=numpy
  • "matrix" - Sets type=array
  • "list" - Sets type=array, col_count=1

Demos

filter_records.py: Link to Code
matrix_transpose.py: Link to Code

Output Components

gradio.outputs.Textbox(self, type="auto", label=None)

Component creates a textbox to render output text or number.

Output type: Union[str, float, int]

Parameters

  • type (str) - Type of value to be passed to component. "str" expects a string, "number" expects a float value, "auto" detects return type.
  • label (str) - component name in interface.

String Shortcuts

  • "text" - Sets type=str
  • "textbox" - Sets type=str
  • "number" - Sets type=number

Demos

sentence_builder.py: Link to Code

gradio.outputs.Label(self, num_top_classes=None, type="auto", label=None)

Component outputs a classification label, along with confidence scores of top categories if provided. Confidence scores are represented as a dictionary mapping labels to scores between 0 and 1.

Output type: Union[Dict[str, float], str, int, float]

Parameters

  • num_top_classes (int) - number of most confident classes to show.
  • type (str) - Type of value to be passed to component. "value" expects a single out label, "confidences" expects a dictionary mapping labels to confidence scores, "auto" detects return type.
  • label (str) - component name in interface.

String Shortcuts

  • "label" - Uses defaults implementation.

Demos

digit_classifier.py: Link to Code

gradio.outputs.Image(self, type="auto", plot=False, label=None)

Component displays an output image.

Output type: Union[numpy.array, PIL.Image, str, matplotlib.pyplot]

Parameters

  • type (str) - Type of value to be passed to component. "numpy" expects a numpy array with shape (width, height, 3), "pil" expects a PIL image object, "file" expects a file path to the saved image, "plot" expects a matplotlib.pyplot object, "auto" detects return type.
  • plot (bool) - DEPRECATED. Whether to expect a plot to be returned by the function.
  • label (str) - component name in interface.

String Shortcuts

  • "image" - Uses defaults implementation.
  • "plot" - Sets type=plot
  • "pil" - Sets type=pil

Demos

image_mod.py: Link to Code
spectogram.py: Link to Code
stock_forecast.py: Link to Code
webcam.py: Link to Code

gradio.outputs.KeyValues(self, label=None)

Component displays a table representing values for multiple fields.

Output type: Union[Dict, List[Tuple[str, Union[str, int, float]]]]

Parameters

  • label (str) - component name in interface.

String Shortcuts

  • "key_values" - Uses defaults implementation.

Demos

text_analysis.py: Link to Code

gradio.outputs.HighlightedText(self, color_map=None, label=None)

Component creates text that contains spans that are highlighted by category or numerical value. Output is represent as a list of Tuple pairs, where the first element represents the span of text represented by the tuple, and the second element represents the category or value of the text.

Output type: List[Tuple[str, Union[float, str]]]

Parameters

  • color_map (Dict[str, str]) - Map between category and respective colors
  • label (str) - component name in interface.

String Shortcuts

  • "highlight" - Uses defaults implementation.

Demos

diff_texts.py: Link to Code
text_analysis.py: Link to Code

gradio.outputs.Audio(self, type="auto", label=None)

Creates an audio player that plays the output audio.

Output type: Union[Tuple[int, numpy.array], str]

Parameters

  • type (str) - Type of value to be passed to component. "numpy" returns a 2-set tuple with an integer sample_rate and the data numpy.array of shape (samples, 2), "file" returns a temporary file path to the saved wav audio file, "auto" detects return type.
  • label (str) - component name in interface.

String Shortcuts

  • "audio" - Uses defaults implementation.

Demos

generate_tone.py: Link to Code
reverse_audio.py: Link to Code

gradio.outputs.JSON(self, label=None)

Used for JSON output. Expects a JSON string or a Python object that is JSON serializable.

Output type: Union[str, Any]

Parameters

  • label (str) - component name in interface.

String Shortcuts

  • "json" - Uses defaults implementation.

Demos

zip_to_json.py: Link to Code

gradio.outputs.HTML(self, label=None)

Used for HTML output. Expects an HTML valid string.

Output type: str

Parameters

  • label (str) - component name in interface.

String Shortcuts

  • "html" - Uses defaults implementation.

Demos

text_analysis.py: Link to Code

gradio.outputs.File(self, label=None)

Used for file output.

Output type: Union[file-like, str]

Parameters

  • label (str) - component name in interface.

String Shortcuts

  • "file" - Uses defaults implementation.

Demos

zip_two_files.py: Link to Code

gradio.outputs.Dataframe(self, headers=None, type="auto", label=None)

Component displays 2D output through a spreadsheet interface.

Output type: Union[pandas.DataFrame, numpy.array, List[Union[str, float]], List[List[Union[str, float]]]]

Parameters

  • headers (List[str]) - Header names to dataframe.
  • type (str) - Type of value to be passed to component. "pandas" for pandas dataframe, "numpy" for numpy array, or "array" for Python array, "auto" detects return type.
  • label (str) - component name in interface.

String Shortcuts

  • "dataframe" - Uses defaults implementation.
  • "numpy" - Sets type=numpy
  • "matrix" - Sets type=array
  • "list" - Sets type=array

Demos

filter_records.py: Link to Code
matrix_transpose.py: Link to Code