Creating an Interface

gradio.Interface(self, fn, inputs=None, outputs=None, verbose=False, examples=None, examples_per_page=10, live=False, layout="unaligned", capture_session=False, interpretation=None, num_shap=2.0, theme=None, title=None, description=None, article=None, thumbnail=None, css=None, server_port=None, server_name="127.0.0.1", allow_screenshot=True, allow_flagging=True, flagging_options=None, encrypt=False, show_tips=False, flagging_dir="flagged", enable_queue=False)

Interfaces are created with Gradio by constructing a `gradio.Interface()` object.

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 (Union[List[List[Any]], str]) - 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. A string path to a directory of examples can also be provided. If there are multiple input components and a directory is provided, a log.csv file must be present in the directory to link corresponding inputs.
  • examples_per_page (int) - If examples are provided, how many to display per page.
  • live (bool) - whether the interface should automatically reload on change.
  • layout (str) - Layout of input and output panels. "horizontal" arranges them as two columns of equal height, "unaligned" arranges them as two columns of unequal height, and "vertical" arranges them vertically.
  • 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.
  • num_shap (float) - a multiplier that determines how many examples are computed for shap-based interpretation. Increasing this value will increase shap runtime, but improve results.
  • 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.
  • article (str) - an expanded article explaining the interface; if provided, appears below the input and output components. Accepts Markdown and HTML content.
  • thumbnail (str) - path to image or src to use as display picture for models listed in gradio.app/hub
  • theme (str) - Theme to use - one of "default", "huggingface", "grass", "peach". Add "dark" prefix, e.g. "darkpeach" or "darkdefault" for darktheme.
  • css (str) - custom css or path to custom css file to use with interface.
  • server_port (int) - will start gradio app on this port (if available)
  • 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_options (List[str]) - if not None, provides options a user must select when flagging.
  • encrypt (bool) - If True, flagged data will be encrypted by key provided by creator at launch
  • flagging_dir (str) - what to name the dir where flagged data is stored.
  • show_tips (bool) - if True, will occasionally show tips about new Gradio features
  • enable_queue (bool) - if True, inference requests will be served through a queue instead of with parallel threads. Required for longer inference times (> 1min) to prevent timeout.

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

Launches the webserver that serves 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.
  • auth (Callable, Union[Tuple[str, str], List[Tuple[str, str]]]) - If provided, username and password (or list of username-password tuples) required to access interface. Can also provide function that takes username and password and returns True if valid login.
  • auth_message (str) - If provided, HTML message provided on login page.

gradio.Interface.load(name, src=None, api_key=None, alias=None)

Class method to construct an Interface from an external source repository, such as huggingface.

Parameters

  • name (str) - the name of the model (e.g. "gpt2"), can include the `src` as prefix (e.g. "huggingface/gpt2")
  • src (str) - the source of the model: `huggingface` or `gradio` (or empty if source is provided as a prefix in `name`)
  • api_key (str) - optional api key for use with Hugging Face Model Hub
  • alias (str) - optional, used as the name of the loaded model instead of the default name

Returns

  • (gradio.Interface) - a Gradio Interface object for the given model

Input Components

