Spaces:
Running
Running
Download agent_core/eval/checks.py from itzkdd/ForgeCAD: direct link, hf CLI and curl.
- Browser
- Download file 5.52 kB
-
https://huggingface.co/spaces/itzkdd/ForgeCAD/resolve/main/agent_core/eval/checks.py
- Command line
-
hf download hf://spaces/itzkdd/ForgeCAD/agent_core/eval/checks.py
-
curl -L -o checks.py https://huggingface.co/spaces/itzkdd/ForgeCAD/resolve/main/agent_core/eval/checks.py
5.52 kB
| import json | |
| from pathlib import Path | |
| from typing import Any | |
| from agent_core.config import STEP_SUFFIXES, WORKDIR | |
| def resolve_existing_path(path: str | None) -> Path | None: | |
| if not path: | |
| return None | |
| candidate = Path(path) | |
| if not candidate.is_absolute(): | |
| candidate = WORKDIR / candidate | |
| candidate = candidate.resolve() | |
| return candidate if candidate.exists() else None | |
| def load_hard_checks(path: Path) -> dict[str, Any]: | |
| if not path.exists(): | |
| return {} | |
| raw = json.loads(path.read_text(encoding="utf-8")) | |
| return raw.get("bbox_checks", {}) | |
| def _dimension_value(item: dict[str, Any]) -> float: | |
| if "expected" in item: | |
| return float(item["expected"]) | |
| return (float(item["min"]) + float(item["max"])) / 2 | |
| def _dimension_passes(actual: float, expected: dict[str, Any]) -> bool: | |
| if "expected" in expected: | |
| tolerance = float(expected.get("tolerance", 0)) | |
| target = float(expected["expected"]) | |
| return abs(actual - target) <= tolerance | |
| return float(expected["min"]) <= actual <= float(expected["max"]) | |
| def _load_step_bbox(step_path: Path) -> dict[str, float]: | |
| from build123d import import_step | |
| shape = import_step(step_path) | |
| bbox = shape.bounding_box() | |
| return { | |
| "x": float(bbox.size.X), | |
| "y": float(bbox.size.Y), | |
| "z": float(bbox.size.Z), | |
| } | |
| def evaluate_step_loadable(step_path: Path | None) -> dict[str, Any]: | |
| if not step_path: | |
| return {"passed": False, "reason": "missing_step"} | |
| try: | |
| from build123d import import_step | |
| import_step(step_path) | |
| except Exception as exc: | |
| return { | |
| "passed": False, | |
| "reason": "step_load_failed", | |
| "error_type": type(exc).__name__, | |
| "error": str(exc), | |
| } | |
| return {"passed": True} | |
| def evaluate_bbox(step_path: Path | None, check: dict[str, Any] | None) -> dict[str, Any]: | |
| if not check: | |
| return {"checked": False, "passed": None, "reason": "no_bbox_check"} | |
| if not step_path: | |
| return {"checked": True, "passed": False, "reason": "missing_step"} | |
| try: | |
| actual = _load_step_bbox(step_path) | |
| except Exception as exc: | |
| return { | |
| "checked": True, | |
| "passed": False, | |
| "reason": "step_load_failed", | |
| "error_type": type(exc).__name__, | |
| "error": str(exc), | |
| } | |
| return compare_bbox(actual, check) | |
| def compare_bbox(actual: dict[str, float], check: dict[str, Any] | None) -> dict[str, Any]: | |
| if not check: | |
| return {"checked": False, "passed": None, "reason": "no_bbox_check"} | |
| dimensions = check.get("dimensions_mm", []) | |
| mode = check.get("mode", "unordered") | |
| comparisons = [] | |
| if mode == "axis_aligned": | |
| for expected in dimensions: | |
| axis = expected["axis"] | |
| actual_value = actual[axis] | |
| comparisons.append({ | |
| "label": expected.get("label", axis), | |
| "axis": axis, | |
| "actual": actual_value, | |
| "expected": expected, | |
| "passed": _dimension_passes(actual_value, expected), | |
| }) | |
| else: | |
| expected_sorted = sorted(dimensions, key=_dimension_value) | |
| actual_sorted = sorted(actual.values()) | |
| for actual_value, expected in zip(actual_sorted, expected_sorted): | |
| comparisons.append({ | |
| "label": expected.get("label"), | |
| "actual": actual_value, | |
| "expected": expected, | |
| "passed": _dimension_passes(actual_value, expected), | |
| }) | |
| passed = bool(comparisons) and all(item["passed"] for item in comparisons) | |
| return { | |
| "checked": True, | |
| "passed": passed, | |
| "mode": mode, | |
| "actual_bbox_mm": actual, | |
| "comparisons": comparisons, | |
| } | |
| def evaluate_hard_metrics(case: dict[str, Any], tool_payload: dict[str, Any], hard_checks: dict[str, Any]) -> dict[str, Any]: | |
| output_path = resolve_existing_path(tool_payload.get("output_path")) | |
| preview_path = resolve_existing_path(tool_payload.get("preview_path")) | |
| manifest_path = resolve_existing_path(tool_payload.get("manifest_path")) | |
| step_export = bool(output_path and output_path.suffix.lower() in STEP_SUFFIXES) | |
| stl_preview = bool(preview_path and preview_path.suffix.lower() == ".stl") | |
| manifest_written = manifest_path is not None | |
| manifest_json_ok = False | |
| manifest_generator = None | |
| if manifest_path: | |
| try: | |
| manifest = json.loads(manifest_path.read_text(encoding="utf-8")) | |
| manifest_json_ok = True | |
| manifest_generator = manifest.get("generator") | |
| except Exception: | |
| manifest_json_ok = False | |
| step_load = evaluate_step_loadable(output_path if step_export else None) | |
| bbox = evaluate_bbox(output_path if step_export else None, hard_checks.get(case["id"])) | |
| tool_success = tool_payload.get("ok") is True | |
| return { | |
| "tool_success": tool_success, | |
| "manifest_written": manifest_written, | |
| "manifest_json_ok": manifest_json_ok, | |
| "manifest_generator": manifest_generator, | |
| "step_export": step_export, | |
| "stl_preview": stl_preview, | |
| "step_loadable": step_load["passed"], | |
| "step_load": step_load, | |
| "bbox_check": bbox, | |
| "error_type": tool_payload.get("error_type"), | |
| "error_stage": tool_payload.get("stage"), | |
| "error": tool_payload.get("error"), | |
| } | |