Gradio Agents & MCP 黑客马拉松
获奖者Gradio Agents & MCP 黑客马拉松
获奖者gradio.LinePlot(···)
def predict(
value: AltairPlotData | None
)
...
x
参数),另一列用于 y 轴(对应于 y
参数)。def predict(···) -> pd.DataFrame | dict | None
...
return value
x_bin: str | float | None
= None
用于聚类 x 值的分组。如果 x 列是数字类型,则应为用于分箱 x 值的数字。如果 x 列是日期时间类型,则应为字符串,例如 "1h", "15m", "10s",使用 "s", "m", "h", "d" 后缀。
y_aggregate: Literal['sum', 'mean', 'median', 'min', 'max', 'count'] | None
= None
用于聚合 y 值的聚合函数,在提供了 x_bin 或 x 为字符串/类别时使用。必须是 "sum", "mean", "median", "min", "max" 中的一个。
color_map: dict[str, str] | None
= None
系列到颜色名称或代码的映射。例如,{"success": "green", "fail": "#FF8888"}。
sort: Literal['x', 'y', '-x', '-y'] | list[str] | None
= None
x 值的排序顺序,如果 x 列是字符串/类别类型。可以是 "x", "y", "-x", "-y",或表示类别顺序的字符串列表。
tooltip: Literal['axis', 'none', 'all'] | list[str]
= "axis"
悬停在点上时显示的工具提示。"axis" 显示轴列的值,"all" 显示所有列的值,"none" 不显示工具提示。也可以提供一个字符串列表,表示要在工具提示中显示的列,这些列将与轴值一起显示。
scale: int | None
= None
相对于相邻组件的相对大小。例如,如果组件 A 和 B 在同一行中,A 的 scale=2,B 的 scale=1,则 A 的宽度将是 B 的两倍。应为一个整数。scale 在 Rows 中以及 Blocks 中 fill_height=True 的顶级组件中应用。
min_width: int
= 160
最小像素宽度,如果屏幕空间不足以满足此值,则会换行。如果某个 scale 值导致此组件比 min_width 更窄,则将优先遵守 min_width 参数。
every: Timer | float | None
= None
如果 `value` 是函数,则连续调用 `value` 重新计算它(否则无效)。可以提供一个 Timer,其计时滴答会重置 `value`,或提供一个浮点数作为重置 Timer 的固定间隔。
inputs: Component | list[Component] | Set[Component] | None
= None
用作计算 `value` 的输入的组件,如果 `value` 是函数(否则无效)。`value` 在输入更改时重新计算。
类 | Interface 字符串快捷方式 | 初始化 |
---|---|---|
| "lineplot" | 使用默认值 |
import pandas as pd
from random import randint, random
import gradio as gr
temp_sensor_data = pd.DataFrame(
{
"time": pd.date_range("2021-01-01", end="2021-01-05", periods=200),
"temperature": [randint(50 + 10 * (i % 2), 65 + 15 * (i % 2)) for i in range(200)],
"humidity": [randint(50 + 10 * (i % 2), 65 + 15 * (i % 2)) for i in range(200)],
"location": ["indoor", "outdoor"] * 100,
}
)
food_rating_data = pd.DataFrame(
{
"cuisine": [["Italian", "Mexican", "Chinese"][i % 3] for i in range(100)],
"rating": [random() * 4 + 0.5 * (i % 3) for i in range(100)],
"price": [randint(10, 50) + 4 * (i % 3) for i in range(100)],
"wait": [random() for i in range(100)],
}
)
with gr.Blocks() as line_plots:
with gr.Row():
start = gr.DateTime("2021-01-01 00:00:00", label="Start")
end = gr.DateTime("2021-01-05 00:00:00", label="End")
apply_btn = gr.Button("Apply", scale=0)
with gr.Row():
group_by = gr.Radio(["None", "30m", "1h", "4h", "1d"], value="None", label="Group by")
aggregate = gr.Radio(["sum", "mean", "median", "min", "max"], value="sum", label="Aggregation")
temp_by_time = gr.LinePlot(
temp_sensor_data,
x="time",
y="temperature",
)
temp_by_time_location = gr.LinePlot(
temp_sensor_data,
x="time",
y="temperature",
color="location",
)
time_graphs = [temp_by_time, temp_by_time_location]
group_by.change(
lambda group: [gr.LinePlot(x_bin=None if group == "None" else group)] * len(time_graphs),
group_by,
time_graphs
)
aggregate.change(
lambda aggregate: [gr.LinePlot(y_aggregate=aggregate)] * len(time_graphs),
aggregate,
time_graphs
)
def rescale(select: gr.SelectData):
return select.index
rescale_evt = gr.on([plot.select for plot in time_graphs], rescale, None, [start, end])
for trigger in [apply_btn.click, rescale_evt.then]:
trigger(
lambda start, end: [gr.LinePlot(x_lim=[start, end])] * len(time_graphs), [start, end], time_graphs
)
price_by_cuisine = gr.LinePlot(
food_rating_data,
x="cuisine",
y="price",
)
with gr.Row():
price_by_rating = gr.LinePlot(
food_rating_data,
x="rating",
y="price",
)
price_by_rating_color = gr.LinePlot(
food_rating_data,
x="rating",
y="price",
color="cuisine",
color_map={"Italian": "red", "Mexican": "green", "Chinese": "blue"},
)
if __name__ == "__main__":
line_plots.launch()
事件监听器允许您响应用户与您在 Gradio Blocks 应用中定义的 UI 组件的交互。当用户与某个元素交互时,例如更改滑块值或上传图像,会调用一个函数。
LinePlot 组件支持以下事件监听器。每个事件监听器接受相同的参数,这些参数列在下面的事件参数表中。
监听器 | 描述 |
---|---|
| 当用户选择或取消选择 NativePlot 时触发的事件监听器。使用事件数据 gradio.SelectData 携带 |
| 双击 NativePlot 时触发。 |
fn: Callable | None | Literal['decorator']
= "decorator"
此事件触发时要调用的函数。通常是机器学习模型的预测函数。函数的每个参数对应一个输入组件,函数应返回单个值或一个元组值,元组中的每个元素对应一个输出组件。
inputs: Component | BlockContext | list[Component | BlockContext] | Set[Component | BlockContext] | None
= None
用作输入的 gradio.components 列表。如果函数不接受输入,则应为空列表。
outputs: Component | BlockContext | list[Component | BlockContext] | Set[Component | BlockContext] | None
= None
用作输出的 gradio.components 列表。如果函数不返回输出,则应为空列表。
api_name: str | None | Literal[False]
= None
定义了端点在 API 文档中的显示方式。可以是字符串、None 或 False。如果设置为字符串,则端点将在 API 文档中以给定名称公开。如果为 None(默认值),将使用函数名称作为 API 端点。如果为 False,则端点不会在 API 文档中公开,并且下游应用(包括那些 `gr.load` 此应用的)将无法使用此事件。
show_progress: Literal['full', 'minimal', 'hidden']
= "full"
事件运行时如何显示进度动画:"full" 显示一个覆盖输出组件区域的旋转图标以及右上角的运行时显示,"minimal" 仅显示运行时显示,"hidden" 完全不显示进度动画
show_progress_on: Component | list[Component] | None
= None
显示进度动画的组件或组件列表。如果为 None,将在所有输出组件上显示进度动画。
queue: bool
= True
如果为 True,并且已启用队列,则将请求放入队列。如果为 False,则即使已启用队列,也不会将此事件放入队列。如果为 None,将使用 Gradio 应用的队列设置。
batch: bool
= False
如果为 True,则函数应处理一批输入,这意味着它应接受每个参数的输入值列表。这些列表应长度相等(且最大长度为 `max_batch_size`)。函数 *必须* 返回一个列表元组(即使只有一个输出组件),元组中的每个列表对应一个输出组件。
preprocess: bool
= True
如果为 False,在运行 'fn' 之前不会对组件数据进行预处理(例如,如果使用 `Image` 组件调用此方法,则将其保留为 base64 字符串)。
cancels: dict[str, Any] | list[dict[str, Any]] | None
= None
触发此监听器时要取消的其他事件列表。例如,设置 cancels=[click_event] 将取消 click_event,其中 click_event 是另一个组件的 .click 方法的返回值。尚未运行的函数(或正在迭代的生成器)将被取消,但当前正在运行的函数将被允许完成。
trigger_mode: Literal['once', 'multiple', 'always_last'] | None
= None
如果为 "once"(除 `.change()` 外所有事件的默认值),则在事件待处理期间不允许任何提交。如果设置为 "multiple",则在待处理期间允许无限次提交;如果设置为 "always_last"(`.change()` 和 `.key_up()` 事件的默认值),则在待处理事件完成后允许第二次提交。
js: str | Literal[True] | None
= None
在运行 'fn' 之前运行的可选前端 js 方法。js 方法的输入参数是 'inputs' 和 'outputs' 的值,返回值应为输出组件的值列表。
concurrency_limit: int | None | Literal['default']
= "default"
如果设置,这是可以同时运行此事件的最大数量。可以设置为 None 表示没有并发限制(此事件的任意数量可以同时运行)。设置为 "default" 以使用默认并发限制(由 `Blocks.queue()` 中的 `default_concurrency_limit` 参数定义,其自身默认为 1)。
concurrency_id: str | None
= None
如果设置,这是并发组的 ID。具有相同 concurrency_id 的事件将受最低设置的 concurrency_limit 限制。