1. 其他教程
  2. Gradio 6 迁移指南

Gradio 6 迁移指南

我们很高兴发布 Gradio 6,这是 Gradio 库的最新主要版本。Gradio 6 在性能上显著提升,更轻量级,并且比以前的版本更容易定制。Gradio 团队只计划维护 Gradio 6 的未来版本,因此我们鼓励所有开发者迁移到 Gradio 6.x。

Gradio 6 包含几个旨在标准化 Python API 的重大更改。本迁移指南列出了这些重大更改以及为实现迁移所需的具体代码更改。了解是否需要进行更改的最简单方法是将 Gradio 应用升级到 5.50(pip install --upgrade gradio==5.50)。Gradio 5.50 会针对 Gradio 6 中移除的任何参数发出弃用警告,让您了解您的 Gradio 应用是否兼容 Gradio 6。

在这里,我们将逐步介绍 Gradio 6 中引入的重大更改。提供了代码片段,使您可以轻松地将代码迁移到 Gradio 6。如果您正在使用 LLM 帮助迁移代码,也可以将本文档复制粘贴为 Markdown。

应用级别更改

应用级别参数已从 Blocks 移至 launch()

gr.Blocks 类构造函数以前包含几个适用于整个 Gradio 应用的参数,特别是:

  • theme:Gradio 应用的主题
  • css:自定义 CSS 代码字符串
  • css_paths:自定义 CSS 文件路径
  • js:自定义 JavaScript 代码
  • head:要插入页面头部的自定义 HTML 代码
  • head_paths:要插入页面头部的自定义 HTML 文件路径

由于 gr.Blocks 可以嵌套,并且不一定对每个 Gradio 应用都是唯一的,因此这些参数现在已移至 Blocks.launch(),它只能为整个 Gradio 应用调用一次。

之前 (Gradio 5.x)

import gradio as gr

with gr.Blocks(
    theme=gr.themes.Soft(),
    css=".my-class { color: red; }",
) as demo:
    gr.Textbox(label="Input")

demo.launch()

之后 (Gradio 6.x)

import gradio as gr

with gr.Blocks() as demo:
    gr.Textbox(label="Input")

demo.launch(
    theme=gr.themes.Soft(),
    css=".my-class { color: red; }",
)

此更改使这些参数更清晰地适用于整个应用,而不是单个 Blocks 实例。

launch() 中的 show_api 参数已被一个更灵活的 footer_links 参数取代,后者允许您控制 Gradio 应用页脚中显示的链接。

Gradio 5.x 中

  • show_api=True(默认值)在页脚显示 API 文档链接
  • show_api=False 隐藏 API 文档链接

Gradio 6.x 中

  • footer_links 接受一个字符串列表:["api", "gradio", "settings"]
  • 您现在可以精确控制显示哪些页脚链接
    • "api":显示 API 文档链接
    • "gradio":显示“Built with Gradio”链接
    • "settings":显示设置链接

之前 (Gradio 5.x)

import gradio as gr

with gr.Blocks() as demo:
    gr.Textbox(label="Input")

demo.launch(show_api=False)

之后 (Gradio 6.x)

import gradio as gr

with gr.Blocks() as demo:
    gr.Textbox(label="Input")

demo.launch(footer_links=["gradio", "settings"])

要重现旧行为

  • show_api=Truefooter_links=["api", "gradio", "settings"](或省略该参数,因为这是默认值)
  • show_api=Falsefooter_links=["gradio", "settings"]

事件监听器参数:show_api 已移除,api_name=False 不再受支持

在事件监听器中(如 .click().change() 等),show_api 参数已被移除,api_name 不再接受 False 作为有效值。这些参数已被一个新的 api_visibility 参数取代,提供了更精细的控制。

Gradio 5.x 中

  • show_api=True(默认值)在 API 文档中显示端点
  • show_api=False 从 API 文档中隐藏端点,但仍允许下游应用使用它
  • api_name=False 完全禁用 API 端点(下游应用无法使用它)

