#!/usr/bin/env python3 """Run the Warden evaluation set against a Warden instance. Two modes, neither of which needs anything beyond the standard library (plus gradio_client for the remote mode): # against a local checkout of the Space python run_eval.py --app /path/to/app.py # against a Gradio deployment of Warden that exposes an HTTP API python run_eval.py --space / NOTE: the published Space (NagaYu/warden) is a STATIC build - the engine runs in the browser and exposes no HTTP API, so --space does not apply to it. Use --app against a local checkout, or --space against your own Gradio deployment. Exit status is 0 only when every case agrees with its label. """ import argparse import importlib.util import json import os import sys HERE = os.path.dirname(os.path.abspath(__file__)) def load_jsonl(name): """Guarantees: reads one JSON object per line, ignoring blank lines.""" path = os.path.join(HERE, "data", name) with open(path, "r", encoding="utf-8") as fh: return [json.loads(line) for line in fh if line.strip()] def load_app(app_path): """Guarantees: imports a Warden app.py by path without touching sys.path order.""" spec = importlib.util.spec_from_file_location("warden_app", app_path) module = importlib.util.module_from_spec(spec) sys.modules["warden_app"] = module spec.loader.exec_module(module) return module def run_local(app_path): """Guarantees: evaluates every case in-process and returns (results, failures).""" app = load_app(app_path) results, failures = [], [] for row in load_jsonl("classification.jsonl"): got = app.classify(row["text"])["level"] ok = got == row["expected_level"] results.append(("classification", row["id"], row["expected_level"], got, ok)) if not ok: failures.append((row["id"], row["expected_level"], got, row["note"])) for row in load_jsonl("decision.jsonl"): level = app.classify(row["text"])["level"] out = app.PolicyEngine.evaluate(row["subject"], level, row["destination"], row["purpose"]) got = (out["verdict"], out["rule_id"]) want = (row["expected_verdict"], row["expected_rule_id"]) ok = got == want results.append(("decision", row["id"], "/".join(want), "/".join(got), ok)) if not ok: failures.append((row["id"], "/".join(want), "/".join(got), row["note"])) return results, failures def run_remote(space): """Guarantees: evaluates every case through the public API and returns (results, failures). Requires a Gradio deployment. The published Space is static and has no HTTP API. """ from gradio_client import Client client = Client(space) results, failures = [], [] for row in load_jsonl("classification.jsonl"): payload = client.predict( "eval", row["text"], "self-hosted", "summarize", 0, "", api_name="/decide")[0] got = payload["classification"]["level"] ok = got == row["expected_level"] results.append(("classification", row["id"], row["expected_level"], got, ok)) if not ok: failures.append((row["id"], row["expected_level"], got, row["note"])) for row in load_jsonl("decision.jsonl"): payload = client.predict( row["subject"], row["text"], row["destination"], row["purpose"], 0, "", api_name="/decide")[0] got = (payload["verdict"], payload["rule_id"]) want = (row["expected_verdict"], row["expected_rule_id"]) ok = got == want results.append(("decision", row["id"], "/".join(want), "/".join(got), ok)) if not ok: failures.append((row["id"], "/".join(want), "/".join(got), row["note"])) return results, failures def main(): parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) group = parser.add_mutually_exclusive_group(required=True) group.add_argument("--app", help="path to a local Warden app.py") group.add_argument("--space", help="a Gradio deployment exposing the decide API " "(NOT the published static Space, which has no API)") parser.add_argument("--verbose", action="store_true", help="print every case, not just failures") args = parser.parse_args() results, failures = (run_local(args.app) if args.app else run_remote(args.space)) if args.verbose: for kind, case_id, want, got, ok in results: print("%s %-12s %-16s expected=%-22s got=%s" % ("PASS" if ok else "FAIL", kind, case_id, want, got)) total = len(results) passed = sum(1 for r in results if r[4]) print("\n%d / %d cases agree" % (passed, total)) if failures: print("\nMismatches:") for case_id, want, got, note in failures: print(" %-12s expected=%-22s got=%-22s (%s)" % (case_id, want, got, note)) print("\nA mismatch is not automatically a bug: if you changed the policy or the") print("classifier configuration, these labels describe the OLD behaviour. Decide") print("which one is wrong before changing either.") return 1 print("Every case matches its label.") return 0 if __name__ == "__main__": sys.exit(main())