jilangdi commited on
Commit
6b6d5cd
·
verified ·
1 Parent(s): fd6787f

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +61 -44
app.py CHANGED
@@ -1,5 +1,6 @@
1
-
2
  import json
 
 
3
  import gradio as gr
4
  import requests
5
  from lagent.schema import AgentStatusCode
@@ -10,18 +11,21 @@ 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
- '''Reset the chatbot memory.'''
 
 
15
  history_planner = []
16
  history_searcher = []
17
  if PLANNER_HISTORY:
18
  PLANNER_HISTORY.clear()
19
  return history_planner, history_searcher
20
 
 
21
  def format_response(gr_history, agent_return):
22
  if agent_return['state'] in [
23
- AgentStatusCode.STREAM_ING,
24
- AgentStatusCode.ANSWER_ING
25
  ]:
26
  gr_history[-1][1] = agent_return['response']
27
  elif agent_return['state'] == AgentStatusCode.PLUGIN_START:
@@ -31,7 +35,8 @@ def format_response(gr_history, agent_return):
31
  elif agent_return['state'] == AgentStatusCode.PLUGIN_END:
32
  thought = gr_history[-1][1].split('```')[0]
33
  if isinstance(agent_return['response'], dict):
34
- gr_history[-1][1] = thought + '\n' + f'```json\n{json.dumps(agent_return["response"], ensure_ascii=False, indent=4)}\n```' # noqa: E501
 
35
  elif agent_return['state'] == AgentStatusCode.PLUGIN_RETURN:
36
  assert agent_return['inner_steps'][-1]['role'] == 'environment'
37
  item = agent_return['inner_steps'][-1]
@@ -42,11 +47,13 @@ def format_response(gr_history, agent_return):
42
  gr_history.append([None, ''])
43
  return
44
 
 
45
  def predict(history_planner, history_searcher):
 
46
  def streaming(raw_response):
47
  for chunk in raw_response.iter_lines(chunk_size=8192,
48
- decode_unicode=False,
49
- delimiter=b'\n'):
50
  if chunk:
51
  decoded = chunk.decode('utf-8')
52
  if decoded == '\r':
@@ -92,31 +99,39 @@ def predict(history_planner, history_searcher):
92
  yield history_planner, history_searcher
93
  return history_planner, history_searcher
94
 
 
95
  examples = [
96
- ["Find legal precedents in contract law."],
97
- ["What are the top 10 e-commerce websites?"],
98
- ["Generate a report on global climate change."],
99
  ]
100
 
101
- import os
102
- css_path = os.path.join(os.path.dirname(__file__), "css", "gradio_front.css")
103
-
104
  with gr.Blocks(css=css_path) as demo:
105
- with gr.Column(elem_classes="chat-box"):
106
  gr.HTML("""<h1 align="center">MindSearch Gradio Demo</h1>""")
107
- gr.HTML("""<p style="text-align: center; font-family: Arial, sans-serif;">
108
- 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)
109
- or open-source LLMs (InternLM2.5-7b-chat).</p> """)
 
 
 
110
  gr.HTML("""
111
  <div style="text-align: center; font-size: 16px;">
112
- <a href="https://github.com/InternLM/MindSearch" style="margin-right: 15px; text-decoration: none; color: #4A90E2;" target="_blank">🔗 GitHub</a>
113
- <a href="https://arxiv.org/abs/2407.20183" style="margin-right: 15px; text-decoration: none; color: #4A90E2;" target="_blank">📄 Arxiv</a>
114
- <a href="https://huggingface.co/papers/2407.20183" style="margin-right: 15px; text-decoration: none; color: #4A90E2;" target="_blank">📚 Hugging Face Papers</a>
115
- <a href="https://huggingface.co/spaces/internlm/MindSearch" style="text-decoration: none; color: #4A90E2;" target="_blank">🤗 Hugging Face Demo</a>
 
 
 
 
116
  </div>""")
117
- gr.HTML("""
118
- <h1 align='right'><img src='https://raw.githubusercontent.com/InternLM/MindSearch/98fd84d566fe9e3adc5028727f72f2944098fd05/assets/logo.svg' alt='MindSearch Logo1' class="logo"></h1>
119
- """)
 
 
120
 
