File size: 961 Bytes
5cfdf88
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import gradio as gr
import random
import time
# 定义一个 JavaScript 函数来隐藏按钮
hide_button_js = """
function hideButton() {
    var button = document.getElementById('my-button');
    button.style.display = 'none';
}
"""
with gr.Blocks() as demo:
    chatbot = gr.Chatbot()
    # 添加按钮到界面
    btn = gr.Button(value="Submit", elem_id="my-button")
    btn.click(
        None, _js=hide_button_js
    )
    # btn.visible = False
    msg = gr.Textbox()
    clear = gr.ClearButton([msg, chatbot])

    def respond(message, chat_history):
        bot_message = random.choice(["How are you?", "I love you", "I'm very hungry"])
        chat_history.append((message, bot_message))
        time.sleep(2)
        print(chat_history)
        btn.visible = True
        gr.update(value="", interactive=True)
        return "", chat_history,btn

    msg.submit(respond, [msg, chatbot], [msg, chatbot])

if __name__ == "__main__":
    demo.launch()