gradio.inputs.Textbox(self, lines=1, placeholder=None, default="", 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.
  • label (str) - component name in interface.

String Shortcuts

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

Demos

hello_world.py: View Code | Run Demo
diff_texts.py: View Code | Run Demo

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.

Demos

tax_calculator.py: View Code | Run Demo
titanic_survival.py: View Code | Run Demo

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

sentence_builder.py: View Code | Run Demo
generate_tone.py: View Code | Run Demo
titanic_survival.py: View Code | Run Demo

gradio.inputs.Checkbox(self, default=False, 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.
  • default (bool) - default value.

String Shortcuts

  • "checkbox" - Uses defaults implementation.

Demos

sentence_builder.py: View Code | Run Demo
titanic_survival.py: View Code | Run Demo

gradio.inputs.CheckboxGroup(self, choices, default=[], 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.
  • default (List[str]) - default selected list of options.
  • 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.

Demos

sentence_builder.py: View Code | Run Demo
titanic_survival.py: View Code | Run Demo
fraud_detector.py: View Code | Run Demo

gradio.inputs.Radio(self, choices, type="value", default=None, 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.
  • default (str) - default value.
  • label (str) - component name in interface.

Demos

sentence_builder.py: View Code | Run Demo
tax_calculator.py: View Code | Run Demo
titanic_survival.py: View Code | Run Demo

gradio.inputs.Dropdown(self, choices, type="value", default=None, 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.
  • default (str) - default value.
  • label (str) - component name in interface.

Demos

sentence_builder.py: View Code | Run Demo
filter_records.py: View Code | Run Demo
titanic_survival.py: View Code | Run Demo

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

Component creates an image upload box with editing capabilities.

Input type: Union[numpy.array, PIL.Image, file-object]

Parameters

  • shape (Tuple[int, int]) - (width, height) 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, "base64" leaves as a base64 string.
  • label (str) - component name in interface.
  • optional (bool) - If True, the interface can be submitted with no uploaded image, in which case the input value is None.

String Shortcuts

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

Demos

image_classifier.py: View Code | Run Demo
image_mod.py: View Code | Run Demo
digit_classifier.py: View Code | Run Demo

gradio.inputs.Video(self, type=None, label=None, optional=False)

Component creates a video file upload that is converted to a file path.

Input type: filepath

Parameters

  • type (str) - Type of video format to be returned by component, such as 'avi' or 'mp4'. If set to None, video will keep uploaded format.
  • label (str) - component name in interface.
  • optional (bool) - If True, the interface can be submitted with no uploaded video, in which case the input value is None.

String Shortcuts

  • "video" - Uses defaults implementation.

Demos

video_flip.py: View Code | Run Demo

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

Component accepts audio input files.

Input type: Union[Tuple[int, numpy.array], file-object, 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.
  • label (str) - component name in interface.
  • optional (bool) - If True, the interface can be submitted with no uploaded audio, in which case the input value is None.

String Shortcuts

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

Demos

main_note.py: View Code | Run Demo
reverse_audio.py: View Code | Run Demo
spectogram.py: View Code | Run Demo

gradio.inputs.File(self, file_count="single", type="file", label=None, optional=False)

Component accepts generic file uploads.

Input type: Union[file-object, bytes, List[Union[file-object, bytes]]]

Parameters

  • file_count (str) - if single, allows user to upload one file. If "multiple", user uploads multiple files. If "directory", user uploads all files in selected directory. Return type will be list for each file in case of "multiple" or "directory".
  • 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.
  • optional (bool) - If True, the interface can be submitted with no uploaded image, in which case the input value is None.

String Shortcuts

  • "file" - Uses defaults implementation.
  • "files" - Sets file_count=multiple

Demos

zip_to_json.py: View Code | Run Demo
zip_two_files.py: View Code | Run Demo

gradio.inputs.Dataframe(self, headers=None, row_count=3, col_count=3, datatype="str", col_width=None, default=None, 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".
  • col_width (Union[int, List[int]]) - Width of columns in pixels. Can be provided as single value or list of values per column.
  • default (List[List[Any]]) - Default value
  • 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: View Code | Run Demo
matrix_transpose.py: View Code | Run Demo
tax_calculator.py: View Code | Run Demo

gradio.inputs.Timeseries(self, x=None, y=None, label=None, optional=False)

Component accepts pandas.DataFrame uploaded as a timeseries csv file.

Input type: pandas.DataFrame

Parameters

  • x (str) - Column name of x (time) series. None if csv has no headers, in which case first column is x series.
  • y (Union[str, List[str]]) - Column name of y series, or list of column names if multiple series. None if csv has no headers, in which case every column after first is a y series.
  • label (str) - component name in interface.
  • optional (bool) - If True, the interface can be submitted with no uploaded csv file, in which case the input value is None.

String Shortcuts

  • "timeseries" - Uses defaults implementation.

Demos

fraud_detector.py: View Code | Run Demo

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

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

image_classifier.py: Link to Code
main_note.py: Link to Code
titanic_survival.py: Link to Code

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

Component displays an output image.

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

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 or a remote URL, "plot" expects a matplotlib.pyplot object, "auto" detects return type.
  • labeled_segments (bool) - If True, expects a two-element tuple to be returned. The first element of the tuple is the image of format specified by type. The second element is a list of tuples, where each tuple represents a labeled segment within the image. The first element of the tuple is the string label of the segment, followed by 4 floats that represent the left-x, top-y, right-x, and bottom-y coordinates of the bounding box.
  • label (str) - component name in interface.

String Shortcuts

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

Demos

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

gradio.outputs.Video(self, type=None, label=None)

Used for video output.

Output type: filepath

Parameters

  • type (str) - Type of video format to be passed to component, such as 'avi' or 'mp4'. Use 'mp4' to ensure browser playability. If set to None, video will keep returned format.
  • label (str) - component name in interface.

String Shortcuts

  • "video" - Uses defaults implementation.
  • "playable_video" - Sets type=mp4

Demos

video_flip.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, max_rows=20, max_cols=None, overflow_row_behaviour="paginate", 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. Only applicable if type is "numpy" or "array".
  • max_rows (int) - Maximum number of rows to display at once. Set to None for infinite.
  • max_cols (int) - Maximum number of columns to display at once. Set to None for infinite.
  • overflow_row_behaviour (str) - If set to "paginate", will create pages for overflow rows. If set to "show_ends", will show initial and final rows and truncate middle rows.
  • 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
fraud_detector.py: Link to Code

Component displays a set of output components that can be scrolled through.

Output type: List[List[Any]]

Parameters

  • components (Union[List[OutputComponent], OutputComponent]) - Classes of component(s) that will be scrolled through.
  • label (str) - component name in interface.

String Shortcuts

None.

Demos

disease_report.py: Link to Code

gradio.outputs.Timeseries(self, x=None, y=None, label=None)

Component accepts pandas.DataFrame.

Output type: pandas.DataFrame

Parameters

  • x (str) - Column name of x (time) series. None if csv has no headers, in which case first column is x series.
  • y (Union[str, List[str]]) - Column name of y series, or list of column names if multiple series. None if csv has no headers, in which case every column after first is a y series.
  • label (str) - component name in interface.

String Shortcuts

  • "timeseries" - Uses defaults implementation.

Demos

fraud_detector.py: Link to Code