# aajil-labs/arabic-pii-py architecture

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

- Interactive diagram: https://gitdiagram.com/aajil-labs/arabic-pii-py
- Repository: https://github.com/aajil-labs/arabic-pii-py

## Overview

apii is a local PII-protection layer for Arabic and Gulf text and documents. Its core workflow detects sensitive spans, applies policy-controlled reversible tokenization, sends only tokenized content through an optional AI proxy, then restores values locally from an encrypted vault. It also exposes document/batch processing, CLI and UI entry points, and Claude Code hooks with a watch view. The graph follows documented workflows and source-index references; dashed links mark optional or incompletely evidenced integrations, and unsampled subsystems are represented only where the README supports them.

Main entry points: [`apii/anonymizer.py`](https://github.com/aajil-labs/arabic-pii-py/blob/main/apii/anonymizer.py) (Tokenization & Restore), [`apii/server.py`](https://github.com/aajil-labs/arabic-pii-py/blob/main/apii/server.py) (Proxy Gateway), [`apii/cli.py`](https://github.com/aajil-labs/arabic-pii-py/blob/main/apii/cli.py) (CLI), [`apii/pipeline.py`](https://github.com/aajil-labs/arabic-pii-py/blob/main/apii/pipeline.py) (Detection Pipeline)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_detection["Detection"]
  node_pipeline["Detection Pipeline<br/>[pipeline.py]"]
  node_normalization["Arabic Normalization<br/>[normalize.py]"]
  node_recognizers["PII Recognizers"]
  node_context["Context Boost<br/>[context.py]"]
  node_ner["On-device NER<br/>[ner.py]"]
  node_geo["Geo Detection<br/>[geo.py]"]
  node_suppression["Phrase Suppression<br/>[suppressor.py]"]
end

subgraph group_redaction["Redaction &amp; Vault"]
  node_anonymizer["Tokenization &amp; Restore<br/>[anonymizer.py]"]
  node_policy["Redaction Policy<br/>[policy.py]"]
  node_vault[("Encrypted Vault<br/>[vault.py]")]
end

subgraph group_interfaces["User Interfaces"]
  node_cli["CLI<br/>[cli.py]"]
  node_ui["Web UI<br/>[ui.py]"]
  node_hook["Claude Hooks<br/>[hook.py]"]
  node_watch["Watch View<br/>[watch.py]"]
end

subgraph group_integrations["AI &amp; Documents"]
  node_batch["Batch Processing<br/>[batch.py]"]
  node_documents["Document Adapters"]
  node_proxy["Proxy Gateway<br/>[server.py]"]
  node_streaming["Stream Handling<br/>[streaming.py]"]
end

node_user(("User / Client"))
node_model(("AI Provider"))

node_user -->|"invokes"| node_cli
node_user -->|"uses"| node_ui
node_user -->|"installs"| node_hook
node_user -->|"routes requests"| node_proxy
node_cli -->|"detects / redacts"| node_anonymizer
node_cli -->|"runs batches"| node_batch
node_cli -->|"starts"| node_proxy
node_cli -->|"manages hooks"| node_hook
node_cli -->|"starts"| node_ui
node_pipeline -->|"normalizes text"| node_normalization
node_pipeline -->|"runs detection"| node_recognizers
node_pipeline -->|"adds detections"| node_geo
node_pipeline -->|"filters spans"| node_suppression
node_pipeline -.->|"merges NER"| node_ner
node_anonymizer -->|"requests detections"| node_pipeline
node_anonymizer -->|"applies policy"| node_policy
node_ui -->|"redacts / restores"| node_anonymizer
node_ui -->|"loads / saves"| node_vault
node_ui -->|"processes files"| node_documents
node_hook -->|"redacts / restores"| node_anonymizer
node_watch -->|"restores view"| node_anonymizer
node_watch -->|"loads records"| node_vault
node_batch -->|"redacts content"| node_anonymizer
node_batch -->|"adapts files"| node_documents
node_batch -->|"persists records"| node_vault
node_documents -->|"passes extracted text"| node_anonymizer
node_proxy -->|"redacts requests"| node_anonymizer
node_proxy -->|"handles streams"| node_streaming
node_proxy -->|"forwards tokens"| node_model
node_model -->|"returns reply"| node_proxy
node_proxy -->|"restores reply"| node_anonymizer

click node_cli "https://github.com/aajil-labs/arabic-pii-py/blob/main/apii/cli.py"
click node_ui "https://github.com/aajil-labs/arabic-pii-py/blob/main/apii/ui.py"
click node_hook "https://github.com/aajil-labs/arabic-pii-py/blob/main/apii/hook.py"
click node_watch "https://github.com/aajil-labs/arabic-pii-py/blob/main/apii/watch.py"
click node_pipeline "https://github.com/aajil-labs/arabic-pii-py/blob/main/apii/pipeline.py"
click node_normalization "https://github.com/aajil-labs/arabic-pii-py/blob/main/apii/normalize.py"
click node_recognizers "https://github.com/aajil-labs/arabic-pii-py/tree/main/apii/recognizers"
click node_context "https://github.com/aajil-labs/arabic-pii-py/blob/main/apii/context.py"
click node_ner "https://github.com/aajil-labs/arabic-pii-py/blob/main/apii/ner.py"
click node_geo "https://github.com/aajil-labs/arabic-pii-py/blob/main/apii/geo.py"
click node_suppression "https://github.com/aajil-labs/arabic-pii-py/blob/main/apii/suppressor.py"
click node_anonymizer "https://github.com/aajil-labs/arabic-pii-py/blob/main/apii/anonymizer.py"
click node_policy "https://github.com/aajil-labs/arabic-pii-py/blob/main/apii/policy.py"
click node_vault "https://github.com/aajil-labs/arabic-pii-py/blob/main/apii/vault.py"
click node_batch "https://github.com/aajil-labs/arabic-pii-py/blob/main/apii/batch.py"
click node_documents "https://github.com/aajil-labs/arabic-pii-py/tree/main/apii/documents"
click node_proxy "https://github.com/aajil-labs/arabic-pii-py/blob/main/apii/server.py"
click node_streaming "https://github.com/aajil-labs/arabic-pii-py/blob/main/apii/streaming.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_pipeline,node_normalization,node_recognizers,node_context,node_ner,node_geo,node_suppression,node_user toneBlue
class node_anonymizer,node_policy,node_vault toneAmber
class node_cli,node_ui,node_hook,node_watch toneMint
class node_batch,node_documents,node_proxy,node_streaming toneRose
class node_model toneIndigo
```

## Components

### Detection

- [Detection Pipeline](https://github.com/aajil-labs/arabic-pii-py/blob/main/apii/pipeline.py): `apii/pipeline.py`
- [Arabic Normalization](https://github.com/aajil-labs/arabic-pii-py/blob/main/apii/normalize.py): `apii/normalize.py`
- [PII Recognizers](https://github.com/aajil-labs/arabic-pii-py/tree/main/apii/recognizers): `apii/recognizers`
- [Context Boost](https://github.com/aajil-labs/arabic-pii-py/blob/main/apii/context.py): `apii/context.py`
- [On-device NER](https://github.com/aajil-labs/arabic-pii-py/blob/main/apii/ner.py): `apii/ner.py`
- [Geo Detection](https://github.com/aajil-labs/arabic-pii-py/blob/main/apii/geo.py): `apii/geo.py`
- [Phrase Suppression](https://github.com/aajil-labs/arabic-pii-py/blob/main/apii/suppressor.py): `apii/suppressor.py`

### Redaction & Vault

- [Tokenization & Restore](https://github.com/aajil-labs/arabic-pii-py/blob/main/apii/anonymizer.py): `apii/anonymizer.py`
- [Redaction Policy](https://github.com/aajil-labs/arabic-pii-py/blob/main/apii/policy.py): `apii/policy.py`
- [Encrypted Vault](https://github.com/aajil-labs/arabic-pii-py/blob/main/apii/vault.py): `apii/vault.py`

### User Interfaces

- [CLI](https://github.com/aajil-labs/arabic-pii-py/blob/main/apii/cli.py): `apii/cli.py`
- [Web UI](https://github.com/aajil-labs/arabic-pii-py/blob/main/apii/ui.py): `apii/ui.py`
- [Claude Hooks](https://github.com/aajil-labs/arabic-pii-py/blob/main/apii/hook.py): `apii/hook.py`
- [Watch View](https://github.com/aajil-labs/arabic-pii-py/blob/main/apii/watch.py): `apii/watch.py`

### AI & Documents

- [Batch Processing](https://github.com/aajil-labs/arabic-pii-py/blob/main/apii/batch.py): `apii/batch.py`
- [Document Adapters](https://github.com/aajil-labs/arabic-pii-py/tree/main/apii/documents): `apii/documents`
- [Proxy Gateway](https://github.com/aajil-labs/arabic-pii-py/blob/main/apii/server.py): `apii/server.py`
- [Stream Handling](https://github.com/aajil-labs/arabic-pii-py/blob/main/apii/streaming.py): `apii/streaming.py`

### Other components

- **User / Client**
- **AI Provider**

## Connections

- User / Client → CLI: invokes
- User / Client → Web UI: uses
- User / Client → Claude Hooks: installs
- User / Client → Proxy Gateway: routes requests
- CLI → Tokenization & Restore: detects / redacts (evidence: `apii/cli.py`)
- CLI → Batch Processing: runs batches (evidence: `apii/cli.py`)
- CLI → Proxy Gateway: starts (evidence: `apii/cli.py`)
- CLI → Claude Hooks: manages hooks (evidence: `apii/cli.py`)
- CLI → Web UI: starts (evidence: `apii/cli.py`)
- Detection Pipeline → Arabic Normalization: normalizes text (evidence: `apii/pipeline.py`)
- Detection Pipeline → PII Recognizers: runs detection (evidence: `apii/pipeline.py`)
- Detection Pipeline → Geo Detection: adds detections (evidence: `apii/pipeline.py`)
- Detection Pipeline → Phrase Suppression: filters spans (evidence: `apii/pipeline.py`)
- Detection Pipeline → On-device NER: merges NER
- Tokenization & Restore → Detection Pipeline: requests detections (evidence: `apii/anonymizer.py`)
- Tokenization & Restore → Redaction Policy: applies policy (evidence: `apii/anonymizer.py`)
- Web UI → Tokenization & Restore: redacts / restores (evidence: `apii/ui.py`)
- Web UI → Encrypted Vault: loads / saves (evidence: `apii/ui.py`)
- Web UI → Document Adapters: processes files (evidence: `apii/ui.py`)
- Claude Hooks → Tokenization & Restore: redacts / restores (evidence: `apii/hook.py`)
- Watch View → Tokenization & Restore: restores view (evidence: `apii/watch.py`)
- Watch View → Encrypted Vault: loads records (evidence: `apii/watch.py`)
- Batch Processing → Tokenization & Restore: redacts content (evidence: `apii/batch.py`)
- Batch Processing → Document Adapters: adapts files (evidence: `apii/batch.py`)
- Batch Processing → Encrypted Vault: persists records (evidence: `apii/batch.py`)
- Document Adapters → Tokenization & Restore: passes extracted text (evidence: `apii/documents/__init__.py`)
- Proxy Gateway → Tokenization & Restore: redacts requests (evidence: `apii/server.py`)
- Proxy Gateway → Stream Handling: handles streams (evidence: `apii/server.py`)
- Proxy Gateway → AI Provider: forwards tokens
- AI Provider → Proxy Gateway: returns reply
- Proxy Gateway → Tokenization & Restore: restores reply (evidence: `apii/server.py`)
