to-be commited on
Commit
b80c16d
·
1 Parent(s): 6266635
Files changed (2) hide show
  1. app.py +79 -4
  2. requirements.txt +3 -0
app.py CHANGED
@@ -1,7 +1,82 @@
1
  import gradio as gr
 
2
 
3
- def greet(name):
4
- return "Hello " + name + "!!"
5
 
6
- demo = gr.Interface(fn=greet, inputs="text", outputs="text")
7
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
+ from gliner import GLiNER
3
 
4
+ # Load the model outside of the processing function to ensure it's only loaded once
5
+ model = GLiNER.from_pretrained("nvidia/gliner-pii")
6
 
7
+ # Define a comprehensive default list of PII labels for the user
8
+ DEFAULT_LABELS = [
9
+ "person_name", "email", "phone_number", "address",
10
+ "ssn", "credit_card_number", "date", "user_name"
11
+ ]
12
+
13
+ def redact_pii(text, labels_str, threshold):
14
+ """
15
+ Redact PII from text using the GLiNER model
16
+
17
+ Args:
18
+ text (str): Input text to redact
19
+ labels_str (str): Comma-separated string of labels or empty for defaults
20
+ threshold (float): Confidence threshold for entity detection
21
+
22
+ Returns:
23
+ str: Redacted text with PII replaced by <REDACTED>
24
+ """
25
+ # Process labels
26
+ if not labels_str or not labels_str.strip():
27
+ labels = DEFAULT_LABELS
28
+ else:
29
+ labels = [label.strip() for label in labels_str.split(",")]
30
+
31
+ # Predict entities
32
+ entities = model.predict_entities(text, labels, threshold=threshold)
33
+
34
+ # Redact entities in reverse order to prevent index shifting
35
+ redacted_text = text
36
+ for entity in reversed(entities):
37
+ start = entity["start"]
38
+ end = entity["end"]
39
+ redacted_text = redacted_text[:start] + "<REDACTED>" + redacted_text[end:]
40
+
41
+ return redacted_text
42
+
43
+ # Create the Gradio interface
44
+ with gr.Blocks(title="GLiNER PII Redaction App") as demo:
45
+ gr.Markdown("# GLiNER PII Redaction App")
46
+ gr.Markdown("This app uses the NVIDIA GLiNER PII model to detect and redact personally identifiable information from text.")
47
+
48
+ with gr.Row():
49
+ with gr.Column():
50
+ input_text = gr.Textbox(
51
+ label="Input Text (Plain, Markdown, or HTML)",
52
+ lines=10,
53
+ placeholder="Enter the text you want to redact..."
54
+ )
55
+ custom_labels = gr.Textbox(
56
+ label="Custom PII Labels (comma-separated)",
57
+ placeholder="person_name, email, phone_number, address, ssn, credit_card_number, date, user_name"
58
+ )
59
+ threshold = gr.Slider(
60
+ minimum=0.0,
61
+ maximum=1.0,
62
+ step=0.05,
63
+ value=0.5,
64
+ label="Confidence Threshold"
65
+ )
66
+ submit_btn = gr.Button("Redact PII")
67
+
68
+ with gr.Column():
69
+ output_text = gr.Textbox(
70
+ label="Redacted Output",
71
+ lines=10,
72
+ interactive=False
73
+ )
74
+
75
+ submit_btn.click(
76
+ fn=redact_pii,
77
+ inputs=[input_text, custom_labels, threshold],
78
+ outputs=output_text
79
+ )
80
+
81
+ if __name__ == "__main__":
82
+ demo.launch()
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ gradio
2
+ gliner
3
+ transformers