Spaces:
Runtime error
Runtime error
| import requests | |
| import gradio as gr | |
| import time | |
| import re | |
| import uuid | |
| import os | |
| handshake_url = os.environ['HANDSHAKE_URL'] | |
| dialog_url = os.environ['DIALOG_URL'] | |
| finish_query_url = os.environ['FINISH_QUERY_URL'] | |
| def get_internet_ip(): | |
| r = requests.get("http://txt.go.sohu.com/ip/soip") | |
| ip = re.findall(r'\d+.\d+.\d+.\d+', r.text) | |
| if ip is not None and len(ip) > 0: | |
| return ip[0] | |
| return None | |
| def get_remote_stream(query, chatbot): | |
| q_id = str(uuid.uuid1()) | |
| r = requests.post(handshake_url, json={"q_id": q_id, "query": query, 'history': chatbot}) | |
| if r.json()['code'] == 202: | |
| for _ in range(1000): | |
| r = requests.get(dialog_url, {"dialogid": q_id}) | |
| history = r.json()['history'] | |
| while len(history[-1]) == 0: | |
| history.pop() | |
| r_finish = requests.get(finish_query_url, {"dialogid": q_id}) | |
| if r_finish.json() == True: | |
| yield history, "" | |
| print('break') | |
| break | |
| yield history, "" | |
| time.sleep(0.5) | |
| with gr.Blocks() as demo: | |
| chatbot = gr.Chatbot([[None, get_internet_ip()]], show_label=False, height=650) | |
| query = gr.Textbox(show_label=False, placeholder="请输入提问内容,按回车进行提交") | |
| query.submit(get_remote_stream,[query, chatbot], [chatbot, query]) | |
| demo.queue(concurrency_count=10) | |
| demo.launch() |