DexterSptizu
commited on
Commit
•
57f002f
1
Parent(s):
a8bb9e9
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,91 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
from transformers import AutoTokenizer, AutoModelForTokenClassification
|
3 |
+
import gradio as gr
|
4 |
+
|
5 |
+
# Load the tokenizer and model
|
6 |
+
model_name = "iiiorg/piiranha-v1-detect-personal-information"
|
7 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
8 |
+
model = AutoModelForTokenClassification.from_pretrained(model_name)
|
9 |
+
|
10 |
+
# Set device to GPU if available
|
11 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
12 |
+
model.to(device)
|
13 |
+
|
14 |
+
def apply_redaction(masked_text, start, end, pii_type, aggregate_redaction):
|
15 |
+
for j in range(start, end):
|
16 |
+
masked_text[j] = ''
|
17 |
+
if aggregate_redaction:
|
18 |
+
masked_text[start] = '[redacted]'
|
19 |
+
else:
|
20 |
+
masked_text[start] = f'[{pii_type}]'
|
21 |
+
|
22 |
+
def mask_pii(text, aggregate_redaction=True):
|
23 |
+
# Tokenize input text
|
24 |
+
inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True)
|
25 |
+
inputs = {k: v.to(device) for k, v in inputs.items()}
|
26 |
+
|
27 |
+
# Get the model predictions
|
28 |
+
with torch.no_grad():
|
29 |
+
outputs = model(**inputs)
|
30 |
+
|
31 |
+
# Get the predicted labels
|
32 |
+
predictions = torch.argmax(outputs.logits, dim=-1)
|
33 |
+
|
34 |
+
# Convert token predictions to word predictions
|
35 |
+
encoded_inputs = tokenizer.encode_plus(text, return_offsets_mapping=True, add_special_tokens=True)
|
36 |
+
offset_mapping = encoded_inputs['offset_mapping']
|
37 |
+
|
38 |
+
masked_text = list(text)
|
39 |
+
is_redacting = False
|
40 |
+
redaction_start = 0
|
41 |
+
current_pii_type = ''
|
42 |
+
|
43 |
+
for i, (start, end) in enumerate(offset_mapping):
|
44 |
+
if start == end: # Special token
|
45 |
+
continue
|
46 |
+
|
47 |
+
label = predictions[0][i].item()
|
48 |
+
if label != model.config.label2id['O']: # Non-O label
|
49 |
+
pii_type = model.config.id2label[label]
|
50 |
+
if not is_redacting:
|
51 |
+
is_redacting = True
|
52 |
+
redaction_start = start
|
53 |
+
current_pii_type = pii_type
|
54 |
+
elif not aggregate_redaction and pii_type != current_pii_type:
|
55 |
+
# End current redaction and start a new one
|
56 |
+
apply_redaction(masked_text, redaction_start, start, current_pii_type, aggregate_redaction)
|
57 |
+
redaction_start = start
|
58 |
+
current_pii_type = pii_type
|
59 |
+
else:
|
60 |
+
if is_redacting:
|
61 |
+
apply_redaction(masked_text, redaction_start, end, current_pii_type, aggregate_redaction)
|
62 |
+
is_redacting = False
|
63 |
+
|
64 |
+
# Handle case where PII is at the end of the text
|
65 |
+
if is_redacting:
|
66 |
+
apply_redaction(masked_text, redaction_start, len(masked_text), current_pii_type, aggregate_redaction)
|
67 |
+
|
68 |
+
return ''.join(masked_text)
|
69 |
+
|
70 |
+
# Define the function for Gradio interface
|
71 |
+
def redact_text(text, aggregate_redaction):
|
72 |
+
return mask_pii(text, aggregate_redaction)
|
73 |
+
|
74 |
+
# Create Gradio Interface
|
75 |
+
demo = gr.Interface(
|
76 |
+
fn=redact_text,
|
77 |
+
inputs=[
|
78 |
+
gr.Textbox(lines=5, label="Enter Text with Potential PII"),
|
79 |
+
gr.Checkbox(label="Aggregate Redaction", value=True)
|
80 |
+
],
|
81 |
+
outputs="text",
|
82 |
+
title="PII Detection and Redaction",
|
83 |
+
description="This application detects personal identifiable information (PII) and redacts it from the provided text. You can choose to either aggregate all PII redaction into a single '[redacted]' label or keep each PII type labeled individually.",
|
84 |
+
examples=[
|
85 |
+
["John Doe's phone number is 123-456-7890, and his email is john.doe@example.com."],
|
86 |
+
["Jane was born on 12th August, 1990 and her SSN is 987-65-4321."]
|
87 |
+
]
|
88 |
+
)
|
89 |
+
|
90 |
+
if __name__ == "__main__":
|
91 |
+
demo.launch()
|