How ankan00v/scalepii works
Purpose and Entry Points
This repository is a local-first DOCX PII pseudonymisation pipeline. It redacts a Word prospectus while preserving layout and substituting stable plausible fakes rather than generic tokens. Primary CLI entry: redact.py; core orchestration: piiredact/pipeline.py. Delivery validation runs through verify_delivery.py. A separate read-only reviewer console starts at web/src/main.tsx.
DOCX Extraction and Structural Preservation
The pipeline operates on Word package structure, including paragraphs, tables, headers, footers, hyperlinks, and embedded media. piiredact/docx_io.py reads/writes DOCX content and scans hidden hyperlink field instructions. piiredact/spans.py flattens text spanning Word runs, reconciles detected character ranges back to runs, and applies replacements without discarding formatting.
Hybrid PII Detection and Policy
Detection combines regex/context rules with spaCy NER, table-column cues, and document-wide entity knowledge. piiredact/detectors.py identifies emails, phones, identifiers, websites, names, and legal entities; it uses the external en_core_web_sm spaCy model. piiredact/allowlist.py defines exclusions for retained public entities such as regulators, statutes, exchanges, and depositories.
Stable Pseudonymisation
Detected canonical values are mapped to seeded, memoised Faker replacements so equivalent source values receive the same fake throughout a run. piiredact/fakes.py generates locale-aware (en_IN) replacements and preserves applicable casing and legal-form conventions. Replacement is bounded by the span-to-run layer in piiredact/spans.py, separating detection from document mutation.
Fail-Closed Non-Text Redaction
The default media policy replaces every embedded raster image with a neutral placeholder, covering QR codes, scans, and other image-borne PII. piiredact/media.py implements this policy; redact.py exposes --keep-images for known-safe inputs. Hidden HYPERLINK field targets are independently scrubbed through the DOCX/pipeline path so visible fake text cannot retain real mailto: URLs.
Outputs, Audit, and Verification
The pipeline writes the redacted DOCX and privacy-safe audit artifacts under output/, including hashed mapping and detection records. piiredact/pipeline.py owns output generation; output/detections.jsonl, output/mapping.csv, and output/field_code_redactions.json provide audit evidence without cleartext source values. verify_delivery.py validates serialized DOCX structure, placeholder images, residual detected values in XML, and audit-file safety.
Evaluation and Reviewer Surface
Evaluation/report generation is separate from redaction: evaluate.py computes metrics, while build_evaluation_report.py renders the DOCX report and reports/evaluation.json persists results. The reviewer UI is a static React/Vite application in web/src/App.tsx, deployed without backend or database infrastructure; it presents committed delivery and evaluation evidence rather than accepting document uploads.
Main entry points: pipeline.py Pipeline orchestration · detectors.py Hybrid PII detection · App.tsx Read-only reviewer console · evaluation.json Evaluation results
Components 20
Redaction pipeline
Local DOCX processing and pseudonymisation.
- Redaction CLI
redact.py - Pipeline orchestration · Coordinates extraction, detection, mutation, and output.
piiredact/pipeline.py - DOCX package I/O · Processes document parts and hidden hyperlink fields.
piiredact/docx_io.py - Span-to-run mutation · Reconciles text spans to Word runs without losing formatting.
piiredact/spans.py - Hybrid PII detection · Combines rules, context, table cues, and entity knowledge.
piiredact/detectors.py - spaCy NER model · en_core_web_sm supplies named-entity recognition.
- Public-entity allowlist · Retains approved public entities and references.
piiredact/allowlist.py - Stable fake generator · Seeded memoised en_IN Faker replacements.
piiredact/fakes.py - Media redaction policy · Replaces embedded raster images unless explicitly retained.
piiredact/media.py
Delivery evidence
Audit, validation, and evaluation outputs.
- Redacted DOCX
- Privacy-safe audit files · Hashed mappings and detection evidence.
- Detection records
output/detections.jsonl - Hashed mapping
output/mapping.csv - Field-code redactions
output/field_code_redactions.json - Delivery verification · Checks serialized DOCX, images, residual values, and audit safety.
verify_delivery.py - Evaluation metrics
evaluate.py - Evaluation report builder
build_evaluation_report.py - Evaluation results
reports/evaluation.json
Reviewer surface
Read-only static evidence viewer.
- Reviewer app entry
web/src/main.tsx - Read-only reviewer console · Presents committed delivery and evaluation evidence without uploads.
web/src/App.tsx
Connections 21
- Redaction CLI → Pipeline orchestration: runs
- Pipeline orchestration → DOCX package I/O: reads and writes package
- DOCX package I/O → Span-to-run mutation: text and run structure
- Pipeline orchestration → Hybrid PII detection: detects PII
- Hybrid PII detection → spaCy NER model: NER
- Hybrid PII detection → Public-entity allowlist: applies exclusions
- Hybrid PII detection → Stable fake generator: canonical values
- Stable fake generator → Span-to-run mutation: replacement text
- Pipeline orchestration → Media redaction policy: applies image policy
- Pipeline orchestration → Redacted DOCX: writes
- Pipeline orchestration → Privacy-safe audit files: writes
- Privacy-safe audit files → Detection records: includes
- Privacy-safe audit files → Hashed mapping: includes
- Privacy-safe audit files → Field-code redactions: includes
- Delivery verification → Redacted DOCX: validates
- Delivery verification → Privacy-safe audit files: checks safety
- Evaluation metrics → Evaluation results: computes
- Evaluation report builder → Evaluation results: renders from
- Reviewer app entry → Read-only reviewer console: boots
- Read-only reviewer console → Privacy-safe audit files: presents committed evidence
- Read-only reviewer console → Evaluation results: presents metrics