Spaces:
Sleeping
Sleeping
mkw18
commited on
Commit
·
79ecf54
1
Parent(s):
30712c0
test
Browse files
app.py
ADDED
@@ -0,0 +1,98 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import mdtex2html
|
3 |
+
import os
|
4 |
+
import json
|
5 |
+
import requests
|
6 |
+
|
7 |
+
def postprocess(self, y):
|
8 |
+
if y is None:
|
9 |
+
return []
|
10 |
+
for i, (message, response) in enumerate(y):
|
11 |
+
y[i] = (
|
12 |
+
None if message is None else mdtex2html.convert((message)),
|
13 |
+
None if response is None else mdtex2html.convert(response),
|
14 |
+
)
|
15 |
+
return y
|
16 |
+
|
17 |
+
|
18 |
+
gr.Chatbot.postprocess = postprocess
|
19 |
+
|
20 |
+
|
21 |
+
def parse_text(text):
|
22 |
+
"""copy from https://github.com/GaiZhenbiao/ChuanhuChatGPT/"""
|
23 |
+
lines = text.split("\n")
|
24 |
+
lines = [line for line in lines if line != ""]
|
25 |
+
count = 0
|
26 |
+
for i, line in enumerate(lines):
|
27 |
+
if "```" in line:
|
28 |
+
count += 1
|
29 |
+
items = line.split('`')
|
30 |
+
if count % 2 == 1:
|
31 |
+
lines[i] = f'<pre><code class="language-{items[-1]}">'
|
32 |
+
else:
|
33 |
+
lines[i] = f'<br></code></pre>'
|
34 |
+
else:
|
35 |
+
if i > 0:
|
36 |
+
if count % 2 == 1:
|
37 |
+
line = line.replace("`", "\`")
|
38 |
+
line = line.replace("<", "<")
|
39 |
+
line = line.replace(">", ">")
|
40 |
+
line = line.replace(" ", " ")
|
41 |
+
line = line.replace("*", "*")
|
42 |
+
line = line.replace("_", "_")
|
43 |
+
line = line.replace("-", "-")
|
44 |
+
line = line.replace(".", ".")
|
45 |
+
line = line.replace("!", "!")
|
46 |
+
line = line.replace("(", "(")
|
47 |
+
line = line.replace(")", ")")
|
48 |
+
line = line.replace("$", "$")
|
49 |
+
lines[i] = "<br>"+line
|
50 |
+
text = "".join(lines)
|
51 |
+
return text
|
52 |
+
|
53 |
+
|
54 |
+
def test(chatbot):
|
55 |
+
chatbot.append((None, parse_text('测试')))
|
56 |
+
chatbot.append((parse_text('测试'), None))
|
57 |
+
return chatbot
|
58 |
+
|
59 |
+
def CheckTrue(chatbot, key):
|
60 |
+
data = {'AgentCheck': True, 'key': key}
|
61 |
+
response=requests.post(os.environ.get("URL"), data=json.dumps(data, ensure_ascii=False).encode('utf-8'))
|
62 |
+
if not response.status_code == 200:
|
63 |
+
chatbot.append((parse_text("出现错误,请联系负责人"), None))
|
64 |
+
return chatbot
|
65 |
+
chatbot.append((parse_text("是"), parse_text(str(response.content, encoding="utf-8"))))
|
66 |
+
return chatbot
|
67 |
+
|
68 |
+
def CheckFalse(chatbot, key):
|
69 |
+
data = {'AgentCheck': False, 'key': key}
|
70 |
+
response=requests.post(os.environ.get("URL"), data=json.dumps(data, ensure_ascii=False).encode('utf-8'))
|
71 |
+
if not response.status_code == 200:
|
72 |
+
chatbot.append((parse_text("出现错误,请联系负责人"), None))
|
73 |
+
return chatbot
|
74 |
+
chatbot.append((parse_text("否"), parse_text(str(response.content, encoding="utf-8"))))
|
75 |
+
return chatbot
|
76 |
+
|
77 |
+
def CheckTerm(chatbot, key):
|
78 |
+
data = {'AgentFinish': True, 'key': key}
|
79 |
+
response=requests.post(os.environ.get("URL"), data=json.dumps(data, ensure_ascii=False).encode('utf-8'))
|
80 |
+
if not response.status_code == 200:
|
81 |
+
chatbot.append((parse_text("出现错误,请联系负责人"), None))
|
82 |
+
return chatbot
|
83 |
+
chatbot = [(None, parse_text(str(response.content, encoding="utf-8")))]
|
84 |
+
return chatbot
|
85 |
+
|
86 |
+
with gr.Blocks() as demo:
|
87 |
+
gr.HTML("""<h1 align="center">Generative Agents测试平台</h1>""")
|
88 |
+
user_key = gr.Textbox(label='Key', placeholder="Input your key...", lines=1, max_lines=1).style(
|
89 |
+
container=False)
|
90 |
+
chatbot = gr.Chatbot([])
|
91 |
+
|
92 |
+
with gr.Row():
|
93 |
+
test_btn = gr.Button('测试')
|
94 |
+
|
95 |
+
user_key.submit(CheckTerm, [chatbot, user_key], [chatbot])
|
96 |
+
test_btn.click(test, [chatbot], [chatbot])
|
97 |
+
|
98 |
+
demo.queue().launch()
|