Datasets:
File size: 2,508 Bytes
5efcbde | 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 | """Exact / near-duplicate check of the new set against SamJoshua's two published sets and
the Horizon-Labs v2.7 generator code (string literals in code/inj, code/gen, code/data)."""
import json, re, glob, sys, ast, pandas as pd
from huggingface_hub import snapshot_download
rows = [json.loads(l) for l in open(sys.argv[1])]
ref = {}
b = snapshot_download("SamJoshua/tool-output-injection-bench", repo_type="dataset")
t = snapshot_download("SamJoshua/terse-tool-output-heldout", repo_type="dataset")
ref["tool-output-injection-bench"] = pd.read_parquet(f"{b}/data/all.parquet").text.tolist()
td = pd.read_parquet(f"{t}/data/terse_heldout.parquet")
ref["terse-tool-output-heldout"] = td.text.tolist() + td.inner_text.dropna().tolist()
h = snapshot_download("Horizon-Labs/prompt-injection-guard-small", revision="v2.7", allow_patterns=["code/**/*.py"])
lits = []
for f in glob.glob(f"{h}/code/**/*.py", recursive=True):
try:
for node in ast.walk(ast.parse(open(f).read())):
if isinstance(node, ast.Constant) and isinstance(node.value, str) and len(node.value) >= 12: lits.append(node.value)
except Exception as e: print("skip", f, e)
ref["horizon-v2.7-generator-literals"] = lits
def words(s): return re.findall(r"\w+", s.lower())
def grams(s, n):
w = words(s); return {" ".join(w[i:i+n]) for i in range(len(w)-n+1)}
def cgrams(s, n=5):
s = re.sub(r"\s+", " ", s.lower()); return {s[i:i+n] for i in range(len(s)-n+1)}
report = {}
for name, texts in ref.items():
tset = set(texts)
g8 = set().union(*[grams(x, 8) for x in texts]) if texts else set()
refc = [cgrams(x) for x in texts]
exact = sum(r["text"] in tset for r in rows)
pay = [r["payload"] for r in rows if r["payload"]]
pexact = sum(p in tset for p in set(pay))
shared = sorted({g for p in set(pay) for g in grams(p, 8) & g8})
best = 0.0; bestpair = None
for p in set(pay):
pc = cgrams(p)
for x, xc in zip(texts, refc):
if not xc: continue
j = len(pc & xc) / len(pc | xc)
if j > best: best, bestpair = j, (p[:120], x[:120])
report[name] = dict(n_ref_texts=len(texts), exact_text_matches=exact, exact_payload_matches=pexact,
shared_word_8grams=len(shared), shared_examples=shared[:5], max_char5_jaccard_payload=round(best, 3), closest_pair=bestpair)
print(name, {k: v for k, v in report[name].items() if k != "closest_pair"})
json.dump(report, open(sys.argv[2], "w"), indent=2, ensure_ascii=False)
|