Spaces:
Runtime error
Runtime error
import gradio as gr | |
import os | |
import requests | |
import json | |
headers = {'content-type': 'application/json'} | |
def ask_question(message, chat_history): | |
chat_history = chat_history + [[message, ""]] | |
payload = {'history': chat_history} | |
url = os.environ.get('CHAT_URL') | |
try: | |
response = requests.post(url, headers=headers, data=json.dumps(payload)) | |
chat_history[-1][1] = response.json()['ai_response'] | |
except: | |
chat_history[-1][1] = "很抱歉,我暂时没法回答这个问题" | |
return chat_history | |
with gr.Blocks() as demo: | |
chatbot = gr.Chatbot(show_label = False, show_share_button = False) | |
msg = gr.Textbox(placeholder="请在这里输入谈话内容,然后按回车键或下面的发送键", show_label = False) | |
def respond(message, chat_history): | |
chat_history = ask_question(message, chat_history) | |
return "", chat_history | |
msg.submit(respond, [msg, chatbot], [msg, chatbot]) | |
with gr.Row(): | |
send = gr.Button(value="发送", show_share_button = False) | |
send.click(respond, [msg, chatbot], [msg, chatbot]) | |
clear = gr.ClearButton([msg, chatbot], value="清除聊天内容") | |
demo.launch() |