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, server_name="127.0.0.1", 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
  • server_name (str) - to make app accessible on local network set to "0.0.0.0".
  • 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 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) - DEPRECATED. Type of value to be returned by component. "str" returns a string, "number" returns a float value. Use Number component in place of number type.
  • label (str) - component name in interface.

String Shortcuts

  • "text" - Uses defaults implementation.
  • "textbox" - Sets lines=7
Default Interpretation: Calculates interpretation score of characters in input by splitting input into tokens, then using a "leave one out" method to calculate the score of each token by removing each token and measuring the delta of the output value.

gradio.inputs.Textbox.interpret(self, separator=" ", replacement=None)

  • separator (str) - Separator to use to split input into tokens.
  • replacement (str) - In the "leave one out" step, the text that the token should be replaced with.
Custom Interpretation: Return the following in the interpretation function.
  • (List[Tuple[str, float]]) - Each tuple set represents a set of characters and their corresponding interpretation score.

Demos

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

gradio.inputs.Number(self, default=None, label=None)

Component creates a field for user to enter numeric input. Provides a nuber as an argument to the wrapped function.

Input type: float

Parameters

  • default (float) - default value.
  • label (str) - component name in interface.

String Shortcuts

  • "number" - Uses defaults implementation.
Default Interpretation: Calculates interpretation scores of numeric values close to the input number.

gradio.inputs.Number.interpret(self, steps=3, delta=1, delta_type="percent")

  • steps (int) - Number of nearby values to measure in each direction (above and below the input number).
  • delta (float) - Size of step in each direction between nearby values.
  • delta_type (str) - "percent" if delta step between nearby values should be a calculated as a percent, or "absolute" if delta should be a constant step change.
Custom Interpretation: Return the following in the interpretation function.
  • (List[Tuple[float, float]]) - Each tuple set represents a numeric value near the input and its corresponding interpretation score.

Demos

tax_calculator.py: Link to Code
titanic_survival.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.
Default Interpretation: Calculates interpretation scores of numeric values ranging between the minimum and maximum values of the slider.

gradio.inputs.Slider.interpret(self, steps=8)

  • steps (int) - Number of neighboring values to measure between the minimum and maximum values of the slider range.
Custom Interpretation: Return the following in the interpretation function.
  • (List[float]) - Each value represents the score corresponding to an evenly spaced range of inputs between the minimum and maximum slider values.

Demos

sentence_builder.py: Link to Code
titanic_survival.py: Link to Code
generate_tone.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.
Default Interpretation: Calculates interpretation score of the input by comparing the output against the output when the input is the inverse boolean value of x.

gradio.inputs.Checkbox.interpret(self)

Custom Interpretation: Return the following in the interpretation function.
  • (Tuple[float, float]) - The first value represents the interpretation score if the input is False, and the second if the input is True.

Demos

sentence_builder.py: Link to Code
titanic_survival.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.
Default Interpretation: Calculates interpretation score of each choice in the input by comparing the output against the outputs when each choice in the input is independently either removed or added.

gradio.inputs.CheckboxGroup.interpret(self)

Custom Interpretation: Return the following in the interpretation function.
  • (List[Tuple[float, float]]) - For each tuple in the list, the first value represents the interpretation score if the input is False, and the second if the input is True.

Demos

sentence_builder.py: Link to Code
titanic_survival.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.
Default Interpretation: Calculates interpretation score of each choice by comparing the output against each of the outputs when alternative choices are selected.

gradio.inputs.Radio.interpret(self)

Custom Interpretation: Return the following in the interpretation function.
  • (List[float]) - Each value represents the interpretation score corresponding to each choice.

Demos

sentence_builder.py: Link to Code
tax_calculator.py: Link to Code
titanic_survival.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.
Default Interpretation: Calculates interpretation score of each choice by comparing the output against each of the outputs when alternative choices are selected.

gradio.inputs.Dropdown.interpret(self)

Custom Interpretation: Return the following in the interpretation function.
  • (List[float]) - Each value represents the interpretation score corresponding to each choice.

Demos

sentence_builder.py: Link to Code
filter_records.py: Link to Code
titanic_survival.py: Link to Code
generate_tone.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
Default Interpretation: Calculates interpretation score of image subsections by splitting the image into subsections, then using a "leave one out" method to calculate the score of each subsection by whiting out the subsection and measuring the delta of the output value.

gradio.inputs.Image.interpret(self, segments=16)

  • segments (int) - Number of interpretation segments to split image into.
Custom Interpretation: Return the following in the interpretation function.
  • (List[List[float]]) - A 2D array representing the interpretation score of each pixel of the image.

Demos

webcam.py: Link to Code
image_classifier.py: Link to Code
digit_classifier.py: Link to Code
image_mod.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
Default Interpretation: Calculates interpretation score of audio subsections by splitting the audio into subsections, then using a "leave one out" method to calculate the score of each subsection by removing the subsection and measuring the delta of the output value.

gradio.inputs.Audio.interpret(self, segments=8)

  • segments (int) - Number of interpretation segments to split audio into.
Custom Interpretation: Return the following in the interpretation function.
  • (List[float]) - Each value represents the interpretation score corresponding to an evenly spaced subsection of audio.

Demos

spectogram.py: Link to Code
reverse_audio.py: Link to Code
main_note.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
Default Interpretation: Calculates interpretation score of each cell in the Dataframe by using a "leave one out" method to calculate the score of each cell by removing the cell and measuring the delta of the output value.

gradio.inputs.Dataframe.interpret(self)

Custom Interpretation: Return the following in the interpretation function.
  • (List[List[float]]) - A 2D array where each value corrseponds to the interpretation score of each cell.

Demos

matrix_transpose.py: Link to Code
filter_records.py: Link to Code
tax_calculator.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
tax_calculator.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

main_note.py: Link to Code
image_classifier.py: Link to Code
digit_classifier.py: Link to Code
titanic_survival.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

spectogram.py: Link to Code
webcam.py: Link to Code
image_mod.py: Link to Code
stock_forecast.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

text_analysis.py: Link to Code
diff_texts.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

reverse_audio.py: Link to Code
generate_tone.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

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