通常适用于一个功能的程序, 即对一个函数的功能进行展现
通过调用定义并调用接口gr.Interface()
进行实现
Interface
接口中有三个必须的参数:
fn
- function - 即功能函数inputs
- 输入根据函数的输入参数, 可以是一个也可以是多个, 多个就用数组进行表示outputs
- 输出, 输出和输入一样, 可以有一个或多个, 多个则用数组表示如下是一个三个输入 / 两个输出
的一个打招呼功能函数
其中最后一个参数temperature
使用了gradio中的gr.Slider
模块, 进行了一个滑动的效果展示
import gradio as grdef greet(name, is_morning, temperature):salutation = "Good morning" if is_morning else "Good evening"greeting = f"{salutation} {name}. It is {temperature} degrees today"celsius = (temperature - 32) * 5 / 9return greeting, round(celsius, 2)demo = gr.Interface(fn=greet,inputs=["text", "checkbox", gr.Slider(0, 100)],outputs=["text", "number"],
)
if __name__ == "__main__":demo.launch()
Parameter | Description |
---|---|
fn Callable required | the function to wrap an interface around. Often a machine learning model’s prediction function. Each parameter of the function corresponds to one input component, and the function should return a single value or a tuple of values, with each element in the tuple corresponding to one output component. |
inputs str | List[str | required | a single Gradio component, or list of Gradio 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. If set to None, then only the output components will be displayed. |
outputs str | List[str | None required | a single Gradio component, or list of Gradio 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. If set to None, then only the input components will be displayed. |
examples List[Any] | List[List[Any]] | str | Nonedefault: None | sample inputs for the function; if provided, appear below the UI components and can be clicked 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, but it should be within the directory with the python file running the gradio app. 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.numpy.core._exceptions._UFuncNoLoopError: ufunc ‘multiply’ did not contain a loop with signature matching types (dtype(‘ |
cache_examples bool | None default: None | If True, caches examples in the server for fast runtime in examples. The default option in HuggingFace Spaces is True. The default option elsewhere is False. |
examples_per_page intdefault: 10 | If examples are provided, how many to display per page. |
live booldefault: False | whether the interface should automatically rerun if any of the inputs change. |
interpretation Callable | str | None default: None | function that provides interpretation explaining prediction output. Pass “default” to use simple built-in interpreter, “shap” to use a built-in shapley-based interpreter, or your own custom interpretation function. For more information on the different interpretation methods, see the Advanced Interface Features guide. |
num_shap float default: 2.0 | a multiplier that determines how many examples are computed for shap-based interpretation. Increasing this value will increase shap runtime, but improve results. Only applies if interpretation is “shap”. |
title str | None default: None | a title for the interface; if provided, appears above the input and output components in large font. Also used as the tab title when opened in a browser window. |
description str | None default: None | a description for the interface; if provided, appears above the input and output components and beneath the title in regular font. Accepts Markdown and HTML content. |
article str | None default: None | an expanded article explaining the interface; if provided, appears below the input and output components in regular font. Accepts Markdown and HTML content. |
它们的功能都很强大
Gradio Docs
回来慢慢看
在使用过程中如果遇到输出不符合预期值的点击flag即可向该程序的创建机器发送一条错误数据, 这些数据会在主机上保存为一个csv文件
balabala的剩下的自己慢慢看
Flagging函数也有三个子函数
当有多个单独的interface之后, 可以使用combining-interface功能将他们组合起来, 展示到同一个页面上(不是同一个标签页)
如果各个接口之间的输入是相同的, 并且输出是相关的, 还可以使用其中的功能对并行比较接口之间的输出效果
如果各个接口的输入和输出是有前后关联的, 还可以将众多接口组合成一个更完整且庞大的功能
一个选项卡式界面由好多个接口创建, 每一个接口的功能都在一个单独的标签页面中进行展现
gradio.TabbedInterface(interface_list, ···)
Parameter | Description |
---|---|
interface_list List[Interface] required 必需的参数, 一系列的功能接口 | a list of interfaces to be rendered in tabs. |
tab_names List[str] | Nonedefault: None 也比较需要, 需要自定义的给每一个标签页进行命名 | a list of tab names. If None, the tab names will be “Tab 1”, “Tab 2”, etc. 所以还是自己定义一下吧 |
title str | Nonedefault: None 可有可没有, 有了更好, 进行一个简短的描述 但是我很疑惑为什么不是一个list, 难道不是每一个接口都需要一个自己的描述吗 | a title for the interface; if provided, appears above the input and output components in large font. Also used as the tab title when opened in a browser window. |
看了示例明白了, 接口中可以定义自己接口的title
, 所以不用在TabbedInterface
中定义了
import gradio as grtitle = "GPT-J-6B"tts_examples = ["I love learning machine learning","How do you do?",
]tts_demo = gr.Interface.load("huggingface/facebook/fastspeech2-en-ljspeech",title=None,examples=tts_examples,description="Give me something to say!",
)stt_demo = gr.Interface.load("huggingface/facebook/wav2vec2-base-960h",title=None,inputs="mic",description="Let me try to guess what you're saying!",
)demo = gr.TabbedInterface([tts_demo, stt_demo], ["Text-to-speech", "Speech-to-text"])if __name__ == "__main__":demo.launch()
并行计算输出
考虑到你有相同类型的功能函数, 但是处理过程不同, 所以可以同时计算比较不同函数功能之间输出的差异
即输入一定要相同, 输出可以不同
更好的是这是构建在同一个页面上的, 而不是一个页面的多个标签页
gradio.Parallel(interfaces, ···)
Parameter | Description |
---|---|
interfaces required | any number of Interface objects that are to be compared in parallel 任意的😭 |
示例中是文本, 就是不知道如果是图像网页要怎么进行排版, 或者是图像能不能进行并行输出
import gradio as grgreeter_1 = gr.Interface(lambda name: f"Hello {name}!", inputs="textbox", outputs=gr.Textbox(label="Greeter 1"))
greeter_2 = gr.Interface(lambda name: f"Greetings {name}!", inputs="textbox", outputs=gr.Textbox(label="Greeter 2"))
demo = gr.Parallel(greeter_1, greeter_2)if __name__ == "__main__":demo.launch()
lambda表达式就是一个简单的函数, 不用进行一般的函数的显示定义, 即创建一个匿名函数
lambda表达式是一种创建匿名函数的简便方式,它不需要定义一个函数名或使用def关键字。
lambda表达式的语法是:
lambda 参数列表: 表达式
append_nice = gr.Interface(lambda greeting: f"{greeting} Nice to meet you!",inputs="textbox", outputs=gr.Textbox(label="Greeting"))
在上述语句中, lambda表达式接受一个参数greeting,它是一个字符串,然后返回一个新的字符串,它在greeting后面加上" Nice to meet you!"。
这个lambda表达式被传递给gr.Interface的fn参数,它是创建Gradio界面的主要函数。
这意味着当用户在输入框中输入一个问候语时,Gradio界面会调用这个lambda表达式,并显示它的返回值。
很清晰, chatbing很厉害
功能前面已经介绍过了
gradio.Series(interfaces, ···)
Parameter | Description |
---|---|
interfaces required | any number of Interface objects that are to be connected in series |
import gradio as grget_name = gr.Interface(lambda name: name, inputs="textbox", outputs="textbox")
prepend_hello = gr.Interface(lambda name: f"Hello {name}!", inputs="textbox", outputs="textbox")
append_nice = gr.Interface(lambda greeting: f"{greeting} Nice to meet you!",inputs="textbox", outputs=gr.Textbox(label="Greeting"))
demo = gr.Series(get_name, prepend_hello, append_nice)if __name__ == "__main__":demo.launch()