Gradio-Learning
创始人
2025-05-31 16:29:45

Gradio-Learning

文章目录

  • Gradio-Learning
    • Interface函数-展示单个function
      • 程序示例
      • 参数
      • interface函数的子函数
    • Flagging函数-返回错误case
    • Combining-Interface功能-That's what i need!!!
      • combining-interface的功能维度考虑
      • 简介
      • TabbedInterface-选项卡式界面!!!-I love it!
        • 函数
        • 参数!
        • 示例
      • Parallel !!! - I love it more!!😭
        • 函数
        • 参数
        • 示例
      • 补充: lambda表达式
      • Series
        • 函数
        • 参数
        • 示例
    • Blocks函数

Interface函数-展示单个function

通常适用于一个功能的程序, 即对一个函数的功能进行展现

通过调用定义并调用接口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()

image-20230321002520012

参数

ParameterDescription
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.
examplesList[Any] | List[List[Any]] | str | None
default: 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(‘ None
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_pageint
default: 10
If examples are provided, how many to display per page.
livebool
default: 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.

interface函数的子函数

  • launch
  • load
  • from_pipeline
  • integrate
  • queue

它们的功能都很强大

Gradio Docs

image-20230321003012402

回来慢慢看

Flagging函数-返回错误case

在使用过程中如果遇到输出不符合预期值的点击flag即可向该程序的创建机器发送一条错误数据, 这些数据会在主机上保存为一个csv文件

balabala的剩下的自己慢慢看

Flagging函数也有三个子函数

image-20230321004920682

Combining-Interface功能-That’s what i need!!!

combining-interface的功能维度考虑

  • 独立性 - 各个接口之间的功能独立, 要分别独自查看功能输出效果, 分多个标签页进行功能展示
  • 整体性 - 各个接口之间功能类似但不相同, 即输入相同输出不同, 要放在一起对比输出效果, 放在一个标签页中将输出进行组合展示
  • 连续性 - 接口之间的功能有承接性, 即上一个接口的输出是下一个接口的输入, 此时可以将一众有前后逻辑连续性的接口放在一个队列中, 有序的协调好它们之间的前后关系, 然后通过一个输入和输出进行功能的展现

简介

当有多个单独的interface之后, 可以使用combining-interface功能将他们组合起来, 展示到同一个页面上(不是同一个标签页)

如果各个接口之间的输入是相同的, 并且输出是相关的, 还可以使用其中的功能对并行比较接口之间的输出效果

如果各个接口的输入和输出是有前后关联的, 还可以将众多接口组合成一个更完整且庞大的功能

TabbedInterface-选项卡式界面!!!-I love it!

函数

一个选项卡式界面由好多个接口创建, 每一个接口的功能都在一个单独的标签页面中进行展现

gradio.TabbedInterface(interface_list, ···)

参数!

ParameterDescription
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()

image-20230321012216913

Parallel !!! - I love it more!!😭

并行计算输出

考虑到你有相同类型的功能函数, 但是处理过程不同, 所以可以同时计算比较不同函数功能之间输出的差异

即输入一定要相同, 输出可以不同

更好的是这是构建在同一个页面上的, 而不是一个页面的多个标签页

函数

gradio.Parallel(interfaces, ···)

参数

ParameterDescription
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()

image-20230321012301700

补充: lambda表达式

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很厉害

Series

功能前面已经介绍过了

函数

gradio.Series(interfaces, ···)

参数

ParameterDescription
interfacesrequiredany 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()

image-20230321014525106

Blocks函数

相关内容

热门资讯

头歌--第1关:Linux文件... 任务描述 假设系统中存在一个文件File,修改该文件的权限,根据实际需求...
【Spring从成神到升仙系列... 👏作者简介:大家好,我是爱敲代码的小黄,独...
梦见蜈蚣是什么意思,做梦梦见蜈... 梦见蜈蚣是什么意思目录梦见蜈蚣是什么意思做梦梦见蜈蚣什么意思梦见蜈蚣是什么意思,哪里有解释啊梦见蜈蚣...
小区车位比一般是多少,车库配比... 小区车位比一般是多少目录小区车位比一般是多少车库配比是什么小区总户数8200,总车位是1450个,配...
车锁上的lock什么意思,汽车... 车锁上的lock什么意思目录车锁上的lock什么意思汽车上lock是什么意思?车子上“lock标志”...
kirin710是什么处理器,... kirin710是什么处理器目录kirin710是什么处理器海思kirin710是高通多少?骁龙71...
程序的循环结构和random库...   第三个参数就是步长     引入文件时记得指明字符格式,否则读入不了 ...
跟着文档制作cocos第一个游... 背景 近期打算学习一下cocos creator,想着开发自己的游戏,是...
乌干达是什么梗,网络语乌干达什... 乌干达是什么梗目录乌干达是什么梗网络语乌干达什么意思?乌干达是什么梗乌干达是什么梗乌干达是什么梗 ...
车载电子狗怎么用,怎样使用电子... 车载电子狗怎么用目录车载电子狗怎么用怎样使用电子狗怎么使用电子狗求简答车载电子狗怎么使用车载电子狗怎...
梦见偷东西是什么意思,梦见自己... 梦见偷东西是什么意思目录梦见偷东西是什么意思梦见自己偷东西是什么意思?做梦梦见自己偷东西好不好梦见偷...
黄金瞳到底是什么,黄金瞳电视剧... 黄金瞳到底是什么目录黄金瞳到底是什么黄金瞳电视剧什么时候上映?《黄金瞳》的结局是什么?电视剧《黄金瞳...
前端-session、jwt 目录:   (1)session (2&#x...
企业即时通讯怎样为企业实现移动... 对于企业来说,在办公过程中少不了工作人员相互传递信息和数据传输,企业内部...
骑行选择什么自行车 极速百科网... 骑行选择什么自行车目录骑行选择什么自行车骑行选择什么自行车 1. 山地自行车:适合崎岖不平的路...
蓝色都有哪几种,蓝色都有什么颜... 蓝色都有哪几种目录蓝色都有哪几种蓝色都有什么颜色的蓝图片,蓝色都有什么颜色的蓝二年级蓝色有哪些种类蓝...
如何自学游泳要安全的,初学游泳... 如何自学游泳要安全的目录如何自学游泳要安全的初学游泳的人需要准备哪些东西,注意哪些事项?如何自学游泳...
一年级家长的话怎么写评语,一年... 一年级家长的话怎么写评语目录一年级家长的话怎么写评语一年级学生评价手册家长寄语怎么写一年级最佳家长评...
EEG微状态的功能意义 导读大脑的瞬时全局功能状态反映在其电场结构上。聚类分析方法一致地提取了四种头表面脑电场结构ÿ...
docker 镜像管理 查看本地镜像 docker images 可以查看本地下载的镜像 docker images [O...
k8s-1.22.15部署ng... 1.介绍 在前面文章中已经提到,Service对集群之外暴露服务的主要方式有两种&#x...
革命烈士寄语怎么写,清明节缅怀... 革命烈士寄语怎么写目录革命烈士寄语怎么写清明节缅怀先烈的寄语有哪些呢?革命烈士寄语怎么写 革命...
5万元以下新车推荐,5万以下买... 本篇文章极速百科给大家谈谈5万元以下新车推荐,5万以下买什么车好,以及5万以下的新车哪款最好对应的知...
真皮沙发翻新一般多少钱?(真皮... 本篇文章极速百科给大家谈谈真皮沙发翻新一般多少钱?,以及真皮沙发翻新一般多少钱一个对应的知识点,希望...
磨皮什么意思(磨皮是啥?) 磨... 本篇文章极速百科给大家谈谈磨皮什么意思,以及磨皮是啥?对应的知识点,希望对各位有所帮助,不要忘了收藏...
进程间通信【Linux】 1. 进程间通信 1.1 什么是进程间通信 在 Linux 系统中,进程间通信...
从NVIDIA GTC大会,看... 从NVIDIA GTC 2023这场全球行业盛宴,我们可以解读出AI算力行业的哪些重要...
请问什么是童子,什么是童子 极... 请问什么是童子目录请问什么是童子什么是童子古代 童子是什么意思童子是什么意思?请问什么是童子 ...
中招考试考哪些科目,中招考试考... 中招考试考哪些科目目录中招考试考哪些科目中招考试考几门科目一共多少分?中考有哪些科目中考考几科,都什...
做电商如何做,电商怎样做才能赚... 做电商如何做目录做电商如何做电商怎样做才能赚钱?做的好的电商朋友可以教教我怎么做吗新手小白怎么做跨境...