IS361Group4 commited on
Commit
43b2e08
·
verified ·
1 Parent(s): 90b519b

Update personal_info_identifier.py

Browse files
Files changed (1) hide show
  1. personal_info_identifier.py +38 -9
personal_info_identifier.py CHANGED
@@ -1,16 +1,45 @@
1
  import gradio as gr
2
  from transformers import pipeline
 
3
 
4
- # โหลด pipeline
5
- pii_detector = pipeline("token-classification", model="iiiorg/piiranha-v1-detect-personal-information", aggregation_strategy="simple")
 
 
 
 
6
 
7
- def detect_pii(text):
8
- results = pii_detector(text)
9
- return results
 
 
 
 
 
 
 
 
 
 
 
 
 
10
 
11
  def create_personal_info_tab():
12
  with gr.Column():
13
- gr.Markdown("### Detect Personal Information (PII)")
14
- textbox = gr.Textbox(label="Enter text", lines=5)
15
- output = gr.JSON(label="Detected Entities")
16
- textbox.change(fn=detect_pii, inputs=textbox, outputs=output)
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
  from transformers import pipeline
3
+ import re
4
 
5
+ # โหลดโมเดล PII Detection
6
+ pii_detector = pipeline(
7
+ "token-classification",
8
+ model="iiiorg/piiranha-v1-detect-personal-information",
9
+ aggregation_strategy="simple"
10
+ )
11
 
12
+ def highlight_pii(text):
13
+ entities = pii_detector(text)
14
+ highlighted_text = text
15
+ offset = 0 # For shifting positions when inserting tags
16
+
17
+ for entity in sorted(entities, key=lambda x: x['start']):
18
+ start = entity['start'] + offset
19
+ end = entity['end'] + offset
20
+ label = entity['entity_group']
21
+
22
+ # wrap entity with a span tag with color (red for demo, can be better)
23
+ span = f'<span style="background-color:#ffcccc; padding:2px 4px; border-radius:4px;">{text[entity["start"]:entity["end"]]} <b style="color:red;">[{label}]</b></span>'
24
+ highlighted_text = highlighted_text[:start] + span + highlighted_text[end:]
25
+ offset += len(span) - (end - start)
26
+
27
+ return highlighted_text
28
 
29
  def create_personal_info_tab():
30
  with gr.Column():
31
+ gr.Markdown("### Personal Information Identifier")
32
+ input_text = gr.Textbox(label="Enter text", lines=5, placeholder="Type your text here...")
33
+ output_html = gr.HTML()
34
+
35
+ submit_button = gr.Button("Submit")
36
+
37
+ submit_button.click(fn=highlight_pii, inputs=input_text, outputs=output_html)
38
+
39
+ # Optional: Add example
40
+ gr.Examples(
41
+ examples=[
42
+ "Hugging Face is a company based in Paris and New York City that acquired Gradio in 2021."
43
+ ],
44
+ inputs=input_text
45
+ )