Spaces:
Running
Running
File size: 11,596 Bytes
084dab1 78a1bf0 084dab1 78a1bf0 084dab1 78a1bf0 084dab1 3caf047 78a1bf0 3caf047 78a1bf0 084dab1 78a1bf0 084dab1 78a1bf0 084dab1 78a1bf0 084dab1 78a1bf0 084dab1 78a1bf0 084dab1 78a1bf0 084dab1 78a1bf0 084dab1 78a1bf0 084dab1 78a1bf0 084dab1 78a1bf0 084dab1 78a1bf0 084dab1 78a1bf0 084dab1 78a1bf0 084dab1 78a1bf0 084dab1 78a1bf0 084dab1 78a1bf0 084dab1 78a1bf0 084dab1 78a1bf0 3caf047 78a1bf0 084dab1 78a1bf0 084dab1 78a1bf0 084dab1 78a1bf0 084dab1 78a1bf0 084dab1 78a1bf0 084dab1 78a1bf0 084dab1 78a1bf0 084dab1 78a1bf0 084dab1 78a1bf0 084dab1 78a1bf0 3caf047 78a1bf0 084dab1 78a1bf0 3caf047 78a1bf0 084dab1 78a1bf0 3caf047 78a1bf0 3caf047 78a1bf0 3caf047 78a1bf0 084dab1 78a1bf0 084dab1 78a1bf0 084dab1 78a1bf0 084dab1 78a1bf0 084dab1 78a1bf0 3caf047 78a1bf0 084dab1 78a1bf0 084dab1 b2417a0 78a1bf0 084dab1 78a1bf0 084dab1 78a1bf0 084dab1 78a1bf0 084dab1 78a1bf0 084dab1 78a1bf0 084dab1 78a1bf0 |
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 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 |
import json
import random
from pathlib import Path
from typing import Dict, List, Tuple
import weave
from datasets import load_dataset
from tqdm import tqdm
# Add this mapping dictionary near the top of the file
PRESIDIO_TO_TRANSFORMER_MAPPING = {
"EMAIL_ADDRESS": "EMAIL",
"PHONE_NUMBER": "TELEPHONENUM",
"US_SSN": "SOCIALNUM",
"CREDIT_CARD": "CREDITCARDNUMBER",
"IP_ADDRESS": "IDCARDNUM",
"DATE_TIME": "DATEOFBIRTH",
"US_PASSPORT": "IDCARDNUM",
"US_DRIVER_LICENSE": "DRIVERLICENSENUM",
"US_BANK_NUMBER": "ACCOUNTNUM",
"LOCATION": "CITY",
"URL": "USERNAME", # URLs often contain usernames
"IN_PAN": "TAXNUM", # Indian Permanent Account Number
"UK_NHS": "IDCARDNUM",
"SG_NRIC_FIN": "IDCARDNUM",
"AU_ABN": "TAXNUM", # Australian Business Number
"AU_ACN": "TAXNUM", # Australian Company Number
"AU_TFN": "TAXNUM", # Australian Tax File Number
"AU_MEDICARE": "IDCARDNUM",
"IN_AADHAAR": "IDCARDNUM", # Indian national ID
"IN_VOTER": "IDCARDNUM",
"IN_PASSPORT": "IDCARDNUM",
"CRYPTO": "ACCOUNTNUM", # Cryptocurrency addresses
"IBAN_CODE": "ACCOUNTNUM",
"MEDICAL_LICENSE": "IDCARDNUM",
"IN_VEHICLE_REGISTRATION": "IDCARDNUM",
}
def load_ai4privacy_dataset(
num_samples: int = 100, split: str = "validation"
) -> List[Dict]:
"""
Load and prepare samples from the ai4privacy dataset.
Args:
num_samples: Number of samples to evaluate
split: Dataset split to use ("train" or "validation")
Returns:
List of prepared test cases
"""
# Load the dataset
dataset = load_dataset("ai4privacy/pii-masking-400k")
# Get the specified split
data_split = dataset[split]
# Randomly sample entries if num_samples is less than total
if num_samples < len(data_split):
indices = random.sample(range(len(data_split)), num_samples)
samples = [data_split[i] for i in indices]
else:
samples = data_split
# Convert to test case format
test_cases = []
for sample in samples:
# Extract entities from privacy_mask
entities: Dict[str, List[str]] = {}
for entity in sample["privacy_mask"]:
label = entity["label"]
value = entity["value"]
if label not in entities:
entities[label] = []
entities[label].append(value)
test_case = {
"description": f"AI4Privacy Sample (ID: {sample['uid']})",
"input_text": sample["source_text"],
"expected_entities": entities,
"masked_text": sample["masked_text"],
"language": sample["language"],
"locale": sample["locale"],
}
test_cases.append(test_case)
return test_cases
@weave.op()
def evaluate_model(guardrail, test_cases: List[Dict]) -> Tuple[Dict, List[Dict]]:
"""
Evaluate a model on the test cases.
Args:
guardrail: Entity recognition guardrail to evaluate
test_cases: List of test cases
Returns:
Tuple of (metrics dict, detailed results list)
"""
metrics = {
"total": len(test_cases),
"passed": 0,
"failed": 0,
"entity_metrics": {}, # Will store precision/recall per entity type
}
detailed_results = []
for test_case in tqdm(test_cases, desc="Evaluating samples"):
# Run detection
result = guardrail.guard(test_case["input_text"])
detected = result.detected_entities
expected = test_case["expected_entities"]
# Map Presidio entities if this is the Presidio guardrail
if isinstance(guardrail, PresidioEntityRecognitionGuardrail):
mapped_detected = {}
for entity_type, values in detected.items():
mapped_type = PRESIDIO_TO_TRANSFORMER_MAPPING.get(entity_type)
if mapped_type:
if mapped_type not in mapped_detected:
mapped_detected[mapped_type] = []
mapped_detected[mapped_type].extend(values)
detected = mapped_detected
# Track entity-level metrics
all_entity_types = set(list(detected.keys()) + list(expected.keys()))
entity_results = {}
for entity_type in all_entity_types:
detected_set = set(detected.get(entity_type, []))
expected_set = set(expected.get(entity_type, []))
# Calculate metrics
true_positives = len(detected_set & expected_set)
false_positives = len(detected_set - expected_set)
false_negatives = len(expected_set - detected_set)
precision = (
true_positives / (true_positives + false_positives)
if (true_positives + false_positives) > 0
else 0
)
recall = (
true_positives / (true_positives + false_negatives)
if (true_positives + false_negatives) > 0
else 0
)
f1 = (
2 * (precision * recall) / (precision + recall)
if (precision + recall) > 0
else 0
)
entity_results[entity_type] = {
"precision": precision,
"recall": recall,
"f1": f1,
"true_positives": true_positives,
"false_positives": false_positives,
"false_negatives": false_negatives,
}
# Aggregate metrics
if entity_type not in metrics["entity_metrics"]:
metrics["entity_metrics"][entity_type] = {
"total_true_positives": 0,
"total_false_positives": 0,
"total_false_negatives": 0,
}
metrics["entity_metrics"][entity_type][
"total_true_positives"
] += true_positives
metrics["entity_metrics"][entity_type][
"total_false_positives"
] += false_positives
metrics["entity_metrics"][entity_type][
"total_false_negatives"
] += false_negatives
# Store detailed result
detailed_result = {
"id": test_case.get("description", ""),
"language": test_case.get("language", ""),
"locale": test_case.get("locale", ""),
"input_text": test_case["input_text"],
"expected_entities": expected,
"detected_entities": detected,
"entity_metrics": entity_results,
"anonymized_text": (
result.anonymized_text if result.anonymized_text else None
),
}
detailed_results.append(detailed_result)
# Update pass/fail counts
if all(entity_results[et]["f1"] == 1.0 for et in entity_results):
metrics["passed"] += 1
else:
metrics["failed"] += 1
# Calculate final entity metrics and track totals for overall metrics
total_tp = 0
total_fp = 0
total_fn = 0
for entity_type, counts in metrics["entity_metrics"].items():
tp = counts["total_true_positives"]
fp = counts["total_false_positives"]
fn = counts["total_false_negatives"]
total_tp += tp
total_fp += fp
total_fn += fn
precision = tp / (tp + fp) if (tp + fp) > 0 else 0
recall = tp / (tp + fn) if (tp + fn) > 0 else 0
f1 = (
2 * (precision * recall) / (precision + recall)
if (precision + recall) > 0
else 0
)
metrics["entity_metrics"][entity_type].update(
{"precision": precision, "recall": recall, "f1": f1}
)
# Calculate overall metrics
overall_precision = (
total_tp / (total_tp + total_fp) if (total_tp + total_fp) > 0 else 0
)
overall_recall = (
total_tp / (total_tp + total_fn) if (total_tp + total_fn) > 0 else 0
)
overall_f1 = (
2 * (overall_precision * overall_recall) / (overall_precision + overall_recall)
if (overall_precision + overall_recall) > 0
else 0
)
metrics["overall"] = {
"precision": overall_precision,
"recall": overall_recall,
"f1": overall_f1,
"total_true_positives": total_tp,
"total_false_positives": total_fp,
"total_false_negatives": total_fn,
}
return metrics, detailed_results
def save_results(
metrics: Dict,
detailed_results: List[Dict],
model_name: str,
output_dir: str = "evaluation_results",
):
"""Save evaluation results to files"""
output_dir = Path(output_dir)
output_dir.mkdir(exist_ok=True)
# Save metrics summary
with open(output_dir / f"{model_name}_metrics.json", "w") as f:
json.dump(metrics, f, indent=2)
# Save detailed results
with open(output_dir / f"{model_name}_detailed_results.json", "w") as f:
json.dump(detailed_results, f, indent=2)
def print_metrics_summary(metrics: Dict):
"""Print a summary of the evaluation metrics"""
print("\nEvaluation Summary")
print("=" * 80)
print(f"Total Samples: {metrics['total']}")
print(f"Passed: {metrics['passed']}")
print(f"Failed: {metrics['failed']}")
print(f"Success Rate: {(metrics['passed']/metrics['total'])*100:.1f}%")
# Print overall metrics
print("\nOverall Metrics:")
print("-" * 80)
print(f"{'Metric':<20} {'Value':>10}")
print("-" * 80)
print(f"{'Precision':<20} {metrics['overall']['precision']:>10.2f}")
print(f"{'Recall':<20} {metrics['overall']['recall']:>10.2f}")
print(f"{'F1':<20} {metrics['overall']['f1']:>10.2f}")
print("\nEntity-level Metrics:")
print("-" * 80)
print(f"{'Entity Type':<20} {'Precision':>10} {'Recall':>10} {'F1':>10}")
print("-" * 80)
for entity_type, entity_metrics in metrics["entity_metrics"].items():
print(
f"{entity_type:<20} {entity_metrics['precision']:>10.2f} {entity_metrics['recall']:>10.2f} {entity_metrics['f1']:>10.2f}"
)
def main():
"""Main evaluation function"""
weave.init("guardrails-genie-pii-evaluation-demo")
# Load test cases
test_cases = load_ai4privacy_dataset(num_samples=100)
# Initialize models to evaluate
models = {
"regex": RegexEntityRecognitionGuardrail(
should_anonymize=True, show_available_entities=True
),
"presidio": PresidioEntityRecognitionGuardrail(
should_anonymize=True, show_available_entities=True
),
"transformers": TransformersEntityRecognitionGuardrail(
should_anonymize=True, show_available_entities=True
),
}
# Evaluate each model
for model_name, guardrail in models.items():
print(f"\nEvaluating {model_name} model...")
metrics, detailed_results = evaluate_model(guardrail, test_cases)
# Print and save results
print_metrics_summary(metrics)
save_results(metrics, detailed_results, model_name)
if __name__ == "__main__":
from guardrails_genie.guardrails.entity_recognition.presidio_entity_recognition_guardrail import (
PresidioEntityRecognitionGuardrail,
)
from guardrails_genie.guardrails.entity_recognition.regex_entity_recognition_guardrail import (
RegexEntityRecognitionGuardrail,
)
from guardrails_genie.guardrails.entity_recognition.transformers_entity_recognition_guardrail import (
TransformersEntityRecognitionGuardrail,
)
main()
|