interface
gradio.Interface(fn, inputs, outputs, ···)
describe
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. You must specify three parameters: (1) a function that creates the GUI for (2) the required input components and (3) the required output components. Other parameters can be used to control the appearance and behavior of the presentation.
Example usage
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()
initialization
parameter | describe |
---|---|
fn transfer Required |
Functions used to wrap interfaces. Typically the prediction function of a machine learning model. Each parameter of the function corresponds to an input component, and the function should return a single value or a tuple of values, with each element in the tuple corresponding to an output component. |
inputs STR |IOComponent |List[str |IOComponent] |None Required |
A single Gradio component or a list of Gradio components. Components can be passed as instantiated objects or referenced by their string shortcuts. The number of input components should match the number of parameters in fn. If set to None, only output components are displayed. |
outputs STR |IOComponent |List[str |IOComponent] |None Required |
A single Gradio component or a list of Gradio components. Components can be passed as instantiated objects or referenced by their string shortcuts. The number of output components should match the number of values returned by fn. If set to None, only input components are displayed. |
examples list[any] | list[list[any]] | STR | none Default: None |
Sample input for the function; if provided, appears below the UI component and can be clicked to populate the interface. Should be nested lists, where the outer list consists of samples and each inner list consists of inputs corresponding to each input component. It is also possible to provide a string path to the examples directory, but it should be in the directory of the python file where the Gradio application is running. If there are multiple input components and a directory is provided, a log.csv file must exist in the directory to link the corresponding inputs. |
cache_examples boolean|none Default: None |
If True, cache the example in the server for fast runtime in the example. If 'fn' is a generator function, then the last generated value is used as output. The default option in Embrace Face Space is True. The default option elsewhere is False. |
examples_per_page internationality Default value: 10 |
If examples are provided, how many examples to display per page. |
live Boolean Default value: false |
Whether the interface should automatically rerun if any input changes. |
interpretation Redeemable | STR | None Default: None |
A function that provides an explanation of the prediction output. Pass "default" to use the simple built-in interpreter, "shap" to use the Shapley-based built-in interpreter, or your own custom interpreter function. For details on the different interpretation methods, see the Advanced Interface Features Guide. |
num_shap float Default value: 2.0 |
A multiplier that determines how many samples are calculated for SHAP-based interpretation. Increasing this value will increase shap runtime but improve results. Applies only when interpreted as "shap". |
title STR | None Default: None |
The title of the interface; if provided, appears in large font above the input and output components. Also used as the tab title when opened in a browser window. |
description STR | None Default: None |
Description of the interface; if provided, appears in regular font above the input and output components and below the title. Markdown and HTML content accepted. |
article STR | None Default: None |
Extension article explaining the interface; if provided, appears in regular font below the input and output components. Markdown and HTML content accepted. |
thumbnail STR | None Default: None |
The path or URL of the image to use as the display image when sharing a web presentation on social media. |
theme Topic | STR | None Default: None |
The theme to use, loaded from gradient.themes. |
css STR | None Default: None |
Custom CSS or path to a custom CSS file to be used with the interface. |
allow_flagging STR | None Default: None |
One of "Never", "Automatic" or "Manual". If "Never" or "Automatic" the user will not see buttons for marking inputs and outputs. If "Manual" the user will see a button to mark. If "auto", every input submitted by the user will be automatically tagged (output will not be tagged). If "Manual", both inputs and outputs will be marked when the user clicks the flag button. This parameter can be set using the environment variable GRADIO_ALLOW_FLAGGING; otherwise it defaults to "manual". |
flagging_options list[str] | list[tuple[str, str]] | None Default: None |
If provided, allows the user to select from a list of options when marking. Applies only if allow_flagging is "manual". Can be a list of tuples of form (label, value), where label is the string that will be displayed on the button and value is the string that will be stored in the tag CSV; or it can be a list of strings ["X", " Y"], in which case the values will be a list of strings and the labels will be ["marked by X", "marked by Y"] etc. |
flagging_dir str Default: "Tagged" |
How to name the directory where marked data is stored. |
flagging_callback Mark callback 默认:CSVLogger() |
FlaggingCallback 子类的一个实例,将在标记示例时调用该子类。默认情况下,记录到本地 CSV 文件。 |
analytics_enabled 布尔 |没有 默认值:无 |
是否允许基本遥测。如果为 None,则将使用环境变量(如果已定义GRADIO_ANALYTICS_ENABLED或默认为 True。 |
batch 布尔 默认值:假 |
如果为 True,则函数应处理一批输入,这意味着它应接受每个参数的输入值列表。列表的长度应相等(长度应为“max_batch_size”)。然后*需要*该函数返回列表元组(即使只有 1 个输出组件),元组中的每个列表对应于一个输出组件。 |
max_batch_size 国际 默认值:4 |
如果从队列中调用,则要批处理在一起的最大输入数(仅当 batch=True 时才相关) |
api_name STR |文字[错误] |没有 默认值:“预测” |
定义终结点在 API 文档中的显示方式。可以是字符串、无或假。如果为 False 或 None,则终结点不会在 api 文档中公开。如果设置为字符串,则终结点将以给定名称在 api 文档中公开。默认值为“预测”。 |
allow_duplication 布尔 默认值:假 |
如果为 True,则将在拥抱面部空间上显示“重复空间”按钮。 |
演示
import gradio as gr
def greet(name):
return "Hello " + name + "!"
demo = gr.Interface(fn=greet, inputs="text", outputs="text")
if __name__ == "__main__":
demo.launch(