# ankan00v/scalepii architecture

> Architecture diagram of the GitHub repository ankan00v/scalepii, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-08-19). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/ankan00v/scalepii
- Repository: https://github.com/ankan00v/scalepii

## Overview

#### 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: [`piiredact/pipeline.py`](https://github.com/ankan00v/scalepii/blob/main/piiredact/pipeline.py) (Pipeline orchestration), [`piiredact/detectors.py`](https://github.com/ankan00v/scalepii/blob/main/piiredact/detectors.py) (Hybrid PII detection), [`web/src/App.tsx`](https://github.com/ankan00v/scalepii/blob/main/web/src/App.tsx) (Read-only reviewer console), [`reports/evaluation.json`](https://github.com/ankan00v/scalepii/blob/main/reports/evaluation.json) (Evaluation results)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_redaction["Redaction pipeline"]
  node_cli["Redaction CLI<br/>Python entry point<br/>[redact.py]"]
  node_pipeline["Pipeline orchestration<br/>Python pipeline<br/>[pipeline.py]"]
  node_docx_io["DOCX package I/O<br/>DOCX adapter<br/>[docx_io.py]"]
  node_spans["Span-to-run mutation<br/>format-preserving editor<br/>[spans.py]"]
  node_detectors["Hybrid PII detection<br/>detection engine<br/>[detectors.py]"]
  node_spacy{{"spaCy NER model<br/>external model"}}
  node_allowlist["Public-entity allowlist<br/>policy rules<br/>[allowlist.py]"]
  node_fakes["Stable fake generator<br/>pseudonymisation<br/>[fakes.py]"]
  node_media["Media redaction policy<br/>image sanitizer<br/>[media.py]"]
end

subgraph group_delivery["Delivery evidence"]
  node_redacted_docx["Redacted DOCX<br/>delivery document"]
  node_audit["Privacy-safe audit files<br/>audit artifacts"]
  node_detections["Detection records<br/>JSONL audit<br/>[detections.jsonl]"]
  node_mapping["Hashed mapping<br/>CSV audit<br/>[mapping.csv]"]
  node_field_audit["Field-code redactions<br/>JSON audit"]
  node_verify["Delivery verification<br/>Python validator<br/>[verify_delivery.py]"]
  node_evaluate["Evaluation metrics<br/>Python evaluation<br/>[evaluate.py]"]
  node_report_builder["Evaluation report builder<br/>Python report renderer"]
  node_evaluation_json["Evaluation results<br/>persisted metrics<br/>[evaluation.json]"]
end

subgraph group_reviewer["Reviewer surface"]
  node_reviewer_entry["Reviewer app entry<br/>React/Vite entry point<br/>[main.tsx]"]
  node_reviewer_app["Read-only reviewer console<br/>React UI<br/>[App.tsx]"]
end

node_cli -->|"runs"| node_pipeline
node_pipeline -->|"reads and writes package"| node_docx_io
node_docx_io -->|"text and run structure"| node_spans
node_pipeline -->|"detects PII"| node_detectors
node_detectors -->|"NER"| node_spacy
node_detectors -->|"applies exclusions"| node_allowlist
node_detectors -->|"canonical values"| node_fakes
node_fakes -->|"replacement text"| node_spans
node_pipeline -->|"applies image policy"| node_media
node_pipeline -->|"writes"| node_redacted_docx
node_pipeline -->|"writes"| node_audit
node_audit -->|"includes"| node_detections
node_audit -->|"includes"| node_mapping
node_audit -->|"includes"| node_field_audit
node_verify -->|"validates"| node_redacted_docx
node_verify -->|"checks safety"| node_audit
node_evaluate -->|"computes"| node_evaluation_json
node_report_builder -->|"renders from"| node_evaluation_json
node_reviewer_entry -->|"boots"| node_reviewer_app
node_reviewer_app -.->|"presents committed evidence"| node_audit
node_reviewer_app -.->|"presents metrics"| node_evaluation_json

