Spaces:
Runtime error
Runtime error
import requests | |
import gradio as gr | |
import time | |
import re | |
import uuid | |
handshake_url = "http://120.46.146.21:7860/chatbot/" | |
dialog_url = "http://120.46.146.21:7860/chatbot/dialoginfo" | |
finish_query_url = "http://120.46.146.21:7860/chatbot/finishquery" | |
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(1) | |
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() |