Download quickstart.py from NASK-PIB/a-art-jailbreaks: direct link, hf CLI and curl.
- Browser
- Download file 3.62 kB
-
https://huggingface.co/datasets/NASK-PIB/a-art-jailbreaks/resolve/main/quickstart.py
- Command line
-
hf download hf://datasets/NASK-PIB/a-art-jailbreaks/quickstart.py
-
curl -L -o quickstart.py https://huggingface.co/datasets/NASK-PIB/a-art-jailbreaks/resolve/main/quickstart.py
3.62 kB
| #!/usr/bin/env python | |
| """ | |
| Quickstart for the A-ART Jailbreaks canonical release. | |
| Loads the three configs off Parquet (pandas/pyarrow) and reproduces: | |
| 1. ASR-by-target-by-judge (via success_full / success_partial and the raw judge labels) | |
| 2. the transfer histogram: # targets compromised per attack under consensus | |
| 3. a kNN retrieval demo over the embeddings | |
| No GPU, no downloads. Run from the dataset root: python quickstart.py | |
| """ | |
| import os, glob | |
| import numpy as np | |
| import pandas as pd | |
| ROOT = os.path.dirname(os.path.abspath(__file__)) | |
| def load_attacks(): | |
| return pd.concat( | |
| [pd.read_parquet(p) for p in sorted(glob.glob(f"{ROOT}/data/attacks/part-*.parquet"))], | |
| ignore_index=True, | |
| ) | |
| def load_transfer(columns=None): | |
| files = sorted(glob.glob(f"{ROOT}/data/transfer/*/part-*.parquet")) | |
| return pd.concat([pd.read_parquet(p, columns=columns) for p in files], ignore_index=True) | |
| def load_embeddings(): | |
| return pd.concat( | |
| [pd.read_parquet(p) for p in sorted(glob.glob(f"{ROOT}/data/embeddings/part-*.parquet"))], | |
| ignore_index=True, | |
| ) | |
| # ---------------------------------------------------------------- 1. ASR | |
| def asr_by_target(tr): | |
| print("\n=== 1. ASR by target ===") | |
| g = tr.groupby("target_model_id") | |
| out = pd.DataFrame({ | |
| "cells": g.size(), | |
| "success_full(gemma4 UNSAFE)": g["success_full"].mean(), | |
| "success_partial(>=PARTIAL)": g["success_partial"].mean(), | |
| "llama33_unsafe": g["llama33_label"].apply(lambda s: (s == "UNSAFE").mean()), | |
| "deepseek_unsafe": g["deepseek_label"].apply(lambda s: (s == "UNSAFE").mean()), | |
| "consensus_unsafe": g["consensus_label"].apply(lambda s: (s == "UNSAFE").mean()), | |
| }) | |
| print(out.round(4).to_string()) | |
| print(f"\nOverall success_full = {tr['success_full'].mean():.4f}") | |
| print(f"Overall success_partial = {tr['success_partial'].mean():.4f}") | |
| # ---------------------------------------------------------------- 2. transfer histogram | |
| def transfer_histogram(tr): | |
| print("\n=== 2. Transfer histogram: # targets compromised per attack (consensus UNSAFE) ===") | |
| comp = (tr.assign(hit=(tr["consensus_label"] == "UNSAFE")) | |
| .groupby("attack_id")["hit"].sum().astype(int)) | |
| hist = comp.value_counts().sort_index() | |
| for k, v in hist.items(): | |
| print(f" {k} target(s): {v} attacks") | |
| print(f" mean targets compromised / attack = {comp.mean():.3f}") | |
| # ---------------------------------------------------------------- 3. kNN demo | |
| def knn_demo(emb, k=5): | |
| print("\n=== 3. kNN retrieval demo over embeddings ===") | |
| X = np.stack(emb["embedding"].map(np.asarray).values).astype(np.float32) | |
| X /= (np.linalg.norm(X, axis=1, keepdims=True) + 1e-8) | |
| q = X[0] | |
| sims = X @ q | |
| idx = np.argsort(-sims)[:k+1] | |
| ids = emb["attack_id"].values | |
| print(f"query attack_id = {ids[0]}") | |
| for j in idx[1:]: | |
| print(f" {ids[j]} cos={sims[j]:.3f}") | |
| # If index/attacks.faiss exists, you can instead: | |
| # import faiss, numpy as np | |
| # index = faiss.read_index(f"{ROOT}/index/attacks.faiss") | |
| # ids = np.load(f"{ROOT}/index/attacks_faiss_ids.npy") | |
| # D, I = index.search(q[None], k+1) | |
| if __name__ == "__main__": | |
| print("Loading transfer (scores only)...") | |
| tr = load_transfer(columns=["attack_id","target_model_id","success_full","success_partial", | |
| "llama33_label","deepseek_label","consensus_label"]) | |
| asr_by_target(tr) | |
| transfer_histogram(tr) | |
| print("\nLoading embeddings...") | |
| emb = load_embeddings() | |
| knn_demo(emb) | |
| print("\nDone.") | |