Skip to content

About

Enterprise-grade, offline PII detector and surgical redactor for PDF, DOCX, XLSX, TXT, and Images using regex, contextual heuristics, and local spaCy Transformer NER (no external LLM). Includes a React UI with side-by-side comparison previews, Tesseract OCR, and category filter toggles.

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Latest commit

Β 

History

100 Commits

Folders and files

NameName
Last commit message
Last commit date
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

PII Redactor

Enterprise-grade, offline Personally Identifiable Information (PII) detector and surgical redactor for PDF, DOCX, XLSX, TXT, and Images β€” powered by rule-based Regex, Contextual heuristics, and local spaCy Transformer NER (No external LLM, No API calls).

Python FastAPI React Vite spaCy Tesseract OCR License


✨ Overview

PII Redactor is a privacy-first, zero-data-leakage redaction suite designed for compliance, data privacy (GDPR, DPDP Act, HIPAA), and secure document sharing. It processes .pdf, .docx, .xlsx, .txt, and standalone images (.png, .jpg, .jpeg, .tiff, .bmp) entirely on your local machine.

Core Highlights

  • 100% Offline & Private: Zero external LLM calls or cloud API dependencies. All models and regex patterns run locally with GPU acceleration support (CUDA).
  • Surgical Redaction Engine: Edits documents in place preserving layout, styles, fonts, and structure:
    • PDF: True vector redaction annotations (fitz.add_redact_annot) permanently sanitizing underlying character streams.
    • DOCX: Paragraph- and table-cell run preservation keeping document styles and tables intact.
    • XLSX: Substring-level cell redaction using openpyxl CellRichText, preserving original cell formatting while styling redacted tokens in bold dark blue (#002060) on clean sheet backgrounds.
    • Images & Scanned Documents: Tesseract OCR with pixel-precise black box redaction over detected text bounding boxes.
  • Modern Web Interface: Built with React 18 and Vite featuring side-by-side original vs. redacted comparison, page navigation, interactive zoom & drag panning, category toggles with select/deselect all, dark/light theme toggle, and 3D interactive globe canvas.
  • Dual Deliverables: Generates both the sanitized document ({filename}_masked.{ext}) and an audit-ready structured report ({filename}_report.json).

πŸ›‘οΈ Supported PII Categories

PII Redactor detects 15 distinct categories spanning financial, personal, contact, technical, and semantic identifiers:

Category Identifier / Entity Detection Method Typical Confidence
EMAIL Email addresses High-precision RFC regex ~0.98
PAN_NUMBER Indian Income Tax PAN (10-char alphanumeric) Regex with structural checksum validation ~0.97
PHONE_NUMBER Domestic & international phone/mobile numbers Regex with prefix/country code anchors ~0.93
DATE_OF_BIRTH Dates (DD/MM/YYYY, ISO, Month-first, ordinals, timestamps) Comprehensive date & timestamp regex ~0.90
IP_ADDRESS IPv4 addresses Word-bounded octet regex ~0.92
AADHAAR_NUMBER 12-digit Indian Aadhaar numbers (spaced/hyphenated) Regex with negative lookahead/lookbehind ~0.85
CREDIT_CARD 16-digit credit and debit card numbers Card grouping regex ~0.88
PINCODE 6-digit Indian postal PIN codes Boundary-constrained digit regex ~0.78
BANK_ACCOUNT_NUMBER Bank account numbers (9–18 digits) Keyword-anchored contextual regex ~0.88
IFSC_CODE 11-character Indian banking IFSC codes Bank code regex (4 letters + '0' + 6 alphanumeric) ~0.95
GSTIN Indian GST Identification Numbers 15-character statutory GST format regex ~0.96
URL Web domains, HTTPS/HTTP URLs, bare domains (.com, .io, etc.) Domain regex with negative lookbehind on @ ~0.91
PERSON_NAME Full names, person names across structured forms/tables spaCy RoBERTa Transformer (en_core_web_trf) + Context rules 0.72 – 0.94
LOCATION Countries (India, USA, etc.), states, cities, and regions spaCy TRF (GPE, LOC, NORP) + Geographic gazetteer 0.85 – 0.94
ORGANIZATION Companies, brands (Microsoft, Azure, AWS, GFG), universities, banks spaCy TRF (ORG, PRODUCT) + Context suffix rules + Brand dictionary 0.75 – 0.94

πŸ—οΈ Architecture & Redaction Pipeline

                              β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                              β”‚   Uploaded Document   β”‚
                              β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                          β”‚
                   β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                   β”‚ Document Parsing & Structure Extraction     β”‚
                   β”‚ β€’ PDF: PyMuPDF text + embedded image OCR    β”‚
                   β”‚ β€’ DOCX: python-docx paragraphs + tables     β”‚
                   β”‚ β€’ XLSX: openpyxl cells & coordinates        β”‚
                   β”‚ β€’ Images: Tesseract OCR word bounding boxes β”‚
                   β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                          β”‚
            β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
            β–Ό                             β–Ό                             β–Ό
   β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”           β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”           β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
   β”‚ Regex Detectors β”‚           β”‚ Local spaCy NER β”‚           β”‚ Context & Brand β”‚
   β”‚ (12 Categories) β”‚           β”‚ (Transformer)   β”‚           β”‚ Rule Engine     β”‚
   β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”˜           β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”˜           β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”˜
            β”‚                             β”‚                             β”‚
            β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                          β”‚
                        β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                        β”‚ Entity Merging & Overlap Resolver β”‚
                        β”‚ β€’ Confidence-based deduplication  β”‚
                        β”‚ β€’ Cross-document consistency pass β”‚
                        β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                          β”‚
                        β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                        β”‚ Format-Specific Surgical Redactor β”‚
                        β”‚ β€’ PDF: True vector redactions     β”‚
                        β”‚ β€’ DOCX: Paragraph run overwrite   β”‚
                        β”‚ β€’ XLSX: RichText dark blue token  β”‚
                        β”‚ β€’ Image: Bounding box blackout    β”‚
                        β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                          β”‚
                        β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                        β”‚     Masked Output + JSON Report   β”‚
                        β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

πŸš€ Quick Start

1. Prerequisites

  • Python 3.10+
  • Node.js 18+ & npm (for the React web frontend)
  • Tesseract OCR (optional, recommended for scanned documents and images):
    • Ubuntu/Debian: sudo apt-get install tesseract-ocr
    • Arch Linux: sudo pacman -S tesseract tesseract-data-eng
    • macOS: brew install tesseract

2. Clone and Setup Backend

# Clone the repository
git clone https://github.com/<your-username>/pii-redactor.git
cd pii-redactor

# Create and activate virtual environment
python3 -m venv venv
source venv/bin/activate

# Install Python requirements
pip install -r requirements.txt

# Download spaCy Transformer model (en_core_web_trf)
python -m spacy download en_core_web_trf

(Note: If running on a resource-constrained machine without GPU, en_core_web_sm is supported as an automatic fallback).

3. Start the Application

Start the FastAPI Server:

uvicorn api:app --reload --host 127.0.0.1 --port 8000
  • API Documentation (Swagger): http://127.0.0.1:8000/docs

Start the React Frontend:

cd frontend
npm install
npm run dev
  • Web UI: http://localhost:5173

πŸ–₯️ Usage

1. Web Application

  1. Open http://localhost:5173 in your browser.
  2. Select or toggle any of the 15 PII categories you want to include or exclude.
  3. Drag and drop your .pdf, .docx, .xlsx, .txt, or image file.
  4. Review original and redacted pages side-by-side with interactive zoom and pan controls.
  5. Download your redacted document and structured JSON audit report.

2. Command Line Interface (CLI)

# Basic redaction
python cli.py sample_contract.pdf --output-dir ./output

# Redact only specific categories
python cli.py patient_record.docx --include EMAIL,PHONE_NUMBER,PERSON_NAME

# Redact everything except locations
python cli.py financial_sheet.xlsx --exclude LOCATION

# Inspect all options
python cli.py --help

3. REST API

Endpoint: POST /api/redact (Frontend & Preview API)

Accepts multipart form data (file, optional include, exclude) and returns a job ID with rendered page previews and detection summaries.

Endpoint: POST /redact (Standard Zip Export)

curl -X POST http://127.0.0.1:8000/redact \
     -F "file=@/path/to/document.pdf" \
     -F "include=EMAIL,PHONE_NUMBER,PERSON_NAME" \
     --output result.zip

unzip result.zip -d ./output

πŸ“„ JSON Report Format

Every redaction operation produces a machine-readable audit report:

{
  "source_file": "financial_records.xlsx",
  "total_entities_found": 3,
  "entities": [
    {
      "text": "Atharva Jadhav",
      "category": "PERSON_NAME",
      "start": 14,
      "end": 28,
      "detection_method": "ner",
      "confidence": 0.9400
    },
    {
      "text": "Microsoft Azure",
      "category": "ORGANIZATION",
      "start": 52,
      "end": 67,
      "detection_method": "context_rule",
      "confidence": 0.9400
    },
    {
      "text": "August 15, 2024",
      "category": "DATE_OF_BIRTH",
      "start": 95,
      "end": 110,
      "detection_method": "regex",
      "confidence": 0.9000
    }
  ],
  "warnings": []
}

πŸ“ Repository Structure

pii-redactor/
β”œβ”€β”€ api.py                    # FastAPI application, streaming previews & endpoints
β”œβ”€β”€ cli.py                    # Command-line interface with rich tabular summaries
β”œβ”€β”€ requirements.txt          # Python library specifications
β”œβ”€β”€ core/                     # Redaction & Rendering Core
β”‚   β”œβ”€β”€ pipeline.py           # End-to-end orchestration and consistency expansion
β”‚   β”œβ”€β”€ redactor.py           # In-place surgical document redactors (PDF/DOCX/XLSX/TXT)
β”‚   β”œβ”€β”€ renderer.py           # Multi-page visual renderer with spreadsheet emulation
β”‚   β”œβ”€β”€ merge.py              # Overlapping span resolver and priority merger
β”‚   └── report.py             # CLI console formatting and JSON report generation
β”œβ”€β”€ detectors/                # Detection Engines
β”‚   β”œβ”€β”€ regex_detector.py     # High-precision patterns (Email, Phone, PAN, Aadhaar, URLs, etc.)
β”‚   β”œβ”€β”€ ner_detector.py       # spaCy Transformer NER + Category correction heuristics
β”‚   └── context_detector.py   # Contextual rules, role-line extractors, and gazetteers
β”œβ”€β”€ parsers/                  # Format Parsers & OCR
β”‚   β”œβ”€β”€ pdf_parser.py         # PyMuPDF text & rect extractor
β”‚   β”œβ”€β”€ docx_parser.py        # python-docx paragraph, run, & table extractor
β”‚   β”œβ”€β”€ xlsx_parser.py        # openpyxl multi-sheet coordinate & cell offset mapper
β”‚   β”œβ”€β”€ txt_parser.py         # Plaintext UTF-8 scanner
β”‚   └── ocr_parser.py         # Tesseract OCR engine with brand & location detection
└── frontend/                 # React 18 + Vite UI
    β”œβ”€β”€ src/
    β”‚   β”œβ”€β”€ App.jsx           # Master UI workflow & state management
    β”‚   └── components/       # ComparisonView, DocumentViewer, ThreeBackground, Dropzone
    └── package.json

🀝 Contributing

Pull requests are welcome! For major changes, please open an issue first to discuss the proposed enhancement.

  1. Fork the repo: git checkout -b feature/my-enhancement
  2. Commit your changes: git commit -m "feat: add enhancement"
  3. Push to branch: git push origin feature/my-enhancement
  4. Open a Pull Request

πŸ“œ License

Distributed under the MIT License. See LICENSE for more information.

About

Enterprise-grade, offline PII detector and surgical redactor for PDF, DOCX, XLSX, TXT, and Images using regex, contextual heuristics, and local spaCy Transformer NER (no external LLM). Includes a React UI with side-by-side comparison previews, Tesseract OCR, and category filter toggles.

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages