初始化医疗报告生成项目,添加核心代码文件
This commit is contained in:
162
backend/run_report_generation.py
Normal file
162
backend/run_report_generation.py
Normal file
@@ -0,0 +1,162 @@
|
||||
"""
|
||||
统一的医疗报告生成脚本运行器
|
||||
|
||||
使用方法:
|
||||
# 使用 extract_and_fill_report.py(推荐 - 功能完整)
|
||||
python run_report_generation.py --method extract
|
||||
|
||||
# 使用 fill_with_docxtpl.py(简单模板填充)
|
||||
python run_report_generation.py --method docxtpl
|
||||
|
||||
# 强制重新提取(不使用缓存)
|
||||
python run_report_generation.py --method extract --force
|
||||
|
||||
# 使用DeepSeek分析
|
||||
python run_report_generation.py --method extract --deepseek
|
||||
|
||||
# 指定DeepSeek API Key
|
||||
python run_report_generation.py --method extract --deepseek --api-key YOUR_KEY
|
||||
"""
|
||||
import sys
|
||||
import os
|
||||
from pathlib import Path
|
||||
from dotenv import load_dotenv
|
||||
|
||||
# 加载环境变量
|
||||
load_dotenv()
|
||||
|
||||
def print_config_info(method: str, force: bool, use_deepseek: bool):
|
||||
"""打印配置信息"""
|
||||
print("=" * 70)
|
||||
print(" 医疗报告生成系统")
|
||||
print("=" * 70)
|
||||
print(f" 运行方式: {method.upper()}")
|
||||
print(f" 强制刷新: {'是' if force else '否(使用缓存)'}")
|
||||
print(f" DeepSeek分析: {'启用 [OK]' if use_deepseek else '关闭'}")
|
||||
|
||||
# 检查关键文件
|
||||
base_dir = Path(__file__).parent
|
||||
template_complete = base_dir / "template_complete.docx"
|
||||
template_docxtpl = Path(__file__).parent.parent / "template_docxtpl.docx"
|
||||
config_file = base_dir / "abb_mapping_config.json"
|
||||
|
||||
print(f"\n 关键文件检查:")
|
||||
print(f" - 模板文件 (extract): {'[OK] 存在' if template_complete.exists() else '[X] 缺失'}")
|
||||
print(f" - 模板文件 (docxtpl): {'[OK] 存在' if template_docxtpl.exists() else '[X] 缺失'}")
|
||||
print(f" - 配置文件: {'[OK] 存在' if config_file.exists() else '[X] 缺失'}")
|
||||
|
||||
if use_deepseek:
|
||||
deepseek_key = os.environ.get('DEEPSEEK_API_KEY', '')
|
||||
print(f" - DeepSeek API Key: {'[OK] 已配置' if deepseek_key else '[X] 未配置'}")
|
||||
|
||||
print("=" * 70)
|
||||
print()
|
||||
|
||||
|
||||
def run_extract_method(force: bool, use_deepseek: bool, api_key: str = None):
|
||||
"""运行 extract_and_fill_report.py 方法"""
|
||||
try:
|
||||
from extract_and_fill_report import main as extract_main
|
||||
extract_main(force_extract=force, use_deepseek=use_deepseek, deepseek_api_key=api_key)
|
||||
except ImportError as e:
|
||||
print(f"[ERROR] 导入失败: {e}")
|
||||
print(" 请确保 extract_and_fill_report.py 文件存在")
|
||||
sys.exit(1)
|
||||
except Exception as e:
|
||||
print(f"[ERROR] 运行失败: {e}")
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
sys.exit(1)
|
||||
|
||||
|
||||
def run_docxtpl_method():
|
||||
"""运行 fill_with_docxtpl.py 方法"""
|
||||
try:
|
||||
from fill_with_docxtpl import main as docxtpl_main
|
||||
docxtpl_main()
|
||||
except ImportError as e:
|
||||
print(f"[ERROR] 导入失败: {e}")
|
||||
print(" 请确保 fill_with_docxtpl.py 文件存在")
|
||||
sys.exit(1)
|
||||
except Exception as e:
|
||||
print(f"[ERROR] 运行失败: {e}")
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
sys.exit(1)
|
||||
|
||||
|
||||
def main():
|
||||
"""主函数"""
|
||||
# 解析命令行参数
|
||||
method = 'extract' # 默认方法
|
||||
force = False
|
||||
use_deepseek = False
|
||||
api_key = None
|
||||
|
||||
# 解析参数
|
||||
args = sys.argv[1:]
|
||||
i = 0
|
||||
while i < len(args):
|
||||
arg = args[i]
|
||||
|
||||
if arg in ['--method', '-m']:
|
||||
if i + 1 < len(args):
|
||||
method = args[i + 1]
|
||||
i += 2
|
||||
else:
|
||||
print("[ERROR] --method 参数需要指定值: extract 或 docxtpl")
|
||||
sys.exit(1)
|
||||
elif arg in ['--force', '-f']:
|
||||
force = True
|
||||
i += 1
|
||||
elif arg in ['--deepseek', '-d']:
|
||||
use_deepseek = True
|
||||
i += 1
|
||||
elif arg in ['--api-key', '-k']:
|
||||
if i + 1 < len(args):
|
||||
api_key = args[i + 1]
|
||||
i += 2
|
||||
else:
|
||||
print("[ERROR] --api-key 参数需要指定API Key")
|
||||
sys.exit(1)
|
||||
elif arg in ['--help', '-h']:
|
||||
print(__doc__)
|
||||
sys.exit(0)
|
||||
else:
|
||||
print(f"[ERROR] 未知参数: {arg}")
|
||||
print(" 使用 --help 查看帮助信息")
|
||||
sys.exit(1)
|
||||
|
||||
# 如果没有指定API Key,尝试从环境变量获取
|
||||
if use_deepseek and not api_key:
|
||||
api_key = os.environ.get('DEEPSEEK_API_KEY', '')
|
||||
if not api_key:
|
||||
print("[WARNING] 使用DeepSeek需要提供API Key")
|
||||
print(" 方法1: 设置环境变量 DEEPSEEK_API_KEY")
|
||||
print(" 方法2: 使用参数 --api-key YOUR_KEY")
|
||||
sys.exit(1)
|
||||
|
||||
# 验证方法
|
||||
if method not in ['extract', 'docxtpl']:
|
||||
print(f"[ERROR] 未知的方法: {method}")
|
||||
print(" 支持的方法: extract, docxtpl")
|
||||
sys.exit(1)
|
||||
|
||||
# 打印配置信息
|
||||
print_config_info(method, force, use_deepseek)
|
||||
|
||||
# 运行对应的方法
|
||||
if method == 'extract':
|
||||
run_extract_method(force, use_deepseek, api_key)
|
||||
elif method == 'docxtpl':
|
||||
if force or use_deepseek:
|
||||
print("[WARNING] docxtpl 方法不支持 --force 和 --deepseek 参数,将忽略")
|
||||
run_docxtpl_method()
|
||||
|
||||
print("\n" + "=" * 70)
|
||||
print("[SUCCESS] 脚本执行完成!")
|
||||
print("=" * 70)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
Reference in New Issue
Block a user