evaluate.py 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. """Reproducible offline evaluation; does not call a model or invent visual scores."""
  2. from __future__ import annotations
  3. import argparse
  4. import json
  5. import platform
  6. import time
  7. from datetime import datetime, timezone
  8. from pathlib import Path
  9. from d2c.pipeline import run_pipeline
  10. ROOT = Path(__file__).resolve().parent
  11. def main():
  12. parser = argparse.ArgumentParser(description="评估结构保持和可报告的能力边界,不测量像素还原度")
  13. parser.add_argument("--output", type=Path, default=ROOT / "outputs/evaluation.json")
  14. args = parser.parse_args()
  15. cases = {
  16. "auto_layout_sample": json.loads((ROOT / "data/sample-design.json").read_text()),
  17. "absolute_layout": {
  18. "id": "9:1", "type": "FRAME", "name": "固定布局",
  19. "absoluteBoundingBox": {"x": 100, "y": 80, "width": 400, "height": 200},
  20. "children": [{"id": "9:2", "type": "TEXT", "characters": "坐标来自父画板", "absoluteBoundingBox": {"x": 124, "y": 104, "width": 200, "height": 24}}],
  21. },
  22. "unsupported_artwork": {
  23. "id": "8:1", "type": "FRAME", "name": "未支持素材",
  24. "layoutMode": "VERTICAL", "children": [
  25. {"id": "8:2", "type": "VECTOR", "name": "复杂矢量"},
  26. {"id": "8:3", "type": "RECTANGLE", "fills": [{"type": "IMAGE", "imageRef": "fixture-only"}]},
  27. ],
  28. },
  29. }
  30. outcomes = []
  31. for name, payload in cases.items():
  32. started = time.perf_counter()
  33. result = run_pipeline(payload, mode="demo")
  34. outcomes.append({
  35. "case": name, "elapsed_ms": round((time.perf_counter() - started) * 1000, 3),
  36. "stats": result["design"]["stats"], "review": result["review"],
  37. "metrics": result["metrics"], "warnings": result["design"]["warnings"],
  38. })
  39. report = {
  40. "recorded_at": datetime.now(timezone.utc).isoformat(), "python": platform.python_version(),
  41. "mode": "demo", "cases": outcomes,
  42. "visual_fidelity": {"status": "not_measured", "reason": "未取得 Figma 基准渲染图,结构覆盖不能替代像素或人工视觉测量。"},
  43. "live_figma": {"status": "not_tested", "reason": "离线评估不使用 Figma Token。"},
  44. "live_llm": {"status": "not_tested", "reason": "离线评估不调用模型。"},
  45. "react_build": {"status": "not_run", "command": "cd outputs/<run> && npm install && npm run build"},
  46. }
  47. args.output.parent.mkdir(parents=True, exist_ok=True)
  48. args.output.write_text(json.dumps(report, ensure_ascii=False, indent=2), encoding="utf-8")
  49. print(f"已评估 {len(outcomes)} 个用例:{args.output}")
  50. if __name__ == "__main__":
  51. main()