A zero-shot NER and document anonymization package.
Project description
Anonymizer
A lightweight Python library for Zero-Shot Named Entity Recognition (NER) and Document Anonymization, powered by GLiNER 2.
Anonymizer allows you to extract custom entities from PDFs and Word documents, dynamically redact them, and most importantly, it includes a built-in memory override so you can easily correct the AI when it makes mistakes.
Installation
pip install anonymizer_ner
(Note: Not actually published to PyPI yet. Install from source).
Features
- Zero-Shot Extraction: Define what you want to extract using natural language descriptions (e.g.,
"address": "street addresses"). - Document Anonymization: Physically redact the extracted entities from PDFs (using secure PyMuPDF redaction) or DOCX files.
- Feedback Memory Loop: If the AI mislabels something, you can add a correction to the memory database. On all future extractions, the package will automatically override the AI and correctly label that specific text.
Usage Guide
1. Extracting Entities
import anonymizer_ner
# 1. Define AI entities using natural language
ai_entities = {
"person": "Names of individuals",
"address": "Street addresses and locations",
"organization": "Companies or corporations"
}
# 2. Define strict pattern matching using Regex
regex_rules = {
"aadhaar": r"\b\d{4}\s?\d{4}\s?\d{4}\b",
"pan_card": r"\b[A-Z]{5}[0-9]{4}[A-Z]{1}\b"
}
with open("document.pdf", "rb") as f:
matches = anonymizer_ner.extract(
file_bytes=f.read(),
filename="document.pdf",
entity_definitions=ai_entities,
regex_rules=regex_rules, # Pass the regex rules here!
threshold=0.5
)
for match in matches:
print(f"[{match.label}] {match.text} (Page: {match.page}, Line: {match.line})")
2. Anonymizing Documents
You can securely redact all the entities you found. You can choose different strategies for how the text is hidden:
import anonymizer_ner
with open("document.pdf", "rb") as f:
pdf_bytes = f.read()
# Default strategy: "redact" (blackout boxes)
redacted_bytes = anonymizer_ner.anonymize(
file_bytes=pdf_bytes,
filename="document.pdf",
matches=matches,
strategy="redact"
)
# Other strategies available:
# strategy="replace_with_tags" -> Replaces "John" with "PERSON"
# strategy="replace_with_fake" -> Replaces "John" with a fake name like "Michael"
# strategy="replace_with_indexed_tags" -> Replaces "HDFC Bank" with "ORGANIZATION_1" consistently
with open("redacted_document.pdf", "wb") as f:
f.write(redacted_bytes)
3. Using the Feedback Loop (Memory)
If the AI makes a mistake (e.g., it tags "John Doe" as an address), you can add a correction. By default, this is saved to ~/.anonymizer_ner/memory.json, but you can isolate it per project:
import anonymizer_ner
# Set a custom memory path for this specific project (optional)
anonymizer_ner.set_memory_path("./my_project_memory.json")
# Save a correction to the local memory database
anonymizer_ner.add_correction("John Doe", "person")
This correction is saved locally to ~/.anonymizer_ner/memory.json.
The next time you call anonymizer_ner.extract(), two things happen automatically:
- The AI's internal prompt is augmented with your examples to help it generalize.
- If the text "John Doe" is found anywhere in the document, it is guaranteed to be tagged as a
personwith 100% confidence, overriding any AI predictions.
4. Using a Custom Fine-Tuned Model
By default, the package uses fastino/gliner2-base-v1. If you have fine-tuned your own GLiNER 2 model on specific domain data, you can easily load it either globally or per-extraction.
Set it globally:
import anonymizer_ner
# Can be a Hugging Face repo ID or a local path to the model weights
anonymizer_ner.set_model("path/to/my/finetuned/gliner_model")
# All subsequent extract() calls will now use your model
matches = anonymizer_ner.extract(...)
Set it per extraction:
matches = anonymizer_ner.extract(
file_bytes=f.read(),
filename="document.pdf",
entity_definitions=entities,
model_name="path/to/my/finetuned/gliner_model"
)
5. Processing Multiple Documents (Low RAM)
The anonymizer_ner package is designed to be highly memory efficient. It breaks large documents into small chunks so a 10,000-page document uses the exact same amount of RAM as a 5-page document.
However, if you are running this on a standard laptop and need to process hundreds of documents, do not use multiprocessing. Loading multiple AI models at the same time will crash your RAM. Instead, use a simple for loop to process them sequentially in complete safety:
import os
import anonymizer_ner
# Completely safe for low-RAM machines!
for filename in os.listdir("my_pdfs/"):
if not filename.endswith(".pdf"):
continue
with open(f"my_pdfs/{filename}", "rb") as f:
matches = anonymizer.extract(f.read(), filename, ai_entities)
# Anonymize and save...
Technical Details
- Model:
fastino/gliner2-base-v1(runs locally, downloaded automatically on first use) - PDF Parsing:
PyMuPDF - DOCX Parsing:
python-docx
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file anonymizer_ner-1.0.0.tar.gz.
File metadata
- Download URL: anonymizer_ner-1.0.0.tar.gz
- Upload date:
- Size: 13.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
30ca830b2fe3620884806371f281132c63b89e761b69fe1f65a18ebb7e2f25ef
|
|
| MD5 |
4ad8af5228942195f916777b3c16fad6
|
|
| BLAKE2b-256 |
233bf4336ef8f733cb6d8600b2b806691904565980b93b7670d54a77da287efa
|
File details
Details for the file anonymizer_ner-1.0.0-py3-none-any.whl.
File metadata
- Download URL: anonymizer_ner-1.0.0-py3-none-any.whl
- Upload date:
- Size: 12.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
26b0e172aedec44f000dd5cf22238871a1a615977fae37b5fe560727b6e69439
|
|
| MD5 |
38f917d73acffaa45228626e679e36cd
|
|
| BLAKE2b-256 |
d10e56fefc03479de44821ece9c07c75c25f53acbd3a362c2fbd2817a7d829c8
|