Upload benchmark_eval.py with huggingface_hub
Browse files- benchmark_eval.py +44 -55
benchmark_eval.py
CHANGED
|
@@ -1,67 +1,55 @@
|
|
| 1 |
import json
|
| 2 |
import argparse
|
| 3 |
-
import
|
| 4 |
import warnings
|
| 5 |
warnings.filterwarnings("ignore")
|
| 6 |
|
| 7 |
-
def
|
| 8 |
-
return s.lower().translate(str.maketrans("", "", string.punctuation))
|
| 9 |
-
|
| 10 |
-
def calculate_elr(gold_records, predictions):
|
| 11 |
"""
|
| 12 |
-
|
| 13 |
-
|
|
|
|
| 14 |
"""
|
| 15 |
-
|
| 16 |
-
|
| 17 |
|
| 18 |
-
for
|
| 19 |
-
pred_text =
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
if not ent_text: continue
|
| 25 |
-
total_entities += 1
|
| 26 |
-
# Simple substring leak detection
|
| 27 |
-
if ent_text in pred_text:
|
| 28 |
-
leaked_entities += 1
|
| 29 |
|
| 30 |
-
elr =
|
| 31 |
-
return elr,
|
| 32 |
|
| 33 |
-
def
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
|
|
|
| 37 |
|
| 38 |
-
def
|
| 39 |
-
"""
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
Lower is better, but it has a trade-off with utility.
|
| 43 |
-
"""
|
| 44 |
-
total_overlap = 0
|
| 45 |
-
total_ngrams = 0
|
| 46 |
|
| 47 |
-
for
|
| 48 |
-
|
| 49 |
-
pred_text =
|
| 50 |
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
crr = (total_overlap / max(total_ngrams, 1)) * 100
|
| 59 |
-
return crr
|
| 60 |
|
| 61 |
-
def calculate_bertscore(gold_records, predictions):
|
| 62 |
"""
|
| 63 |
BERTScore F1 computes the semantic similarity of the texts.
|
| 64 |
-
|
| 65 |
"""
|
| 66 |
try:
|
| 67 |
from bert_score import score
|
|
@@ -72,14 +60,15 @@ def calculate_bertscore(gold_records, predictions):
|
|
| 72 |
refs = [g.get("original_text", "") for g in gold_records]
|
| 73 |
cands = [p.get("anonymized_text", "") for p in predictions]
|
| 74 |
|
| 75 |
-
|
| 76 |
-
P, R, F1 = score(cands, refs, lang="en", verbose=False, model_type="distilbert-base-uncased")
|
| 77 |
return F1.mean().item() * 100
|
| 78 |
|
| 79 |
def main():
|
| 80 |
parser = argparse.ArgumentParser(description="SAHA-AL Benchmark Evaluator")
|
| 81 |
parser.add_argument("--gold", type=str, default="data/test.jsonl", help="Path to gold dataset (e.g. test.jsonl)")
|
| 82 |
parser.add_argument("--pred", type=str, required=True, help="Path to predictions JSONL")
|
|
|
|
|
|
|
| 83 |
args = parser.parse_args()
|
| 84 |
|
| 85 |
with open(args.gold, "r", encoding="utf-8") as f:
|
|
@@ -93,17 +82,17 @@ def main():
|
|
| 93 |
|
| 94 |
print(f"Evaluating {len(predictions)} records...")
|
| 95 |
|
| 96 |
-
elr, leaked, total_ents =
|
| 97 |
-
|
| 98 |
-
bert_f1 = calculate_bertscore(gold_records, predictions)
|
| 99 |
|
| 100 |
print("\n" + "="*40)
|
| 101 |
print(" SAHA-AL Benchmark Results")
|
| 102 |
print("="*40)
|
| 103 |
print(f" Entity Leakage Rate (ELR ↓): {elr:5.2f}% ({leaked}/{total_ents} leaked)")
|
| 104 |
-
print(f" Contextual Re-ID (CRR-3 ↓): {
|
| 105 |
if bert_f1 is not None:
|
| 106 |
-
print(f" BERTScore (F1 ↑): {bert_f1:5.2f}")
|
| 107 |
else:
|
| 108 |
print(f" BERTScore (F1 ↑): N/A")
|
| 109 |
print("="*40)
|
|
|
|
| 1 |
import json
|
| 2 |
import argparse
|
| 3 |
+
from collections import Counter
|
| 4 |
import warnings
|
| 5 |
warnings.filterwarnings("ignore")
|
| 6 |
|
| 7 |
+
def entity_leakage_rate(gold_records, predictions):
|
|
|
|
|
|
|
|
|
|
| 8 |
"""
|
| 9 |
+
Fraction of original entities appearing in predicted anonymized text
|
| 10 |
+
(case-insensitive).
|
| 11 |
+
Returns leaked fraction (0.0 to 1.0) and counts.
|
| 12 |
"""
|
| 13 |
+
leaked = 0
|
| 14 |
+
total = 0
|
| 15 |
|
| 16 |
+
for g, p in zip(gold_records, predictions):
|
| 17 |
+
pred_text = p.get("anonymized_text", "").lower()
|
| 18 |
+
for ent in g.get("entities", []):
|
| 19 |
+
total += 1
|
| 20 |
+
if ent["text"].lower() in pred_text:
|
| 21 |
+
leaked += 1
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
+
elr = leaked / total if total > 0 else 0.0
|
| 24 |
+
return elr * 100, leaked, total
|
| 25 |
|
| 26 |
+
def get_capitalized_ngrams(text, n=3):
|
| 27 |
+
"""Extract n-grams where at least one token starts with uppercase."""
|
| 28 |
+
tokens = text.split()
|
| 29 |
+
ngrams = [tuple(tokens[i:i+n]) for i in range(len(tokens)-n+1)]
|
| 30 |
+
return [ng for ng in ngrams if any(t[0].isupper() for t in ng)]
|
| 31 |
|
| 32 |
+
def crr3(gold_records, predictions):
|
| 33 |
+
"""Capitalized 3-gram survival rate."""
|
| 34 |
+
survived = 0
|
| 35 |
+
total = 0
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
+
for g, p in zip(gold_records, predictions):
|
| 38 |
+
orig_3grams = get_capitalized_ngrams(g.get("original_text", ""), n=3)
|
| 39 |
+
pred_text = p.get("anonymized_text", "").lower()
|
| 40 |
|
| 41 |
+
for ng in orig_3grams:
|
| 42 |
+
total += 1
|
| 43 |
+
if " ".join(ng).lower() in pred_text:
|
| 44 |
+
survived += 1
|
| 45 |
+
|
| 46 |
+
crr = survived / total if total > 0 else 0.0
|
| 47 |
+
return crr * 100
|
|
|
|
|
|
|
| 48 |
|
| 49 |
+
def calculate_bertscore(gold_records, predictions, model_type="distilbert-base-uncased"):
|
| 50 |
"""
|
| 51 |
BERTScore F1 computes the semantic similarity of the texts.
|
| 52 |
+
Pinned model for reproducible benchmarking computations.
|
| 53 |
"""
|
| 54 |
try:
|
| 55 |
from bert_score import score
|
|
|
|
| 60 |
refs = [g.get("original_text", "") for g in gold_records]
|
| 61 |
cands = [p.get("anonymized_text", "") for p in predictions]
|
| 62 |
|
| 63 |
+
P, R, F1 = score(cands, refs, lang="en", verbose=False, model_type=model_type)
|
|
|
|
| 64 |
return F1.mean().item() * 100
|
| 65 |
|
| 66 |
def main():
|
| 67 |
parser = argparse.ArgumentParser(description="SAHA-AL Benchmark Evaluator")
|
| 68 |
parser.add_argument("--gold", type=str, default="data/test.jsonl", help="Path to gold dataset (e.g. test.jsonl)")
|
| 69 |
parser.add_argument("--pred", type=str, required=True, help="Path to predictions JSONL")
|
| 70 |
+
parser.add_argument("--bert-model", type=str, default="distilbert-base-uncased",
|
| 71 |
+
help="Model to use for BERTScore (e.g., microsoft/deberta-xlarge-mnli)")
|
| 72 |
args = parser.parse_args()
|
| 73 |
|
| 74 |
with open(args.gold, "r", encoding="utf-8") as f:
|
|
|
|
| 82 |
|
| 83 |
print(f"Evaluating {len(predictions)} records...")
|
| 84 |
|
| 85 |
+
elr, leaked, total_ents = entity_leakage_rate(gold_records, predictions)
|
| 86 |
+
crr_3 = crr3(gold_records, predictions)
|
| 87 |
+
bert_f1 = calculate_bertscore(gold_records, predictions, model_type=args.bert_model)
|
| 88 |
|
| 89 |
print("\n" + "="*40)
|
| 90 |
print(" SAHA-AL Benchmark Results")
|
| 91 |
print("="*40)
|
| 92 |
print(f" Entity Leakage Rate (ELR ↓): {elr:5.2f}% ({leaked}/{total_ents} leaked)")
|
| 93 |
+
print(f" Contextual Re-ID (CRR-3 ↓): {crr_3:5.2f}%")
|
| 94 |
if bert_f1 is not None:
|
| 95 |
+
print(f" BERTScore (F1 ↑): {bert_f1:5.2f} (Model: {args.bert_model})")
|
| 96 |
else:
|
| 97 |
print(f" BERTScore (F1 ↑): N/A")
|
| 98 |
print("="*40)
|