121
  with gr.Row():
122
  with gr.Column(scale=10):
@@ -127,31 +142,33 @@ with gr.Blocks(css=css_path) as demo:
127
  show_copy_button=True,
128
  bubble_full_width=False,
129
  render_markdown=True,
130
- elem_classes="chatbot-container")
131
  with gr.Column():
132
  searcher = gr.Chatbot(label='searcher',
133
  show_label=True,
134
  show_copy_button=True,
135
  bubble_full_width=False,
136
  render_markdown=True,
137
- elem_classes="chatbot-container")
138
 
139
- with gr.Row(elem_classes="chat-box"):
140
  # Text input area
141
- user_input = gr.Textbox(
142
- show_label=False,
143
- placeholder="Type your message...",
144
- lines=1,
145
- container=False,
146
- elem_classes="editor"
147
- )
148
  # Buttons (now in the same Row)
149
- submitBtn = gr.Button("submit", variant="primary", elem_classes="toolbarButton")
150
- clearBtn = gr.Button("clear", variant="secondary", elem_classes="toolbarButton")
151
-
152
- with gr.Row(elem_classes="examples-container"):
153
- examples_component = gr.Examples(examples, inputs=user_input,
154
- label="Try these examples:")
 
 
 
 
155
 
156
  def user(query, history):
157
  return '', history + [[query, '']]
@@ -160,13 +177,13 @@ with gr.Blocks(css=css_path) as demo:
160
  return user(example[0], planner.value)
161
 
162
  submitBtn.click(user, [user_input, planner], [user_input, planner],
163
- queue=False).then(predict, [planner, searcher],
164
- [planner, searcher])
165
  clearBtn.click(rst_mem, [planner, searcher], [planner, searcher],
166
- queue=False)
167
 
168
  demo.queue()
169
  demo.launch(server_name='0.0.0.0',
170
  server_port=7860,
171
  inbrowser=True,
172
- share=True)
 
 
1
  import json
2
+ import os
3
+
4
  import gradio as gr
5
  import requests
6
  from lagent.schema import AgentStatusCode
 
11
  PLANNER_HISTORY = []
12
  SEARCHER_HISTORY = []
13
 
14
+
15
  def rst_mem(history_planner: list, history_searcher: list):
16
+ '''
17
+ Reset the chatbot memory.
18
+ '''
19
  history_planner = []
20
  history_searcher = []
21
  if PLANNER_HISTORY:
22
  PLANNER_HISTORY.clear()
23
  return history_planner, history_searcher
24
 
25
+
26
  def format_response(gr_history, agent_return):
27
  if agent_return['state'] in [
28
+ AgentStatusCode.STREAM_ING, AgentStatusCode.ANSWER_ING
 
29
  ]:
30
  gr_history[-1][1] = agent_return['response']
31
  elif agent_return['state'] == AgentStatusCode.PLUGIN_START:
 
35
  elif agent_return['state'] == AgentStatusCode.PLUGIN_END:
36
  thought = gr_history[-1][1].split('```')[0]
37
  if isinstance(agent_return['response'], dict):
38
+ gr_history[-1][
39
+ 1] = thought + '\n' + f'```json\n{json.dumps(agent_return["response"], ensure_ascii=False, indent=4)}\n```' # noqa: E501
40
  elif agent_return['state'] == AgentStatusCode.PLUGIN_RETURN:
41
  assert agent_return['inner_steps'][-1]['role'] == 'environment'
42
  item = agent_return['inner_steps'][-1]
 
47
  gr_history.append([None, ''])
48
  return
49
 
50
+
51
  def predict(history_planner, history_searcher):
52
+
53
  def streaming(raw_response):
54
  for chunk in raw_response.iter_lines(chunk_size=8192,
55
+ decode_unicode=False,
56
+ delimiter=b'\n'):
57
  if chunk:
58
  decoded = chunk.decode('utf-8')
59
  if decoded == '\r':
 
99
  yield history_planner, history_searcher
