Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import re
|
| 3 |
+
|
| 4 |
+
# === Redaction patterns ===
|
| 5 |
+
PII_PATTERNS = {
|
| 6 |
+
"EMAIL": re.compile(r"\b[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}\b"),
|
| 7 |
+
"ZIP": re.compile(r"\b\d{5}(?:-\d{4})?\b"),
|
| 8 |
+
"ADDRESS": re.compile(r"\b\d{1,5}\s\w+(?:\s\w+)*,\s*\w+,\s*[A-Z]{2}\s*\d{5}\b"),
|
| 9 |
+
"NAME": re.compile(r"\b([A-Z][a-z]+(?:\s+[A-Z][a-z]+)+)\b")
|
| 10 |
+
}
|
| 11 |
+
|
| 12 |
+
def redact_text(sentence):
|
| 13 |
+
for label, pattern in PII_PATTERNS.items():
|
| 14 |
+
sentence = pattern.sub(f"[REDACTED_{label}]", sentence)
|
| 15 |
+
return sentence
|
| 16 |
+
|
| 17 |
+
# === Gradio UI ===
|
| 18 |
+
demo = gr.Interface(
|
| 19 |
+
fn=redact_text,
|
| 20 |
+
inputs=gr.Textbox(label="Enter a sentence containing PII"),
|
| 21 |
+
outputs=gr.Textbox(label="Redacted sentence"),
|
| 22 |
+
title="PII Redactor Demo",
|
| 23 |
+
description="Paste a sentence and see PII like emails, addresses, ZIPs, or names redacted in real-time.",
|
| 24 |
+
allow_flagging="never"
|
| 25 |
+
)
|
| 26 |
+
|
| 27 |
+
demo.launch()
|