# tnruok/pii_shield architecture

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

- Interactive diagram: https://gitdiagram.com/tnruok/pii_shield
- Repository: https://github.com/tnruok/pii_shield

## Overview

PII Shield is a local document privacy pipeline: users submit business documents, receive extracted chunks, have PII detected and replaced with stable placeholders, and get a leakage safety check and masked audit report. Its core processing stages are distinct from its browser-prompt shielding capability, which uses a local bridge. The graph reflects README descriptions and the supplied source excerpts and index; unsampled implementation details and unshown call relationships are not inferred.

Main entry points: [`app/gradio_app.py`](https://github.com/tnruok/pii_shield/blob/main/app/gradio_app.py) (Gradio workflow), [`detect/merge.py`](https://github.com/tnruok/pii_shield/blob/main/detect/merge.py) (Detection merge), [`extract/router.py`](https://github.com/tnruok/pii_shield/blob/main/extract/router.py) (Format router), [`extract/docx.py`](https://github.com/tnruok/pii_shield/blob/main/extract/docx.py) (DOCX extraction)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_extract["Document extraction"]
  node_router["Format router<br/>[router.py]"]
  node_docx["DOCX extraction<br/>[docx.py]"]
  node_pdf["PDF extraction<br/>[pdf.py]"]
  node_pptx["PPTX extraction<br/>[pptx.py]"]
  node_ocr["OCR processing<br/>[ocr.py]"]
  node_regions["Image regions<br/>[regions.py]"]
  node_chunks["Chunk data contract<br/>[models.py]"]
end

subgraph group_detect["PII detection"]
  node_rules["Regex rules<br/>[rules.py]"]
  node_ner["Local NER<br/>[ner.py]"]
  node_context["Context boosting<br/>[context.py]"]
  node_validators["PII validators<br/>[validators.py]"]
  node_ocrrepair["OCR repair<br/>[ocr_repair.py]"]
  node_suspect["Suspect OCR<br/>[suspect.py]"]
  node_merge["Detection merge<br/>[merge.py]"]
  node_propagate["Name propagation<br/>[propagate.py]"]
end

subgraph group_protect["Redaction and safety"]
  node_redactor["Placeholder redaction<br/>[redactor.py]"]
  node_mapping[("Encrypted mappings<br/>[mapping.py]")]
  node_leakage["Leakage safety check<br/>[leakage.py]"]
end

subgraph group_interfaces["User interfaces"]
  node_gradio["Gradio workflow<br/>[gradio_app.py]"]
  node_browser["Browser extension<br/>[content.js]"]
  node_bridge["Local extension bridge<br/>[server.py]"]
end

subgraph group_audit["Audit reporting"]
  node_report["Masked audit reports<br/>[report.py]"]
end

node_user(("Document user"))
node_browseruser(("Browser user"))

node_user -->|"submits documents"| node_gradio
node_gradio -->|"routes files"| node_router
node_router -->|"dispatches PDF"| node_pdf
node_router -->|"dispatches DOCX"| node_docx
node_router -->|"dispatches PPTX"| node_pptx
node_docx -->|"creates chunks"| node_chunks
node_pdf -->|"creates chunks"| node_chunks
node_pptx -->|"creates chunks"| node_chunks
node_docx -->|"runs OCR"| node_ocr
node_pptx -->|"runs OCR"| node_ocr
node_pdf -->|"runs OCR"| node_ocr
node_docx -->|"detects image regions"| node_regions
node_pptx -->|"detects image regions"| node_regions
node_gradio -->|"detects PII"| node_merge
node_merge -->|"runs rules"| node_rules
node_merge -->|"runs NER"| node_ner
node_merge -->|"boosts context"| node_context
node_merge -->|"checks suspect OCR"| node_suspect
node_merge -->|"repairs OCR candidates"| node_ocrrepair
node_rules -->|"validates matches"| node_validators
node_ocrrepair -->|"validates candidates"| node_validators
node_gradio -->|"propagates aliases"| node_propagate
node_gradio -->|"redacts chunks"| node_redactor
node_redactor -->|"assigns placeholders"| node_mapping
node_gradio -->|"checks residual PII"| node_leakage
node_leakage -->|"re-scans chunks"| node_merge
node_gradio -->|"generates audit"| node_report
node_browseruser -->|"shields prompts"| node_browser
node_browser -->|"uses local bridge"| node_bridge
node_bridge -.->|"processes documents"| node_router

click node_gradio "https://github.com/tnruok/pii_shield/blob/main/app/gradio_app.py"
click node_router "https://github.com/tnruok/pii_shield/blob/main/extract/router.py"
click node_docx "https://github.com/tnruok/pii_shield/blob/main/extract/docx.py"
click node_pdf "https://github.com/tnruok/pii_shield/blob/main/extract/pdf.py"
click node_pptx "https://github.com/tnruok/pii_shield/blob/main/extract/pptx.py"
click node_ocr "https://github.com/tnruok/pii_shield/blob/main/extract/ocr.py"
click node_regions "https://github.com/tnruok/pii_shield/blob/main/extract/regions.py"
click node_chunks "https://github.com/tnruok/pii_shield/blob/main/extract/models.py"
click node_rules "https://github.com/tnruok/pii_shield/blob/main/detect/rules.py"
click node_ner "https://github.com/tnruok/pii_shield/blob/main/detect/ner.py"
click node_context "https://github.com/tnruok/pii_shield/blob/main/detect/context.py"
click node_validators "https://github.com/tnruok/pii_shield/blob/main/detect/validators.py"
click node_ocrrepair "https://github.com/tnruok/pii_shield/blob/main/detect/ocr_repair.py"
click node_suspect "https://github.com/tnruok/pii_shield/blob/main/detect/suspect.py"
click node_merge "https://github.com/tnruok/pii_shield/blob/main/detect/merge.py"
click node_propagate "https://github.com/tnruok/pii_shield/blob/main/detect/propagate.py"
click node_redactor "https://github.com/tnruok/pii_shield/blob/main/redact/redactor.py"
click node_mapping "https://github.com/tnruok/pii_shield/blob/main/redact/mapping.py"
click node_leakage "https://github.com/tnruok/pii_shield/blob/main/verify/leakage.py"
click node_report "https://github.com/tnruok/pii_shield/blob/main/report/report.py"
click node_browser "https://github.com/tnruok/pii_shield/blob/main/extension/content.js"
click node_bridge "https://github.com/tnruok/pii_shield/blob/main/extension_server/server.py"

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_router,node_docx,node_pdf,node_pptx,node_ocr,node_regions,node_chunks,node_user,node_browseruser toneBlue
class node_rules,node_ner,node_context,node_validators,node_ocrrepair,node_suspect,node_merge,node_propagate toneAmber
class node_redactor,node_mapping,node_leakage toneMint
class node_gradio,node_browser,node_bridge toneRose
class node_report toneIndigo
```

## Components

### Document extraction

- [Format router](https://github.com/tnruok/pii_shield/blob/main/extract/router.py): `extract/router.py`
- [DOCX extraction](https://github.com/tnruok/pii_shield/blob/main/extract/docx.py): `extract/docx.py`
- [PDF extraction](https://github.com/tnruok/pii_shield/blob/main/extract/pdf.py): `extract/pdf.py`
- [PPTX extraction](https://github.com/tnruok/pii_shield/blob/main/extract/pptx.py): `extract/pptx.py`
- [OCR processing](https://github.com/tnruok/pii_shield/blob/main/extract/ocr.py): `extract/ocr.py`
- [Image regions](https://github.com/tnruok/pii_shield/blob/main/extract/regions.py): `extract/regions.py`
- [Chunk data contract](https://github.com/tnruok/pii_shield/blob/main/extract/models.py): `extract/models.py`

### PII detection

- [Regex rules](https://github.com/tnruok/pii_shield/blob/main/detect/rules.py): `detect/rules.py`
- [Local NER](https://github.com/tnruok/pii_shield/blob/main/detect/ner.py): `detect/ner.py`
- [Context boosting](https://github.com/tnruok/pii_shield/blob/main/detect/context.py): `detect/context.py`
- [PII validators](https://github.com/tnruok/pii_shield/blob/main/detect/validators.py): `detect/validators.py`
- [OCR repair](https://github.com/tnruok/pii_shield/blob/main/detect/ocr_repair.py): `detect/ocr_repair.py`
- [Suspect OCR](https://github.com/tnruok/pii_shield/blob/main/detect/suspect.py): `detect/suspect.py`
- [Detection merge](https://github.com/tnruok/pii_shield/blob/main/detect/merge.py): `detect/merge.py`
- [Name propagation](https://github.com/tnruok/pii_shield/blob/main/detect/propagate.py): `detect/propagate.py`

### Redaction and safety

- [Placeholder redaction](https://github.com/tnruok/pii_shield/blob/main/redact/redactor.py): `redact/redactor.py`
- [Encrypted mappings](https://github.com/tnruok/pii_shield/blob/main/redact/mapping.py): `redact/mapping.py`
- [Leakage safety check](https://github.com/tnruok/pii_shield/blob/main/verify/leakage.py): `verify/leakage.py`

### User interfaces

- [Gradio workflow](https://github.com/tnruok/pii_shield/blob/main/app/gradio_app.py): `app/gradio_app.py`
- [Browser extension](https://github.com/tnruok/pii_shield/blob/main/extension/content.js): `extension/content.js`
- [Local extension bridge](https://github.com/tnruok/pii_shield/blob/main/extension_server/server.py): `extension_server/server.py`

### Audit reporting

- [Masked audit reports](https://github.com/tnruok/pii_shield/blob/main/report/report.py): `report/report.py`

### Other components

- **Document user**
- **Browser user**

## Connections

- Document user → Gradio workflow: submits documents (evidence: `README.md`)
- Gradio workflow → Format router: routes files (evidence: `app/gradio_app.py`)
- Format router → PDF extraction: dispatches PDF (evidence: `extract/router.py`)
- Format router → DOCX extraction: dispatches DOCX (evidence: `extract/router.py`)
- Format router → PPTX extraction: dispatches PPTX (evidence: `extract/router.py`)
- DOCX extraction → Chunk data contract: creates chunks (evidence: `extract/docx.py`)
- PDF extraction → Chunk data contract: creates chunks (evidence: `extract/pdf.py`)
- PPTX extraction → Chunk data contract: creates chunks (evidence: `extract/pptx.py`)
- DOCX extraction → OCR processing: runs OCR (evidence: `extract/docx.py`)
- PPTX extraction → OCR processing: runs OCR (evidence: `extract/pptx.py`)
- PDF extraction → OCR processing: runs OCR (evidence: `extract/pdf.py`)
- DOCX extraction → Image regions: detects image regions (evidence: `extract/docx.py`)
- PPTX extraction → Image regions: detects image regions (evidence: `extract/pptx.py`)
- Gradio workflow → Detection merge: detects PII (evidence: `app/gradio_app.py`)
- Detection merge → Regex rules: runs rules (evidence: `detect/merge.py`)
- Detection merge → Local NER: runs NER (evidence: `detect/merge.py`)
- Detection merge → Context boosting: boosts context (evidence: `detect/merge.py`)
- Detection merge → Suspect OCR: checks suspect OCR (evidence: `detect/merge.py`)
- Detection merge → OCR repair: repairs OCR candidates (evidence: `detect/merge.py`)
- Regex rules → PII validators: validates matches (evidence: `detect/rules.py`)
- OCR repair → PII validators: validates candidates (evidence: `detect/ocr_repair.py`)
- Gradio workflow → Name propagation: propagates aliases (evidence: `app/gradio_app.py`)
- Gradio workflow → Placeholder redaction: redacts chunks (evidence: `app/gradio_app.py`)
- Placeholder redaction → Encrypted mappings: assigns placeholders (evidence: `redact/redactor.py`)
- Gradio workflow → Leakage safety check: checks residual PII (evidence: `app/gradio_app.py`)
- Leakage safety check → Detection merge: re-scans chunks (evidence: `verify/leakage.py`)
- Gradio workflow → Masked audit reports: generates audit (evidence: `app/gradio_app.py`)
- Browser user → Browser extension: shields prompts (evidence: `README.md`)
- Browser extension → Local extension bridge: uses local bridge (evidence: `README.md`)
- Local extension bridge → Format router: processes documents (evidence: `extension_server/server.py`)
