Upload 2 files
Browse filesupdated masks personnumber also better recognition of names.
- anonymize.py +140 -127
- app.py +64 -33
anonymize.py
CHANGED
|
@@ -1,128 +1,141 @@
|
|
| 1 |
-
import regex as re
|
| 2 |
-
from typing import List, Tuple, Dict, Set
|
| 3 |
-
from transformers import AutoTokenizer, AutoModelForTokenClassification, pipeline
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
class SwedishTextMasker:
|
| 7 |
-
def __init__(self, model_name: str = "RecordedFuture/Swedish-NER", threshold: float = 0.85):
|
| 8 |
-
self.tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 9 |
-
self.model = AutoModelForTokenClassification.from_pretrained(model_name)
|
| 10 |
-
self.ner_pipeline = pipeline("ner", model=self.model, tokenizer=self.tokenizer, aggregation_strategy="simple")
|
| 11 |
-
self.threshold = threshold
|
| 12 |
-
|
| 13 |
-
def _reconstruct_entities(self, tokens_with_labels: List[Tuple[str, str, float]]) -> List[Tuple[str, str]]:
|
| 14 |
-
words = []
|
| 15 |
-
current_word = ''
|
| 16 |
-
current_label = ''
|
| 17 |
-
scores = []
|
| 18 |
-
|
| 19 |
-
for token, label, score in tokens_with_labels:
|
| 20 |
-
if token.startswith('##'):
|
| 21 |
-
current_word += token[2:]
|
| 22 |
-
scores.append(score)
|
| 23 |
-
else:
|
| 24 |
-
if current_word:
|
| 25 |
-
words.append((current_word, current_label , sum(scores) / len(scores)))
|
| 26 |
-
current_word, current_label = token, label
|
| 27 |
-
scores = [score]
|
| 28 |
-
|
| 29 |
-
if current_word:
|
| 30 |
-
words.append((current_word, current_label , sum(scores) / len(scores)))
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 128 |
return text
|
|
|
|
| 1 |
+
import regex as re
|
| 2 |
+
from typing import List, Tuple, Dict, Set
|
| 3 |
+
from transformers import AutoTokenizer, AutoModelForTokenClassification, pipeline
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
class SwedishTextMasker:
|
| 7 |
+
def __init__(self, model_name: str = "RecordedFuture/Swedish-NER", threshold: float = 0.85):
|
| 8 |
+
self.tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 9 |
+
self.model = AutoModelForTokenClassification.from_pretrained(model_name)
|
| 10 |
+
self.ner_pipeline = pipeline("ner", model=self.model, tokenizer=self.tokenizer, aggregation_strategy="simple")
|
| 11 |
+
self.threshold = threshold
|
| 12 |
+
|
| 13 |
+
def _reconstruct_entities(self, tokens_with_labels: List[Tuple[str, str, float]]) -> List[Tuple[str, str]]:
|
| 14 |
+
words = []
|
| 15 |
+
current_word = ''
|
| 16 |
+
current_label = ''
|
| 17 |
+
scores = []
|
| 18 |
+
print("token with labels: " ,tokens_with_labels)
|
| 19 |
+
for token, label, score in tokens_with_labels:
|
| 20 |
+
if token.startswith('##'):
|
| 21 |
+
current_word += token[2:]
|
| 22 |
+
scores.append(score)
|
| 23 |
+
else:
|
| 24 |
+
if current_word:
|
| 25 |
+
words.append((current_word, current_label , sum(scores) / len(scores)))
|
| 26 |
+
current_word, current_label = token, label
|
| 27 |
+
scores = [score]
|
| 28 |
+
|
| 29 |
+
if current_word:
|
| 30 |
+
words.append((current_word, current_label , sum(scores) / len(scores)))
|
| 31 |
+
|
| 32 |
+
words = [(re.sub(r'\s*-\s*', '-', w[0]), w[1], w[2]) for w in words]
|
| 33 |
+
result = self._in_order_ent_list(words)
|
| 34 |
+
print("\n\n\n the result of inorder ent list : \n\n" , result)
|
| 35 |
+
return result
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
def _in_order_ent_list(self , all_ents_list):
|
| 39 |
+
threshold_ents = [ent for ent in all_ents_list if ent[2]>=self.threshold]
|
| 40 |
+
threshold_ents_word = {ent[0] for ent in threshold_ents}
|
| 41 |
+
result = [(ent[0] , ent[1]) for ent in all_ents_list if ent[0] in threshold_ents_word and len(ent[0]) >=2]
|
| 42 |
+
return result
|
| 43 |
+
|
| 44 |
+
def _get_chunks(self, text_list: List[str], chunk_size: int = 100) -> List[List[str]]:
|
| 45 |
+
return [text_list[i:i + chunk_size] for i in range(0, len(text_list), chunk_size)]
|
| 46 |
+
|
| 47 |
+
def _retrieve_ner(self, text: str) -> List[Tuple[str, str, float]]:
|
| 48 |
+
results = self.ner_pipeline(text)
|
| 49 |
+
return [
|
| 50 |
+
(ent["word"], ent["entity_group"], ent["score"])
|
| 51 |
+
for ent in results
|
| 52 |
+
if ent["entity_group"] in {"ORG", "PER", "TIT"}
|
| 53 |
+
]
|
| 54 |
+
|
| 55 |
+
def _get_entities(self, text: str) -> List[Tuple[str, str, float]]:
|
| 56 |
+
tokens = text.split()
|
| 57 |
+
if len(tokens) > 100:
|
| 58 |
+
chunks = self._get_chunks(tokens)
|
| 59 |
+
all_ents = []
|
| 60 |
+
for chunk in chunks:
|
| 61 |
+
chunk_text = " ".join(chunk)
|
| 62 |
+
all_ents.extend(self._retrieve_ner(chunk_text))
|
| 63 |
+
return all_ents
|
| 64 |
+
else:
|
| 65 |
+
return self._retrieve_ner(text)
|
| 66 |
+
|
| 67 |
+
def _get_entity_dicts(self, entities: List[Tuple[str, str, float]]) -> Tuple[Dict[str, str], Dict[str, str], Set[str]]:
|
| 68 |
+
persons = [ent[0] for ent in entities if ent[1] == "PER"]
|
| 69 |
+
companies = [ent[0] for ent in entities if ent[1] == "ORG"]
|
| 70 |
+
titles = {ent[0] for ent in entities if ent[1] == "TIT"}
|
| 71 |
+
|
| 72 |
+
person_dict = {name: f"Person {chr(ord('A') + i)}" for i, name in enumerate(dict.fromkeys(persons))}
|
| 73 |
+
company_dict = {name: f"ORG_COMPANY {chr(ord('A') + i)}" for i, name in enumerate(dict.fromkeys(companies))}
|
| 74 |
+
|
| 75 |
+
return person_dict, company_dict, titles
|
| 76 |
+
|
| 77 |
+
|
| 78 |
+
def remove_personnummer(self, text):
|
| 79 |
+
pattern = r"""
|
| 80 |
+
(?<!\d)
|
| 81 |
+
(?:
|
| 82 |
+
\d{6,8}[\-\s]?\d{4} |
|
| 83 |
+
\d{5}[\-\s]?\d{4}
|
| 84 |
+
)
|
| 85 |
+
(?!\d)
|
| 86 |
+
"""
|
| 87 |
+
return re.sub(pattern, '[person/org nummer]', text, flags=re.VERBOSE)
|
| 88 |
+
|
| 89 |
+
@staticmethod
|
| 90 |
+
def mask_digits(text: str) -> str:
|
| 91 |
+
return re.sub(r'\d', 'x', text)
|
| 92 |
+
|
| 93 |
+
def mask_phone_numbers(self, text: str) -> str:
|
| 94 |
+
phone_regex = re.compile(r'(?:\+|00)?\d[\d\s\-()]{5,}\d')
|
| 95 |
+
return phone_regex.sub(lambda m: self.mask_digits(m.group()), text)
|
| 96 |
+
|
| 97 |
+
# def mask_org_numbers(self, text: str) -> str:
|
| 98 |
+
# org_regex = re.compile(r'\b\d{6}-?\d{4}\b')
|
| 99 |
+
# return org_regex.sub(lambda m: self.mask_digits(m.group()), text)
|
| 100 |
+
|
| 101 |
+
def mask_emails(self, text: str) -> str:
|
| 102 |
+
email_regex = re.compile(r'\b([a-zA-Z0-9._%+-]+)@([a-zA-Z0-9.-]+\.[a-zA-Z]{2,})\b')
|
| 103 |
+
def email_masker(match):
|
| 104 |
+
local, domain = match.groups()
|
| 105 |
+
return f"{re.sub(r'[a-zA-Z0-9]', 'x', local)}@{re.sub(r'[a-zA-Z0-9]', 'x', domain)}"
|
| 106 |
+
return email_regex.sub(email_masker, text)
|
| 107 |
+
|
| 108 |
+
def mask_addresses(self, text: str) -> str:
|
| 109 |
+
address_regex = re.compile(
|
| 110 |
+
r'\b(?:[A-ZÅÄÖa-zåäöéÉèÈçÇß0-9\-]+\s)+\d{1,4}\s*,?\s*\d{3}\s?\d{2}\s+[A-ZÅÄÖa-zåäö\-]+',
|
| 111 |
+
re.UNICODE
|
| 112 |
+
)
|
| 113 |
+
return address_regex.sub('[ADDRESS]', text)
|
| 114 |
+
|
| 115 |
+
def mask_entities(self, text: str, entity_dict: Dict[str, str], tag: str) -> str:
|
| 116 |
+
for original, masked in entity_dict.items():
|
| 117 |
+
text = re.sub(re.escape(original), f"[{masked}]", text)
|
| 118 |
+
return text
|
| 119 |
+
|
| 120 |
+
def mask_titles(self, text: str, titles: Set[str]) -> str:
|
| 121 |
+
for title in titles:
|
| 122 |
+
text = re.sub(re.escape(title), "[Person_Title]", text)
|
| 123 |
+
return text
|
| 124 |
+
|
| 125 |
+
def mask_all(self, text: str) -> str:
|
| 126 |
+
old_text_backup = text
|
| 127 |
+
text = self.remove_personnummer(text)
|
| 128 |
+
text = self.mask_phone_numbers(text)
|
| 129 |
+
# text = self.mask_org_numbers(text)
|
| 130 |
+
text = self.mask_emails(text)
|
| 131 |
+
text = self.mask_addresses(text)
|
| 132 |
+
|
| 133 |
+
ents_raw = self._get_entities(old_text_backup)
|
| 134 |
+
ents = self._reconstruct_entities(ents_raw)
|
| 135 |
+
person_dict, company_dict, title_set = self._get_entity_dicts(ents)
|
| 136 |
+
|
| 137 |
+
text = self.mask_entities(text, company_dict, "ORG")
|
| 138 |
+
text = self.mask_entities(text, person_dict, "PER")
|
| 139 |
+
text = self.mask_titles(text, title_set)
|
| 140 |
+
|
| 141 |
return text
|
app.py
CHANGED
|
@@ -1,33 +1,64 @@
|
|
| 1 |
-
import fitz # PyMuPDF
|
| 2 |
-
import gradio as gr
|
| 3 |
-
from anonymize import SwedishTextMasker
|
| 4 |
-
|
| 5 |
-
# Instantiate once, globally
|
| 6 |
-
text_anonymizer = SwedishTextMasker(threshold= 0.
|
| 7 |
-
|
| 8 |
-
def
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
if
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import fitz # PyMuPDF
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from anonymize import SwedishTextMasker
|
| 4 |
+
|
| 5 |
+
# Instantiate once, globally
|
| 6 |
+
text_anonymizer = SwedishTextMasker(threshold= 0.5)
|
| 7 |
+
|
| 8 |
+
def join_short_lines(text, min_length=30):
|
| 9 |
+
"""
|
| 10 |
+
Joins lines that are shorter than min_length with the next line.
|
| 11 |
+
"""
|
| 12 |
+
lines = text.split('\n')
|
| 13 |
+
new_lines = []
|
| 14 |
+
buffer = ""
|
| 15 |
+
for line in lines:
|
| 16 |
+
stripped = line.strip()
|
| 17 |
+
if not stripped:
|
| 18 |
+
if buffer:
|
| 19 |
+
new_lines.append(buffer)
|
| 20 |
+
buffer = ""
|
| 21 |
+
new_lines.append("") # preserve empty lines
|
| 22 |
+
continue
|
| 23 |
+
if len(stripped) < min_length and not stripped.endswith(('.', ':', ';', '?', '!')):
|
| 24 |
+
buffer += " " + stripped if buffer else stripped
|
| 25 |
+
else:
|
| 26 |
+
if buffer:
|
| 27 |
+
buffer += " " + stripped
|
| 28 |
+
new_lines.append(buffer)
|
| 29 |
+
buffer = ""
|
| 30 |
+
else:
|
| 31 |
+
new_lines.append(stripped)
|
| 32 |
+
if buffer:
|
| 33 |
+
new_lines.append(buffer)
|
| 34 |
+
return "\n".join(new_lines)
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
def extract_text_from_pdf(pdf_file):
|
| 38 |
+
if pdf_file is None:
|
| 39 |
+
return "No file uploaded."
|
| 40 |
+
|
| 41 |
+
# Approach 1: open via file path (usually safer)
|
| 42 |
+
with fitz.open(pdf_file.name) as doc:
|
| 43 |
+
text_output = ""
|
| 44 |
+
for page in doc:
|
| 45 |
+
text_output += page.get_text(flags=1)
|
| 46 |
+
|
| 47 |
+
raw_text = text_output.strip()
|
| 48 |
+
# raw_text = join_short_lines(raw_text) # <--- Add this line!
|
| 49 |
+
print(raw_text)
|
| 50 |
+
anonymized_text = text_anonymizer.mask_all(raw_text)
|
| 51 |
+
return anonymized_text
|
| 52 |
+
|
| 53 |
+
# Gradio interface
|
| 54 |
+
with gr.Blocks(title="PDF -> Anonymized Text") as demo:
|
| 55 |
+
gr.Markdown("### 📄 PDF Anonymizer (text only, skips images)")
|
| 56 |
+
with gr.Row():
|
| 57 |
+
pdf_input = gr.File(label="Upload a PDF", file_types=[".pdf"])
|
| 58 |
+
text_output = gr.Textbox(label="Anonymized Output", lines=20, interactive=False)
|
| 59 |
+
|
| 60 |
+
extract_button = gr.Button("Anonymize Text")
|
| 61 |
+
extract_button.click(fn=extract_text_from_pdf, inputs=pdf_input, outputs=text_output)
|
| 62 |
+
|
| 63 |
+
if __name__ == "__main__":
|
| 64 |
+
demo.launch()
|