Skip to main content

Production-grade PII detection with multi-model ensemble

Project description

pii-protector

Production-grade PII (Personally Identifiable Information) detection library with a sequential multi-model ensemble. Designed for speed — 90%+ of texts are handled by the regex layer alone in under 3ms, with heavier NER models invoked only when needed.

Architecture

Layer 1 — Regex                  always runs              ~0.3–3ms
    ↓  escalation score >= 3?
Layer 2 — Presidio + spaCy NER   names / orgs / loc       ~5ms
    ↓  confidence low or conflict?
Layer 3 — NER Transformer        high-accuracy names      ~20ms
    +
Layer 4 — PII Model              structured PII           ~15ms

Each layer decides whether the next one is needed. On clean structured text, only Layer 1 runs.

Installation

# Regex layer only — zero dependencies
pip install pii-protector

# + Presidio / spaCy NER (Layer 2)
pip install "pii-protector[presidio]"
python -m spacy download en_core_web_lg

# + Transformer models (Layer 3 & 4)
pip install "pii-protector[transformers]"

# Full install (all layers)
pip install "pii-protector[full]"
python -m spacy download en_core_web_lg

Quick Start

from pii_detector import AdvancedPIIDetector

# Regex only (fastest, zero dependencies)
detector = AdvancedPIIDetector(use_presidio=False, use_transformers=False, use_pii_model=False)

results = detector.detect("My SSN is 123-45-6789 and email is john@example.com")
for entity in results:
    print(entity)
# {'entity_type': 'SSN',   'start': 10, 'end': 21, 'score': 0.95, 'text': '123-45-6789'}
# {'entity_type': 'EMAIL', 'start': 36, 'end': 52, 'score': 0.95, 'text': 'john@example.com'}

Full ensemble (all layers)

detector = AdvancedPIIDetector()

# Detailed analysis with risk score
analysis = detector.get_detailed_analysis("Dr. John Smith's Aadhaar is 1234 5678 9012")
print(analysis['risk_assessment'])
print(analysis['timing'])

Batch detection

texts = ["call me at +91-9876543210", "my PAN is ABCDE1234F", "nothing sensitive here"]
results = detector.detect_batch(texts)

Risk assessment

risk = detector.get_risk_assessment("SSN 123-45-6789, VISA 4111 1111 1111 1111")
# {'risk_level': 'CRITICAL', 'score': 0.95, 'high_value_pii': ['SSN', 'CREDIT_CARD']}

CLI

# Basic scan
pii-detect "My credit card is 4111 1111 1111 1111"

# From file
pii-detect --file document.txt

# JSON output
pii-detect --json "Aadhaar: 1234 5678 9012"

# Detailed analysis
pii-detect --detailed "SSN 123-45-6789, John Smith, john@corp.com"

# Risk score only
pii-detect --risk "GSTIN 22ABCDE1234F1Z5, PAN ABCDE1234F"

# Disable heavy models for speed
pii-detect --no-presidio --no-transformer --no-pii-model "card: 4111111111111111"

Supported PII Types

Category Types
Core EMAIL, PHONE, SSN, CREDIT_CARD, IP_ADDRESS, ADDRESS, PASSWORD, USERNAME
India AADHAAR, PAN, GSTIN, UPI_ID, IFSC, VOTER_ID, MOBILE_IN, EPFO_UAN, DRIVING_LICENSE_IN, PASSPORT_IN, BANK_ACCOUNT_IN, PINCODE
North America DRIVER_LICENSE, PASSPORT, BANK_ACCOUNT, TAX_ID, SIN, ZIP_CODE, POSTAL_CA
NER NAME, ORGANIZATION, LOCATION, FACILITY
Finance CREDIT_CARD, BANK_ACCOUNT, MONEY
Dates & URLs DATE_OF_BIRTH, DATE, URL
API Secrets AWS_ACCESS_KEY, OPENAI_API_KEY, GITHUB_TOKEN, GITLAB_TOKEN, STRIPE_KEY, JWT_TOKEN, BEARER_TOKEN, PRIVATE_KEY, DATABASE_URL, SLACK_TOKEN, DISCORD_TOKEN, 20+ more

Configuration

detector = AdvancedPIIDetector(
    use_presidio=True,           # Layer 2: spaCy NER via Presidio
    use_transformers=True,       # Layer 3: NER transformer
    use_pii_model=True,          # Layer 4: PII model
    confidence_threshold=0.5,   # Early-stop threshold
    spacy_model="en_core_web_lg",
    transformer_model="jainsatyam26/pii-ner-onnx",
    pii_model="jainsatyam26/pii-detector-onnx",
)

Environment variable overrides

SPACY_MODEL_NAME=en_core_web_sm
TRANSFORMER_MODEL_NAME=jainsatyam26/pii-ner-onnx
PII_MODEL_NAME=jainsatyam26/pii-detector-onnx
SPACY_DATA_PATH=/path/to/models

Models

Layer Model What it detects
2 en_core_web_lg via Presidio NAME, ORG, LOCATION
3 jainsatyam26/pii-ner-onnx High-accuracy person names
4 jainsatyam26/pii-detector-onnx PHONE, EMAIL, SSN, DOB, ADDRESS

ONNX-optimized versions:

License

MIT — tensoryug

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pii_protector-2.2.2.tar.gz (48.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pii_protector-2.2.2-py3-none-any.whl (53.7 kB view details)

Uploaded Python 3

File details

Details for the file pii_protector-2.2.2.tar.gz.

File metadata

  • Download URL: pii_protector-2.2.2.tar.gz
  • Upload date:
  • Size: 48.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.10

File hashes

Hashes for pii_protector-2.2.2.tar.gz
Algorithm Hash digest
SHA256 e0cd105953373acf22dbdbc40a54df8b25acfd7f60780fab3e163695d8c8da62
MD5 27ea94152596bb6d8ff856dfa8b8ff1d
BLAKE2b-256 c038d8240ab6a219daec1ed4f5bd512d49392de27086f783c63b11355f5da66c

See more details on using hashes here.

File details

Details for the file pii_protector-2.2.2-py3-none-any.whl.

File metadata

  • Download URL: pii_protector-2.2.2-py3-none-any.whl
  • Upload date:
  • Size: 53.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.10

File hashes

Hashes for pii_protector-2.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 3c6e8764bc8a99a5d8c6956160d5ecf990242ab5b076ee16bbe26b6d1c703a13
MD5 edacfa022ccd665de87c86ce90325866
BLAKE2b-256 30047270dcb8e95a8df7f05c4747aa9a1190bc728ace0340a98434dbda5611f3

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page