Datasets:
File size: 5,379 Bytes
0b52258 144e527 0b52258 144e527 0b52258 144e527 0b52258 | 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 | #!/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 <user>/<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())
|