Spaces:
Runtime error
Runtime error
Commit
·
c81175d
1
Parent(s):
e9597b9
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import requests
|
2 |
+
import gradio as gr
|
3 |
+
import time
|
4 |
+
import re
|
5 |
+
import uuid
|
6 |
+
|
7 |
+
|
8 |
+
handshake_url = "http://120.46.146.21:7860/chatbot/"
|
9 |
+
dialog_url = "http://120.46.146.21:7860/chatbot/dialoginfo"
|
10 |
+
finish_query_url = "http://120.46.146.21:7860/chatbot/finishquery"
|
11 |
+
|
12 |
+
def get_internet_ip():
|
13 |
+
r = requests.get("http://txt.go.sohu.com/ip/soip")
|
14 |
+
ip = re.findall(r'\d+.\d+.\d+.\d+', r.text)
|
15 |
+
if ip is not None and len(ip) > 0:
|
16 |
+
return ip[0]
|
17 |
+
return None
|
18 |
+
|
19 |
+
|
20 |
+
def get_remote_stream(query, chatbot):
|
21 |
+
q_id = str(uuid.uuid1())
|
22 |
+
r = requests.post(handshake_url, json={"q_id": q_id, "query": query, 'history': chatbot})
|
23 |
+
if r.json()['code'] == 202:
|
24 |
+
for _ in range(1000):
|
25 |
+
r = requests.get(dialog_url, {"dialogid": q_id})
|
26 |
+
history = r.json()['history']
|
27 |
+
while len(history[-1]) == 0:
|
28 |
+
history.pop()
|
29 |
+
r_finish = requests.get(finish_query_url, {"dialogid": q_id})
|
30 |
+
if r_finish.json() == True:
|
31 |
+
yield history, ""
|
32 |
+
print('break')
|
33 |
+
break
|
34 |
+
|
35 |
+
yield history, ""
|
36 |
+
time.sleep(1)
|
37 |
+
|
38 |
+
|
39 |
+
with gr.Blocks() as demo:
|
40 |
+
chatbot = gr.Chatbot([[None, get_internet_ip()]], show_label=False, height=650)
|
41 |
+
query = gr.Textbox(show_label=False, placeholder="请输入提问内容,按回车进行提交")
|
42 |
+
|
43 |
+
query.submit(get_remote_stream,[query, chatbot], [chatbot, query])
|
44 |
+
|
45 |
+
demo.queue(concurrency_count=10)
|
46 |
+
demo.launch()
|