Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,174 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import re
|
3 |
+
from typing import List, Optional, Union
|
4 |
+
from qwen_agent import Agent, MultiAgentHub
|
5 |
+
from qwen_agent.gui.gradio_utils import format_cover_html
|
6 |
+
from qwen_agent.gui.utils import convert_fncall_to_text, convert_history_to_chatbot, get_avatar_image
|
7 |
+
from qwen_agent.llm.schema import CONTENT, IMAGE, FILE, NAME, USER, Message
|
8 |
+
from qwen_agent.log import logger
|
9 |
+
from qwen_agent.utils.utils import print_traceback
|
10 |
+
|
11 |
+
class WebUI:
|
12 |
+
"""Chatbot application for managing multiple agents."""
|
13 |
+
|
14 |
+
def __init__(self, agent: Union[Agent, MultiAgentHub, List[Agent]], chatbot_config: Optional[dict] = None):
|
15 |
+
"""
|
16 |
+
Initialize the chatbot application with one or more agents and configuration options.
|
17 |
+
"""
|
18 |
+
chatbot_config = chatbot_config or {}
|
19 |
+
self.agent_list = [agent] if not isinstance(agent, list) else agent
|
20 |
+
self.agent_hub = None if isinstance(agent, list) else agent
|
21 |
+
|
22 |
+
self.user_config = {
|
23 |
+
'name': chatbot_config.get('user.name', 'user'),
|
24 |
+
'avatar': chatbot_config.get('user.avatar', get_avatar_image(chatbot_config.get('user.name', 'user')))
|
25 |
+
}
|
26 |
+
|
27 |
+
self.agent_config_list = [{
|
28 |
+
'name': agent.name,
|
29 |
+
'avatar': chatbot_config.get('agent.avatar', os.path.join(os.path.dirname(__file__), 'assets/logo.jpeg')),
|
30 |
+
'description': agent.description or "I'm a helpful assistant."
|
31 |
+
} for agent in self.agent_list]
|
32 |
+
|
33 |
+
self.input_placeholder = chatbot_config.get('input.placeholder', 'Chat with me!')
|
34 |
+
self.prompt_suggestions = chatbot_config.get('prompt.suggestions', [])
|
35 |
+
self.verbose = chatbot_config.get('verbose', False)
|
36 |
+
|
37 |
+
def run(self, messages: List[Message] = None, share: bool = False, server_name: str = None,
|
38 |
+
server_port: int = None, concurrency_limit: int = 80, enable_mention: bool = False, **kwargs):
|
39 |
+
|
40 |
+
from qwen_agent.gui.gradio import gr, mgr
|
41 |
+
|
42 |
+
custom_theme = gr.themes.Default(primary_hue=gr.themes.utils.colors.blue, radius_size=gr.themes.utils.sizes.radius_none)
|
43 |
+
|
44 |
+
with gr.Blocks(css=os.path.join(os.path.dirname(__file__), 'assets/appBot.css'), theme=custom_theme) as demo:
|
45 |
+
history = gr.State([])
|
46 |
+
|
47 |
+
with gr.Row(elem_classes='container'):
|
48 |
+
with gr.Column(scale=4):
|
49 |
+
chatbot = mgr.Chatbot(
|
50 |
+
value=convert_history_to_chatbot(messages=messages),
|
51 |
+
avatar_images=[self.user_config, self.agent_config_list],
|
52 |
+
height=850, avatar_image_width=80, flushing=False, show_copy_button=True,
|
53 |
+
latex_delimiters=[{'left': '\\(', 'right': '\\)', 'display': True}, {'left': '\\begin{equation}', 'right': '\\end{equation}', 'display': True}]
|
54 |
+
)
|
55 |
+
|
56 |
+
input_box = mgr.MultimodalInput(placeholder=self.input_placeholder, upload_button_props=dict(visible=True))
|
57 |
+
|
58 |
+
with gr.Column(scale=1):
|
59 |
+
agent_selector = gr.Dropdown(
|
60 |
+
[(agent.name, i) for i, agent in enumerate(self.agent_list)],
|
61 |
+
label='Agents', value=0, interactive=True) if len(self.agent_list) > 1 else None
|
62 |
+
|
63 |
+
agent_info_block = self._create_agent_info_block()
|
64 |
+
|
65 |
+
if self.prompt_suggestions:
|
66 |
+
gr.Examples(label='Suggested Conversations', examples=self.prompt_suggestions, inputs=[input_box])
|
67 |
+
|
68 |
+
if agent_selector:
|
69 |
+
agent_selector.change(fn=self.change_agent, inputs=[agent_selector], outputs=[agent_selector, agent_info_block])
|
70 |
+
|
71 |
+
input_promise = input_box.submit(fn=self.add_text, inputs=[input_box, chatbot, history], outputs=[input_box, chatbot, history])
|
72 |
+
|
73 |
+
if len(self.agent_list) > 1 and enable_mention:
|
74 |
+
input_promise = input_promise.then(self.add_mention, [chatbot, agent_selector], [chatbot, agent_selector]) \
|
75 |
+
.then(self.agent_run, [chatbot, history, agent_selector], [chatbot, history, agent_selector])
|
76 |
+
else:
|
77 |
+
input_promise = input_promise.then(self.agent_run, [chatbot, history], [chatbot, history])
|
78 |
+
|
79 |
+
input_promise.then(self.flushed, None, [input_box])
|
80 |
+
|
81 |
+
demo.load(None)
|
82 |
+
|
83 |
+
demo.queue(default_concurrency_limit=concurrency_limit).launch(share=share, server_name=server_name, server_port=server_port)
|
84 |
+
|
85 |
+
def change_agent(self, agent_selector):
|
86 |
+
yield agent_selector, self._create_agent_info_block(agent_selector), self._create_agent_plugins_block(agent_selector)
|
87 |
+
|
88 |
+
def add_text(self, _input, _chatbot, _history):
|
89 |
+
from qwen_agent.gui.gradio import gr
|
90 |
+
if _input.text == "/clear":
|
91 |
+
_chatbot.clear()
|
92 |
+
_history.clear()
|
93 |
+
yield gr.update(interactive=False, value=""), _chatbot, _history
|
94 |
+
return
|
95 |
+
|
96 |
+
if _history:
|
97 |
+
gr.Warning("Only the most recent query is retained.", duration=5)
|
98 |
+
_chatbot.clear()
|
99 |
+
_history.clear()
|
100 |
+
|
101 |
+
_history.append({ROLE: USER, CONTENT: [{'text': _input.text}]})
|
102 |
+
if self.user_config[NAME]:
|
103 |
+
_history[-1][NAME] = self.user_config[NAME]
|
104 |
+
|
105 |
+
if _input.files:
|
106 |
+
for file in _input.files:
|
107 |
+
if file.mime_type.startswith('image/'):
|
108 |
+
_history[-1][CONTENT].append({IMAGE: f'file://{file.path}'})
|
109 |
+
else:
|
110 |
+
_history[-1][CONTENT].append({FILE: file.path})
|
111 |
+
|
112 |
+
_chatbot.append([_input, None])
|
113 |
+
yield gr.update(interactive=False, value=None), _chatbot, _history
|
114 |
+
|
115 |
+
def add_mention(self, _chatbot, _agent_selector):
|
116 |
+
query = _chatbot[-1][0].text
|
117 |
+
match = re.search(r'@\w+\b', query)
|
118 |
+
if match:
|
119 |
+
_agent_selector = self._get_agent_index_by_name(match.group()[1:])
|
120 |
+
agent_name = self.agent_list[_agent_selector].name
|
121 |
+
if ('@' + agent_name) not in query and self.agent_hub is None:
|
122 |
+
_chatbot[-1][0].text = f'@{agent_name} ' + query
|
123 |
+
|
124 |
+
yield _chatbot, _agent_selector
|
125 |
+
|
126 |
+
def agent_run(self, _chatbot, _history, _agent_selector=None):
|
127 |
+
if not _history:
|
128 |
+
yield _chatbot, _history, _agent_selector if _agent_selector else _chatbot, _history
|
129 |
+
return
|
130 |
+
|
131 |
+
if self.verbose:
|
132 |
+
logger.info(f'agent_run input:\n{_history}')
|
133 |
+
|
134 |
+
agent_runner = self.agent_list[_agent_selector or 0] if self.agent_hub is None else self.agent_hub
|
135 |
+
responses = agent_runner.run(_history, **self.run_kwargs)
|
136 |
+
|
137 |
+
for response in responses:
|
138 |
+
if response[CONTENT] == 'PENDING_USER_INPUT':
|
139 |
+
logger.info('Waiting for user input!')
|
140 |
+
break
|
141 |
+
display_responses = convert_fncall_to_text(response)
|
142 |
+
if not display_responses or display_responses[-1][CONTENT] is None:
|
143 |
+
continue
|
144 |
+
|
145 |
+
_chatbot.append([None, None] * (len(display_responses) - len(_chatbot)))
|
146 |
+
for i, rsp in enumerate(display_responses):
|
147 |
+
_chatbot[-1][1][self._get_agent_index_by_name(rsp[NAME])] = rsp[CONTENT]
|
148 |
+
|
149 |
+
if self.verbose:
|
150 |
+
logger.info(f'agent_run response:\n{responses}')
|
151 |
+
|
152 |
+
yield _chatbot, _history, _agent_selector if _agent_selector else _chatbot, _history
|
153 |
+
|
154 |
+
def flushed(self):
|
155 |
+
from qwen_agent.gui.gradio import gr
|
156 |
+
return gr.update(interactive=True)
|
157 |
+
|
158 |
+
def _get_agent_index_by_name(self, agent_name):
|
159 |
+
try:
|
160 |
+
return next(i for i, agent in enumerate(self.agent_list) if agent.name.strip() == agent_name.strip())
|
161 |
+
except StopIteration:
|
162 |
+
print_traceback()
|
163 |
+
return 0
|
164 |
+
|
165 |
+
def _create_agent_info_block(self, agent_index=0):
|
166 |
+
from qwen_agent.gui.gradio import gr
|
167 |
+
agent_config = self.agent_config_list[agent_index]
|
168 |
+
return gr.HTML(format_cover_html(bot_name=agent_config['name'], bot_description=agent_config['description'], bot_avatar=agent_config['avatar']))
|
169 |
+
|
170 |
+
def _create_agent_plugins_block(self, agent_index=0):
|
171 |
+
from qwen_agent.gui.gradio import gr
|
172 |
+
agent = self.agent_list[agent_index]
|
173 |
+
capabilities = list(agent.function_map.keys()) if agent.function_map else []
|
174 |
+
return gr.CheckboxGroup(label='Plugins', value=capabilities, choices=capabilities, interactive=False)
|