-
Notifications
You must be signed in to change notification settings - Fork 558
Expand file tree
/
Copy pathwebui.py
More file actions
440 lines (380 loc) · 15.5 KB
/
Copy pathwebui.py
File metadata and controls
440 lines (380 loc) · 15.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
import sys
import time
import uuid
from datetime import datetime
from pathlib import Path
from typing import Any
import gradio as gr
from deeppresenter.main import AgentLoop
from deeppresenter.utils.config import DeepPresenterConfig
from deeppresenter.utils.constants import WORKSPACE_BASE
from deeppresenter.utils.log import create_logger
from deeppresenter.utils.typings import ChatMessage, ConvertType, InputRequest, Role
from pptagent import PPTAgentServer
config = DeepPresenterConfig.load_from_file()
timestamp = datetime.now().strftime("%Y-%m-%dT%H:%M:%S")
logger = create_logger(
"DeepPresenterUI",
log_file=str(Path.home() / f".cache/deeppresenter/logs/{timestamp}.log"),
)
ROLE_EMOJI = {
Role.SYSTEM: "⚙️",
Role.USER: "👤",
Role.ASSISTANT: "🤖",
Role.TOOL: "📝",
}
CONVERT_MAPPING = {
"自由生成 (freeform)": ConvertType.DEEPPRESENTER,
"模版 (templates)": ConvertType.PPTAGENT,
}
gradio_css = """
.center-title {
text-align: center;
margin-bottom: 10px;
}
.center-subtitle {
text-align: center;
margin-bottom: 20px;
opacity: 0.8;
}
.token-display {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
line-height: 1.6;
padding: 10px;
}
.token-display h2 {
color: #2c3e50;
font-size: 1.2em;
margin-bottom: 15px;
}
.gradio-container {
max-width: 100% !important;
overflow-x: hidden !important;
}
.file-container .wrap {
min-height: auto !important;
height: auto !important;
}
.file-container .upload-container {
display: none !important; /* 隐藏大的拖拽区域 */
}
.file-container .file-list {
min-height: 40px !important;
padding: 8px !important;
}
footer {
display: none !important;
}
.gradio-container .footer {
display: none !important;
}
body {
margin: 5px !important;
padding: 0 !important;
}
.html-container {
padding: 0 !important;
}
"""
class UserSession:
"""简化的用户会话类"""
def __init__(self):
self.loop = AgentLoop(
config,
session_id=f"{datetime.now().strftime('%Y%m%d')}/{uuid.uuid4().hex[:8]}",
)
self.created_time = time.time()
class ChatDemo:
def create_interface(self):
"""创建 Gradio 界面"""
with gr.Blocks(
title="DeepPresenter",
theme=gr.themes.Soft(),
css=gradio_css,
) as demo:
gr.Markdown(
"# DeepPresenter",
elem_classes=["center-title"],
)
with gr.Row():
with gr.Column():
chatbot = gr.Chatbot(
value=[],
height=300,
show_label=False,
type="messages",
render_markdown=True,
elem_classes=["chat-container"],
)
with gr.Accordion("📊 Token 使用统计", open=False):
token_display = gr.Markdown(
value="暂无数据",
elem_classes=["token-display"],
)
with gr.Row():
pages_dd = gr.Dropdown(
label="幻灯片页数 (#pages)",
choices=["auto"] + [str(i) for i in range(1, 31)],
value="auto",
scale=1,
)
convert_type_dd = gr.Dropdown(
label="输出类型 (output type)",
choices=list(CONVERT_MAPPING),
value=list(CONVERT_MAPPING)[0],
scale=1,
)
template_choices = PPTAgentServer.list_templates()
template_dd = gr.Dropdown(
label="选择模板 (template)",
choices=template_choices + ["auto"],
value="auto",
scale=2,
visible=False,
)
def _toggle_template_visibility(v: str) -> dict:
return gr.update(visible=("模版" in v))
convert_type_dd.change(
_toggle_template_visibility,
inputs=[convert_type_dd],
outputs=[template_dd],
)
with gr.Row():
msg_input = gr.Textbox(
placeholder="You instruction here",
scale=4,
container=False,
)
send_btn = gr.Button("发送", scale=1, variant="primary")
download_btn = gr.DownloadButton(
"📥 下载文件",
scale=1,
variant="secondary",
)
attachments_input = gr.File(
file_count="multiple",
type="filepath",
elem_classes=["file-container"],
)
def collect_token_stats(loop: AgentLoop) -> str:
"""收集所有 agents 的 token 统计并生成显示文本"""
all_agent_costs = {}
if hasattr(loop, "research_agent") and loop.research_agent:
all_agent_costs["Research Agent"] = {
"prompt": getattr(loop.research_agent.cost, "prompt", 0),
"completion": getattr(
loop.research_agent.cost, "completion", 0
),
"total": getattr(loop.research_agent.cost, "total", 0),
"model": loop.config.research_agent.model_name,
}
if hasattr(loop, "designagent") and loop.designagent:
all_agent_costs["Design Agent"] = {
"prompt": getattr(loop.designagent.cost, "prompt", 0),
"completion": getattr(loop.designagent.cost, "completion", 0),
"total": getattr(loop.designagent.cost, "total", 0),
"model": loop.config.design_agent.model_name,
}
elif hasattr(loop, "pptagent") and loop.pptagent:
all_agent_costs["PPT Agent"] = {
"prompt": getattr(loop.pptagent.cost, "prompt", 0),
"completion": getattr(loop.pptagent.cost, "completion", 0),
"total": getattr(loop.pptagent.cost, "total", 0),
"model": loop.config.research_agent.model_name,
}
token_lines = ["## Token 使用统计\n"]
total_prompt = 0
total_completion = 0
total_all = 0
for agent_name, cost_info in all_agent_costs.items():
prompt = cost_info.get("prompt", 0)
completion = cost_info.get("completion", 0)
total = cost_info.get("total", 0)
model = cost_info.get("model", "N/A")
total_prompt += prompt
total_completion += completion
total_all += total
token_lines.append(
f"**{agent_name}** (Model: `{model}`) \n"
f"- 输入: {prompt:,} tokens \n"
f"- 输出: {completion:,} tokens \n"
f"- 小计: {total:,} tokens \n"
)
if total_all > 0:
token_lines.append("\n---\n")
token_lines.append(
f"**总计** \n"
f"- 输入: {total_prompt:,} tokens \n"
f"- 输出: {total_completion:,} tokens \n"
f"- **总计: {total_all:,} tokens**"
)
return "\n".join(token_lines) if total_all > 0 else "暂无 token 数据"
def format_chat_message(msg: ChatMessage) -> list[dict[str, Any]]:
parts: list[dict[str, Any]] = []
role_msg = f"{ROLE_EMOJI[msg.role]} **{str(msg.role).title()} Message**"
if msg.reasoning and msg.reasoning.strip():
parts.append(
{
"role": "assistant",
"content": msg.reasoning.strip(),
"metadata": {
"title": "🧠 Reasoning",
"status": "done",
},
}
)
if msg.text and msg.text.strip():
if msg.role == Role.TOOL:
content = "```json\n" + msg.text.replace("\\n", "\n") + "\n```"
else:
content = msg.text
parts.append(
{
"role": "assistant",
"content": f"{role_msg}\n\n{content}",
}
)
if msg.tool_calls:
for tool_call in msg.tool_calls:
tool_msg = f"{ROLE_EMOJI.get(msg.role, '💬')} **Tool Call: {tool_call.function.name}**"
content_parts = [tool_msg]
if hasattr(tool_call.function, "arguments"):
args_str = tool_call.function.arguments
args_msg = f"```json\n{args_str}\n```"
content_parts.append(args_msg)
parts.append(
{
"role": "assistant",
"content": "\n\n".join(content_parts),
}
)
return parts
async def send_message(
message,
history,
attachments,
convert_type_value,
template_value,
num_pages_value,
request: gr.Request,
):
user_session = UserSession()
has_message = bool(message and message.strip())
has_attachments = bool(attachments)
if not has_message and not has_attachments:
yield (
history,
message,
gr.update(value=None),
gr.update(),
gr.update(),
)
return
history.append(
{"role": "user", "content": message or "请根据上传的附件制作 PPT"}
)
response_messages: list[dict[str, Any]] = []
def build_history() -> list[dict[str, Any]]:
return history + response_messages
loop = user_session.loop
selected_convert_type = CONVERT_MAPPING[convert_type_value]
selected_num_pages = (
None if num_pages_value == "auto" else int(num_pages_value)
)
if template_value == "auto":
template_value = None
async for yield_msg in loop.run(
InputRequest(
instruction=message or "请根据上传的附件制作 PPT",
template=template_value,
attachments=attachments or [],
num_pages=str(selected_num_pages),
convert_type=selected_convert_type,
)
):
if isinstance(yield_msg, (str, Path)):
file_content = "📄 幻灯片生成完成,点击下方按钮下载文件"
response_messages.append(
{"role": "assistant", "content": file_content}
)
token_text = collect_token_stats(loop)
yield (
build_history(),
"",
gr.update(value=None),
gr.update(value=str(yield_msg)),
gr.update(value=token_text),
)
elif isinstance(yield_msg, ChatMessage):
response_messages.extend(format_chat_message(yield_msg))
token_text = collect_token_stats(loop)
yield (
build_history(),
message,
gr.update(value=None),
gr.update(),
gr.update(value=token_text),
)
else:
raise ValueError(
f"Unsupported response message type: {type(yield_msg)}"
)
msg_input.submit(
send_message,
inputs=[
msg_input,
chatbot,
attachments_input,
convert_type_dd,
template_dd,
pages_dd,
],
outputs=[
chatbot,
msg_input,
attachments_input,
download_btn,
token_display,
],
concurrency_limit=None,
)
send_btn.click(
send_message,
inputs=[
msg_input,
chatbot,
attachments_input,
convert_type_dd,
template_dd,
pages_dd,
],
outputs=[
chatbot,
msg_input,
attachments_input,
download_btn,
token_display,
],
concurrency_limit=None,
)
return demo
if __name__ == "__main__":
import warnings
chat_demo = ChatDemo()
demo = chat_demo.create_interface()
warnings.filterwarnings(
"ignore", category=DeprecationWarning, module="websockets.legacy"
)
warnings.filterwarnings(
"ignore", category=DeprecationWarning, module="uvicorn.protocols.websockets"
)
serve_url = "localhost" if len(sys.argv) == 1 else sys.argv[1]
print("Please visit http://localhost:7861")
demo.launch(
debug=True,
server_name=serve_url,
server_port=7861,
share=False,
max_threads=16,
allowed_paths=[WORKSPACE_BASE],
)