click node_cli "https://github.com/ankan00v/scalepii/blob/main/redact.py"
click node_pipeline "https://github.com/ankan00v/scalepii/blob/main/piiredact/pipeline.py"
click node_docx_io "https://github.com/ankan00v/scalepii/blob/main/piiredact/docx_io.py"
click node_spans "https://github.com/ankan00v/scalepii/blob/main/piiredact/spans.py"
click node_detectors "https://github.com/ankan00v/scalepii/blob/main/piiredact/detectors.py"
click node_allowlist "https://github.com/ankan00v/scalepii/blob/main/piiredact/allowlist.py"
click node_fakes "https://github.com/ankan00v/scalepii/blob/main/piiredact/fakes.py"
click node_media "https://github.com/ankan00v/scalepii/blob/main/piiredact/media.py"
click node_detections "https://github.com/ankan00v/scalepii/blob/main/output/detections.jsonl"
click node_mapping "https://github.com/ankan00v/scalepii/blob/main/output/mapping.csv"
click node_field_audit "https://github.com/ankan00v/scalepii/blob/main/output/field_code_redactions.json"
click node_verify "https://github.com/ankan00v/scalepii/blob/main/verify_delivery.py"
click node_evaluate "https://github.com/ankan00v/scalepii/blob/main/evaluate.py"
click node_report_builder "https://github.com/ankan00v/scalepii/blob/main/build_evaluation_report.py"
click node_evaluation_json "https://github.com/ankan00v/scalepii/blob/main/reports/evaluation.json"
click node_reviewer_entry "https://github.com/ankan00v/scalepii/blob/main/web/src/main.tsx"
click node_reviewer_app "https://github.com/ankan00v/scalepii/blob/main/web/src/App.tsx"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_cli,node_pipeline,node_docx_io,node_spans,node_detectors,node_spacy,node_allowlist,node_fakes,node_media toneBlue
class node_redacted_docx,node_audit,node_detections,node_mapping,node_field_audit,node_verify,node_evaluate,node_report_builder,node_evaluation_json toneAmber
class node_reviewer_entry,node_reviewer_app toneMint
```

## Components

### Redaction pipeline

Local DOCX processing and pseudonymisation.

- [Redaction CLI](https://github.com/ankan00v/scalepii/blob/main/redact.py): Python entry point · `redact.py`
- [Pipeline orchestration](https://github.com/ankan00v/scalepii/blob/main/piiredact/pipeline.py): Python pipeline · Coordinates extraction, detection, mutation, and output. · `piiredact/pipeline.py`
- [DOCX package I/O](https://github.com/ankan00v/scalepii/blob/main/piiredact/docx_io.py): DOCX adapter · Processes document parts and hidden hyperlink fields. · `piiredact/docx_io.py`
- [Span-to-run mutation](https://github.com/ankan00v/scalepii/blob/main/piiredact/spans.py): format-preserving editor · Reconciles text spans to Word runs without losing formatting. · `piiredact/spans.py`
- [Hybrid PII detection](https://github.com/ankan00v/scalepii/blob/main/piiredact/detectors.py): detection engine · Combines rules, context, table cues, and entity knowledge. · `piiredact/detectors.py`
- **spaCy NER model**: external model · en_core_web_sm supplies named-entity recognition.
- [Public-entity allowlist](https://github.com/ankan00v/scalepii/blob/main/piiredact/allowlist.py): policy rules · Retains approved public entities and references. · `piiredact/allowlist.py`
- [Stable fake generator](https://github.com/ankan00v/scalepii/blob/main/piiredact/fakes.py): pseudonymisation · Seeded memoised en_IN Faker replacements. · `piiredact/fakes.py`
- [Media redaction policy](https://github.com/ankan00v/scalepii/blob/main/piiredact/media.py): image sanitizer · Replaces embedded raster images unless explicitly retained. · `piiredact/media.py`

### Delivery evidence

Audit, validation, and evaluation outputs.

- **Redacted DOCX**: delivery document
- **Privacy-safe audit files**: audit artifacts · Hashed mappings and detection evidence.
- [Detection records](https://github.com/ankan00v/scalepii/blob/main/output/detections.jsonl): JSONL audit · `output/detections.jsonl`
- [Hashed mapping](https://github.com/ankan00v/scalepii/blob/main/output/mapping.csv): CSV audit · `output/mapping.csv`
- [Field-code redactions](https://github.com/ankan00v/scalepii/blob/main/output/field_code_redactions.json): JSON audit · `output/field_code_redactions.json`
- [Delivery verification](https://github.com/ankan00v/scalepii/blob/main/verify_delivery.py): Python validator · Checks serialized DOCX, images, residual values, and audit safety. · `verify_delivery.py`
- [Evaluation metrics](https://github.com/ankan00v/scalepii/blob/main/evaluate.py): Python evaluation · `evaluate.py`
- [Evaluation report builder](https://github.com/ankan00v/scalepii/blob/main/build_evaluation_report.py): Python report renderer · `build_evaluation_report.py`
- [Evaluation results](https://github.com/ankan00v/scalepii/blob/main/reports/evaluation.json): persisted metrics · `reports/evaluation.json`

### Reviewer surface

Read-only static evidence viewer.

- [Reviewer app entry](https://github.com/ankan00v/scalepii/blob/main/web/src/main.tsx): React/Vite entry point · `web/src/main.tsx`
- [Read-only reviewer console](https://github.com/ankan00v/scalepii/blob/main/web/src/App.tsx): React UI · Presents committed delivery and evaluation evidence without uploads. · `web/src/App.tsx`

## Connections

- 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
