在渐变选项卡 GUI 中,一个按钮调用另一个选项卡

问题描述 投票:0回答:1

我有以下脚本

import gradio as gr

# Define the function for the first tab
def greet(text):
    return f"Hello {text}"

# Define the function for the second tab
def farewell(text):
    return f"Goodbye {text}"

# Create the interface for the first tab
with gr.Blocks() as greet_interface:
    input_text = gr.Textbox(label="Input Text")
    output_text = gr.Textbox(label="Output Text")
    button = gr.Button("Submit")
    button.click(greet, inputs=input_text, outputs=output_text)

# Create the interface for the second tab
with gr.Blocks() as farewell_interface:
    input_text = gr.Textbox(label="Input Text")
    output_text = gr.Textbox(label="Output Text")
    button = gr.Button("Submit")
    button.click(farewell, inputs=input_text, outputs=output_text)

# Combine the interfaces into tabs
with gr.Blocks() as demo:
    with gr.Tabs():
        with gr.TabItem("Greet"):
            greet_interface.render()
        with gr.TabItem("Farewell"):
            farewell_interface.render()

# Launch the interface
# demo.launch()
demo.launch(server_name="0.0.0.0", server_port=7861)

我很挠头,因为这在我所拥有的一种环境中有效,但在另一种环境中却严重失败。

它是如何失败的:

在第二个选项卡(告别)中按下按钮时,它实际上调用了greet函数。告别永远不会被召唤。

我可以看到第二个选项卡的output_text 中正在进行一些处理,但它从未完成。相反,第一个选项卡的输出文本被填充

我无法理解为什么会发生这种情况。

我唯一的区别是环境

  1. 工作环境:
  • Python 3.11.1
  • 使用venv
  • 渐变4.37.2
  1. 失败的环境
  • Python 3.9.16
  • 使用诗歌
  • 渐变4.32.2

有人可以帮我解决这个奇怪的现象吗?选项卡式渐变有问题吗?

顺便说一句,我已经尝试过在每个选项卡上使用完全不同的变量,但这不起作用

python gradio
1个回答
0
投票

是的,选项卡式渐变有问题。在4.36.1版本中已修复 请参阅变更日志

修复了仅触发第一个界面事件的 TabbedInterface 错误。

这解释了您在两个版本之间看到的差异(4.32.2 < 4.36.1 < 4.37.2)

© www.soinside.com 2019 - 2024. All rights reserved.