Spaces:
Running
Running
Update web_ui.py
#5
by
MotZepka
- opened
web_ui.py
CHANGED
@@ -17,13 +17,11 @@ class WebUI:
|
|
17 |
|
18 |
def __init__(self, agent: Union[Agent, MultiAgentHub, List[Agent]], chatbot_config: Optional[dict] = None):
|
19 |
"""
|
20 |
-
|
21 |
|
22 |
Args:
|
23 |
-
agent: The agent or a list of agents,
|
24 |
-
|
25 |
-
chatbot_config: The chatbot configuration.
|
26 |
-
Set the configuration as {'user.name': '', 'user.avatar': '', 'agent.avatar': '', 'input.placeholder': '', 'prompt.suggestions': []}.
|
27 |
"""
|
28 |
chatbot_config = chatbot_config or {}
|
29 |
|
@@ -59,13 +57,6 @@ class WebUI:
|
|
59 |
self.prompt_suggestions = chatbot_config.get('prompt.suggestions', [])
|
60 |
self.verbose = chatbot_config.get('verbose', False)
|
61 |
|
62 |
-
"""
|
63 |
-
Run the chatbot.
|
64 |
-
|
65 |
-
Args:
|
66 |
-
messages: The chat history.
|
67 |
-
"""
|
68 |
-
|
69 |
def run(self,
|
70 |
messages: List[Message] = None,
|
71 |
share: bool = False,
|
@@ -78,14 +69,14 @@ class WebUI:
|
|
78 |
|
79 |
from qwen_agent.gui.gradio import gr, mgr
|
80 |
|
81 |
-
|
82 |
primary_hue=gr.themes.utils.colors.blue,
|
83 |
radius_size=gr.themes.utils.sizes.radius_none,
|
84 |
)
|
85 |
|
86 |
with gr.Blocks(
|
87 |
css=os.path.join(os.path.dirname(__file__), 'assets/appBot.css'),
|
88 |
-
theme=
|
89 |
) as demo:
|
90 |
history = gr.State([])
|
91 |
|
@@ -130,7 +121,7 @@ class WebUI:
|
|
130 |
'display': True
|
131 |
}])
|
132 |
|
133 |
-
|
134 |
|
135 |
with gr.Column(scale=1):
|
136 |
if len(self.agent_list) > 1:
|
@@ -144,27 +135,25 @@ class WebUI:
|
|
144 |
|
145 |
agent_info_block = self._create_agent_info_block()
|
146 |
|
147 |
-
# agent_plugins_block = self._create_agent_plugins_block()
|
148 |
-
|
149 |
if self.prompt_suggestions:
|
150 |
gr.Examples(
|
151 |
label='推荐对话',
|
152 |
examples=self.prompt_suggestions,
|
153 |
-
inputs=[
|
154 |
)
|
155 |
|
156 |
if len(self.agent_list) > 1:
|
157 |
agent_selector.change(
|
158 |
fn=self.change_agent,
|
159 |
inputs=[agent_selector],
|
160 |
-
outputs=[agent_selector, agent_info_block,
|
161 |
queue=False,
|
162 |
)
|
163 |
|
164 |
-
input_promise =
|
165 |
fn=self.add_text,
|
166 |
-
inputs=[
|
167 |
-
outputs=[
|
168 |
queue=False,
|
169 |
)
|
170 |
|
@@ -185,7 +174,7 @@ class WebUI:
|
|
185 |
[chatbot, history],
|
186 |
)
|
187 |
|
188 |
-
input_promise.then(self.flushed, None, [
|
189 |
|
190 |
demo.load(None)
|
191 |
|
@@ -194,22 +183,19 @@ class WebUI:
|
|
194 |
server_port=server_port)
|
195 |
|
196 |
def change_agent(self, agent_selector):
|
197 |
-
yield agent_selector, self._create_agent_info_block(agent_selector), self._create_agent_plugins_block(
|
198 |
-
agent_selector)
|
199 |
|
200 |
def add_text(self, _input, _chatbot, _history):
|
201 |
from qwen_agent.gui.gradio import gr
|
202 |
if _input.text == "/clear":
|
203 |
_chatbot = []
|
204 |
_history.clear()
|
205 |
-
yield gr.update(interactive=False, value="")
|
206 |
return
|
207 |
|
208 |
_history.append({
|
209 |
ROLE: USER,
|
210 |
-
CONTENT: [{
|
211 |
-
'text': _input.text
|
212 |
-
}],
|
213 |
})
|
214 |
|
215 |
if self.user_config[NAME]:
|
@@ -250,7 +236,6 @@ class WebUI:
|
|
250 |
yield _chatbot, _history
|
251 |
return
|
252 |
|
253 |
-
|
254 |
if self.verbose:
|
255 |
logger.info('agent_run input:\n' + pprint.pformat(_history, indent=2))
|
256 |
|
@@ -262,25 +247,21 @@ class WebUI:
|
|
262 |
if self.agent_hub:
|
263 |
agent_runner = self.agent_hub
|
264 |
responses = []
|
265 |
-
for responses in agent_runner.run(_history, **self.run_kwargs):
|
266 |
-
# usage = responses.usage
|
267 |
-
# responses = [Message(ASSISTANT, responses.output.choices[0].message.content)]
|
268 |
|
269 |
-
|
|
|
270 |
continue
|
271 |
-
if
|
272 |
logger.info('Interrupted. Waiting for user input!')
|
273 |
break
|
274 |
|
275 |
-
display_responses = convert_fncall_to_text(
|
276 |
-
# display_responses[-1][CONTENT] += "\n<summary>" + repr({"usage": usage}) + "</summary>"
|
277 |
if not display_responses:
|
278 |
continue
|
279 |
if display_responses[-1][CONTENT] is None:
|
280 |
continue
|
281 |
|
282 |
while len(display_responses) > num_output_bubbles:
|
283 |
-
# Create a new chat bubble
|
284 |
_chatbot.append([None, None])
|
285 |
_chatbot[-1][1] = [None for _ in range(len(self.agent_list))]
|
286 |
num_output_bubbles += 1
|
@@ -315,7 +296,6 @@ class WebUI:
|
|
315 |
|
316 |
def flushed(self):
|
317 |
from qwen_agent.gui.gradio import gr
|
318 |
-
|
319 |
return gr.update(interactive=True)
|
320 |
|
321 |
def _get_agent_index_by_name(self, agent_name):
|
@@ -357,11 +337,10 @@ class WebUI:
|
|
357 |
choices=capabilities,
|
358 |
interactive=False,
|
359 |
)
|
360 |
-
|
361 |
else:
|
362 |
return gr.CheckboxGroup(
|
363 |
label='插件',
|
364 |
value=[],
|
365 |
choices=[],
|
366 |
interactive=False,
|
367 |
-
)
|
|
|
17 |
|
18 |
def __init__(self, agent: Union[Agent, MultiAgentHub, List[Agent]], chatbot_config: Optional[dict] = None):
|
19 |
"""
|
20 |
+
Initialize the chatbot.
|
21 |
|
22 |
Args:
|
23 |
+
agent: The agent or a list of agents, supports various types of agents such as Assistant, GroupChat, Router, etc.
|
24 |
+
chatbot_config: The chatbot configuration. Set the configuration as {'user.name': '', 'user.avatar': '', 'agent.avatar': '', 'input.placeholder': '', 'prompt.suggestions': []}.
|
|
|
|
|
25 |
"""
|
26 |
chatbot_config = chatbot_config or {}
|
27 |
|
|
|
57 |
self.prompt_suggestions = chatbot_config.get('prompt.suggestions', [])
|
58 |
self.verbose = chatbot_config.get('verbose', False)
|
59 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
60 |
def run(self,
|
61 |
messages: List[Message] = None,
|
62 |
share: bool = False,
|
|
|
69 |
|
70 |
from qwen_agent.gui.gradio import gr, mgr
|
71 |
|
72 |
+
custom_theme = gr.themes.Default(
|
73 |
primary_hue=gr.themes.utils.colors.blue,
|
74 |
radius_size=gr.themes.utils.sizes.radius_none,
|
75 |
)
|
76 |
|
77 |
with gr.Blocks(
|
78 |
css=os.path.join(os.path.dirname(__file__), 'assets/appBot.css'),
|
79 |
+
theme=custom_theme,
|
80 |
) as demo:
|
81 |
history = gr.State([])
|
82 |
|
|
|
121 |
'display': True
|
122 |
}])
|
123 |
|
124 |
+
input_component = mgr.MultimodalInput(placeholder=self.input_placeholder, upload_button_props=dict(file_types=[".pdf", ".docx", ".pptx", ".txt", ".html", ".csv", ".tsv", ".xlsx", ".xls"] + ["." + file_type for file_type in common_programming_language_extensions]))
|
125 |
|
126 |
with gr.Column(scale=1):
|
127 |
if len(self.agent_list) > 1:
|
|
|
135 |
|
136 |
agent_info_block = self._create_agent_info_block()
|
137 |
|
|
|
|
|
138 |
if self.prompt_suggestions:
|
139 |
gr.Examples(
|
140 |
label='推荐对话',
|
141 |
examples=self.prompt_suggestions,
|
142 |
+
inputs=[input_component],
|
143 |
)
|
144 |
|
145 |
if len(self.agent_list) > 1:
|
146 |
agent_selector.change(
|
147 |
fn=self.change_agent,
|
148 |
inputs=[agent_selector],
|
149 |
+
outputs=[agent_selector, agent_info_block, agent_info_block], # Assuming you want to update this block too
|
150 |
queue=False,
|
151 |
)
|
152 |
|
153 |
+
input_promise = input_component.submit(
|
154 |
fn=self.add_text,
|
155 |
+
inputs=[input_component, chatbot, history],
|
156 |
+
outputs=[input_component, chatbot, history],
|
157 |
queue=False,
|
158 |
)
|
159 |
|
|
|
174 |
[chatbot, history],
|
175 |
)
|
176 |
|
177 |
+
input_promise.then(self.flushed, None, [input_component])
|
178 |
|
179 |
demo.load(None)
|
180 |
|
|
|
183 |
server_port=server_port)
|
184 |
|
185 |
def change_agent(self, agent_selector):
|
186 |
+
yield agent_selector, self._create_agent_info_block(agent_selector), self._create_agent_plugins_block(agent_selector)
|
|
|
187 |
|
188 |
def add_text(self, _input, _chatbot, _history):
|
189 |
from qwen_agent.gui.gradio import gr
|
190 |
if _input.text == "/clear":
|
191 |
_chatbot = []
|
192 |
_history.clear()
|
193 |
+
yield gr.update(interactive=False, value="")
|
194 |
return
|
195 |
|
196 |
_history.append({
|
197 |
ROLE: USER,
|
198 |
+
CONTENT: [{'text': _input.text}],
|
|
|
|
|
199 |
})
|
200 |
|
201 |
if self.user_config[NAME]:
|
|
|
236 |
yield _chatbot, _history
|
237 |
return
|
238 |
|
|
|
239 |
if self.verbose:
|
240 |
logger.info('agent_run input:\n' + pprint.pformat(_history, indent=2))
|
241 |
|
|
|
247 |
if self.agent_hub:
|
248 |
agent_runner = self.agent_hub
|
249 |
responses = []
|
|
|
|
|
|
|
250 |
|
251 |
+
for response in agent_runner.run(_history, **self.run_kwargs):
|
252 |
+
if not response:
|
253 |
continue
|
254 |
+
if response[-1][CONTENT] == PENDING_USER_INPUT:
|
255 |
logger.info('Interrupted. Waiting for user input!')
|
256 |
break
|
257 |
|
258 |
+
display_responses = convert_fncall_to_text(response)
|
|
|
259 |
if not display_responses:
|
260 |
continue
|
261 |
if display_responses[-1][CONTENT] is None:
|
262 |
continue
|
263 |
|
264 |
while len(display_responses) > num_output_bubbles:
|
|
|
265 |
_chatbot.append([None, None])
|
266 |
_chatbot[-1][1] = [None for _ in range(len(self.agent_list))]
|
267 |
num_output_bubbles += 1
|
|
|
296 |
|
297 |
def flushed(self):
|
298 |
from qwen_agent.gui.gradio import gr
|
|
|
299 |
return gr.update(interactive=True)
|
300 |
|
301 |
def _get_agent_index_by_name(self, agent_name):
|
|
|
337 |
choices=capabilities,
|
338 |
interactive=False,
|
339 |
)
|
|
|
340 |
else:
|
341 |
return gr.CheckboxGroup(
|
342 |
label='插件',
|
343 |
value=[],
|
344 |
choices=[],
|
345 |
interactive=False,
|
346 |
+
)
|