- 组件
- MultimodalTextbox
MultimodalTextbox
gradio.MultimodalTextbox(···)描述
创建一个文本区域,供用户输入字符串或显示字符串输出,同时允许上传多媒体文件。
行为
作为输入组件: 将文本值和文件列表作为 dict 传递给函数。
您的函数应接受其中一种类型
def predict(
value: MultimodalValue | None
)
...作为输出组件: 期望一个带有 "text" 和 "files" 的 dict,两者都是可选的。文件数组是文件路径或 URL 的列表。
您的函数应返回其中一种类型
def predict(···) -> MultimodalValue | None
...
return value初始化
value: str | dict[str, str | list] | Callable | None
value: str | dict[str, str | list] | Callable | None= None在 MultimodalTextbox 中显示的默认值。一个字符串值,或者一个形如 {"text": "sample text", "files": [{path: "files/file.jpg", orig_name: "file.jpg", url: "http://image_url.jpg", size: 100}]} 的字典。如果提供了一个函数,每次应用程序加载时都会调用该函数来设置此组件的初始值。
sources: list[Literal['upload', 'microphone']] | Literal['upload', 'microphone'] | None
sources: list[Literal['upload', 'microphone']] | Literal['upload', 'microphone'] | None= None允许的源列表。"upload" 创建一个按钮,用户可以点击上传或拖放文件,"microphone" 创建一个麦克风输入。如果为 None,则默认为 ["upload"]。
file_types: list[str] | None
file_types: list[str] | None= None允许上传的文件扩展名或文件类型列表(例如 ['image', '.json', '.mp4'])。"file" 允许上传任何文件,"image" 只允许上传图像文件,"audio" 只允许上传音频文件,"video" 只允许上传视频文件,"text" 只允许上传文本文件。
file_count: Literal['single', 'multiple', 'directory']
file_count: Literal['single', 'multiple', 'directory']= "single"如果为 "single",则允许用户上传一个文件。如果为 "multiple",则用户上传多个文件。如果为 "directory",则用户上传选定目录中的所有文件。在 "multiple" 或 "directory" 的情况下,返回类型将是每个文件的列表。
label: str | I18nData | None
label: str | I18nData | None= None此组件的标签,如果 `show_label` 为 `True`,则显示在该组件上方,如果存在此组件的示例表格,它也将用作标题。如果为 None 且在 `gr.Interface` 中使用,则标签将是此组件对应的参数的名称。
every: Timer | float | None
every: Timer | float | None= None持续调用 `value` 来重新计算它(如果 `value` 是一个函数,否则无效)。可以提供一个 Timer,其滴答声会重置 `value`,或者提供一个浮点数来为重置 Timer 提供常规间隔。
inputs: Component | list[Component] | set[Component] | None
inputs: Component | list[Component] | set[Component] | None= None用作计算 `value` 的输入的组件(如果 `value` 是一个函数,否则无效)。`value` 在输入更改时会重新计算。
scale: int | None
scale: int | None= None与相邻组件相比的相对大小。例如,如果组件 A 和 B 在一个 Row 中,A 的 scale 为 2,B 的 scale 为 1,则 A 的宽度将是 B 的两倍。应为整数。scale 适用于 Rows,以及 Blocks 中 fill_height=True 的顶级组件。
min_width: int
min_width: int= 160最小像素宽度,如果屏幕空间不足以满足此值,则会换行。如果某个 scale 值导致此组件比 min_width 窄,则将首先考虑 min_width 参数。
visible: bool | Literal['hidden']
visible: bool | Literal['hidden']= True如果为 False,则组件将隐藏。如果为“hidden”,则组件将视觉上隐藏并且不会在布局中占用空间,但仍存在于 DOM 中。
key: int | str | tuple[int | str, ...] | None
key: int | str | tuple[int | str, ...] | None= None在 `gr.render` 中,跨重渲染具有相同键的组件被视为同一个组件,而不是一个新组件。在 `preserved_by_key` 中设置的属性在重渲染时不会重置。
preserved_by_key: list[str] | str | None
preserved_by_key: list[str] | str | None= "value"此组件构造函数中的参数列表。在 `gr.render()` 函数内部,如果一个组件使用相同的键进行重渲染,则这些(也是唯一的)参数将在 UI 中被保留(如果它们已被用户或事件监听器更改),而不是根据构造函数中提供的值进行重渲染。
快捷方式
gradio.MultimodalTextbox
gradio.MultimodalTextbox"multimodaltextbox"演示
事件监听器
描述
事件监听器允许您响应 Gradio Blocks 应用中定义的 UI 组件的用户交互。当用户与元素交互时(例如,更改滑块值或上传图像),会调用一个函数。
支持的事件监听器
MultimodalTextbox 组件支持以下事件监听器。每个事件监听器都采用相同的参数,这些参数列在下面的 事件参数 表中。
MultimodalTextbox.change(fn, ···)
MultimodalTextbox.change(fn, ···)当 MultimodalTextbox 的值发生变化时触发,无论是由于用户输入(例如用户在文本框中输入)还是由于函数更新(例如图像从事件触发器的输出接收到值)。有关仅由用户输入触发的监听器,请参阅 .input()。
MultimodalTextbox.input(fn, ···)
MultimodalTextbox.input(fn, ···)当用户更改 MultimodalTextbox 的值时触发此监听器。
MultimodalTextbox.select(fn, ···)
MultimodalTextbox.select(fn, ···)当用户选择或取消选择 MultimodalTextbox 时触发的事件监听器。使用事件数据 gradio.SelectData 来携带 value(指 MultimodalTextbox 的标签)和 selected(指 MultimodalTextbox 的状态)。有关更多详细信息,请参阅 https://gradio.org.cn/docs/gradio/eventdata。
MultimodalTextbox.submit(fn, ···)
MultimodalTextbox.submit(fn, ···)当 MultimodalTextbox 获得焦点时用户按下 Enter 键时触发此监听器。
MultimodalTextbox.focus(fn, ···)
MultimodalTextbox.focus(fn, ···)当 MultimodalTextbox 获得焦点时触发此监听器。
MultimodalTextbox.blur(fn, ···)
MultimodalTextbox.blur(fn, ···)当 MultimodalTextbox 失去焦点/模糊时触发此监听器。
MultimodalTextbox.stop(fn, ···)
MultimodalTextbox.stop(fn, ···)当用户在 MultimodalTextbox 中播放的媒体结束时触发此监听器。
事件参数
fn: Callable | None | Literal['decorator']
fn: Callable | None | Literal['decorator']= "decorator"当此事件触发时调用的函数。通常是机器学习模型的预测函数。函数的每个参数对应一个输入组件,函数应返回一个单一值或一个值元组,元组中的每个元素对应一个输出组件。
inputs: Component | BlockContext | list[Component | BlockContext] | Set[Component | BlockContext] | None
inputs: Component | BlockContext | list[Component | BlockContext] | Set[Component | BlockContext] | None= None用作输入的 gradio.components 列表。如果函数不接受输入,则此列表应为空。
outputs: Component | BlockContext | list[Component | BlockContext] | Set[Component | BlockContext] | None
outputs: Component | BlockContext | list[Component | BlockContext] | Set[Component | BlockContext] | None= None用作输出的 gradio.components 列表。如果函数不返回输出,则此列表应为空。
api_name: str | None
api_name: str | None= None定义该端点在 API 文档中如何显示。可以是字符串或 None。如果设置为字符串,则该端点将在 API 文档中以给定名称显示。如果为 None(默认),则使用函数的名称作为 API 端点。
api_description: str | None | Literal[False]
api_description: str | None | Literal[False]= NoneAPI 端点的描述。可以是字符串、None 或 False。如果设置为字符串,则该端点将在 API 文档中以给定描述显示。如果为 None,则使用函数的 docstring 作为 API 端点描述。如果为 False,则 API 文档中不会显示任何描述。
show_progress: Literal['full', 'minimal', 'hidden']
show_progress: Literal['full', 'minimal', 'hidden']= "full"事件运行时如何显示进度动画:“full”显示一个覆盖输出组件区域的加载指示器,并在右上角显示运行时信息,“minimal”仅显示运行时信息,“hidden”不显示任何进度动画。
show_progress_on: Component | list[Component] | None
show_progress_on: Component | list[Component] | None= None用于显示进度动画的组件或组件列表。如果为 None,则进度动画将显示在所有输出组件上。
queue: bool
queue: bool= True如果为 True,则会将请求放在队列中(如果队列已启用)。如果为 False,则不会将此事件放入队列,即使队列已启用。如果为 None,则将使用 Gradio 应用的队列设置。
batch: bool
batch: bool= False如果为 True,则函数应处理一批输入,这意味着它应该为每个参数接受一个输入值列表。列表的长度应相同(最多为 `max_batch_size`)。然后,该函数*必须*返回一个元组的列表(即使只有一个输出组件),元组中的每个列表对应一个输出组件。
preprocess: bool
preprocess: bool= True如果为 False,则在运行 'fn' 之前不会进行组件数据预处理(例如,当使用 `Image` 组件调用此方法时,将其保留为 base64 字符串)。
cancels: dict[str, Any] | list[dict[str, Any]] | None
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
trigger_mode: Literal['once', 'multiple', 'always_last'] | None= None如果设置为 "once"(除 `.change()` 之外所有事件的默认值),则在事件挂起时不允许任何提交。如果设置为 "multiple",则在事件挂起时允许无限次提交,而 "always_last"(`.change()` 和 `.key_up()` 事件的默认值)则允许在挂起事件完成后进行第二次提交。
js: str | Literal[True] | None
js: str | Literal[True] | None= None在运行 'fn' 之前运行可选的前端 JS 方法。JS 方法的输入参数是 'inputs' 和 'outputs' 的值,返回值应为输出组件的值列表。
concurrency_limit: int | None | Literal['default']
concurrency_limit: int | None | Literal['default']= "default"如果设置,这是可以同时运行的此事件的最大数量。可以设置为 None,表示没有并发限制(此事件可以同时运行任意数量)。设置为 "default" 可使用 `Blocks.queue()` 中的 `default_concurrency_limit` 参数定义的默认并发限制(该参数本身默认为 1)。
concurrency_id: str | None
concurrency_id: str | None= None如果设置,这是并发组的 ID。具有相同 `concurrency_id` 的事件将受到最低设置的 `concurrency_limit` 的限制。
api_visibility: Literal['public', 'private', 'undocumented']
api_visibility: Literal['public', 'private', 'undocumented']= "public"控制此端点的可见性和可访问性。可以是 "public"(在 API 文档中显示并可由客户端调用)、"private"(在 API 文档中隐藏,不可由客户端调用)或 "undocumented"(在 API 文档中隐藏,但可由客户端通过 gr.load 调用)。如果 fn 为 None,则 api_visibility 将自动设置为 "private"。