100
  return history_planner, history_searcher
101
 
102
+
103
  examples = [
104
+ ['Find legal precedents in contract law.'],
105
+ ['What are the top 10 e-commerce websites?'],
106
+ ['Generate a report on global climate change.'],
107
  ]
108
 
109
+ css_path = os.path.join(os.path.dirname(__file__), 'css', 'gradio_front.css')
 
 
110
  with gr.Blocks(css=css_path) as demo:
111
+ with gr.Column(elem_classes='chat-box'):
112
  gr.HTML("""<h1 align="center">MindSearch Gradio Demo</h1>""")
113
+ gr.HTML(
114
+ """<p style="text-align: center; font-family: Arial, sans-serif;">
115
+ MindSearch is an open-source AI Search Engine Framework with Perplexity.ai Pro performance.
116
+ You can deploy your own Perplexity.ai-style search engine using either
117
+ closed-source LLMs (GPT, Claude)
118
+ or open-source LLMs (InternLM2.5-7b-chat).</p> """)
119
  gr.HTML("""
120
  <div style="text-align: center; font-size: 16px;">
121
+ <a href="https://github.com/InternLM/MindSearch" style="margin-right: 15px;
122
+ text-decoration: none; color: #4A90E2;" target="_blank">🔗 GitHub</a>
123
+ <a href="https://arxiv.org/abs/2407.20183" style="margin-right: 15px;
124
+ text-decoration: none; color: #4A90E2;" target="_blank">📄 Arxiv</a>
125
+ <a href="https://huggingface.co/papers/2407.20183" style="margin-right:
126
+ 15px; text-decoration: none; color: #4A90E2;" target="_blank">📚 Hugging Face Papers</a>
127
+ <a href="https://huggingface.co/spaces/internlm/MindSearch"
128
+ style="text-decoration: none; color: #4A90E2;" target="_blank">🤗 Hugging Face Demo</a>
129
  </div>""")
130
+ gr.HTML("""
131
+ <h1 align='right'><img
132
+ src=
133
+ 'https://raw.githubusercontent.com/InternLM/MindSearch/98fd84d566fe9e3adc5028727f72f2944098fd05/assets/logo.svg'
134
+ alt='MindSearch Logo1' class="logo"></h1> """)
135
 
136
  with gr.Row():
137
  with gr.Column(scale=10):
 
142
  show_copy_button=True,
143
  bubble_full_width=False,
144
  render_markdown=True,
145
+ elem_classes='chatbot-container')
146
  with gr.Column():
147
  searcher = gr.Chatbot(label='searcher',
148
  show_label=True,
149
  show_copy_button=True,
150
  bubble_full_width=False,
151
  render_markdown=True,
152
+ elem_classes='chatbot-container')
153
 
154
+ with gr.Row(elem_classes='chat-box'):
155
  # Text input area
156
+ user_input = gr.Textbox(show_label=False,
157
+ placeholder='Type your message...',
158
+ lines=1,
159
+ container=False,
160
+ elem_classes='editor')
 
 
161
  # Buttons (now in the same Row)
162
+ submitBtn = gr.Button('submit',
163
+ variant='primary',
164
+ elem_classes='toolbarButton')
165
+ clearBtn = gr.Button('clear',
166
+ variant='secondary',
167
+ elem_classes='toolbarButton')
168
+ with gr.Row(elem_classes='examples-container'):
169
+ examples_component = gr.Examples(examples,
170
+ inputs=user_input,
171
+ label='Try these examples:')
172
 
173
  def user(query, history):
174
  return '', history + [[query, '']]
 
177
  return user(example[0], planner.value)
178
 
179
  submitBtn.click(user, [user_input, planner], [user_input, planner],
180
+ queue=False).then(predict, [planner, searcher],
181
+ [planner, searcher])
182
  clearBtn.click(rst_mem, [planner, searcher], [planner, searcher],
183
+ queue=False)
184
 
185
  demo.queue()
186
  demo.launch(server_name='0.0.0.0',
187
  server_port=7860,
188
  inbrowser=True,
189
+ share=True)