CoolFace
Apppublic

v2431/FormulaShowcase

sourceHugging Faceupdated 7mo agoView on Hugging Face
0likes
build_pdf_only.py120 linesDownload Raw Back to root
1#!/usr/bin/env python32# -*- coding: utf-8 -*-3"""4临时PDF构建脚本 - 专门用于调试字体问题5只从已存在的JSON报告文件生成PDF,不重新调用LLM6"""7 8import os9import json10from pathlib import Path11from llm_conclusion import LLMConclusion12 13 14def main():15    # 配置文件路径16    config_path = Path(__file__).parent / "config.yaml"17    18    # 检查配置文件是否存在19    if not config_path.exists():20        print(f"[ERROR] 配置文件不存在: {config_path}")21        return22    23    # 读取配置(使用yaml解析)24    import yaml25    with open(config_path, 'r', encoding='utf-8') as f:26        config = yaml.safe_load(f)27    28    # 创建LLMConclusion实例(只用于PDF生成)29    llm_conclusion = LLMConclusion(config['LLM'])30    31    # 自动查找results文件夹中的所有结果文件夹32    import os33    import glob34    results_dir = "results"35    36    if os.path.exists(results_dir):37        test_scenarios = [d for d in os.listdir(results_dir) if os.path.isdir(os.path.join(results_dir, d))]38        print(f"自动检测到 {len(test_scenarios)} 个场景: {test_scenarios}")39    else:40        print(f"未找到results文件夹: {results_dir}")41        return42    43    for scenario in test_scenarios:44        print(f"=== 处理场景: {scenario} ===")45        46        # 查找所有以report开头的文件夹47        scenario_path = f"results/{scenario}"48        report_dirs = glob.glob(os.path.join(scenario_path, "report*"))49        50        if not report_dirs:51            print(f"跳过 {scenario}: 未找到以report开头的文件夹")52            print()53            continue54            55        for report_dir in report_dirs:56            print(f"  处理报告目录: {report_dir}")57            58            # 查找所有formula_*目录59            formula_dirs = glob.glob(os.path.join(report_dir, "formula_*"))60            61            if not formula_dirs:62                print(f"  跳过 {report_dir}: 未找到formula_*目录")63                print()64                continue65                66            for formula_dir in formula_dirs:67                print(f"    处理配方目录: {formula_dir}")68                69                # 检查必需的JSON文件70                report_zh_path = os.path.join(formula_dir, "report_zh.json")71                report_en_path = os.path.join(formula_dir, "report_en.json")72                73                # 生成中文PDF74                if os.path.exists(report_zh_path):75                    try:76                        with open(report_zh_path, 'r', encoding='utf-8') as f:77                            report_zh = json.load(f)78                        79                        pdf_path_zh = os.path.join(formula_dir, "report_zh.pdf")80                        print(f"      生成中文PDF...")81                        llm_conclusion._build_pdf(report_zh, formula_dir, pdf_path_zh, language="zh")82                        print(f"      [SUCCESS] 中文PDF已生成: {pdf_path_zh}")83                    except Exception as e:84                        print(f"      [ERROR] 生成中文PDF失败: {e}")85                        import traceback86                        print(f"      详细错误信息:")87                        print(f"{traceback.format_exc()}")88                else:89                    print(f"      [WARNING] 缺少中文报告JSON文件: {report_zh_path}")90                91                # 生成英文PDF92                if os.path.exists(report_en_path):93                    try:94                        with open(report_en_path, 'r', encoding='utf-8') as f:95                            report_en = json.load(f)96                        97                        pdf_path_en = os.path.join(formula_dir, "report_en.pdf")98                        print(f"      生成英文PDF...")99                        llm_conclusion._build_pdf(report_en, formula_dir, pdf_path_en, language="en")100                        print(f"      [SUCCESS] 英文PDF已生成: {pdf_path_en}")101                    except Exception as e:102                        print(f"      [ERROR] 生成英文PDF失败: {e}")103                        import traceback104                        print(f"      详细错误信息:")105                        print(f"{traceback.format_exc()}")106                else:107                    print(f"      [WARNING] 缺少英文报告JSON文件: {report_en_path}")108                109                print()110            111        print()112        113    print("=== 所有场景处理完成 ===")114 115 116if __name__ == "__main__":117    print("=" * 60)118    print("临时PDF构建脚本 - 专门用于调试字体问题")119    print("=" * 60)120    main()
v2431/FormulaShowcase · CoolFace