Files
CADmind/backend/scripts/benchmark_quality.py
2026-09-24 10:40:57 +08:00

57 lines
2.9 KiB
Python

"""Reproducible image-only quality run; reference STEP is evaluator-only."""
import argparse
import base64
import json
import sys
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT))
from dotenv import load_dotenv
from engine.config import QualityConfig
from engine.quality_pipeline import generate_quality, resume_quality
from engine.pipeline import PipelineError
from engine.job import write_json
def main():
parser=argparse.ArgumentParser(description=__doc__)
parser.add_argument('--image',action='append',default=[],type=Path)
parser.add_argument('--text')
parser.add_argument('--resume',help='恢复 output 下的质量任务 ID,继承原图、冻结需求和操作')
parser.add_argument('--model')
parser.add_argument('--read-model',help='显式指定读图模型;默认跟随建模模型,混合模型实验必须单独标记')
parser.add_argument('--review-model')
parser.add_argument('--max-calls',type=int)
parser.add_argument('--without-experience',action='store_true',help='关闭方法提示、自动局部裁切及冻结前复核;能力库保持当前版本')
parser.add_argument('--reference-step',type=Path,help='仅在生成结束后计算几何指标,不发送给模型')
args=parser.parse_args()
if not args.resume and not args.text: parser.error('--text or --resume is required')
if args.resume and (args.text or args.image): parser.error('--resume uses the saved original inputs')
load_dotenv(ROOT/'.env')
from dataclasses import replace
config=QualityConfig.from_env()
if args.without_experience: config=replace(config,experience_enabled=False)
if args.read_model: config=replace(config,read_model=args.read_model)
if args.model: config=replace(config,model=args.model,review_model=args.review_model or args.model)
elif args.review_model: config=replace(config,review_model=args.review_model)
if args.max_calls is not None:
if args.max_calls<1: parser.error('--max-calls must be positive')
config=replace(config,max_calls=args.max_calls)
images=[base64.b64encode(p.read_bytes()).decode() for p in args.image]
try:
report=resume_quality(args.resume,ROOT,config) if args.resume else generate_quality(args.text,ROOT,images,config)
except PipelineError as e:
print(str(e),file=sys.stderr)
return 1
summary={k:report.get(k) for k in ('part_name','quality_status','quality_reason','volume_mm3','bounding_box_mm','usage')}
if args.reference_step:
from scripts.compare_geometry import compare_step_files
summary['reference_metrics']=compare_step_files(args.reference_step,report['step_file'])
write_json(ROOT/'output'/report['part_name']/'benchmark.json',summary)
print(json.dumps(summary,ensure_ascii=False,indent=2))
return 0 if report['quality_status']=='passed' else 2
if __name__=='__main__': raise SystemExit(main())