feihu.hf commited on
Commit
e300bac
1 Parent(s): bcf46f4
Files changed (8) hide show
  1. README.md +4 -4
  2. app.py +54 -0
  3. assets/app.css +147 -0
  4. assets/appBot.css +129 -0
  5. assets/logo.jpeg +0 -0
  6. assets/user.jpeg +0 -0
  7. requirements.txt +1 -0
  8. web_ui.py +368 -0
README.md CHANGED
@@ -1,13 +1,13 @@
1
  ---
2
- title: QwQ 32B Preview
3
- emoji:
4
  colorFrom: purple
5
  colorTo: purple
6
  sdk: gradio
7
- sdk_version: 5.6.0
8
  app_file: app.py
9
  pinned: false
10
- short_description: 'QwQ-32B-Preview is an experimental version developed by the '
11
  ---
12
 
13
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
1
  ---
2
+ title: QwQ-32B-Preview
3
+ emoji: 🔍
4
  colorFrom: purple
5
  colorTo: purple
6
  sdk: gradio
7
+ sdk_version: 4.44.0
8
  app_file: app.py
9
  pinned: false
10
+ short_description: 'QwQ-32B-Preview'
11
  ---
12
 
13
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
app.py ADDED
@@ -0,0 +1,54 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import List, Tuple, Union
2
+ from web_ui import WebUI
3
+ import math
4
+ import os
5
+
6
+ from qwen_agent.agents import Assistant
7
+ from qwen_agent.gui.gradio import gr
8
+
9
+ def app_gui():
10
+ # Define the agent
11
+ bot = Assistant(llm={
12
+ 'model': os.environ.get("MODELNAME"),
13
+ 'generate_cfg': {
14
+ 'max_input_tokens': 32768,
15
+ 'max_retries': 10,
16
+ 'temperature': float(os.environ.get("T", 0.001)),
17
+ 'repetition_penalty': float(os.environ.get("R", 1.0)),
18
+ "top_k": int(os.environ.get("K", 20)),
19
+ "top_p": float(os.environ.get("P", 0.8)),
20
+ }},
21
+ name='QwQ-32B-preview',
22
+ description='QwQ-32B-Preview is an experimental version developed by the Qwen Team as part of our efforts to create a reasoning model. It is currently only optimized for reasoning tasks such as Code and Math, and has some limitations such as code switching, endless repetition. Only single-turn queries are supported in this demo.',
23
+ system_message= 'You are a helpful and harmless assistant. You are Qwen developed by Alibaba. You should think step-by-step.',
24
+ rag_cfg={'max_ref_token': 32768, 'rag_searchers': []},
25
+ )
26
+ chatbot_config = {
27
+ 'input.placeholder': "Type \"/clear\" to clear the history",
28
+ 'verbose': True,
29
+ 'prompt.suggestions': [
30
+ {
31
+ 'text': 'How many r in strawberry'
32
+ },
33
+ {
34
+ 'text': 'Find the least odd prime factor of $2019^8+1$.'
35
+ },
36
+ {
37
+ 'text': '''S先生、P先生、Q先生他们知道桌子的抽屉里有16张扑克牌:红桃A、Q、4 黑桃J、8、4、2、7、3 草花K、Q、5、4、6 方块A、5。约翰教授从这16张牌中挑出一张牌来,并把这张牌的点数告诉 P先生,把这张牌的花色告诉Q先生。这时,约翰教授问P先生和Q 先生:你们能从已知的点数或花色中推知这张牌是什么牌吗?于是,S先生听到如下的对话:
38
+
39
+ P先生:我不知道这张牌。
40
+
41
+ Q先生:我知道你不知道这张牌。
42
+
43
+ P先生:现在我知道这张牌了。
44
+
45
+ Q先生:我也知道了。
46
+
47
+ 请问:这张牌是什么牌?'''
48
+ },
49
+ ]
50
+ }
51
+ WebUI(bot, chatbot_config=chatbot_config).run()
52
+
53
+ if __name__ == '__main__':
54
+ app_gui()
assets/app.css ADDED
@@ -0,0 +1,147 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /* code highlight: https://python-markdown.github.io/extensions/code_hilite/ */
2
+ .codehilite .hll { background-color: #ffffcc }
3
+ .codehilite { background: #f8f8f8; }
4
+ .codehilite .c { color: #408080; font-style: italic } /* Comment */
5
+ .codehilite .err { border: 1px solid #FF0000 } /* Error */
6
+ .codehilite .k { color: #008000; font-weight: bold } /* Keyword */
7
+ .codehilite .o { color: #666666 } /* Operator */
8
+ .codehilite .ch { color: #408080; font-style: italic } /* Comment.Hashbang */
9
+ .codehilite .cm { color: #408080; font-style: italic } /* Comment.Multiline */
10
+ .codehilite .cp { color: #BC7A00 } /* Comment.Preproc */
11
+ .codehilite .cpf { color: #408080; font-style: italic } /* Comment.PreprocFile */
12
+ .codehilite .c1 { color: #408080; font-style: italic } /* Comment.Single */
13
+ .codehilite .cs { color: #408080; font-style: italic } /* Comment.Special */
14
+ .codehilite .gd { color: #A00000 } /* Generic.Deleted */
15
+ .codehilite .ge { font-style: italic } /* Generic.Emph */
16
+ .codehilite .gr { color: #FF0000 } /* Generic.Error */
17
+ .codehilite .gh { color: #000080; font-weight: bold } /* Generic.Heading */
18
+ .codehilite .gi { color: #00A000 } /* Generic.Inserted */
19
+ .codehilite .go { color: #888888 } /* Generic.Output */
20
+ .codehilite .gp { color: #000080; font-weight: bold } /* Generic.Prompt */
21
+ .codehilite .gs { font-weight: bold } /* Generic.Strong */
22
+ .codehilite .gu { color: #800080; font-weight: bold } /* Generic.Subheading */
23
+ .codehilite .gt { color: #0044DD } /* Generic.Traceback */
24
+ .codehilite .kc { color: #008000; font-weight: bold } /* Keyword.Constant */
25
+ .codehilite .kd { color: #008000; font-weight: bold } /* Keyword.Declaration */
26
+ .codehilite .kn { color: #008000; font-weight: bold } /* Keyword.Namespace */
27
+ .codehilite .kp { color: #008000 } /* Keyword.Pseudo */
28
+ .codehilite .kr { color: #008000; font-weight: bold } /* Keyword.Reserved */
29
+ .codehilite .kt { color: #B00040 } /* Keyword.Type */
30
+ .codehilite .m { color: #666666 } /* Literal.Number */
31
+ .codehilite .s { color: #BA2121 } /* Literal.String */
32
+ .codehilite .na { color: #7D9029 } /* Name.Attribute */
33
+ .codehilite .nb { color: #008000 } /* Name.Builtin */
34
+ .codehilite .nc { color: #0000FF; font-weight: bold } /* Name.Class */
35
+ .codehilite .no { color: #880000 } /* Name.Constant */
36
+ .codehilite .nd { color: #AA22FF } /* Name.Decorator */
37
+ .codehilite .ni { color: #999999; font-weight: bold } /* Name.Entity */
38
+ .codehilite .ne { color: #D2413A; font-weight: bold } /* Name.Exception */
39
+ .codehilite .nf { color: #0000FF } /* Name.Function */
40
+ .codehilite .nl { color: #A0A000 } /* Name.Label */
41
+ .codehilite .nn { color: #0000FF; font-weight: bold } /* Name.Namespace */
42
+ .codehilite .nt { color: #008000; font-weight: bold } /* Name.Tag */
43
+ .codehilite .nv { color: #19177C } /* Name.Variable */
44
+ .codehilite .ow { color: #AA22FF; font-weight: bold } /* Operator.Word */
45
+ .codehilite .w { color: #bbbbbb } /* Text.Whitespace */
46
+ .codehilite .mb { color: #666666 } /* Literal.Number.Bin */
47
+ .codehilite .mf { color: #666666 } /* Literal.Number.Float */
48
+ .codehilite .mh { color: #666666 } /* Literal.Number.Hex */
49
+ .codehilite .mi { color: #666666 } /* Literal.Number.Integer */
50
+ .codehilite .mo { color: #666666 } /* Literal.Number.Oct */
51
+ .codehilite .sa { color: #BA2121 } /* Literal.String.Affix */
52
+ .codehilite .sb { color: #BA2121 } /* Literal.String.Backtick */
53
+ .codehilite .sc { color: #BA2121 } /* Literal.String.Char */
54
+ .codehilite .dl { color: #BA2121 } /* Literal.String.Delimiter */
55
+ .codehilite .sd { color: #BA2121; font-style: italic } /* Literal.String.Doc */
56
+ .codehilite .s2 { color: #BA2121 } /* Literal.String.Double */
57
+ .codehilite .se { color: #BB6622; font-weight: bold } /* Literal.String.Escape */
58
+ .codehilite .sh { color: #BA2121 } /* Literal.String.Heredoc */
59
+ .codehilite .si { color: #BB6688; font-weight: bold } /* Literal.String.Interpol */
60
+ .codehilite .sx { color: #008000 } /* Literal.String.Other */
61
+ .codehilite .sr { color: #BB6688 } /* Literal.String.Regex */
62
+ .codehilite .s1 { color: #BA2121 } /* Literal.String.Single */
63
+ .codehilite .ss { color: #19177C } /* Literal.String.Symbol */
64
+ .codehilite .bp { color: #008000 } /* Name.Builtin.Pseudo */
65
+ .codehilite .fm { color: #0000FF } /* Name.Function.Magic */
66
+ .codehilite .vc { color: #19177C } /* Name.Variable.Class */
67
+ .codehilite .vg { color: #19177C } /* Name.Variable.Global */
68
+ .codehilite .vi { color: #19177C } /* Name.Variable.Instance */
69
+ .codehilite .vm { color: #19177C } /* Name.Variable.Magic */
70
+ .codehilite .il { color: #666666 } /* Literal.Number.Integer.Long */
71
+
72
+ .preview_header {
73
+ font-size: 18px;
74
+ font-weight: 500;
75
+ text-align: center;
76
+ margin-bottom: -12px;
77
+ }
78
+
79
+ .bot_cover {
80
+ display: flex;
81
+ flex-direction: column;
82
+ justify-content: center;
83
+ align-items: center;
84
+ min-height: 650px;
85
+ border: 1px solid rgb(229, 231, 235);
86
+ border-radius: 8px;
87
+ padding: 20px 40px;
88
+ }
89
+
90
+ .bot_avatar {
91
+ width: 100px;
92
+ height: 100px;
93
+ border-radius: 50%;
94
+ overflow: hidden;
95
+ }
96
+
97
+ .bot_avatar img {
98
+ width: 100px;
99
+ height: 100px;
100
+ }
101
+
102
+ .bot_name {
103
+ font-size: 36px;
104
+ margin-top: 10px;
105
+ }
106
+
107
+ .bot_desp {
108
+ color: #ddd;
109
+ }
110
+
111
+ .publish_link_container > a {
112
+ display: block;
113
+ border-radius: var(--button-large-radius);
114
+ padding: var(--button-large-padding);
115
+ font-weight: var(--button-large-text-weight);
116
+ font-size: var(--button-large-text-size);
117
+ border: var(--button-border-width) solid var(--button-secondary-border-color);
118
+ background: var(--button-secondary-background-fill);
119
+ color: var(--button-secondary-text-color) !important;
120
+ cursor: pointer;
121
+ text-decoration: none !important;
122
+ text-align: center;
123
+ }
124
+
125
+ .publish_link_container > .disabled {
126
+ cursor: not-allowed;
127
+ opacity: .5;
128
+ filter: grayscale(30%);
129
+ }
130
+
131
+ .markdown-body .message {
132
+ white-space: pre-wrap;
133
+ }
134
+
135
+ .markdown-body details {
136
+ white-space: nowrap;
137
+ }
138
+ .markdown-body .bot details:not(:last-child) {
139
+ margin-bottom: 1px;
140
+ }
141
+ .markdown-body summary {
142
+ background-color: #4b5563;
143
+ color: #eee;
144
+ padding: 0 4px;
145
+ border-radius: 4px;
146
+ font-size: 0.9em;
147
+ }
assets/appBot.css ADDED
@@ -0,0 +1,129 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /* code highlight: https://python-markdown.github.io/extensions/code_hilite/ */
2
+ .codehilite .hll { background-color: #ffffcc }
3
+ .codehilite { background: #f8f8f8; }
4
+ .codehilite .c { color: #408080; font-style: italic } /* Comment */
5
+ .codehilite .err { border: 1px solid #FF0000 } /* Error */
6
+ .codehilite .k { color: #008000; font-weight: bold } /* Keyword */
7
+ .codehilite .o { color: #666666 } /* Operator */
8
+ .codehilite .ch { color: #408080; font-style: italic } /* Comment.Hashbang */
9
+ .codehilite .cm { color: #408080; font-style: italic } /* Comment.Multiline */
10
+ .codehilite .cp { color: #BC7A00 } /* Comment.Preproc */
11
+ .codehilite .cpf { color: #408080; font-style: italic } /* Comment.PreprocFile */
12
+ .codehilite .c1 { color: #408080; font-style: italic } /* Comment.Single */
13
+ .codehilite .cs { color: #408080; font-style: italic } /* Comment.Special */
14
+ .codehilite .gd { color: #A00000 } /* Generic.Deleted */
15
+ .codehilite .ge { font-style: italic } /* Generic.Emph */
16
+ .codehilite .gr { color: #FF0000 } /* Generic.Error */
17
+ .codehilite .gh { color: #000080; font-weight: bold } /* Generic.Heading */
18
+ .codehilite .gi { color: #00A000 } /* Generic.Inserted */
19
+ .codehilite .go { color: #888888 } /* Generic.Output */
20
+ .codehilite .gp { color: #000080; font-weight: bold } /* Generic.Prompt */
21
+ .codehilite .gs { font-weight: bold } /* Generic.Strong */
22
+ .codehilite .gu { color: #800080; font-weight: bold } /* Generic.Subheading */
23
+ .codehilite .gt { color: #0044DD } /* Generic.Traceback */
24
+ .codehilite .kc { color: #008000; font-weight: bold } /* Keyword.Constant */
25
+ .codehilite .kd { color: #008000; font-weight: bold } /* Keyword.Declaration */
26
+ .codehilite .kn { color: #008000; font-weight: bold } /* Keyword.Namespace */
27
+ .codehilite .kp { color: #008000 } /* Keyword.Pseudo */
28
+ .codehilite .kr { color: #008000; font-weight: bold } /* Keyword.Reserved */
29
+ .codehilite .kt { color: #B00040 } /* Keyword.Type */
30
+ .codehilite .m { color: #666666 } /* Literal.Number */
31
+ .codehilite .s { color: #BA2121 } /* Literal.String */
32
+ .codehilite .na { color: #7D9029 } /* Name.Attribute */
33
+ .codehilite .nb { color: #008000 } /* Name.Builtin */
34
+ .codehilite .nc { color: #0000FF; font-weight: bold } /* Name.Class */
35
+ .codehilite .no { color: #880000 } /* Name.Constant */
36
+ .codehilite .nd { color: #AA22FF } /* Name.Decorator */
37
+ .codehilite .ni { color: #999999; font-weight: bold } /* Name.Entity */
38
+ .codehilite .ne { color: #D2413A; font-weight: bold } /* Name.Exception */
39
+ .codehilite .nf { color: #0000FF } /* Name.Function */
40
+ .codehilite .nl { color: #A0A000 } /* Name.Label */
41
+ .codehilite .nn { color: #0000FF; font-weight: bold } /* Name.Namespace */
42
+ .codehilite .nt { color: #008000; font-weight: bold } /* Name.Tag */
43
+ .codehilite .nv { color: #19177C } /* Name.Variable */
44
+ .codehilite .ow { color: #AA22FF; font-weight: bold } /* Operator.Word */
45
+ .codehilite .w { color: #bbbbbb } /* Text.Whitespace */
46
+ .codehilite .mb { color: #666666 } /* Literal.Number.Bin */
47
+ .codehilite .mf { color: #666666 } /* Literal.Number.Float */
48
+ .codehilite .mh { color: #666666 } /* Literal.Number.Hex */
49
+ .codehilite .mi { color: #666666 } /* Literal.Number.Integer */
50
+ .codehilite .mo { color: #666666 } /* Literal.Number.Oct */
51
+ .codehilite .sa { color: #BA2121 } /* Literal.String.Affix */
52
+ .codehilite .sb { color: #BA2121 } /* Literal.String.Backtick */
53
+ .codehilite .sc { color: #BA2121 } /* Literal.String.Char */
54
+ .codehilite .dl { color: #BA2121 } /* Literal.String.Delimiter */
55
+ .codehilite .sd { color: #BA2121; font-style: italic } /* Literal.String.Doc */
56
+ .codehilite .s2 { color: #BA2121 } /* Literal.String.Double */
57
+ .codehilite .se { color: #BB6622; font-weight: bold } /* Literal.String.Escape */
58
+ .codehilite .sh { color: #BA2121 } /* Literal.String.Heredoc */
59
+ .codehilite .si { color: #BB6688; font-weight: bold } /* Literal.String.Interpol */
60
+ .codehilite .sx { color: #008000 } /* Literal.String.Other */
61
+ .codehilite .sr { color: #BB6688 } /* Literal.String.Regex */
62
+ .codehilite .s1 { color: #BA2121 } /* Literal.String.Single */
63
+ .codehilite .ss { color: #19177C } /* Literal.String.Symbol */
64
+ .codehilite .bp { color: #008000 } /* Name.Builtin.Pseudo */
65
+ .codehilite .fm { color: #0000FF } /* Name.Function.Magic */
66
+ .codehilite .vc { color: #19177C } /* Name.Variable.Class */
67
+ .codehilite .vg { color: #19177C } /* Name.Variable.Global */
68
+ .codehilite .vi { color: #19177C } /* Name.Variable.Instance */
69
+ .codehilite .vm { color: #19177C } /* Name.Variable.Magic */
70
+ .codehilite .il { color: #666666 } /* Literal.Number.Integer.Long */
71
+
72
+ .preview_header {
73
+ font-size: 24px;
74
+ font-weight: 500;
75
+ text-align: center;
76
+ }
77
+
78
+ .bot_cover {
79
+ display: flex;
80
+ flex-direction: column;
81
+ justify-content: center;
82
+ align-items: center;
83
+ min-height: 300px;
84
+ border: 1px solid rgb(229, 231, 235);
85
+ padding: 20px 20px;
86
+ }
87
+
88
+ .bot_avatar {
89
+ width: 100px;
90
+ height: 100px;
91
+ border-radius: 50%;
92
+ overflow: hidden;
93
+ }
94
+
95
+ .bot_avatar img {
96
+ width: 100px;
97
+ height: 100px;
98
+ }
99
+
100
+ .bot_name {
101
+ font-size: 36px;
102
+ margin-top: 10px;
103
+ }
104
+
105
+ /* .bot_desp {
106
+ color: #ddd;
107
+ } */
108
+
109
+ .container {
110
+ /* flex-direction: row-reverse; */
111
+ }
112
+
113
+ .markdown-body .message {
114
+ white-space: pre-wrap;
115
+ }
116
+
117
+ .markdown-body details {
118
+ white-space: nowrap;
119
+ }
120
+ .markdown-body .bot details:not(:last-child) {
121
+ margin-bottom: 1px;
122
+ }
123
+ .markdown-body summary {
124
+ background-color: #4b5563;
125
+ color: #eee;
126
+ padding: 0 4px;
127
+ border-radius: 4px;
128
+ font-size: 0.9em;
129
+ }
assets/logo.jpeg ADDED
assets/user.jpeg ADDED
requirements.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ qwen-agent[gui,rag] == 0.0.10
web_ui.py ADDED
@@ -0,0 +1,368 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import pprint
3
+ import re
4
+ from typing import List, Optional, Union
5
+
6
+ from qwen_agent import Agent, MultiAgentHub
7
+ from qwen_agent.agents.user_agent import PENDING_USER_INPUT
8
+ from qwen_agent.gui.gradio_utils import format_cover_html
9
+ from qwen_agent.gui.utils import convert_fncall_to_text, convert_history_to_chatbot, get_avatar_image
10
+ from qwen_agent.llm.schema import CONTENT, FILE, IMAGE, NAME, ROLE, USER, Message
11
+ from qwen_agent.log import logger
12
+ from qwen_agent.utils.utils import print_traceback
13
+
14
+ class WebUI:
15
+ """A Common chatbot application for agent."""
16
+
17
+ def __init__(self, agent: Union[Agent, MultiAgentHub, List[Agent]], chatbot_config: Optional[dict] = None):
18
+ """
19
+ Initialization the chatbot.
20
+
21
+ Args:
22
+ agent: The agent or a list of agents,
23
+ supports various types of agents such as Assistant, GroupChat, Router, etc.
24
+ chatbot_config: The chatbot configuration.
25
+ Set the configuration as {'user.name': '', 'user.avatar': '', 'agent.avatar': '', 'input.placeholder': '', 'prompt.suggestions': []}.
26
+ """
27
+ chatbot_config = chatbot_config or {}
28
+
29
+ if isinstance(agent, MultiAgentHub):
30
+ self.agent_list = [agent for agent in agent.nonuser_agents]
31
+ self.agent_hub = agent
32
+ elif isinstance(agent, list):
33
+ self.agent_list = agent
34
+ self.agent_hub = None
35
+ else:
36
+ self.agent_list = [agent]
37
+ self.agent_hub = None
38
+
39
+ user_name = chatbot_config.get('user.name', 'user')
40
+ self.user_config = {
41
+ 'name': user_name,
42
+ 'avatar': chatbot_config.get(
43
+ 'user.avatar',
44
+ get_avatar_image(user_name),
45
+ ),
46
+ }
47
+
48
+ self.agent_config_list = [{
49
+ 'name': agent.name,
50
+ 'avatar': chatbot_config.get(
51
+ 'agent.avatar',
52
+ os.path.join(os.path.dirname(__file__), 'assets/logo.jpeg'),
53
+ ),
54
+ 'description': agent.description or "I'm a helpful assistant.",
55
+ } for agent in self.agent_list]
56
+
57
+ self.input_placeholder = chatbot_config.get('input.placeholder', '跟我聊聊吧~')
58
+ self.prompt_suggestions = chatbot_config.get('prompt.suggestions', [])
59
+ self.verbose = chatbot_config.get('verbose', False)
60
+
61
+ """
62
+ Run the chatbot.
63
+
64
+ Args:
65
+ messages: The chat history.
66
+ """
67
+
68
+ def run(self,
69
+ messages: List[Message] = None,
70
+ share: bool = False,
71
+ server_name: str = None,
72
+ server_port: int = None,
73
+ concurrency_limit: int = 10,
74
+ enable_mention: bool = False,
75
+ **kwargs):
76
+ self.run_kwargs = kwargs
77
+
78
+ from qwen_agent.gui.gradio import gr, mgr
79
+
80
+ customTheme = gr.themes.Default(
81
+ primary_hue=gr.themes.utils.colors.blue,
82
+ radius_size=gr.themes.utils.sizes.radius_none,
83
+ )
84
+
85
+ with gr.Blocks(
86
+ css=os.path.join(os.path.dirname(__file__), 'assets/appBot.css'),
87
+ theme=customTheme,
88
+ ) as demo:
89
+ history = gr.State([])
90
+
91
+ with gr.Row(elem_classes='container'):
92
+ with gr.Column(scale=4):
93
+ chatbot = mgr.Chatbot(value=convert_history_to_chatbot(messages=messages),
94
+ avatar_images=[
95
+ self.user_config,
96
+ self.agent_config_list,
97
+ ],
98
+ height=850,
99
+ avatar_image_width=80,
100
+ flushing=False,
101
+ show_copy_button=True,
102
+ latex_delimiters=[{
103
+ 'left': '\\(',
104
+ 'right': '\\)',
105
+ 'display': True
106
+ }, {
107
+ 'left': '\\begin{equation}',
108
+ 'right': '\\end{equation}',
109
+ 'display': True
110
+ }, {
111
+ 'left': '\\begin{align}',
112
+ 'right': '\\end{align}',
113
+ 'display': True
114
+ }, {
115
+ 'left': '\\begin{alignat}',
116
+ 'right': '\\end{alignat}',
117
+ 'display': True
118
+ }, {
119
+ 'left': '\\begin{gather}',
120
+ 'right': '\\end{gather}',
121
+ 'display': True
122
+ }, {
123
+ 'left': '\\begin{CD}',
124
+ 'right': '\\end{CD}',
125
+ 'display': True
126
+ }, {
127
+ 'left': '\\[',
128
+ 'right': '\\]',
129
+ 'display': True
130
+ }])
131
+
132
+ input = mgr.MultimodalInput(placeholder=self.input_placeholder, upload_button_props=dict(visible=False))
133
+
134
+ with gr.Column(scale=1):
135
+ if len(self.agent_list) > 1:
136
+ agent_selector = gr.Dropdown(
137
+ [(agent.name, i) for i, agent in enumerate(self.agent_list)],
138
+ label='Agents',
139
+ info='选择一个Agent',
140
+ value=0,
141
+ interactive=True,
142
+ )
143
+
144
+ agent_info_block = self._create_agent_info_block()
145
+
146
+ # agent_plugins_block = self._create_agent_plugins_block()
147
+
148
+ if self.prompt_suggestions:
149
+ gr.Examples(
150
+ label='推荐对话',
151
+ examples=self.prompt_suggestions,
152
+ inputs=[input],
153
+ )
154
+
155
+ if len(self.agent_list) > 1:
156
+ agent_selector.change(
157
+ fn=self.change_agent,
158
+ inputs=[agent_selector],
159
+ outputs=[agent_selector, agent_info_block, agent_plugins_block],
160
+ queue=False,
161
+ )
162
+
163
+ input_promise = input.submit(
164
+ fn=self.add_text,
165
+ inputs=[input, chatbot, history],
166
+ outputs=[input, chatbot, history],
167
+ queue=False,
168
+ )
169
+
170
+ if len(self.agent_list) > 1 and enable_mention:
171
+ input_promise = input_promise.then(
172
+ self.add_mention,
173
+ [chatbot, agent_selector],
174
+ [chatbot, agent_selector],
175
+ ).then(
176
+ self.agent_run,
177
+ [chatbot, history, agent_selector],
178
+ [chatbot, history, agent_selector],
179
+ )
180
+ else:
181
+ input_promise = input_promise.then(
182
+ self.agent_run,
183
+ [chatbot, history],
184
+ [chatbot, history],
185
+ )
186
+
187
+ input_promise.then(self.flushed, None, [input])
188
+
189
+ demo.load(None)
190
+
191
+ demo.queue(default_concurrency_limit=concurrency_limit).launch(share=share,
192
+ server_name=server_name,
193
+ server_port=server_port)
194
+
195
+ def change_agent(self, agent_selector):
196
+ yield agent_selector, self._create_agent_info_block(agent_selector), self._create_agent_plugins_block(
197
+ agent_selector)
198
+
199
+ def add_text(self, _input, _chatbot, _history):
200
+ from qwen_agent.gui.gradio import gr
201
+ if _input.text == "/clear":
202
+ _chatbot = []
203
+ _history.clear()
204
+ yield gr.update(interactive=False, value=""), _chatbot, _history
205
+ return
206
+
207
+ if _history:
208
+ gr.Warning("Only the most recent query is retained because multi-turn conversations are not currently supported.", duration=5)
209
+ _chatbot = []
210
+ _history.clear()
211
+
212
+ _history.append({
213
+ ROLE: USER,
214
+ CONTENT: [{
215
+ 'text': _input.text
216
+ }],
217
+ })
218
+
219
+ if self.user_config[NAME]:
220
+ _history[-1][NAME] = self.user_config[NAME]
221
+
222
+ if _input.files:
223
+ for file in _input.files:
224
+ if file.mime_type.startswith('image/'):
225
+ _history[-1][CONTENT].append({IMAGE: 'file://' + file.path})
226
+ else:
227
+ _history[-1][CONTENT].append({FILE: file.path})
228
+
229
+ _chatbot.append([_input, None])
230
+
231
+ yield gr.update(interactive=False, value=None), _chatbot, _history
232
+
233
+ def add_mention(self, _chatbot, _agent_selector):
234
+ if len(self.agent_list) == 1:
235
+ yield _chatbot, _agent_selector
236
+
237
+ query = _chatbot[-1][0].text
238
+ match = re.search(r'@\w+\b', query)
239
+ if match:
240
+ _agent_selector = self._get_agent_index_by_name(match.group()[1:])
241
+
242
+ agent_name = self.agent_list[_agent_selector].name
243
+
244
+ if ('@' + agent_name) not in query and self.agent_hub is None:
245
+ _chatbot[-1][0].text = '@' + agent_name + ' ' + query
246
+
247
+ yield _chatbot, _agent_selector
248
+
249
+ def agent_run(self, _chatbot, _history, _agent_selector=None):
250
+ if not _history:
251
+ if _agent_selector is not None:
252
+ yield _chatbot, _history, _agent_selector
253
+ else:
254
+ yield _chatbot, _history
255
+ return
256
+
257
+
258
+ if self.verbose:
259
+ logger.info('agent_run input:\n' + pprint.pformat(_history, indent=2))
260
+
261
+ num_input_bubbles = len(_chatbot) - 1
262
+ num_output_bubbles = 1
263
+ _chatbot[-1][1] = [None for _ in range(len(self.agent_list))]
264
+
265
+ agent_runner = self.agent_list[_agent_selector or 0]
266
+ if self.agent_hub:
267
+ agent_runner = self.agent_hub
268
+ responses = []
269
+ for responses in agent_runner.run(_history, **self.run_kwargs):
270
+ # usage = responses.usage
271
+ # responses = [Message(ASSISTANT, responses.output.choices[0].message.content)]
272
+
273
+ if not responses:
274
+ continue
275
+ if responses[-1][CONTENT] == PENDING_USER_INPUT:
276
+ logger.info('Interrupted. Waiting for user input!')
277
+ break
278
+
279
+ display_responses = convert_fncall_to_text(responses)
280
+ if not display_responses:
281
+ continue
282
+ if display_responses[-1][CONTENT] is None:
283
+ continue
284
+
285
+ while len(display_responses) > num_output_bubbles:
286
+ # Create a new chat bubble
287
+ _chatbot.append([None, None])
288
+ _chatbot[-1][1] = [None for _ in range(len(self.agent_list))]
289
+ num_output_bubbles += 1
290
+
291
+ assert num_output_bubbles == len(display_responses)
292
+ assert num_input_bubbles + num_output_bubbles == len(_chatbot)
293
+
294
+ for i, rsp in enumerate(display_responses):
295
+ agent_index = self._get_agent_index_by_name(rsp[NAME])
296
+ _chatbot[num_input_bubbles + i][1][agent_index] = rsp[CONTENT]
297
+
298
+ if len(self.agent_list) > 1:
299
+ _agent_selector = agent_index
300
+
301
+ if _agent_selector is not None:
302
+ yield _chatbot, _history, _agent_selector
303
+ else:
304
+ yield _chatbot, _history
305
+
306
+ if responses:
307
+ _history.extend([res for res in responses if res[CONTENT] != PENDING_USER_INPUT])
308
+
309
+ if _agent_selector is not None:
310
+ yield _chatbot, _history, _agent_selector
311
+ else:
312
+ yield _chatbot, _history
313
+
314
+ if self.verbose:
315
+ logger.info('agent_run response:\n' + pprint.pformat(responses, indent=2))
316
+
317
+ def flushed(self):
318
+ from qwen_agent.gui.gradio import gr
319
+
320
+ return gr.update(interactive=True)
321
+
322
+ def _get_agent_index_by_name(self, agent_name):
323
+ if agent_name is None:
324
+ return 0
325
+
326
+ try:
327
+ agent_name = agent_name.strip()
328
+ for i, agent in enumerate(self.agent_list):
329
+ if agent.name == agent_name:
330
+ return i
331
+ return 0
332
+ except Exception:
333
+ print_traceback()
334
+ return 0
335
+
336
+ def _create_agent_info_block(self, agent_index=0):
337
+ from qwen_agent.gui.gradio import gr
338
+
339
+ agent_config_interactive = self.agent_config_list[agent_index]
340
+
341
+ return gr.HTML(
342
+ format_cover_html(
343
+ bot_name=agent_config_interactive['name'],
344
+ bot_description=agent_config_interactive['description'],
345
+ bot_avatar=agent_config_interactive['avatar'],
346
+ ))
347
+
348
+ def _create_agent_plugins_block(self, agent_index=0):
349
+ from qwen_agent.gui.gradio import gr
350
+
351
+ agent_interactive = self.agent_list[agent_index]
352
+
353
+ if agent_interactive.function_map:
354
+ capabilities = [key for key in agent_interactive.function_map.keys()]
355
+ return gr.CheckboxGroup(
356
+ label='插件',
357
+ value=capabilities,
358
+ choices=capabilities,
359
+ interactive=False,
360
+ )
361
+
362
+ else:
363
+ return gr.CheckboxGroup(
364
+ label='插件',
365
+ value=[],
366
+ choices=[],
367
+ interactive=False,
368
+ )