-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_usage.py
More file actions
191 lines (145 loc) · 6.93 KB
/
example_usage.py
File metadata and controls
191 lines (145 loc) · 6.93 KB
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
"""
Example usage of the PII Validation Classifier.
Demonstrates various use cases and integration patterns.
"""
from inference.pipeline import PII_ValidationPipeline
import pandas as pd
def example_single_validation():
"""Example: Validate a single entity."""
print("=" * 60)
print("Example 1: Single Entity Validation")
print("=" * 60)
pipeline = PII_ValidationPipeline()
# Test a Chilean RUT
result = pipeline.validate(
text="15.783.037-6",
entity_type="ID",
country="CL"
)
print(f"\nInput: 15.783.037-6 (Chilean RUT)")
print(f"Is PII: {result['is_pii']}")
print(f"Confidence: {result['confidence']:.2%}")
print(f"Validation Path: {result['validation_path']}")
print(f"Reason: {result['reason']}")
print(f"\nDetails:")
for key, value in result['details'].items():
print(f" {key}: {value}")
def example_batch_validation():
"""Example: Validate multiple entities at once."""
print("\n" + "=" * 60)
print("Example 2: Batch Validation")
print("=" * 60)
pipeline = PII_ValidationPipeline()
entities = [
{"text": "15.783.037-6", "entity_type": "ID", "country": "CL"},
{"text": "andres.vera@gmail.com", "entity_type": "EMAIL", "country": "CL"},
{"text": "+56 9 8765 4321", "entity_type": "PHONE", "country": "CL"},
{"text": "Juan Pérez González", "entity_type": "PER", "country": "CL"},
{"text": "test@test", "entity_type": "EMAIL", "country": "CL"}, # Invalid
{"text": "1111111111", "entity_type": "PHONE", "country": "CL"}, # Invalid
]
results = pipeline.validate_batch(entities)
print("\nValidation Results:")
print("-" * 60)
for entity, result in zip(entities, results):
status = "✓ PII" if result['is_pii'] else "✗ NOT PII"
print(f"{entity['text']:30s} | {status:10s} | Conf: {result['confidence']:.2%}")
def example_multi_country():
"""Example: Validate entities from different countries."""
print("\n" + "=" * 60)
print("Example 3: Multi-Country Validation")
print("=" * 60)
pipeline = PII_ValidationPipeline()
test_cases = [
# Chile
{"text": "15.783.037-6", "entity_type": "ID", "country": "CL", "label": "Chilean RUT"},
# Brazil
{"text": "123.456.789-09", "entity_type": "ID", "country": "BR", "label": "Brazilian CPF"},
{"text": "+55 (11) 9 8765-4321", "entity_type": "PHONE", "country": "BR", "label": "Brazilian Mobile"},
# Uruguay
{"text": "1.234.567-8", "entity_type": "ID", "country": "UY", "label": "Uruguayan CI"},
# Colombia
{"text": "12345678", "entity_type": "ID", "country": "CO", "label": "Colombian CC"},
{"text": "+57 310 123 4567", "entity_type": "PHONE", "country": "CO", "label": "Colombian Mobile"},
]
print("\nResults:")
print("-" * 80)
print(f"{'Label':<25s} {'Text':<25s} {'Result':<12s} {'Confidence':>10s}")
print("-" * 80)
for test_case in test_cases:
label = test_case.pop('label')
result = pipeline.validate(**test_case)
status = "✓ PII" if result['is_pii'] else "✗ NOT PII"
print(f"{label:<25s} {test_case['text']:<25s} {status:<12s} {result['confidence']:>9.1%}")
def example_false_positives():
"""Example: Testing false positive detection."""
print("\n" + "=" * 60)
print("Example 4: False Positive Detection")
print("=" * 60)
pipeline = PII_ValidationPipeline()
# These should be detected as NOT PII
false_positives = [
{"text": "FAC-12345678", "entity_type": "ID", "country": "CL", "description": "Invoice number"},
{"text": "ORD-123456-7", "entity_type": "ID", "country": "CL", "description": "Order number"},
{"text": "Santiago", "entity_type": "PER", "country": "CL", "description": "City name"},
{"text": "Nike", "entity_type": "PER", "country": "CL", "description": "Brand name"},
{"text": "Centro", "entity_type": "LOC", "country": "CL", "description": "Generic location"},
{"text": "32/13/2023", "entity_type": "DATE", "country": "CL", "description": "Invalid date"},
{"text": "test@test", "entity_type": "EMAIL", "country": "CL", "description": "Test email"},
]
print("\nFalse Positive Tests (should all be NOT PII):")
print("-" * 80)
correct = 0
for test_case in false_positives:
desc = test_case.pop('description')
result = pipeline.validate(**test_case)
is_correct = not result['is_pii']
correct += is_correct
status = "✗ NOT PII" if not result['is_pii'] else "✓ PII"
check = "✓" if is_correct else "✗ WRONG"
print(f"{check} {desc:<20s} | {test_case['text']:<20s} | {status}")
print(f"\nCorrectly identified: {correct}/{len(false_positives)} ({correct/len(false_positives)*100:.0f}%)")
def example_dataframe_integration():
"""Example: Integrate with pandas DataFrame (typical NER output)."""
print("\n" + "=" * 60)
print("Example 5: DataFrame Integration")
print("=" * 60)
# Simulate NER model output
ner_output = pd.DataFrame([
{"entity": "15.783.037-6", "entity_type": "ID", "country": "CL"},
{"entity": "andres.vera@gmail.com", "entity_type": "EMAIL", "country": "CL"},
{"entity": "test@test", "entity_type": "EMAIL", "country": "CL"},
{"entity": "Juan Pérez", "entity_type": "PER", "country": "CL"},
{"entity": "Santiago", "entity_type": "PER", "country": "CL"},
{"entity": "+56 9 8765 4321", "entity_type": "PHONE", "country": "CL"},
])
print("\nOriginal NER Output:")
print(ner_output)
# Validate all entities
pipeline = PII_ValidationPipeline()
entities = ner_output.to_dict('records')
entities = [{"text": e["entity"], "entity_type": e["entity_type"], "country": e["country"]}
for e in entities]
results = pipeline.validate_batch(entities)
# Add validation results to DataFrame
ner_output['is_pii'] = [r['is_pii'] for r in results]
ner_output['confidence'] = [r['confidence'] for r in results]
ner_output['validation_path'] = [r['validation_path'] for r in results]
# Filter to keep only validated PII
validated_pii = ner_output[ner_output['is_pii']].copy()
print("\n\nAfter PII Validation (filtered):")
print(validated_pii)
print(f"\n\nSummary:")
print(f" Original entities: {len(ner_output)}")
print(f" Validated as PII: {len(validated_pii)}")
print(f" Filtered out (false positives): {len(ner_output) - len(validated_pii)}")
if __name__ == "__main__":
# Run all examples
example_single_validation()
example_batch_validation()
example_multi_country()
example_false_positives()
example_dataframe_integration()
print("\n" + "=" * 60)
print("All examples completed!")
print("=" * 60)