forked from icip-cas/PPTAgent
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebui.py
More file actions
327 lines (281 loc) · 10.9 KB
/
Copy pathwebui.py
File metadata and controls
327 lines (281 loc) · 10.9 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
import sys
import time
import uuid
from datetime import datetime
from pathlib import Path
import gradio as gr
from deeppresenter.main import AgentLoop
from deeppresenter.utils.constants import WORKSPACE_BASE
from deeppresenter.utils.log import create_logger
from deeppresenter.utils.typings import ChatMessage, ConvertType, InputRequest, Role
from platformdirs import user_cache_dir
from pptagent import PPTAgentServer
timestamp = datetime.now().strftime("%Y-%m-%dT%H:%M:%S")
logger = create_logger(
"DeepPresenterUI",
log_file=user_cache_dir("deeppresenter") + f"/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;
}
.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(
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.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):
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"],
)
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(),
)
return
history.append(
{"role": "user", "content": message or "请根据上传的附件制作 PPT"}
)
aggregated_parts: list[str] = []
history.append({"role": "assistant", "content": ""})
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 = "📄 幻灯片生成完成,点击下方按钮下载文件"
aggregated_parts.append(file_content)
aggregated_text = "\n\n".join(aggregated_parts).strip()
history[-1]["content"] = aggregated_text
yield (
history,
"",
gr.update(value=None),
gr.update(value=str(yield_msg)),
)
elif isinstance(yield_msg, ChatMessage):
role_msg = f"{ROLE_EMOJI[yield_msg.role]} **{str(yield_msg.role).title()} Message**"
if yield_msg.text:
aggregated_parts.append(role_msg)
if yield_msg.text is not None and yield_msg.text.strip():
if yield_msg.role == Role.TOOL:
aggregated_parts.append(
"```json\n"
+ yield_msg.text.replace("\\n", "\n")
+ "\n```"
)
else:
aggregated_parts.append(yield_msg.text)
if yield_msg.tool_calls:
for tool_call in yield_msg.tool_calls:
tool_msg = f"{ROLE_EMOJI.get(yield_msg.role, '💬')} **Tool Call: {tool_call.function.name}**"
aggregated_parts.append(tool_msg)
if hasattr(tool_call.function, "arguments"):
args_str = tool_call.function.arguments
args_msg = f"```json\n{args_str}\n```"
aggregated_parts.append(args_msg)
aggregated_text = "\n\n".join(aggregated_parts).strip()
history[-1]["content"] = aggregated_text
yield (
history,
message,
gr.update(value=None),
gr.update(),
)
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,
],
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,
],
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]
demo.launch(
debug=True,
server_name=serve_url,
server_port=7861,
share=False,
max_threads=16,
allowed_paths=[WORKSPACE_BASE],
)