Upload evaluate.py
Browse files- evaluate.py +104 -0
evaluate.py
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""
|
| 3 |
+
Evaluate privacy-filter-enhanced on fax_number, credit_card_last4, company_contact_block.
|
| 4 |
+
Compare against baseline openai/privacy-filter.
|
| 5 |
+
"""
|
| 6 |
+
|
| 7 |
+
import json
|
| 8 |
+
import torch
|
| 9 |
+
from transformers import AutoTokenizer, AutoModelForTokenClassification
|
| 10 |
+
from seqeval.metrics import classification_report
|
| 11 |
+
|
| 12 |
+
TEST_CASES = [
|
| 13 |
+
{
|
| 14 |
+
"text": "Please send the contract via fax to (555) 867-5309. Attn: Legal Dept.",
|
| 15 |
+
"expected": [("(555) 867-5309", "fax_number")]
|
| 16 |
+
},
|
| 17 |
+
{
|
| 18 |
+
"text": "Your Visa card ending in 4242 was charged $99.00 on 2024-01-15.",
|
| 19 |
+
"expected": [("4242", "credit_card_last4")]
|
| 20 |
+
},
|
| 21 |
+
{
|
| 22 |
+
"text": "Acme Corp\n123 Main St, Suite 100\nPhone: (555) 987-6543\nEmail: contact@acme.com",
|
| 23 |
+
"expected": [("Acme Corp\n123 Main St, Suite 100\nPhone: (555) 987-6543\nEmail: contact@acme.com", "company_contact_block")]
|
| 24 |
+
},
|
| 25 |
+
{
|
| 26 |
+
"text": "From: Alice Smith \u003calice@example.com\u003e\nFax: (212) 555-0199\nCard ending in 8765",
|
| 27 |
+
"expected": [("Alice Smith", "private_person"), ("alice@example.com", "private_email"),
|
| 28 |
+
("(212) 555-0199", "fax_number"), ("8765", "credit_card_last4")]
|
| 29 |
+
},
|
| 30 |
+
]
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
def predict(model_name, text):
|
| 34 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
|
| 35 |
+
model = AutoModelForTokenClassification.from_pretrained(model_name, trust_remote_code=True)
|
| 36 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 37 |
+
model = model.to(device)
|
| 38 |
+
|
| 39 |
+
inputs = tokenizer(text, return_tensors="pt", truncation=True, max_length=512).to(device)
|
| 40 |
+
with torch.no_grad():
|
| 41 |
+
logits = model(**inputs).logits
|
| 42 |
+
preds = torch.argmax(logits, dim=2)[0].cpu().numpy()
|
| 43 |
+
tokens = tokenizer.convert_ids_to_tokens(inputs["input_ids"][0])
|
| 44 |
+
labels = [model.config.id2label.get(p, "O") for p in preds]
|
| 45 |
+
|
| 46 |
+
# Decode BIOES spans
|
| 47 |
+
spans = []
|
| 48 |
+
i = 0
|
| 49 |
+
while i < len(labels):
|
| 50 |
+
lab = labels[i]
|
| 51 |
+
if lab == "O" or lab.startswith("I-") or lab.startswith("E-"):
|
| 52 |
+
i += 1
|
| 53 |
+
continue
|
| 54 |
+
if lab.startswith("S-"):
|
| 55 |
+
cat = lab[2:]
|
| 56 |
+
spans.append((tokens[i], cat))
|
| 57 |
+
i += 1
|
| 58 |
+
elif lab.startswith("B-"):
|
| 59 |
+
cat = lab[2:]
|
| 60 |
+
j = i + 1
|
| 61 |
+
while j < len(labels) and labels[j] in (f"I-{cat}", f"E-{cat}"):
|
| 62 |
+
j += 1
|
| 63 |
+
span_tokens = tokens[i:j]
|
| 64 |
+
spans.append(("".join(span_tokens).replace("##", ""), cat))
|
| 65 |
+
i = j
|
| 66 |
+
else:
|
| 67 |
+
i += 1
|
| 68 |
+
return spans
|
| 69 |
+
|
| 70 |
+
|
| 71 |
+
def main():
|
| 72 |
+
models = {
|
| 73 |
+
"baseline": "openai/privacy-filter",
|
| 74 |
+
"enhanced": "narcolepticchicken/privacy-filter-enhanced",
|
| 75 |
+
}
|
| 76 |
+
|
| 77 |
+
for model_name, model_path in models.items():
|
| 78 |
+
print(f"\n{'='*60}")
|
| 79 |
+
print(f"Model: {model_name}")
|
| 80 |
+
print(f"{'='*60}")
|
| 81 |
+
for case in TEST_CASES:
|
| 82 |
+
text = case["text"]
|
| 83 |
+
expected = case["expected"]
|
| 84 |
+
try:
|
| 85 |
+
preds = predict(model_path, text)
|
| 86 |
+
except Exception as e:
|
| 87 |
+
preds = []
|
| 88 |
+
print(f"ERROR: {e}")
|
| 89 |
+
|
| 90 |
+
print(f"\nText: {text[:80]}...")
|
| 91 |
+
print(f"Expected: {expected}")
|
| 92 |
+
print(f"Predicted: {preds}")
|
| 93 |
+
|
| 94 |
+
# Simple match check
|
| 95 |
+
matched = 0
|
| 96 |
+
for exp_text, exp_label in expected:
|
| 97 |
+
found = any(exp_text in p[0] and p[1] == exp_label for p in preds)
|
| 98 |
+
if found:
|
| 99 |
+
matched += 1
|
| 100 |
+
print(f"Matches: {matched}/{len(expected)}")
|
| 101 |
+
|
| 102 |
+
|
| 103 |
+
if __name__ == "__main__":
|
| 104 |
+
main()
|