Update app.py
Browse files
app.py
CHANGED
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr # 导入gradio包
|
2 |
+
import random # 导入random包
|
3 |
+
|
4 |
+
|
5 |
+
def respond(message, chat_history): # 定义一个respond函数,接收用户输入和历史对话
|
6 |
+
bot_message = random.choice(["How are you?", "I love you", "I'm very hungry"]) # 随机选择一个回复
|
7 |
+
chat_history.append((message, bot_message)) # 将用户输入和回复拼接起来,添加到历史对话中
|
8 |
+
return "", chat_history # 返回空字符串和拼接后的历史对话
|
9 |
+
|
10 |
+
|
11 |
+
with gr.Blocks() as demo: # 创建一个demo
|
12 |
+
chatbot = gr.Chatbot(label='历史对话') # 创建一个chatbot
|
13 |
+
msg = gr.Textbox(label='输入消息,按回车发送') # 创建一个输入框
|
14 |
+
|
15 |
+
msg.submit(respond, [msg, chatbot], [msg, chatbot]) # 设置输入框的回调函数
|
16 |
+
|
17 |
+
|
18 |
+
# demo.launch() # 启动demo
|