import gradio as gr # 导入gradio包 | |
import random # 导入random包 | |
def respond(message, chat_history,key): # 定义一个respond函数,接收用户输入和历史对话 | |
bot_message = random.choice(["How are you?", "I love you", "I'm very hungry"]) # 随机选择一个回复 | |
chat_history.append((message, bot_message)) # 将用户输入和回复拼接起来,添加到历史对话中 | |
return "", chat_history # 返回空字符串和拼接后的历史对话 | |
with gr.Blocks() as demo: # 创建一个demo | |
key = gr.Textbox(label='输入你的OpenAI Key') # 创建一个输入框 | |
chatbot = gr.Chatbot(label='历史对话') # 创建一个chatbot | |
msg = gr.Textbox(label='输入消息,按回车发送') # 创建一个输入框 | |
msg.submit(respond, [msg, chatbot,key], [msg, chatbot]) # 设置输入框的回调函数 | |
demo.queue().launch() |