Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -176,22 +176,30 @@ def export_history_to_markdown(history):
|
|
| 176 |
def extract_gdl_and_narrative(content):
|
| 177 |
"""提取 GDL 和自然语言部分"""
|
| 178 |
# 查找 GDL 和自然语言部分的起始位置
|
| 179 |
-
gdl_start = content.find("GDL描述")
|
| 180 |
-
narrative_start = content.find("自然语言规则说明")
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 189 |
|
| 190 |
def save_gdl_and_narrative(gdl_content, narrative_content):
|
| 191 |
"""保存 GDL 和自然语言内容到文件"""
|
| 192 |
# 定义文件存储路径
|
| 193 |
-
|
| 194 |
-
|
|
|
|
|
|
|
|
|
|
| 195 |
|
| 196 |
# 保存 GDL
|
| 197 |
with open(gdl_file_path, "w", encoding="utf-8") as f:
|
|
@@ -279,8 +287,8 @@ with gr.Blocks(
|
|
| 279 |
)
|
| 280 |
# 用 State 保存 messages 历史
|
| 281 |
chat_state = gr.State([]) # list[dict]: [{"role":"user","content":...}, {"role":"assistant","content":...}]
|
| 282 |
-
gdl_file_path = gr.State(
|
| 283 |
-
narrative_file_path = gr.State(
|
| 284 |
|
| 285 |
user_input = gr.Textbox(
|
| 286 |
placeholder="例如:设计一个适合3-5人的派对风格扑克游戏(请写清目标人群/时长/创新点)…",
|
|
@@ -300,17 +308,17 @@ with gr.Blocks(
|
|
| 300 |
user_text = (user_text or "").strip()
|
| 301 |
if not user_text:
|
| 302 |
# 不提交空消息:输出不变
|
| 303 |
-
yield
|
| 304 |
return
|
| 305 |
|
| 306 |
# 立即显示“用户消息”
|
| 307 |
history = list(history_msgs or [])
|
| 308 |
history.append({"role": "user", "content": user_text})
|
| 309 |
-
yield history, "", history
|
| 310 |
|
| 311 |
# 添加空的助手气泡,用于逐步填充
|
| 312 |
history.append({"role": "assistant", "content": ""})
|
| 313 |
-
yield history, "", history
|
| 314 |
|
| 315 |
# 流式生成内容
|
| 316 |
tuples_hist = _messages_to_tuples(history)
|
|
@@ -321,10 +329,10 @@ with gr.Blocks(
|
|
| 321 |
if not piece:
|
| 322 |
continue
|
| 323 |
history[-1]["content"] += str(piece)
|
| 324 |
-
yield history, "", history
|
| 325 |
except Exception as e:
|
| 326 |
history[-1]["content"] += f"\n(流式出错){type(e).__name__}: {e}"
|
| 327 |
-
yield history, "", history
|
| 328 |
else:
|
| 329 |
try:
|
| 330 |
full = design_poker_game(user_text, tuples_hist, files, custom_prompt, mode)
|
|
@@ -332,30 +340,34 @@ with gr.Blocks(
|
|
| 332 |
full = f"(出错){type(e).__name__}: {e}"
|
| 333 |
for piece in _chunk_fake_stream(str(full), step=40):
|
| 334 |
history[-1]["content"] += piece
|
| 335 |
-
yield history, "", history
|
| 336 |
|
| 337 |
# 提取 GDL 和自然语言描述并保存
|
| 338 |
-
|
| 339 |
-
|
| 340 |
-
|
| 341 |
-
|
| 342 |
-
|
| 343 |
-
|
| 344 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 345 |
|
| 346 |
# 绑定:回车提交(Enter=提交;Shift+Enter=换行由浏览器处理)
|
| 347 |
user_input.submit(
|
| 348 |
fn=on_submit,
|
| 349 |
inputs=[user_input, chat_state, file_uploader, custom_prompt_box, prompt_mode],
|
| 350 |
-
outputs=[chatbot, user_input, chat_state],
|
| 351 |
preprocess=True,
|
| 352 |
)
|
| 353 |
|
| 354 |
-
# 绑定:点击
|
| 355 |
send_btn.click(
|
| 356 |
fn=on_submit,
|
| 357 |
inputs=[user_input, chat_state, file_uploader, custom_prompt_box, prompt_mode],
|
| 358 |
-
outputs=[chatbot, user_input, chat_state],
|
| 359 |
preprocess=True,
|
| 360 |
)
|
| 361 |
|
|
@@ -369,12 +381,12 @@ with gr.Blocks(
|
|
| 369 |
clear_dialog_btn = gr.Button("清空对话", variant="secondary")
|
| 370 |
|
| 371 |
def _clear_chat():
|
| 372 |
-
return [], "", []
|
| 373 |
|
| 374 |
clear_dialog_btn.click(
|
| 375 |
fn=_clear_chat,
|
| 376 |
inputs=None,
|
| 377 |
-
outputs=[chatbot, user_input, chat_state],
|
| 378 |
)
|
| 379 |
|
| 380 |
# ==================== 下载按钮部分 ====================
|
|
@@ -386,16 +398,27 @@ with gr.Blocks(
|
|
| 386 |
download_gdl_file = gr.File(label="GDL 文件", interactive=False) # 文件下载区域
|
| 387 |
download_narrative_file = gr.File(label="自然语言文件", interactive=False) # 文件下载区域
|
| 388 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 389 |
# 绑定下载按钮与文件路径
|
| 390 |
download_gdl_btn.click(
|
| 391 |
-
fn=
|
| 392 |
-
inputs=[],
|
| 393 |
outputs=[download_gdl_file]
|
| 394 |
)
|
| 395 |
|
| 396 |
download_narrative_btn.click(
|
| 397 |
-
fn=
|
| 398 |
-
inputs=[],
|
| 399 |
outputs=[download_narrative_file]
|
| 400 |
)
|
| 401 |
|
|
|
|
| 176 |
def extract_gdl_and_narrative(content):
|
| 177 |
"""提取 GDL 和自然语言部分"""
|
| 178 |
# 查找 GDL 和自然语言部分的起始位置
|
| 179 |
+
gdl_start = content.find("GDL描述")
|
| 180 |
+
narrative_start = content.find("自然语言规则说明")
|
| 181 |
+
|
| 182 |
+
if gdl_start != -1 and narrative_start != -1:
|
| 183 |
+
# 获取 GDL 部分
|
| 184 |
+
gdl_end = narrative_start
|
| 185 |
+
gdl_content = content[gdl_start:gdl_end].strip()
|
| 186 |
+
|
| 187 |
+
# 获取自然语言部分
|
| 188 |
+
narrative_content = content[narrative_start:].strip()
|
| 189 |
+
|
| 190 |
+
return gdl_content, narrative_content
|
| 191 |
+
else:
|
| 192 |
+
# 如果找不到标记,返回空字符串
|
| 193 |
+
return "", ""
|
| 194 |
|
| 195 |
def save_gdl_and_narrative(gdl_content, narrative_content):
|
| 196 |
"""保存 GDL 和自然语言内容到文件"""
|
| 197 |
# 定义文件存储路径
|
| 198 |
+
export_dir = os.path.join("exports")
|
| 199 |
+
os.makedirs(export_dir, exist_ok=True)
|
| 200 |
+
|
| 201 |
+
gdl_file_path = os.path.join(export_dir, "gdl_output.txt")
|
| 202 |
+
narrative_file_path = os.path.join(export_dir, "narrative_output.txt")
|
| 203 |
|
| 204 |
# 保存 GDL
|
| 205 |
with open(gdl_file_path, "w", encoding="utf-8") as f:
|
|
|
|
| 287 |
)
|
| 288 |
# 用 State 保存 messages 历史
|
| 289 |
chat_state = gr.State([]) # list[dict]: [{"role":"user","content":...}, {"role":"assistant","content":...}]
|
| 290 |
+
gdl_file_path = gr.State("")
|
| 291 |
+
narrative_file_path = gr.State("")
|
| 292 |
|
| 293 |
user_input = gr.Textbox(
|
| 294 |
placeholder="例如:设计一个适合3-5人的派对风格扑克游戏(请写清目标人群/时长/创新点)…",
|
|
|
|
| 308 |
user_text = (user_text or "").strip()
|
| 309 |
if not user_text:
|
| 310 |
# 不提交空消息:输出不变
|
| 311 |
+
yield history_msgs, "", history_msgs, "", "" # 🟩【修改】输出数量改为5个
|
| 312 |
return
|
| 313 |
|
| 314 |
# 立即显示“用户消息”
|
| 315 |
history = list(history_msgs or [])
|
| 316 |
history.append({"role": "user", "content": user_text})
|
| 317 |
+
yield history, "", history, "", ""
|
| 318 |
|
| 319 |
# 添加空的助手气泡,用于逐步填充
|
| 320 |
history.append({"role": "assistant", "content": ""})
|
| 321 |
+
yield history, "", history, "", ""
|
| 322 |
|
| 323 |
# 流式生成内容
|
| 324 |
tuples_hist = _messages_to_tuples(history)
|
|
|
|
| 329 |
if not piece:
|
| 330 |
continue
|
| 331 |
history[-1]["content"] += str(piece)
|
| 332 |
+
yield history, "", history, "", ""
|
| 333 |
except Exception as e:
|
| 334 |
history[-1]["content"] += f"\n(流式出错){type(e).__name__}: {e}"
|
| 335 |
+
yield history, "", history, "", ""
|
| 336 |
else:
|
| 337 |
try:
|
| 338 |
full = design_poker_game(user_text, tuples_hist, files, custom_prompt, mode)
|
|
|
|
| 340 |
full = f"(出错){type(e).__name__}: {e}"
|
| 341 |
for piece in _chunk_fake_stream(str(full), step=40):
|
| 342 |
history[-1]["content"] += piece
|
| 343 |
+
yield history, "", history, "", ""
|
| 344 |
|
| 345 |
# 提取 GDL 和自然语言描述并保存
|
| 346 |
+
try:
|
| 347 |
+
gdl_content, narrative_content = extract_gdl_and_narrative(history[-1]["content"])
|
| 348 |
+
|
| 349 |
+
# 保存 GDL 和自然语言文件
|
| 350 |
+
gdl_path, narrative_path = save_gdl_and_narrative(gdl_content, narrative_content)
|
| 351 |
+
|
| 352 |
+
# 返回文件路径,以便下载
|
| 353 |
+
yield history, "", history, gdl_path, narrative_path # 🟩【修改】返回文件路径
|
| 354 |
+
except Exception as e:
|
| 355 |
+
print(f"保存GDL和自然语言文件时出错: {e}")
|
| 356 |
+
yield history, "", history, "", "" # 🟩【修改】返回空路径
|
| 357 |
|
| 358 |
# 绑定:回车提交(Enter=提交;Shift+Enter=换行由浏览器处理)
|
| 359 |
user_input.submit(
|
| 360 |
fn=on_submit,
|
| 361 |
inputs=[user_input, chat_state, file_uploader, custom_prompt_box, prompt_mode],
|
| 362 |
+
outputs=[chatbot, user_input, chat_state, gdl_file_path, narrative_file_path], # 🟩【修改】输出改为5个
|
| 363 |
preprocess=True,
|
| 364 |
)
|
| 365 |
|
| 366 |
+
# 绑定:点击"发送"
|
| 367 |
send_btn.click(
|
| 368 |
fn=on_submit,
|
| 369 |
inputs=[user_input, chat_state, file_uploader, custom_prompt_box, prompt_mode],
|
| 370 |
+
outputs=[chatbot, user_input, chat_state, gdl_file_path, narrative_file_path], # 🟩【修改】输出改为5个
|
| 371 |
preprocess=True,
|
| 372 |
)
|
| 373 |
|
|
|
|
| 381 |
clear_dialog_btn = gr.Button("清空对话", variant="secondary")
|
| 382 |
|
| 383 |
def _clear_chat():
|
| 384 |
+
return [], "", [], "", ""
|
| 385 |
|
| 386 |
clear_dialog_btn.click(
|
| 387 |
fn=_clear_chat,
|
| 388 |
inputs=None,
|
| 389 |
+
outputs=[chatbot, user_input, chat_state, gdl_file_path, narrative_file_path], # 🟩【修改】输出改为5个
|
| 390 |
)
|
| 391 |
|
| 392 |
# ==================== 下载按钮部分 ====================
|
|
|
|
| 398 |
download_gdl_file = gr.File(label="GDL 文件", interactive=False) # 文件下载区域
|
| 399 |
download_narrative_file = gr.File(label="自然语言文件", interactive=False) # 文件下载区域
|
| 400 |
|
| 401 |
+
# 🟩【修改】绑定下载按钮与文件路径 - 修复版本
|
| 402 |
+
def get_gdl_file(gdl_path):
|
| 403 |
+
if gdl_path and os.path.exists(gdl_path):
|
| 404 |
+
return gdl_path
|
| 405 |
+
return None
|
| 406 |
+
|
| 407 |
+
def get_narrative_file(narrative_path):
|
| 408 |
+
if narrative_path and os.path.exists(narrative_path):
|
| 409 |
+
return narrative_path
|
| 410 |
+
return None
|
| 411 |
+
|
| 412 |
# 绑定下载按钮与文件路径
|
| 413 |
download_gdl_btn.click(
|
| 414 |
+
fn=get_gdl_file,
|
| 415 |
+
inputs=[gdl_file_path], # 🟩【修改】接收State中的路径
|
| 416 |
outputs=[download_gdl_file]
|
| 417 |
)
|
| 418 |
|
| 419 |
download_narrative_btn.click(
|
| 420 |
+
fn=get_narrative_file,
|
| 421 |
+
inputs=[narrative_file_path], # 🟩【修改】接收State中的路径
|
| 422 |
outputs=[download_narrative_file]
|
| 423 |
)
|
| 424 |
|