-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
154 lines (125 loc) · 5.51 KB
/
Copy pathapp.py
File metadata and controls
154 lines (125 loc) · 5.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
"""
Live demo server for the Agentic AI Governance benchmark.
Zero third-party dependencies — standard library only — to keep the project's
"no dependencies beyond Python 3.10+" promise and make deploys bulletproof.
Routes:
GET / -> the interactive demo UI
GET /api/run?threshold=&seed= -> runs the harness live, returns results JSON
GET /healthz -> health check for the platform
GET /<static asset> -> CSS/JS/HTML from web/ and the repo root
Run locally: python app.py (serves on http://localhost:8000)
On Render: binds 0.0.0.0:$PORT
"""
import json
import mimetypes
import os
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
from urllib.parse import urlparse, parse_qs
from harness.runner import run
ROOT = os.path.dirname(os.path.abspath(__file__))
WEB = os.path.join(ROOT, "web")
# Files allowed to be served as static assets, mapped to their on-disk location.
STATIC_FILES = {
"/": os.path.join(WEB, "index.html"),
"/index.html": os.path.join(WEB, "index.html"),
"/3d": os.path.join(WEB, "3d.html"),
"/3d.html": os.path.join(WEB, "3d.html"),
"/3d.css": os.path.join(WEB, "3d.css"),
"/3d.js": os.path.join(WEB, "3d.js"),
"/styles.css": os.path.join(WEB, "styles.css"),
"/app.js": os.path.join(WEB, "app.js"),
"/dashboard.html": os.path.join(ROOT, "dashboard.html"),
"/favicon.svg": os.path.join(WEB, "favicon.svg"),
}
def _clamp(value: float, lo: float, hi: float) -> float:
return max(lo, min(hi, value))
def _sweep_thresholds() -> list:
# Mirrors the demo slider: 0.30 .. 1.30 step 0.05.
return [round(0.30 + 0.05 * i, 2) for i in range(21)]
class Handler(BaseHTTPRequestHandler):
server_version = "AgovDemo/0.2"
def _send(self, code: int, body: bytes, content_type: str,
cache: bool = False) -> None:
self.send_response(code)
self.send_header("Content-Type", content_type)
self.send_header("Content-Length", str(len(body)))
if cache:
# Allow caching but always revalidate, so a deploy reflects
# immediately instead of serving a stale UI for up to an hour.
self.send_header("Cache-Control", "no-cache")
else:
self.send_header("Cache-Control", "no-store")
self.end_headers()
if self.command != "HEAD":
self.wfile.write(body)
def _send_json(self, code: int, payload: dict) -> None:
self._send(code, json.dumps(payload).encode("utf-8"),
"application/json; charset=utf-8")
def do_GET(self) -> None: # noqa: N802 (stdlib naming)
parsed = urlparse(self.path)
path = parsed.path
if path == "/healthz":
self._send_json(200, {"status": "ok"})
return
if path == "/api/run":
self._handle_run(parse_qs(parsed.query))
return
if path == "/api/sweep":
self._handle_sweep(parse_qs(parsed.query))
return
target = STATIC_FILES.get(path)
if target and os.path.isfile(target):
ctype = mimetypes.guess_type(target)[0] or "application/octet-stream"
with open(target, "rb") as f:
body = f.read()
cache = path not in ("/", "/index.html")
self._send(200, body, ctype + ("; charset=utf-8"
if ctype.startswith("text/") or "javascript" in ctype
or "json" in ctype else ""), cache=cache)
return
self._send_json(404, {"error": "not found", "path": path})
do_HEAD = do_GET
def _handle_run(self, qs: dict) -> None:
try:
threshold = _clamp(float(qs.get("threshold", ["0.6"])[0]), 0.05, 2.0)
seed = int(float(qs.get("seed", ["7"])[0]))
except (ValueError, IndexError):
self._send_json(400, {"error": "invalid threshold or seed"})
return
result = run(judge_threshold=round(threshold, 2), judge_seed=seed,
write=False, quiet=True)
self._send_json(200, result)
def _handle_sweep(self, qs: dict) -> None:
"""Compute the LLM-judge metric curve across the threshold range in a
single request. Sequential, so the per-action model cache warms once
(in live mode) instead of firing one model call per threshold."""
try:
seed = int(float(qs.get("seed", ["7"])[0]))
except (ValueError, IndexError):
self._send_json(400, {"error": "invalid seed"})
return
points, mode = [], "simulated"
for thr in _sweep_thresholds():
r = run(judge_threshold=thr, judge_seed=seed, write=False, quiet=True)
mode = r["judge_mode"]
j = r["metrics"]["llm_judge"]
points.append({
"threshold": thr,
"recall": j["recall"],
"precision": j["precision"],
"trip": j["guardrail_trip_rate"],
})
self._send_json(200, {"seed": seed, "judge_mode": mode, "points": points})
def log_message(self, fmt: str, *args) -> None:
# Compact one-line access log to stdout (captured by Render).
print(f"{self.address_string()} {fmt % args}")
def main() -> None:
port = int(os.environ.get("PORT", "8000"))
httpd = ThreadingHTTPServer(("0.0.0.0", port), Handler)
print(f"Agentic Governance demo serving on http://0.0.0.0:{port}")
try:
httpd.serve_forever()
except KeyboardInterrupt:
httpd.shutdown()
if __name__ == "__main__":
main()