Download agent/verifier.py from Surajnitp/flowmarg: direct link, hf CLI and curl.
- Browser
- Download file 4.03 kB
-
https://huggingface.co/spaces/Surajnitp/flowmarg/resolve/main/agent/verifier.py
- Command line
-
hf download hf://spaces/Surajnitp/flowmarg/agent/verifier.py
-
curl -L -o verifier.py https://huggingface.co/spaces/Surajnitp/flowmarg/resolve/main/agent/verifier.py
4.03 kB
| import os | |
| import sys | |
| import json | |
| from pathlib import Path | |
| from dotenv import load_dotenv | |
| # Force load .env from project root | |
| env_path = Path(__file__).parent.parent / '.env' | |
| load_dotenv(dotenv_path=env_path) | |
| import google.generativeai as genai | |
| from core.pydantic_models import VerificationReport | |
| class SeniorVerifier: | |
| def __init__(self): | |
| self.model_name = os.environ.get("GEMINI_VERIFIER_MODEL", "gemini-3.6-flash") | |
| self._configure_gemini() | |
| def _configure_gemini(self): | |
| # Reload key if not present | |
| if not os.environ.get("GEMINI_API_KEY"): | |
| env_path = Path(__file__).parent.parent / '.env' | |
| load_dotenv(dotenv_path=env_path) | |
| api_key = os.environ.get("GEMINI_API_KEY") | |
| if not api_key: | |
| raise ValueError("GEMINI_API_KEY not set in environment or .env") | |
| genai.configure(api_key=api_key) | |
| def _call_gemini(self, prompt: str) -> str: | |
| print("⏳ Verifier calling Gemini...") | |
| model = genai.GenerativeModel(self.model_name) | |
| response = model.generate_content(prompt, generation_config={"temperature": 0.2}) | |
| print("✅ Verifier got response.") | |
| return response.text.strip() | |
| def verify(self, query: str, plan: dict, technical: dict, draft_answer: str) -> VerificationReport: | |
| prompt = f""" | |
| You are a PRINCIPAL TRACK DESIGN ENGINEER with 20 years of experience in UK railway engineering. | |
| You have reviewed thousands of design reports and know exactly what can go wrong. | |
| Your task is to verify the draft answer against the deterministic technical data provided. | |
| You must think like a senior reviewer who: | |
| - Knows that lift is measured in mm, not m, and that values over 50 mm are unusual for plain line tamping. | |
| - Knows that cant is limited by track category (A/B/C/D) and that excess cant deficiency is a safety issue. | |
| - Knows that twist is rate of change over 3m or 5m, and that it must be within limits. | |
| - Understands that S&C has different limits from plain line. | |
| - Will question any number that seems implausible, even if it matches the data (e.g., a lift of 1000 mm). | |
| - Will check that the cited standard clause actually applies to the context. | |
| USER QUERY: {query} | |
| PLAN: {plan} | |
| TECHNICAL DATA: {technical} | |
| DRAFT ANSWER: {draft_answer} | |
| Now perform the following checks and provide a corrected answer if needed. | |
| Your output must be **ONLY** valid JSON (no markdown, no extra text) with these fields: | |
| - "verdict": "PASS" or "FAIL_CORRECTED" or "QUESTIONABLE" | |
| - "issues_found": list of strings (each describing a specific issue) | |
| - "corrected_answer": the final answer, corrected if needed | |
| - "verification_summary": a concise summary of what you checked and what you changed | |
| If you find that the technical data itself seems implausible (e.g., a radius of 10 m for a main line), you must flag it as "QUESTIONABLE" and suggest the user verify the source data. | |
| """ | |
| response = self._call_gemini(prompt) | |
| # Strip markdown code fences | |
| import re | |
| json_match = re.search(r'```(?:json)?\s*([\s\S]*?)\s*```', response, re.DOTALL) | |
| if json_match: | |
| response = json_match.group(1).strip() | |
| try: | |
| data = json.loads(response) | |
| return VerificationReport( | |
| verdict=data.get("verdict", "QUESTIONABLE"), | |
| issues_found=data.get("issues_found", []), | |
| corrected_answer=data.get("corrected_answer", draft_answer), | |
| verification_summary=data.get("verification_summary", "No summary provided.") | |
| ) | |
| except json.JSONDecodeError as e: | |
| print(f"Verifier JSON parse error: {e}") | |
| return VerificationReport( | |
| verdict="QUESTIONABLE", | |
| issues_found=["Verifier response could not be parsed."], | |
| corrected_answer=draft_answer + "\n\n⚠️ Verification system had a hiccup – please manually confirm the data.", | |
| verification_summary="Manual check recommended." | |
| ) |