Gradio 6.x 中

  • api_visibility 接受以下三个字符串值之一
    • "public":端点在 API 文档中显示并可供所有人访问(相当于旧的 show_api=True
    • "undocumented":端点在 API 文档中隐藏但仍可供下游应用访问(相当于旧的 show_api=False
    • "private":端点被完全禁用且无法访问(相当于旧的 api_name=False

之前 (Gradio 5.x)

import gradio as gr

with gr.Blocks() as demo:
    btn = gr.Button("Click me")
    output = gr.Textbox()
    
    btn.click(fn=lambda: "Hello", outputs=output, show_api=False)
    
demo.launch()

或完全禁用 API

btn.click(fn=lambda: "Hello", outputs=output, api_name=False)

之后 (Gradio 6.x)

import gradio as gr

with gr.Blocks() as demo:
    btn = gr.Button("Click me")
    output = gr.Textbox()
    
    btn.click(fn=lambda: "Hello", outputs=output, api_visibility="undocumented")
    
demo.launch()

或完全禁用 API

btn.click(fn=lambda: "Hello", outputs=output, api_visibility="private")

要重现旧行为

  • show_api=Trueapi_visibility="public"(或省略该参数,因为这是默认值)
  • show_api=Falseapi_visibility="undocumented"
  • api_name=Falseapi_visibility="private"

like_user_message.like() 事件移至构造函数

like_user_message 参数已从 .like() 事件监听器移至 Chatbot 构造函数。

之前 (Gradio 5.x)

chatbot = gr.Chatbot()
chatbot.like(print_like_dislike, None, None, like_user_message=True)

之后 (Gradio 6.x)

chatbot = gr.Chatbot(like_user_message=True)
chatbot.like(print_like_dislike, None, None)

InterfaceChatInterface 的默认 API 名称现在使用函数名称

gr.Interfacegr.ChatInterface 的默认 API 端点名称已更改,以与 gr.Blocks 事件的工作方式保持一致,并更好地支持 MCP(模型上下文协议)工具。

Gradio 5.x 中

  • gr.Interface 的默认 API 名称为 /predict
  • gr.ChatInterface 的默认 API 名称为 /chat

Gradio 6.x 中

  • 现在 gr.Interfacegr.ChatInterface 都使用您传入的函数名称作为默认 API 端点名称
  • 这使得 API 更具描述性,并与 gr.Blocks 行为保持一致

例如,如果您的 Gradio 应用是

import gradio as gr

def generate_text(prompt):
    return f"Generated: {prompt}"

demo = gr.Interface(fn=generate_text, inputs="text", outputs="text")
demo.launch()

以前,Gradio 生成的 API 端点将是:/predict。现在,API 端点将是:/generate_text

保留旧的端点名称

如果您需要保留旧的端点名称以实现向后兼容性(例如,如果您有外部服务调用这些端点),您可以显式设置 api_name 参数

demo = gr.Interface(fn=generate_text, inputs="text", outputs="text", api_name="predict")

ChatInterface 同理

demo = gr.ChatInterface(fn=chat_function, api_name="chat")

gr.Chatbotgr.ChatInterface 元组格式已移除

Gradio 6.0 中已移除聊天机器人消息的元组格式。您现在必须使用包含 "role" 和 "content" 键的字典消息格式。

Gradio 5.x 中

  • 您可以使用 type="tuples" 或默认的元组格式:[["user message", "assistant message"], ...]
  • 元组格式是一个列表,其中每个内部列表包含两个元素:[user_message, assistant_message]

Gradio 6.x 中

  • 仅支持消息格式:type="messages"
  • 消息必须是包含 "role" 和 "content" 键的字典:[{"role": "user", "content": "Hello"}, {"role": "assistant", "content": "Hi there!"}]

之前 (Gradio 5.x)

import gradio as gr

# Using tuple format
chatbot = gr.Chatbot(value=[["Hello", "Hi there!"]])

或使用 type="tuples"

chatbot = gr.Chatbot(value=[["Hello", "Hi there!"]], type="tuples")

之后 (Gradio 6.x)

import gradio as gr

# Must use messages format
chatbot = gr.Chatbot(
    value=[
        {"role": "user", "content": "Hello"},
        {"role": "assistant", "content": "Hi there!"}
    ],
    type="messages"
)

gr.ChatInterface 同理,如果您手动设置聊天记录

# Before (Gradio 5.x)
demo = gr.ChatInterface(
    fn=chat_function,
    examples=[["Hello", "Hi there!"]]
)

# After (Gradio 6.x)
demo = gr.ChatInterface(
    fn=chat_function,
    examples=[{"role": "user", "content": "Hello"}, {"role": "assistant", "content": "Hi there!"}]
)

注意:如果您使用 gr.ChatInterface 且函数返回消息,则函数应返回新格式的消息。元组格式不再受支持。

gr.ChatInterface history 格式现在使用结构化内容

gr.ChatInterface 中的 history 格式已更新为始终使用 OpenAI 风格的结构化内容格式。即使对于简单的文本消息,内容现在也始终是内容块列表。

Gradio 5.x 中

  • 内容可以是简单的字符串:{"role": "user", "content": "Hello"}
  • 简单的文本消息直接使用字符串

Gradio 6.x 中

  • 内容始终是内容块列表:{"role": "user", "content": [{"type": "text", "text": "Hello"}]}
  • 此格式与 OpenAI 的消息格式一致,并支持多模态内容(文本、图像等)

之前 (Gradio 5.x)

history = [
    {"role": "user", "content": "What is the capital of France?"},
    {"role": "assistant", "content": "Paris"}
]

之后 (Gradio 6.x)

history = [
    {"role": "user", "content": [{"type": "text", "text": "What is the capital of France?"}]},
    {"role": "assistant", "content": [{"type": "text", "text": "Paris"}]}
]

包含文件时

当文件上传到聊天中时,它们表示为 "type": "file" 的内容块。所有内容块(文件和文本)都分组在同一条消息的内容数组中

history = [
    {
        "role": "user",
        "content": [
            {"type": "file", "file": {"path": "cat1.png"}},
            {"type": "file", "file": {"path": "cat2.png"}},
            {"type": "text", "text": "What's the difference between these two images?"}
        ]
    }
]

这种结构化格式允许聊天消息中包含多模态内容(文本、图像、文件等),使其与 OpenAI 的 API 格式保持一致。在一条消息中上传的所有文件都与任何文本内容一起分组在 content 数组中。

cache_examples 参数已更新并引入 cache_mode

cache_examples 参数(用于 InterfaceChatInterfaceExamples)不再接受字符串值 "lazy"。它现在只接受布尔值(TrueFalse)。为了控制缓存策略,引入了一个新的 cache_mode 参数。

Gradio 5.x 中

  • cache_examples 接受 TrueFalse"lazy"

Gradio 6.x 中

  • cache_examples 只接受 TrueFalse
  • cache_mode 接受 "eager"(默认值)或 "lazy"

之前 (Gradio 5.x)

import gradio as gr

demo = gr.Interface(
    fn=predict, 
    inputs="text", 
    outputs="text", 
    examples=["Hello", "World"],
    cache_examples="lazy"
)

之后 (Gradio 6.x)

您现在必须设置 cache_examples=True 并单独指定模式

import gradio as gr

demo = gr.Interface(
    fn=predict, 
    inputs="text", 
    outputs="text", 
    examples=["Hello", "World"],
    cache_examples=True,
    cache_mode="lazy"
)

如果您以前使用 cache_examples=True(隐含急切缓存),则无需更改,因为 cache_mode 默认为 "eager"

组件级别更改

gr.Video 不再接受视频和字幕的元组值

返回带字幕视频的元组格式已被弃用。现在您应该直接使用 gr.Video 组件并传入 subtitles 参数,而不是返回元组 (video_path, subtitle_path)

Gradio 5.x 中

  • 您可以从函数返回一个元组 (video_path, subtitle_path)
  • 元组格式为 (str | Path, str | Path | None)

Gradio 6.x 中

  • 返回一个带有 subtitles 参数的 gr.Video 组件实例
  • 这提供了更大的灵活性并与其他组件保持一致

之前 (Gradio 5.x)

import gradio as gr

def generate_video_with_subtitles(input):
    video_path = "output.mp4"
    subtitle_path = "subtitles.srt"
    return (video_path, subtitle_path)

demo = gr.Interface(
    fn=generate_video_with_subtitles,
    inputs="text",
    outputs=gr.Video()
)
demo.launch()

之后 (Gradio 6.x)

import gradio as gr

def generate_video_with_subtitles(input):
    video_path = "output.mp4"
    subtitle_path = "subtitles.srt"
    return gr.Video(value=video_path, subtitles=subtitle_path)

demo = gr.Interface(
    fn=generate_video_with_subtitles,
    inputs="text",
    outputs=gr.Video()
)
demo.launch()

gr.HTML padding 参数默认值更改为 False

gr.HTMLpadding 参数的默认值已从 True 更改为 False,以便与 gr.Markdown 保持一致。

Gradio 5.x 中

  • padding=Truegr.HTML 的默认值
  • HTML 组件默认有内边距

Gradio 6.x 中

  • padding=Falsegr.HTML 的默认值
  • 这与 gr.Markdown 的默认行为保持一致

要保持旧行为

如果您想保留 Gradio 5.x 中存在的内边距,请显式设置 padding=True

html = gr.HTML("<div>Content</div>", padding=True)

gr.Dataframe row_countcol_count 参数重构

gr.Dataframe 中的 row_countcol_count 参数已重构,以提供更大的灵活性和清晰度。用于指定固定/动态行为的元组格式已被替换为用于初始计数和限制的单独参数。

Gradio 5.x 中

  • row_count: int | tuple[int, str] - 可以是 int 或元组,例如 (5, "fixed")(5, "dynamic")
  • col_count: int | tuple[int, str] | None - 可以是 int 或元组,例如 (3, "fixed")(3, "dynamic")

Gradio 6.x 中

  • row_count: int | None - 仅初始行数
  • row_limits: tuple[int | None, int | None] | None - 指定 (min_rows, max_rows) 约束的元组
  • column_count: int | None - 初始列数
  • column_limits: tuple[int | None, int | None] | None - 指定 (min_columns, max_columns) 约束的元组

之前 (Gradio 5.x)

import gradio as gr

# Fixed number of rows (users can't add/remove rows)
df = gr.Dataframe(row_count=(5, "fixed"), col_count=(3, "dynamic"))

或使用动态行

# Dynamic rows (users can add/remove rows)
df = gr.Dataframe(row_count=(5, "dynamic"), col_count=(3, "fixed"))

或仅使用整数(默认为动态)

df = gr.Dataframe(row_count=5, col_count=3)

之后 (Gradio 6.x)

import gradio as gr

# Fixed number of rows (users can't add/remove rows)
df = gr.Dataframe(row_count=5, row_limits=(5, 5), column_count=3, column_limits=None)

或使用动态行(用户可以添加/删除行)

# Dynamic rows with no limits
df = gr.Dataframe(row_count=5, row_limits=None, column_count=3, column_limits=None)

或使用最小/最大约束

# Rows between 3 and 10, columns between 2 and 5
df = gr.Dataframe(row_count=5, row_limits=(3, 10), column_count=3, column_limits=(2, 5))

迁移示例

  • row_count=(5, "fixed")row_count=5, row_limits=(5, 5)
  • row_count=(5, "dynamic")row_count=5, row_limits=None
  • row_count=5row_count=5, row_limits=None(行为相同)
  • col_count=(3, "fixed")column_count=3, column_limits=(3, 3)
  • col_count=(3, "dynamic")column_count=3, column_limits=None
  • col_count=3column_count=3, column_limits=None(行为相同)

gr.Chatbot 默认启用 allow_tags=True

由于 LLM 返回 HTML、Markdown 标签和自定义标签(例如 <thinking> 标签)的情况越来越多,Gradio 6 中 gr.Chatbotallow_tags 的默认值已从 False 更改为 True

Gradio 5.x 中

  • allow_tags=False 是默认值
  • 所有 HTML 和自定义标签都从聊天机器人消息中清除/移除(除非显式允许)

Gradio 6.x 中

  • allow_tags=True 是默认值
  • 所有自定义标签(非标准 HTML 标签)都保留在聊天机器人消息中
  • 除非 sanitize_html=False,否则标准 HTML 标签仍会出于安全考虑被清除

之前 (Gradio 5.x)

import gradio as gr

chatbot = gr.Chatbot()

这将从消息中删除所有标签,包括 <thinking> 等自定义标签。

之后 (Gradio 6.x)

import gradio as gr

chatbot = gr.Chatbot()

现在将保留消息中的自定义标签,例如 <thinking>

要保持旧行为

如果您想继续删除聊天机器人消息中的所有标签(旧的默认行为),请显式设置 allow_tags=False

import gradio as gr

chatbot = gr.Chatbot(allow_tags=False)

注意:您还可以指定要允许的特定标签列表

chatbot = gr.Chatbot(allow_tags=["thinking", "tool_call"])

这将只保留 <thinking><tool_call> 标签,同时删除所有其他自定义标签。

其他已移除的组件参数

Gradio 6.0 中已移除几个组件参数。这些参数以前已被弃用,现在已完全移除。

gr.Chatbot 移除的参数

bubble_full_width - 此参数已移除,因为它不再起作用。

resizeable - 此参数(带有拼写错误)已移除。请改用 resizable

之前 (Gradio 5.x)

chatbot = gr.Chatbot(resizeable=True)

之后 (Gradio 6.x)

chatbot = gr.Chatbot(resizable=True)

show_copy_button, show_copy_all_button, show_share_button - 这些参数已移除。请改用 buttons 参数。

之前 (Gradio 5.x)

chatbot = gr.Chatbot(show_copy_button=True, show_copy_all_button=True, show_share_button=True)

之后 (Gradio 6.x)

chatbot = gr.Chatbot(buttons=["copy", "copy_all", "share"])

gr.Audio / WaveformOptions 移除的参数

show_controls - WaveformOptions 中的此参数已移除。请改用 show_recording_waveform

之前 (Gradio 5.x)

audio = gr.Audio(
    waveform_options=gr.WaveformOptions(show_controls=False)
)

之后 (Gradio 6.x)

audio = gr.Audio(
    waveform_options=gr.WaveformOptions(show_recording_waveform=False)
)

min_length and max_length - 这些参数已移除。请改用事件监听器上的验证器。

之前 (Gradio 5.x)

audio = gr.Audio(min_length=1, max_length=10)

之后 (Gradio 6.x)

audio = gr.Audio()

audio.upload(
    fn=process_audio,
    validator=lambda audio: gr.validators.is_audio_correct_length(audio, min_length=1, max_length=10),
    inputs=audio
)

show_download_button, show_share_button - 这些参数已移除。请改用 buttons 参数。

之前 (Gradio 5.x)

audio = gr.Audio(show_download_button=True, show_share_button=True)

之后 (Gradio 6.x)

audio = gr.Audio(buttons=["download", "share"])

注意:对于 show_share_button 默认值为 None(将在 Spaces 上显示按钮)的组件,您可以使用 buttons=["share"] 始终显示它,或者从列表中省略它以隐藏它。

gr.Image 移除的参数

mirror_webcam - 此参数已移除。请改用带有 gr.WebcamOptionswebcam_options

之前 (Gradio 5.x)

image = gr.Image(mirror_webcam=True)

之后 (Gradio 6.x)

image = gr.Image(webcam_options=gr.WebcamOptions(mirror=True))

webcam_constraints - 此参数已移除。请改用带有 gr.WebcamOptionswebcam_options

之前 (Gradio 5.x)

image = gr.Image(webcam_constraints={"facingMode": "user"})

之后 (Gradio 6.x)

image = gr.Image(webcam_options=gr.WebcamOptions(constraints={"facingMode": "user"}))

show_download_button, show_share_button, show_fullscreen_button - 这些参数已移除。请改用 buttons 参数。

之前 (Gradio 5.x)

image = gr.Image(show_download_button=True, show_share_button=True, show_fullscreen_button=True)

之后 (Gradio 6.x)

image = gr.Image(buttons=["download", "share", "fullscreen"])

gr.Video 移除的参数

mirror_webcam - 此参数已移除。请改用带有 gr.WebcamOptionswebcam_options

之前 (Gradio 5.x)

video = gr.Video(mirror_webcam=True)

之后 (Gradio 6.x)

video = gr.Video(webcam_options=gr.WebcamOptions(mirror=True))

webcam_constraints - 此参数已移除。请改用带有 gr.WebcamOptionswebcam_options

之前 (Gradio 5.x)

video = gr.Video(webcam_constraints={"facingMode": "user"})

之后 (Gradio 6.x)

video = gr.Video(webcam_options=gr.WebcamOptions(constraints={"facingMode": "user"}))

min_length and max_length - 这些参数已移除。请改用事件监听器上的验证器。

之前 (Gradio 5.x)

video = gr.Video(min_length=1, max_length=10)

之后 (Gradio 6.x)

video = gr.Video()

video.upload(
    fn=process_video,
    validator=lambda video: gr.validators.is_video_correct_length(video, min_length=1, max_length=10),
    inputs=video
)

show_download_button, show_share_button - 这些参数已移除。请改用 buttons 参数。

之前 (Gradio 5.x)

video = gr.Video(show_download_button=True, show_share_button=True)

之后 (Gradio 6.x)

video = gr.Video(buttons=["download", "share"])

gr.ImageEditor 移除的参数

crop_size - 此参数已移除。请改用 canvas_size

之前 (Gradio 5.x)

editor = gr.ImageEditor(crop_size=(512, 512))

之后 (Gradio 6.x)

editor = gr.ImageEditor(canvas_size=(512, 512))

移除的组件

gr.LogoutButton - 此组件已移除。请改用 gr.LoginButton,它处理登录和注销过程。

之前 (Gradio 5.x)

logout_btn = gr.LogoutButton()

之后 (Gradio 6.x)

login_btn = gr.LoginButton()

原生绘图组件移除的参数

以下参数已从 gr.LinePlotgr.BarPlotgr.ScatterPlot 中移除

  • overlay_point - 此参数已移除。
  • width - 此参数已移除。请改用 CSS 样式或容器宽度。
  • stroke_dash - 此参数已移除。
  • interactive - 此参数已移除。
  • show_actions_button - 此参数已移除。
  • color_legend_title - 此参数已移除。请改用 color_title
  • show_fullscreen_button, show_export_button - 这些参数已移除。请改用 buttons 参数。

之前 (Gradio 5.x)

plot = gr.LinePlot(
    value=data,
    x="date",
    y="downloads",
    overlay_point=True,
    width=900,
    show_fullscreen_button=True,
    show_export_button=True
)

之后 (Gradio 6.x)

plot = gr.LinePlot(
    value=data,
    x="date",
    y="downloads",
    buttons=["fullscreen", "export"]
)

注意:对于 color_legend_title,请改用 color_title

之前 (Gradio 5.x)

plot = gr.ScatterPlot(color_legend_title="Category")

之后 (Gradio 6.x)

plot = gr.ScatterPlot(color_title="Category")

gr.Textbox 移除的参数

show_copy_button - 此参数已移除。请改用 buttons 参数。

之前 (Gradio 5.x)

text = gr.Textbox(show_copy_button=True)

之后 (Gradio 6.x)

text = gr.Textbox(buttons=["copy"])

gr.Markdown 移除的参数

show_copy_button - 此参数已移除。请改用 buttons 参数。

之前 (Gradio 5.x)

markdown = gr.Markdown(show_copy_button=True)

之后 (Gradio 6.x)

markdown = gr.Markdown(buttons=["copy"])

gr.Dataframe 移除的参数

show_copy_button, show_fullscreen_button - 这些参数已移除。请改用 buttons 参数。

之前 (Gradio 5.x)

df = gr.Dataframe(show_copy_button=True, show_fullscreen_button=True)

之后 (Gradio 6.x)

df = gr.Dataframe(buttons=["copy", "fullscreen"])

gr.Slider 移除的参数

show_reset_button - 此参数已移除。请改用 buttons 参数。

之前 (Gradio 5.x)

slider = gr.Slider(show_reset_button=True)

之后 (Gradio 6.x)

slider = gr.Slider(buttons=["reset"])

CLI 更改

gradio sketch 命令已移除

gradio sketch 命令行工具已弃用,并已在 Gradio 6 中完全移除。此工具用于通过可视化界面创建 Gradio 应用。

Gradio 5.x 中

  • 您可以运行 gradio sketch 来启动用于构建 Gradio 应用的交互式 GUI
  • 该工具会以可视方式生成 Python 代码

Gradio 6.x 中

  • gradio sketch 命令已移除
  • 运行 gradio sketch 将会引发 DeprecationWarning

Python 客户端更改

Clienthf_token 参数重命名为 token

Client 类中的 hf_token 参数已重命名为 token,以保持一致性和简洁性。

之前 (Gradio 5.x)

from gradio_client import Client

client = Client("abidlabs/my-private-space", hf_token="hf_...")

之后 (Gradio 6.x)

from gradio_client import Client

client = Client("abidlabs/my-private-space", token="hf_...")

deploy_discord 方法已弃用

Client 类中的 deploy_discord 方法已弃用,并将在 Gradio 6.0 中移除。此方法用于将 Gradio 应用部署为 Discord 机器人。

之前 (Gradio 5.x)

from gradio_client import Client

client = Client("username/space-name")
client.deploy_discord(discord_bot_token="...")

之后 (Gradio 6.x)

deploy_discord 方法不再可用。请参阅有关使用 Gradio 创建 Discord 机器人的文档以获取替代方法。

AppError 现在继承自 Exception 而不是 ValueError

Python 客户端中的 AppError 异常类现在直接继承自 Exception,而不是 ValueError。如果您有专门捕获 ValueError 来处理 AppError 实例的代码,这是一个重大更改。

之前 (Gradio 5.x)

from gradio_client import Client
from gradio_client.exceptions import AppError

try:
    client = Client("username/space-name")
    result = client.predict("/predict", inputs)
except ValueError as e:
    # This would catch AppError in Gradio 5.x
    print(f"Error: {e}")

之后 (Gradio 6.x)

from gradio_client import Client
from gradio_client.exceptions import AppError

try:
    client = Client("username/space-name")
    result = client.predict("/predict", inputs)
except AppError as e:
    # Explicitly catch AppError
    print(f"App error: {e}")
except ValueError as e:
    # This will no longer catch AppError
    print(f"Value error: {e}")
gradio