|
| 1 | +import os |
1 | 2 | from pathlib import Path
|
2 | 3 | import requests
|
3 | 4 | from PIL import Image
|
4 | 5 | from typing import List
|
5 | 6 | from qwen_agent.llm.schema import CONTENT, ROLE, USER, Message
|
6 | 7 | from qwen_agent.gui.utils import convert_history_to_chatbot
|
7 |
| -from qwen_agent.gui.gradio import gr, mgr |
| 8 | +from qwen_agent.gui.gradio_dep import gr, mgr |
8 | 9 | from qwen_agent.gui import WebUI
|
9 | 10 |
|
10 | 11 |
|
|
30 | 31 |
|
31 | 32 |
|
32 | 33 | class OpenVINOUI(WebUI):
|
33 |
| - def request_cancel(self): |
34 |
| - self.agent_list[0].llm.ov_model.request.cancel() |
35 |
| - |
36 |
| - def clear_history(self): |
37 |
| - return [] |
38 |
| - |
39 |
| - def add_text(self, _input, _chatbot, _history): |
40 |
| - _history.append( |
41 |
| - { |
42 |
| - ROLE: USER, |
43 |
| - CONTENT: [{"text": _input}], |
44 |
| - } |
45 |
| - ) |
46 |
| - _chatbot.append([_input, None]) |
47 |
| - yield gr.update(interactive=False, value=None), _chatbot, _history |
48 |
| - |
49 |
| - def run( |
50 |
| - self, |
51 |
| - messages: List[Message] = None, |
52 |
| - share: bool = False, |
53 |
| - server_name: str = None, |
54 |
| - server_port: int = None, |
55 |
| - **kwargs, |
56 |
| - ): |
| 34 | + """A Common chatbot application for agent.""" |
| 35 | + |
| 36 | + def run(self, |
| 37 | + messages: List[Message] = None, |
| 38 | + share: bool = False, |
| 39 | + server_name: str = None, |
| 40 | + server_port: int = None, |
| 41 | + concurrency_limit: int = 10, |
| 42 | + enable_mention: bool = False, |
| 43 | + **kwargs): |
57 | 44 | self.run_kwargs = kwargs
|
58 | 45 |
|
| 46 | + from qwen_agent.gui.gradio_dep import gr, mgr, ms |
| 47 | + |
| 48 | + customTheme = gr.themes.Default( |
| 49 | + primary_hue=gr.themes.utils.colors.blue, |
| 50 | + radius_size=gr.themes.utils.sizes.radius_none, |
| 51 | + ) |
| 52 | + |
59 | 53 | with gr.Blocks(
|
60 |
| - theme=gr.themes.Soft(), |
61 |
| - css=".disclaimer {font-variant-caps: all-small-caps;}", |
62 |
| - ) as self.demo: |
63 |
| - gr.Markdown("""<h1><center>OpenVINO Qwen Agent </center></h1>""") |
| 54 | + css=os.path.join(os.path.dirname(__file__), 'assets/appBot.css'), |
| 55 | + theme=customTheme, |
| 56 | + ) as demo: |
64 | 57 | history = gr.State([])
|
| 58 | + with ms.Application(): |
| 59 | + with gr.Row(elem_classes='container'): |
| 60 | + with gr.Column(scale=4): |
| 61 | + chatbot = mgr.Chatbot(value=convert_history_to_chatbot(messages=messages), |
| 62 | + avatar_images=[ |
| 63 | + self.user_config, |
| 64 | + self.agent_config_list, |
| 65 | + ], |
| 66 | + height=900, |
| 67 | + avatar_image_width=80, |
| 68 | + flushing=False, |
| 69 | + show_copy_button=True, |
| 70 | + latex_delimiters=[{ |
| 71 | + 'left': '\\(', |
| 72 | + 'right': '\\)', |
| 73 | + 'display': True |
| 74 | + }, { |
| 75 | + 'left': '\\begin{equation}', |
| 76 | + 'right': '\\end{equation}', |
| 77 | + 'display': True |
| 78 | + }, { |
| 79 | + 'left': '\\begin{align}', |
| 80 | + 'right': '\\end{align}', |
| 81 | + 'display': True |
| 82 | + }, { |
| 83 | + 'left': '\\begin{alignat}', |
| 84 | + 'right': '\\end{alignat}', |
| 85 | + 'display': True |
| 86 | + }, { |
| 87 | + 'left': '\\begin{gather}', |
| 88 | + 'right': '\\end{gather}', |
| 89 | + 'display': True |
| 90 | + }, { |
| 91 | + 'left': '\\begin{CD}', |
| 92 | + 'right': '\\end{CD}', |
| 93 | + 'display': True |
| 94 | + }, { |
| 95 | + 'left': '\\[', |
| 96 | + 'right': '\\]', |
| 97 | + 'display': True |
| 98 | + }]) |
| 99 | + |
| 100 | + input = mgr.MultimodalInput(placeholder=self.input_placeholder,) |
| 101 | + |
| 102 | + with gr.Column(scale=1): |
| 103 | + if len(self.agent_list) > 1: |
| 104 | + agent_selector = gr.Dropdown( |
| 105 | + [(agent.name, i) for i, agent in enumerate(self.agent_list)], |
| 106 | + label='Agents', |
| 107 | + info='Select an Agent', |
| 108 | + value=0, |
| 109 | + interactive=True, |
| 110 | + ) |
| 111 | + |
| 112 | + agent_info_block = self._create_agent_info_block() |
| 113 | + |
| 114 | + agent_plugins_block = self._create_agent_plugins_block() |
| 115 | + |
| 116 | + if self.prompt_suggestions: |
| 117 | + gr.Examples( |
| 118 | + label='Example questions', |
| 119 | + examples=self.prompt_suggestions, |
| 120 | + inputs=[input], |
| 121 | + ) |
| 122 | + |
| 123 | + if len(self.agent_list) > 1: |
| 124 | + agent_selector.change( |
| 125 | + fn=self.change_agent, |
| 126 | + inputs=[agent_selector], |
| 127 | + outputs=[agent_selector, agent_info_block, agent_plugins_block], |
| 128 | + queue=False, |
| 129 | + ) |
65 | 130 |
|
66 |
| - with gr.Row(): |
67 |
| - with gr.Column(scale=4): |
68 |
| - chatbot = mgr.Chatbot( |
69 |
| - value=convert_history_to_chatbot(messages=messages), |
70 |
| - avatar_images=[ |
71 |
| - self.user_config, |
72 |
| - self.agent_config_list, |
73 |
| - ], |
74 |
| - height=900, |
75 |
| - avatar_image_width=80, |
76 |
| - flushing=False, |
77 |
| - show_copy_button=True, |
| 131 | + input_promise = input.submit( |
| 132 | + fn=self.add_text, |
| 133 | + inputs=[input, chatbot, history], |
| 134 | + outputs=[input, chatbot, history], |
| 135 | + queue=False, |
78 | 136 | )
|
79 |
| - with gr.Column(): |
80 |
| - input = gr.Textbox( |
81 |
| - label="Chat Message Box", |
82 |
| - placeholder="Chat Message Box", |
83 |
| - show_label=False, |
84 |
| - container=False, |
| 137 | + |
| 138 | + if len(self.agent_list) > 1 and enable_mention: |
| 139 | + input_promise = input_promise.then( |
| 140 | + self.add_mention, |
| 141 | + [chatbot, agent_selector], |
| 142 | + [chatbot, agent_selector], |
| 143 | + ).then( |
| 144 | + self.agent_run, |
| 145 | + [chatbot, history, agent_selector], |
| 146 | + [chatbot, history, agent_selector], |
85 | 147 | )
|
86 |
| - with gr.Column(): |
87 |
| - with gr.Row(): |
88 |
| - submit = gr.Button("Submit", variant="primary") |
89 |
| - stop = gr.Button("Stop") |
90 |
| - clear = gr.Button("Clear") |
91 |
| - with gr.Column(scale=1): |
92 |
| - agent_interactive = self.agent_list[0] |
93 |
| - capabilities = [key for key in agent_interactive.function_map.keys()] |
94 |
| - gr.CheckboxGroup( |
95 |
| - label="Tools", |
96 |
| - value=capabilities, |
97 |
| - choices=capabilities, |
98 |
| - interactive=False, |
99 |
| - ) |
100 |
| - with gr.Row(): |
101 |
| - gr.Examples(self.prompt_suggestions, inputs=[input], label="Click on any example and press the 'Submit' button") |
102 |
| - |
103 |
| - input_promise = submit.click( |
104 |
| - fn=self.add_text, |
105 |
| - inputs=[input, chatbot, history], |
106 |
| - outputs=[input, chatbot, history], |
107 |
| - queue=False, |
108 |
| - ) |
109 |
| - input_promise = input_promise.then( |
110 |
| - self.agent_run, |
111 |
| - [chatbot, history], |
112 |
| - [chatbot, history], |
113 |
| - ) |
114 |
| - input_promise.then(self.flushed, None, [input]) |
115 |
| - stop.click( |
116 |
| - fn=self.request_cancel, |
117 |
| - inputs=None, |
118 |
| - outputs=None, |
119 |
| - cancels=[input_promise], |
120 |
| - queue=False, |
121 |
| - ) |
122 |
| - clear.click(lambda: None, None, chatbot, queue=False).then(self.clear_history, None, history) |
| 148 | + else: |
| 149 | + input_promise = input_promise.then( |
| 150 | + self.agent_run, |
| 151 | + [chatbot, history], |
| 152 | + [chatbot, history], |
| 153 | + ) |
| 154 | + |
| 155 | + input_promise.then(self.flushed, None, [input]) |
| 156 | + |
| 157 | + demo.load(None) |
| 158 | + |
| 159 | + demo.queue(default_concurrency_limit=concurrency_limit).launch(share=share, |
| 160 | + server_name=server_name, |
| 161 | + server_port=server_port) |
| 162 | + |
123 | 163 |
|
124 |
| - self.demo.load(None) |
| 164 | + def _create_agent_plugins_block(self, agent_index=0): |
| 165 | + from qwen_agent.gui.gradio_dep import gr |
125 | 166 |
|
126 |
| - self.demo.launch(share=share, server_name=server_name, server_port=server_port) |
| 167 | + agent_interactive = self.agent_list[agent_index] |
| 168 | + |
| 169 | + if agent_interactive.function_map: |
| 170 | + capabilities = [key for key in agent_interactive.function_map.keys()] |
| 171 | + return gr.CheckboxGroup( |
| 172 | + label='Tools', |
| 173 | + value=capabilities, |
| 174 | + choices=capabilities, |
| 175 | + interactive=False, |
| 176 | + ) |
| 177 | + |
| 178 | + else: |
| 179 | + return gr.CheckboxGroup( |
| 180 | + label='Tools', |
| 181 | + value=[], |
| 182 | + choices=[], |
| 183 | + interactive=False, |
| 184 | + ) |
127 | 185 |
|
128 | 186 |
|
129 | 187 | def make_demo(bot):
|
|
0 commit comments