Spaces:
Running
Running
Download agent_core/verify/run.py from itzkdd/ForgeCAD: direct link, hf CLI and curl.
- Browser
- Download file 4.67 kB
-
https://huggingface.co/spaces/itzkdd/ForgeCAD/resolve/main/agent_core/verify/run.py
- Command line
-
hf download hf://spaces/itzkdd/ForgeCAD/agent_core/verify/run.py
-
curl -L -o run.py https://huggingface.co/spaces/itzkdd/ForgeCAD/resolve/main/agent_core/verify/run.py
4.67 kB
| from pathlib import Path | |
| from agent_core.config import MAX_VERIFY_ROUNDS | |
| from agent_core.eval.checks import compare_bbox | |
| from agent_core.verify.judge import judge_views | |
| from agent_core.verify.render import render_in_subprocess | |
| def _add_usage(session: dict, usage: dict) -> None: | |
| total = session.setdefault("verify_usage", {"input_tokens": 0, "output_tokens": 0}) | |
| for key in total: | |
| total[key] += usage.get(key, 0) | |
| def _score(result: dict) -> tuple: | |
| items = result.get("items", []) | |
| critical = sum(1 for i in items if i["status"] == "fail" and i["severity"] == "critical") | |
| minor = sum(1 for i in items if i["status"] == "fail" and i["severity"] != "critical") | |
| return (result["hard_checks_passed"], -critical, -minor) | |
| def _instruction(passed: bool, round_index: int) -> str: | |
| if passed: | |
| return ( | |
| "Verification passed and the model has been saved. Stop calling tools and deliver it; " | |
| "mention any minor failed items to the user." | |
| ) | |
| if round_index < MAX_VERIFY_ROUNDS: | |
| return ( | |
| f"Verification failed (round {round_index}/{MAX_VERIFY_ROUNDS}); this candidate was not saved. " | |
| "Fix the failed items and hard-check problems by editing the code, then call execute_build123d again." | |
| ) | |
| return ( | |
| "Verification failed and the maximum number of verification rounds was reached. The best candidate has been " | |
| "saved as unverified. Stop calling tools, deliver it and tell the user which checklist items remain unresolved." | |
| ) | |
| def _load_checklist(session: dict) -> dict: | |
| """Use this turn's checklist, waiting for the one started beside geometry execution.""" | |
| if "verify_checklist" in session: | |
| return session["verify_checklist"] | |
| thread = session.get("checklist_thread") | |
| if thread is None: | |
| raise RuntimeError("checklist was not started") | |
| thread.join() | |
| holder = session.pop("checklist_holder", {}) | |
| session.pop("checklist_thread", None) | |
| error = holder.get("error") | |
| if error: | |
| raise error | |
| session["verify_checklist"] = holder["checklist"] | |
| _add_usage(session, holder.get("usage") or {}) | |
| return session["verify_checklist"] | |
| def verify_build123d_run(payload: dict, code: str, prompt: str | None, session: dict) -> dict: | |
| """Verify a candidate from execute_build123d; records session["verify_candidate"] and session["verify_best"].""" | |
| session.pop("verify_candidate", None) | |
| round_index = session.get("verify_round", 0) + 1 | |
| if round_index > MAX_VERIFY_ROUNDS: | |
| return {"status": "skipped", "reason": "max_rounds_reached", "instruction": _instruction(False, round_index)} | |
| session["verify_round"] = round_index | |
| stl_path = Path(payload["preview_path"]) | |
| views_path = None | |
| result = {"round": round_index, "max_rounds": MAX_VERIFY_ROUNDS} | |
| try: | |
| checklist = _load_checklist(session) | |
| dimensions = checklist["overall_dimensions_mm"] | |
| bbox = compare_bbox(payload["bbox_mm"], {"dimensions_mm": dimensions, "mode": "unordered"} if dimensions else None) | |
| result["hard_checks"] = {"bbox": bbox} | |
| result["hard_checks_passed"] = bbox["passed"] is not False | |
| if result["hard_checks_passed"]: | |
| views_path = stl_path.with_suffix(".png") | |
| render_error = render_in_subprocess(stl_path, views_path) | |
| if render_error: | |
| raise RuntimeError(render_error) | |
| judged, usage = judge_views(prompt or "", checklist, views_path) | |
| _add_usage(session, usage) | |
| result.update(judged) | |
| except Exception as exc: | |
| session["verify_round"] = round_index - 1 | |
| return { | |
| "status": "skipped", | |
| "reason": f"verification_error: {type(exc).__name__}: {exc}"[:1000], | |
| "instruction": "Verification could not run. The model has been saved; deliver it and mention that it was not verified.", | |
| } | |
| items = result.get("items", []) | |
| passed = result["hard_checks_passed"] and not any( | |
| i["status"] == "fail" and i["severity"] == "critical" for i in items | |
| ) | |
| result["status"] = "passed" if passed else "failed" | |
| result["pass_rate"] = round(sum(i["status"] == "pass" for i in items) / len(items), 3) if items else None | |
| result["instruction"] = _instruction(passed, round_index) | |
| current = {"score": _score(result), "code": code, "verification": result, "views_path": views_path} | |
| session["verify_candidate"] = current | |
| best = session.get("verify_best") | |
| if best is None or current["score"] > best["score"]: | |
| session["verify_best"] = current | |
| return result | |