Views
No views yet
1
2# First install Hugging Face transformers library
3!pip install transformers
4
5# Initialize and run the PII detection pipeline to extract PII entities
6from transformers import pipeline
7
8## Initialize the PII detection pipeline
9ner = pipeline("ner", model="kalyan-ks/ettin-conll-ner-17m", aggregation_strategy="simple")
10
11input_text = "Kalyan KS is from India. His email id is kalyan.ks@yahoo.com"
12
13## Run the PII detection to extract PII entities
14pii_entities = ner(input_text)
15
16## Process the extracted PII entities
17def format_pii_entities(entities, original_text):
18 if not entities:
19 return []
20
21 merged_entities = []
22
23 entities = sorted(entities, key=lambda x: x['start'])
24
25 current_entity = {
26 'start': entities[0]['start'],
27 'end': entities[0]['end'],
28 'label': entities[0]['entity_group'],
29 'text': entities[0]['word']
30 }
31
32 for next_ent in entities[1:]:
33 is_same_label = next_ent['entity_group'] == current_entity['label']
34 is_adjacent = next_ent['start'] <= current_entity['end'] + 1
35
36 if is_same_label and is_adjacent:
37 current_entity['end'] = max(current_entity['end'], next_ent['end'])
38 current_entity['text'] = original_text[current_entity['start']:current_entity['end']]
39 else:
40 merged_entities.append(clean_entity(current_entity))
41 current_entity = {
42 'start': next_ent['start'],
43 'end': next_ent['end'],
44 'label': next_ent['entity_group'],
45 'text': next_ent['word']
46 }
47
48 merged_entities.append(clean_entity(current_entity))
49 return merged_entities
50
51def clean_entity(ent):
52
53 raw_text = ent['text']
54 stripped_text = raw_text.strip()
55 leading_spaces = len(raw_text) - len(raw_text.lstrip())
56
57 return {
58 'start': ent['start'] + leading_spaces,
59 'end': ent['start'] + leading_spaces + len(stripped_text),
60 'text': stripped_text,
61 'label': ent['label']
62 }
63
64# Display the extracted PII entities
65formatted_entities = format_pii_entities(pii_entities, input_text)
66print(formatted_entities)
67
68# Output
69[{'start': 0, 'end': 9, 'text': 'Kalyan KS', 'label': 'first_name'}, {'start': 18, 'end': 23, 'text': 'India', 'label': 'country'}, {'start': 41, 'end': 60, 'text': 'kalyan.ks@yahoo.com', 'label': 'email'}]1@misc{ettin-conll-ner-17m,
2 title = {ettin-conll-ner-17m-2026: NER Model},
3 author = {Kalyan KS},
4 year = {2026},
5 publisher = {Hugging Face},
6 url = {https://huggingface.co/kalyan-ks/ettin-conll-ner-17m}
7}