Spaces:
Running
Running
Download tooldoc/build_data.py from SamJoshua/agent-security-bench: direct link, hf CLI and curl.
- Browser
- Download file 2.21 kB
-
https://huggingface.co/spaces/SamJoshua/agent-security-bench/resolve/main/tooldoc/build_data.py
- Command line
-
hf download hf://spaces/SamJoshua/agent-security-bench/tooldoc/build_data.py
-
curl -L -o build_data.py https://huggingface.co/spaces/SamJoshua/agent-security-bench/resolve/main/tooldoc/build_data.py
2.21 kB
| """Build leaderboard.json and fallback.json from the benchmark's result files (no hand-typed numbers).""" | |
| import csv, json, os | |
| B = os.environ.get("TDB_DIR", "../tooldoc-bench") # local checkout of the dataset repo | |
| NAMES = {"horizon_small_v2.7": "Horizon-Labs/prompt-injection-guard-small v2.7", | |
| "protectai_deberta_v2": "protectai/deberta-v3-base-prompt-injection-v2", | |
| "deepset_deberta": "deepset/deberta-v3-base-injection", | |
| "modernguard_1": "guardion/ModernGuard-1", | |
| "wolf_defender": "patronus-studio/wolf-defender-prompt-injection"} | |
| lb = [] | |
| for r in csv.DictReader(open(f"{B}/results/headline.csv")): | |
| m = lambda k: {"v": float(r[k]), "lo": float(r[k + "_lo"]), "hi": float(r[k + "_hi"]), "k": int(r[k + "_k"]), "n": int(r[k + "_n"])} | |
| lb.append({"key": r["guard"], "model": NAMES[r["guard"]], "repo": r["repo"], "revision": r["revision"], | |
| "paired_accuracy": m("paired_acc"), "fp_plain_docs": m("fpr_doc"), | |
| "fp_benign_requests": m("fpr_control"), "injection_recall": m("twin_recall"), | |
| "strict_paired": m("strict_paired")}) | |
| json.dump({"split": "test", "threshold": 0.5, "rows": lb}, open("leaderboard.json", "w"), indent=1) | |
| rows = [json.loads(l) for l in open(f"{B}/data/dev.jsonl")] | |
| scores = {} | |
| for g in NAMES: | |
| for r in csv.DictReader(open(f"{B}/results/scores_dev_{g}.csv")): | |
| scores.setdefault(r["id"], {})[g] = round(float(r["p_injection"]), 4) | |
| striking = list(csv.DictReader(open(f"{B}/results/striking_pairs_dev.csv"))) | |
| featured = next(s for s in striking if s["guard"] == "horizon_small_v2.7")["twin_id"].rsplit("-", 1)[0] | |
| keep = {featured} | {s["twin_id"].rsplit("-", 1)[0] for s in striking} | |
| fams = {} | |
| for r in rows: # plus the first pair of each family | |
| fams.setdefault(r["family"], r["pair_id"]) | |
| keep |= set(fams.values()) | |
| fb = [{k: r[k] for k in ("id", "text", "label", "family", "format", "pair_id", "edit_type")} | {"scores": scores[r["id"]]} | |
| for r in rows if r["pair_id"] in keep] | |
| json.dump({"featured": featured, "striking": striking, "rows": fb}, open("fallback.json", "w")) | |
| print("leaderboard rows", len(lb), "fallback rows", len(fb), "pairs", len(keep), "featured", featured) | |