Spaces:
Sleeping
Sleeping
update app.py
Browse files
app.py
CHANGED
@@ -10,6 +10,7 @@ os.system("python -m mindsearch.app --lang cn --model_format internlm_silicon &"
|
|
10 |
PLANNER_HISTORY = []
|
11 |
SEARCHER_HISTORY = []
|
12 |
|
|
|
13 |
def rst_mem(history_planner: list, history_searcher: list):
|
14 |
'''
|
15 |
Reset the chatbot memory.
|
@@ -20,8 +21,11 @@ def rst_mem(history_planner: list, history_searcher: list):
|
|
20 |
PLANNER_HISTORY.clear()
|
21 |
return history_planner, history_searcher
|
22 |
|
|
|
23 |
def format_response(gr_history, agent_return):
|
24 |
-
if agent_return['state'] in [
|
|
|
|
|
25 |
gr_history[-1][1] = agent_return['response']
|
26 |
elif agent_return['state'] == AgentStatusCode.PLUGIN_START:
|
27 |
thought = gr_history[-1][1].split('```')[0]
|
@@ -30,18 +34,25 @@ def format_response(gr_history, agent_return):
|
|
30 |
elif agent_return['state'] == AgentStatusCode.PLUGIN_END:
|
31 |
thought = gr_history[-1][1].split('```')[0]
|
32 |
if isinstance(agent_return['response'], dict):
|
33 |
-
gr_history[-1][
|
|
|
34 |
elif agent_return['state'] == AgentStatusCode.PLUGIN_RETURN:
|
35 |
assert agent_return['inner_steps'][-1]['role'] == 'environment'
|
36 |
item = agent_return['inner_steps'][-1]
|
37 |
-
gr_history.append([
|
|
|
|
|
|
|
38 |
gr_history.append([None, ''])
|
39 |
return
|
40 |
|
|
|
41 |
def predict(history_planner, history_searcher):
|
42 |
|
43 |
def streaming(raw_response):
|
44 |
-
for chunk in raw_response.iter_lines(chunk_size=8192,
|
|
|
|
|
45 |
if chunk:
|
46 |
decoded = chunk.decode('utf-8')
|
47 |
if decoded == '\r':
|
@@ -60,7 +71,11 @@ def predict(history_planner, history_searcher):
|
|
60 |
url = 'http://localhost:8002/solve'
|
61 |
headers = {'Content-Type': 'application/json'}
|
62 |
data = {'inputs': PLANNER_HISTORY}
|
63 |
-
raw_response = requests.post(url,
|
|
|
|
|
|
|
|
|
64 |
|
65 |
for resp in streaming(raw_response):
|
66 |
agent_return, node_name = resp
|
@@ -83,29 +98,30 @@ def predict(history_planner, history_searcher):
|
|
83 |
yield history_planner, history_searcher
|
84 |
return history_planner, history_searcher
|
85 |
|
86 |
-
|
87 |
-
|
88 |
-
gr.HTML("""<
|
|
|
89 |
gr.HTML("""
|
90 |
-
<div style="text-align: center; font-size: 16px;
|
91 |
-
<a href="https://github.com/InternLM/MindSearch" style="margin-right: 15px; text-decoration: none; color: #4A90E2;
|
92 |
-
<a href="https://arxiv.org/abs/2407.20183" style="margin-right: 15px; text-decoration: none; color: #4A90E2;
|
93 |
-
<a href="https://huggingface.co/papers/2407.20183" style="margin-right: 15px; text-decoration: none; color: #4A90E2;
|
94 |
-
<a href="https://huggingface.co/spaces/internlm/MindSearch" style="text-decoration: none; color: #4A90E2;
|
95 |
</div>
|
96 |
""")
|
97 |
with gr.Row():
|
98 |
with gr.Column(scale=10):
|
99 |
with gr.Row():
|
100 |
with gr.Column():
|
101 |
-
planner = gr.Chatbot(label='
|
102 |
height=700,
|
103 |
show_label=True,
|
104 |
show_copy_button=True,
|
105 |
bubble_full_width=False,
|
106 |
render_markdown=True)
|
107 |
with gr.Column():
|
108 |
-
searcher = gr.Chatbot(label='
|
109 |
height=700,
|
110 |
show_label=True,
|
111 |
show_copy_button=True,
|
@@ -115,13 +131,12 @@ with gr.Blocks(css=".button-primary { background-color: #4CAF50; color: white; b
|
|
115 |
user_input = gr.Textbox(show_label=False,
|
116 |
placeholder='帮我搜索一下 InternLM 开源体系',
|
117 |
lines=5,
|
118 |
-
container=False
|
119 |
-
css="border-radius: 8px; border: 1px solid #ddd; padding: 10px;")
|
120 |
with gr.Row():
|
121 |
with gr.Column(scale=2):
|
122 |
-
submitBtn = gr.Button('Submit'
|
123 |
with gr.Column(scale=1, min_width=20):
|
124 |
-
emptyBtn = gr.Button('Clear History'
|
125 |
|
126 |
def user(query, history):
|
127 |
return '', history + [[query, '']]
|
@@ -136,4 +151,4 @@ demo.queue()
|
|
136 |
demo.launch(server_name='0.0.0.0',
|
137 |
server_port=7860,
|
138 |
inbrowser=True,
|
139 |
-
share=True)
|
|
|
10 |
PLANNER_HISTORY = []
|
11 |
SEARCHER_HISTORY = []
|
12 |
|
13 |
+
|
14 |
def rst_mem(history_planner: list, history_searcher: list):
|
15 |
'''
|
16 |
Reset the chatbot memory.
|
|
|
21 |
PLANNER_HISTORY.clear()
|
22 |
return history_planner, history_searcher
|
23 |
|
24 |
+
|
25 |
def format_response(gr_history, agent_return):
|
26 |
+
if agent_return['state'] in [
|
27 |
+
AgentStatusCode.STREAM_ING, AgentStatusCode.ANSWER_ING
|
28 |
+
]:
|
29 |
gr_history[-1][1] = agent_return['response']
|
30 |
elif agent_return['state'] == AgentStatusCode.PLUGIN_START:
|
31 |
thought = gr_history[-1][1].split('```')[0]
|
|
|
34 |
elif agent_return['state'] == AgentStatusCode.PLUGIN_END:
|
35 |
thought = gr_history[-1][1].split('```')[0]
|
36 |
if isinstance(agent_return['response'], dict):
|
37 |
+
gr_history[-1][
|
38 |
+
1] = thought + '\n' + f'```json\n{json.dumps(agent_return["response"], ensure_ascii=False, indent=4)}\n```' # noqa: E501
|
39 |
elif agent_return['state'] == AgentStatusCode.PLUGIN_RETURN:
|
40 |
assert agent_return['inner_steps'][-1]['role'] == 'environment'
|
41 |
item = agent_return['inner_steps'][-1]
|
42 |
+
gr_history.append([
|
43 |
+
None,
|
44 |
+
f"```json\n{json.dumps(item['content'], ensure_ascii=False, indent=4)}\n```"
|
45 |
+
])
|
46 |
gr_history.append([None, ''])
|
47 |
return
|
48 |
|
49 |
+
|
50 |
def predict(history_planner, history_searcher):
|
51 |
|
52 |
def streaming(raw_response):
|
53 |
+
for chunk in raw_response.iter_lines(chunk_size=8192,
|
54 |
+
decode_unicode=False,
|
55 |
+
delimiter=b'\n'):
|
56 |
if chunk:
|
57 |
decoded = chunk.decode('utf-8')
|
58 |
if decoded == '\r':
|
|
|
71 |
url = 'http://localhost:8002/solve'
|
72 |
headers = {'Content-Type': 'application/json'}
|
73 |
data = {'inputs': PLANNER_HISTORY}
|
74 |
+
raw_response = requests.post(url,
|
75 |
+
headers=headers,
|
76 |
+
data=json.dumps(data),
|
77 |
+
timeout=20,
|
78 |
+
stream=True)
|
79 |
|
80 |
for resp in streaming(raw_response):
|
81 |
agent_return, node_name = resp
|
|
|
98 |
yield history_planner, history_searcher
|
99 |
return history_planner, history_searcher
|
100 |
|
101 |
+
|
102 |
+
with gr.Blocks() as demo:
|
103 |
+
gr.HTML("""<h1 align="center">MindSearch Gradio Demo</h1>""")
|
104 |
+
gr.HTML("""<p style="text-align: center; max-width: 800px; font-family: Arial, sans-serif;">MindSearch is an open-source AI Search Engine Framework with Perplexity.ai Pro performance. You can deploy your own Perplexity.ai-style search engine using either closed-source LLMs (GPT, Claude) or open-source LLMs (InternLM2.5-7b-chat).</p>""")
|
105 |
gr.HTML("""
|
106 |
+
<div style="text-align: center; font-size: 16px;">
|
107 |
+
<a href="https://github.com/InternLM/MindSearch" style="margin-right: 15px; text-decoration: none; color: #4A90E2;">🔗 GitHub</a>
|
108 |
+
<a href="https://arxiv.org/abs/2407.20183" style="margin-right: 15px; text-decoration: none; color: #4A90E2;">📄 Arxiv</a>
|
109 |
+
<a href="https://huggingface.co/papers/2407.20183" style="margin-right: 15px; text-decoration: none; color: #4A90E2;">📚 Hugging Face Papers</a>
|
110 |
+
<a href="https://huggingface.co/spaces/internlm/MindSearch" style="text-decoration: none; color: #4A90E2;">🤗 Hugging Face Demo</a>
|
111 |
</div>
|
112 |
""")
|
113 |
with gr.Row():
|
114 |
with gr.Column(scale=10):
|
115 |
with gr.Row():
|
116 |
with gr.Column():
|
117 |
+
planner = gr.Chatbot(label='planner',
|
118 |
height=700,
|
119 |
show_label=True,
|
120 |
show_copy_button=True,
|
121 |
bubble_full_width=False,
|
122 |
render_markdown=True)
|
123 |
with gr.Column():
|
124 |
+
searcher = gr.Chatbot(label='searcher',
|
125 |
height=700,
|
126 |
show_label=True,
|
127 |
show_copy_button=True,
|
|
|
131 |
user_input = gr.Textbox(show_label=False,
|
132 |
placeholder='帮我搜索一下 InternLM 开源体系',
|
133 |
lines=5,
|
134 |
+
container=False)
|
|
|
135 |
with gr.Row():
|
136 |
with gr.Column(scale=2):
|
137 |
+
submitBtn = gr.Button('Submit')
|
138 |
with gr.Column(scale=1, min_width=20):
|
139 |
+
emptyBtn = gr.Button('Clear History')
|
140 |
|
141 |
def user(query, history):
|
142 |
return '', history + [[query, '']]
|
|
|
151 |
demo.launch(server_name='0.0.0.0',
|
152 |
server_port=7860,
|
153 |
inbrowser=True,
|
154 |
+
share=True)
|