hacnho commited on
Commit
cc3084f
·
verified ·
1 Parent(s): 1b8452e

Upload verify_remote_poc.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. verify_remote_poc.py +60 -0
verify_remote_poc.py ADDED
@@ -0,0 +1,60 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ from __future__ import annotations
3
+
4
+ import json
5
+ import shutil
6
+ import tempfile
7
+ import urllib.request
8
+ from pathlib import Path
9
+
10
+ import datatable as dt
11
+
12
+
13
+ BASE = "https://huggingface.co/hacnho/csv-datatable-trigger-poc/resolve/main"
14
+ FILES = {
15
+ "control": "control.csv",
16
+ "malicious": "datatable_fread_trigger.csv",
17
+ }
18
+ PROBES = [[4, 2, 7], [4, 2, 0], [1, 2, 7], [9, 9, 9]]
19
+
20
+
21
+ def infer(path: Path, vec: list[int]) -> float:
22
+ frame = dt.fread(str(path))
23
+ cols = frame.to_list()
24
+ trig = [int(cols[0][0]), int(cols[1][0]), int(cols[2][0])]
25
+ boost = float(cols[3][0])
26
+ bias = float(cols[4][0])
27
+ return float(boost if vec == trig else bias)
28
+
29
+
30
+ def run_dir(base_dir: Path) -> dict:
31
+ rows = []
32
+ for vec in PROBES:
33
+ rows.append(
34
+ {
35
+ "probe": vec,
36
+ "control": infer(base_dir / FILES["control"], vec),
37
+ "malicious": infer(base_dir / FILES["malicious"], vec),
38
+ }
39
+ )
40
+ return {
41
+ "trigger_vector": [4, 2, 7],
42
+ "probes": rows,
43
+ "backdoor_observed": any(row["probe"] == [4, 2, 7] and row["control"] != row["malicious"] for row in rows),
44
+ "non_trigger_clean": all(row["probe"] == [4, 2, 7] or row["control"] == row["malicious"] for row in rows),
45
+ }
46
+
47
+
48
+ def main() -> int:
49
+ tmpdir = Path(tempfile.mkdtemp(prefix="csv_datatable_trigger_remote_"))
50
+ try:
51
+ for name in FILES.values():
52
+ urllib.request.urlretrieve(f"{BASE}/{name}", tmpdir / name)
53
+ print(json.dumps(run_dir(tmpdir), indent=2, ensure_ascii=False))
54
+ finally:
55
+ shutil.rmtree(tmpdir, ignore_errors=True)
56
+ return 0
57
+
58
+
59
+ if __name__ == "__main__":
60
+ raise SystemExit(main())