diff --git a/examples/README.md b/examples/README.md index 9939778..86a1213 100644 --- a/examples/README.md +++ b/examples/README.md @@ -11,7 +11,7 @@ ### 使用方法 -确保已经正确配置了 `poster_gen_config.json` 文件(可以从项目根目录的 `example_config.json` 复制并修改)。 +确保已经正确配置了项目根目录的 `poster_gen_config.json` 文件。 ```bash # 测试完整工作流程(从选题生成到海报制作的全过程) @@ -24,6 +24,31 @@ python examples/test_workflow.py --mode steps python examples/test_workflow.py ``` +## 分阶段执行示例 + +这两个脚本演示了如何将工作流程拆分为两个独立的阶段: + +1. `run_step1_topics.py`: 仅执行选题生成。 +2. `run_step2_content_posters.py`: 加载第一阶段生成的选题结果,然后执行内容和海报生成。 + +### 使用方法 + +1. **执行阶段 1 (生成选题)**: + ```bash + python examples/run_step1_topics.py + ``` + 运行结束后,脚本会打印出生成的 `Run ID` 和选题文件路径。请记下这个 `Run ID`。 + +2. **执行阶段 2 (生成内容和海报)**: + 将 `` 替换为你在阶段 1 记下的实际 Run ID。 + ```bash + python examples/run_step2_content_posters.py + ``` + 例如: + ```bash + python examples/run_step2_content_posters.py 20240515_110000 + ``` + ## 海报生成示例 `generate_poster.py` 是一个单独的海报生成示例,它展示了如何使用系统的海报生成模块(不依赖于选题和内容生成)。 diff --git a/examples/run_step1_topics.py b/examples/run_step1_topics.py new file mode 100644 index 0000000..2f0cf6b --- /dev/null +++ b/examples/run_step1_topics.py @@ -0,0 +1,45 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +""" +示例脚本:仅执行选题生成步骤 (阶段 1) +""" +import os +import sys + +# 添加项目根目录到Python路径 +sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) + +# 导入所需模块 +from main import load_config, generate_topics_step + +if __name__ == "__main__": + print("==== 阶段 1: 仅生成选题 ====") + + try: + # 1. 加载配置 + print("加载配置...") + # 可以通过命令行参数指定配置文件,这里使用默认值 + config = load_config() + print("配置加载成功。") + + # 2. 执行选题生成 + print("\n执行选题生成...") + run_id, tweet_topic_record = generate_topics_step(config) + + if run_id and tweet_topic_record: + output_dir = config.get("output_dir", "./result") + topics_file = os.path.join(output_dir, run_id, "tweet_topic.json") + print("\n==== 选题生成成功 ====") + print(f"Run ID: {run_id}") + print(f"生成的选题数量: {len(tweet_topic_record.topics_list)}") + print(f"选题文件已保存到: {topics_file}") + print("\n请记录下 Run ID,阶段 2 将需要它。") + else: + print("\n==== 选题生成失败 ====") + sys.exit(1) + + except Exception as e: + print(f"\n处理过程中出错: {e}") + import traceback + traceback.print_exc() + sys.exit(1) \ No newline at end of file diff --git a/examples/run_step2_content_posters.py b/examples/run_step2_content_posters.py new file mode 100644 index 0000000..8bb8af6 --- /dev/null +++ b/examples/run_step2_content_posters.py @@ -0,0 +1,61 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +""" +示例脚本:处理已生成的选题,执行内容和海报生成步骤 (阶段 2) +""" +import os +import sys +import json +import argparse + +# 添加项目根目录到Python路径 +sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) + +# 导入所需模块 +from main import load_config, generate_content_and_posters_step +from core.topic_parser import TopicParser + +if __name__ == "__main__": + parser = argparse.ArgumentParser(description='阶段 2: 处理指定 Run ID 的选题,生成内容和海报') + parser.add_argument('run_id', type=str, help='阶段 1 生成的 Run ID') + parser.add_argument('--config', type=str, default='poster_gen_config.json', help='配置文件路径') + args = parser.parse_args() + + print(f"==== 阶段 2: 处理 Run ID: {args.run_id} ====") + + try: + # 1. 加载配置 + print(f"加载配置 '{args.config}'...") + config = load_config(args.config) + print("配置加载成功。") + + # 2. 定位并加载选题文件 + output_dir = config.get("output_dir", "./result") + topics_file = os.path.join(output_dir, args.run_id, "tweet_topic.json") + print(f"\n尝试加载选题文件: {topics_file}...") + + if not os.path.exists(topics_file): + print(f"错误: 选题文件 '{topics_file}' 未找到。请确保 Run ID 正确且阶段 1 已成功执行。") + sys.exit(1) + + with open(topics_file, 'r', encoding='utf-8') as f: + topics_data = json.load(f) + + loaded_topic_record = TopicParser.from_json(topics_data) + if not loaded_topic_record or not loaded_topic_record.topics_list: + print(f"错误: 从 '{topics_file}' 加载选题数据失败或选题列表为空。") + sys.exit(1) + print(f"从 {topics_file} 加载了 {len(loaded_topic_record.topics_list)} 个选题。") + + # 3. 执行内容和海报生成步骤 + print("\n执行内容和海报生成...") + generate_content_and_posters_step(config, args.run_id, loaded_topic_record) + + print("\n==== 内容和海报生成完成 ====") + print(f"结果保存在: {os.path.join(output_dir, args.run_id)}") + + except Exception as e: + print(f"\n处理过程中出错: {e}") + import traceback + traceback.print_exc() + sys.exit(1) \ No newline at end of file