Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -64,8 +64,8 @@ else:
|
|
| 64 |
ROOT_BASE = os.getcwd()
|
| 65 |
|
| 66 |
TASKS_ROOT_DIR = os.path.join(ROOT_BASE, "nihaisha_tasks_v14_final")
|
| 67 |
-
CORPUS_ROOT_DIR = os.path.join(ROOT_BASE, "nihaisha_corpus_lib")
|
| 68 |
-
PERSONAS_FILE = os.path.join(ROOT_BASE, "personas_config.json")
|
| 69 |
|
| 70 |
os.makedirs(TASKS_ROOT_DIR, exist_ok=True)
|
| 71 |
os.makedirs(CORPUS_ROOT_DIR, exist_ok=True)
|
|
@@ -219,10 +219,11 @@ class TaskManager:
|
|
| 219 |
def create_task(self, task_name_prefix="task", custom_label=None):
|
| 220 |
timestamp = datetime.now().strftime('%Y%m%d_%H%M%S')
|
| 221 |
|
| 222 |
-
# 任务文件夹命名逻辑
|
| 223 |
if custom_label:
|
| 224 |
-
safe_label = re.sub(r'[\\/*?:"<>|]', "", custom_label)[:
|
| 225 |
-
|
|
|
|
| 226 |
else:
|
| 227 |
task_id = f"{task_name_prefix}_{timestamp}"
|
| 228 |
|
|
@@ -422,22 +423,26 @@ def process_single_topic(idx, topic, persona_name, custom_sys, custom_user, ref_
|
|
| 422 |
|
| 423 |
generated_files = []
|
| 424 |
|
|
|
|
| 425 |
meta_content = f"【标题】\n{title_part}\n\n【YouTube视频描述】\n{desc_part}"
|
| 426 |
-
meta_path = os.path.join(res_dir, f"{idx+1}_{safe_filename}
|
| 427 |
with open(meta_path, "w", encoding="utf-8") as f:
|
| 428 |
f.write(meta_content)
|
| 429 |
generated_files.append(meta_path)
|
| 430 |
|
| 431 |
-
|
|
|
|
| 432 |
with open(tts_path, "w", encoding="utf-8") as f:
|
| 433 |
f.write(cleaned_tts_body)
|
| 434 |
generated_files.append(tts_path)
|
| 435 |
|
|
|
|
| 436 |
debug_path = os.path.join(res_dir, f"{idx+1}_debug.log")
|
| 437 |
with open(debug_path, "w", encoding="utf-8") as f:
|
| 438 |
f.write(f"PROMPT:\n{final_user_prompt}")
|
| 439 |
|
| 440 |
logger_func(f"✅ 完成!正文约 {len(cleaned_tts_body)} 字")
|
|
|
|
| 441 |
return generated_files, title_part.split('\n')[0]
|
| 442 |
|
| 443 |
def worker(task_id, topics, persona_name, custom_sys, custom_user, selected_refs, manual_topic_name):
|
|
@@ -481,8 +486,9 @@ def worker(task_id, topics, persona_name, custom_sys, custom_user, selected_refs
|
|
| 481 |
traceback.print_exc()
|
| 482 |
|
| 483 |
if all_files_to_zip:
|
|
|
|
| 484 |
if manual_topic_name:
|
| 485 |
-
safe_zip_name = re.sub(r'[\\/*?:"<>|]', "", manual_topic_name)[:
|
| 486 |
zip_filename = f"{safe_zip_name}.zip"
|
| 487 |
elif first_auto_title:
|
| 488 |
safe_zip_name = re.sub(r'[\\/*?:"<>|]', "", first_auto_title)[:30]
|
|
@@ -491,10 +497,14 @@ def worker(task_id, topics, persona_name, custom_sys, custom_user, selected_refs
|
|
| 491 |
zip_filename = f"Batch_{len(all_files_to_zip)}_Files.zip"
|
| 492 |
|
| 493 |
zip_path = os.path.join(task_dir, zip_filename)
|
|
|
|
| 494 |
with zipfile.ZipFile(zip_path, 'w') as z:
|
| 495 |
for f in all_files_to_zip: z.write(f, os.path.basename(f))
|
| 496 |
|
| 497 |
-
|
|
|
|
|
|
|
|
|
|
| 498 |
log(f"🎉 任务结束,ZIP已打包: {zip_filename}")
|
| 499 |
else:
|
| 500 |
task_manager.update_status(task_id, "failed")
|
|
@@ -530,10 +540,12 @@ def start_execution(topic_text, persona_name, p_sys, p_user, selected_refs):
|
|
| 530 |
return "❌ 既无主题也无素材,无法生成", None
|
| 531 |
|
| 532 |
topics = topic_text.split('\n')
|
|
|
|
| 533 |
manual_topic_name = topics[0].strip() if topic_text.strip() else None
|
| 534 |
|
| 535 |
if not topic_text.strip(): topics = [""]
|
| 536 |
|
|
|
|
| 537 |
tid, _ = task_manager.create_task(custom_label=manual_topic_name)
|
| 538 |
|
| 539 |
threading.Thread(target=worker, args=(tid, topics, persona_name, p_sys, p_user, selected_refs, manual_topic_name)).start()
|
|
|
|
| 64 |
ROOT_BASE = os.getcwd()
|
| 65 |
|
| 66 |
TASKS_ROOT_DIR = os.path.join(ROOT_BASE, "nihaisha_tasks_v14_final")
|
| 67 |
+
CORPUS_ROOT_DIR = os.path.join(ROOT_BASE, "nihaisha_corpus_lib")
|
| 68 |
+
PERSONAS_FILE = os.path.join(ROOT_BASE, "personas_config.json")
|
| 69 |
|
| 70 |
os.makedirs(TASKS_ROOT_DIR, exist_ok=True)
|
| 71 |
os.makedirs(CORPUS_ROOT_DIR, exist_ok=True)
|
|
|
|
| 219 |
def create_task(self, task_name_prefix="task", custom_label=None):
|
| 220 |
timestamp = datetime.now().strftime('%Y%m%d_%H%M%S')
|
| 221 |
|
| 222 |
+
# 任务文件夹命名逻辑 - 【修复】如果输入了主题,直接作为ID前缀
|
| 223 |
if custom_label:
|
| 224 |
+
safe_label = re.sub(r'[\\/*?:"<>|]', "", custom_label)[:30]
|
| 225 |
+
# 将自定义标签放在前面,方便查找
|
| 226 |
+
task_id = f"{safe_label}_{timestamp}"
|
| 227 |
else:
|
| 228 |
task_id = f"{task_name_prefix}_{timestamp}"
|
| 229 |
|
|
|
|
| 423 |
|
| 424 |
generated_files = []
|
| 425 |
|
| 426 |
+
# 【修复】文件1:标题+视频详情 (Meta)
|
| 427 |
meta_content = f"【标题】\n{title_part}\n\n【YouTube视频描述】\n{desc_part}"
|
| 428 |
+
meta_path = os.path.join(res_dir, f"{idx+1}_{safe_filename}_标题与详情.txt")
|
| 429 |
with open(meta_path, "w", encoding="utf-8") as f:
|
| 430 |
f.write(meta_content)
|
| 431 |
generated_files.append(meta_path)
|
| 432 |
|
| 433 |
+
# 【修复】文件2:正文 (TTS)
|
| 434 |
+
tts_path = os.path.join(res_dir, f"{idx+1}_{safe_filename}_正文(TTS).txt")
|
| 435 |
with open(tts_path, "w", encoding="utf-8") as f:
|
| 436 |
f.write(cleaned_tts_body)
|
| 437 |
generated_files.append(tts_path)
|
| 438 |
|
| 439 |
+
# Debug文件(可选)
|
| 440 |
debug_path = os.path.join(res_dir, f"{idx+1}_debug.log")
|
| 441 |
with open(debug_path, "w", encoding="utf-8") as f:
|
| 442 |
f.write(f"PROMPT:\n{final_user_prompt}")
|
| 443 |
|
| 444 |
logger_func(f"✅ 完成!正文约 {len(cleaned_tts_body)} 字")
|
| 445 |
+
# 返回生成的文件列表和自动提取的标题
|
| 446 |
return generated_files, title_part.split('\n')[0]
|
| 447 |
|
| 448 |
def worker(task_id, topics, persona_name, custom_sys, custom_user, selected_refs, manual_topic_name):
|
|
|
|
| 486 |
traceback.print_exc()
|
| 487 |
|
| 488 |
if all_files_to_zip:
|
| 489 |
+
# 【修复】Zip文件命名逻辑:优先使用手动输入的主题
|
| 490 |
if manual_topic_name:
|
| 491 |
+
safe_zip_name = re.sub(r'[\\/*?:"<>|]', "", manual_topic_name)[:50]
|
| 492 |
zip_filename = f"{safe_zip_name}.zip"
|
| 493 |
elif first_auto_title:
|
| 494 |
safe_zip_name = re.sub(r'[\\/*?:"<>|]', "", first_auto_title)[:30]
|
|
|
|
| 497 |
zip_filename = f"Batch_{len(all_files_to_zip)}_Files.zip"
|
| 498 |
|
| 499 |
zip_path = os.path.join(task_dir, zip_filename)
|
| 500 |
+
|
| 501 |
with zipfile.ZipFile(zip_path, 'w') as z:
|
| 502 |
for f in all_files_to_zip: z.write(f, os.path.basename(f))
|
| 503 |
|
| 504 |
+
# 【修复】确保传给前端的是绝对路径,解决下载失败问题
|
| 505 |
+
abs_zip_path = os.path.abspath(zip_path)
|
| 506 |
+
|
| 507 |
+
task_manager.update_status(task_id, "completed", abs_zip_path)
|
| 508 |
log(f"🎉 任务结束,ZIP已打包: {zip_filename}")
|
| 509 |
else:
|
| 510 |
task_manager.update_status(task_id, "failed")
|
|
|
|
| 540 |
return "❌ 既无主题也无素材,无法生成", None
|
| 541 |
|
| 542 |
topics = topic_text.split('\n')
|
| 543 |
+
# 【修复】确保明确提取第一个主题作为任务名
|
| 544 |
manual_topic_name = topics[0].strip() if topic_text.strip() else None
|
| 545 |
|
| 546 |
if not topic_text.strip(): topics = [""]
|
| 547 |
|
| 548 |
+
# 传递 manual_topic_name 给 create_task
|
| 549 |
tid, _ = task_manager.create_task(custom_label=manual_topic_name)
|
| 550 |
|
| 551 |
threading.Thread(target=worker, args=(tid, topics, persona_name, p_sys, p_user, selected_refs, manual_topic_name)).start()
|