# shivammitra/data-anonymization architecture

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

- Interactive diagram: https://gitdiagram.com/shivammitra/data-anonymization
- Repository: https://github.com/shivammitra/data-anonymization

## Overview

#### Purpose and Entry Points
This is a Python text anonymization utility that detects sensitive entities and replaces them with category placeholders while retaining surrounding text. The primary executable and importable entry point is `data_anonymizer.py`; it exposes the `DataAnonymizer` workflow and can be run directly. `app.py` may provide an additional application-facing entry point, but its role is not established by the supplied documentation.

#### Core Anonymization Orchestrator
`data_anonymizer.py` is the architectural center. Its anonymizer class/function accepts raw text, coordinates entity detection, and emits anonymized text. This module is intended to be embedded in other data-processing pipelines as well as used as a standalone script.

Paths:
- `data_anonymizer.py`
- `app.py`

#### Remote BERT NER Stage
Named-entity recognition is delegated to Hugging Face’s hosted inference API using the `dbmdz/bert-large-cased-finetuned-conll03-english` model. The model supplies context-sensitive entity identification, including person, organization, location, and miscellaneous entity categories. This is an external-service boundary: anonymization depends on network availability and Hugging Face API behavior.

Paths:
- `data_anonymizer.py`
- `requirements.txt`

#### Credential and Runtime Boundary
The runtime is Python 3.8+ and requires dependencies installed from `requirements.txt`. A Hugging Face token is expected through the `HF_TOKENS` environment variable, typically loaded from a local `.env` file. Secrets are intentionally outside the committed repository tree and must be provisioned by deployment environments.

Paths:
- `requirements.txt`
- `data_anonymizer.py`

#### Deterministic Regex Detection Stage
Alongside model-based NER, the pipeline performs regex-based detection for emails and phone numbers. This covers structured identifiers that may be missed, fragmented, or inconsistently labeled by the NER model. Regex detection is a local processing boundary and does not require the remote inference service.

Paths:
- `data_anonymizer.py`

#### Replacement and Output Semantics
Detected values are transformed into stable semantic labels such as `[Person]`, `[SURNAME]`, `[ORGANIZATION]`, `[LOCATION]`, `[EMAIL]`, and `[PHONE]`. The output is plain anonymized text, not a separate structured entity record. Correct replacement ordering and text-span handling are therefore central: model entities and regex matches must be applied without corrupting offsets or overlapping replacements.

Paths:
- `data_anonymizer.py`

#### Deployment Characteristics and Extension Direction
The current design is lightweight and service-dependent rather than GPU- or local-model-dependent. It is suited to batch scripts or integration as a Python module. Planned evolution includes offline Hugging Face Transformers inference, domain-specific NER models, multilingual support, and a Streamlit UI; these would primarily extend the inference boundary or add a presentation layer without changing the core text-in/text-out contract.

Paths:
- `data_anonymizer.py`
- `app.py`

Main entry points: [`data_anonymizer.py`](https://github.com/shivammitra/data-anonymization/blob/main/data_anonymizer.py) (DataAnonymizer), [`app.py`](https://github.com/shivammitra/data-anonymization/blob/main/app.py) (Application entry point), [`requirements.txt`](https://github.com/shivammitra/data-anonymization/blob/main/requirements.txt) (Python dependencies)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_runtime["Runtime"]
  node_consumer(("Script or Python consumer<br/>caller"))
  node_app["Application entry point<br/>Python entry point<br/>[app.py]"]
  node_dependencies["Python dependencies<br/>dependency manifest<br/>[requirements.txt]"]
  node_hf_token{{"HF_TOKENS<br/>environment secret"}}
end

subgraph group_pipeline["Anonymization Pipeline"]
  node_orchestrator["DataAnonymizer<br/>Python orchestrator<br/>[data_anonymizer.py]"]
  node_raw_text["Raw text<br/>input"]
  node_ner_stage["Remote NER detection<br/>inference client<br/>[data_anonymizer.py]"]
  node_regex_stage["Regex detection<br/>local detector<br/>[data_anonymizer.py]"]
  node_span_replacement["Span replacement<br/>text transformer<br/>[data_anonymizer.py]"]
  node_anonymized_text["Anonymized text<br/>output<br/>[data_anonymizer.py]"]
end

subgraph group_external["External Service"]
  node_huggingface_api{{"Hugging Face Inference API<br/>hosted inference service"}}
  node_bert_model["BERT CoNLL03 model<br/>NER model"]
end

node_consumer -->|"runs or imports"| node_orchestrator
node_app -.->|"possible integration"| node_orchestrator
node_dependencies -->|"provides packages"| node_orchestrator
node_raw_text -->|"text input"| node_orchestrator
node_orchestrator -->|"coordinates"| node_ner_stage
node_orchestrator -->|"coordinates"| node_regex_stage
node_hf_token -->|"authenticates"| node_ner_stage
node_ner_stage -->|"inference request"| node_huggingface_api
node_huggingface_api -->|"serves"| node_bert_model
node_ner_stage -->|"NER spans"| node_span_replacement
node_regex_stage -->|"email and phone spans"| node_span_replacement
node_span_replacement -->|"semantic placeholders"| node_anonymized_text

click node_app "https://github.com/shivammitra/data-anonymization/blob/main/app.py"
click node_dependencies "https://github.com/shivammitra/data-anonymization/blob/main/requirements.txt"
click node_orchestrator "https://github.com/shivammitra/data-anonymization/blob/main/data_anonymizer.py"
click node_ner_stage "https://github.com/shivammitra/data-anonymization/blob/main/data_anonymizer.py"
click node_regex_stage "https://github.com/shivammitra/data-anonymization/blob/main/data_anonymizer.py"
click node_span_replacement "https://github.com/shivammitra/data-anonymization/blob/main/data_anonymizer.py"
click node_anonymized_text "https://github.com/shivammitra/data-anonymization/blob/main/data_anonymizer.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_consumer,node_app,node_dependencies,node_hf_token toneBlue
class node_orchestrator,node_raw_text,node_ner_stage,node_regex_stage,node_span_replacement,node_anonymized_text toneAmber
class node_huggingface_api,node_bert_model toneMint
```

## Components

### Runtime

- **Script or Python consumer**: caller · Runs or imports the anonymization workflow.
- [Application entry point](https://github.com/shivammitra/data-anonymization/blob/main/app.py): Python entry point · Application-facing role is not established. · `app.py`
- [Python dependencies](https://github.com/shivammitra/data-anonymization/blob/main/requirements.txt): dependency manifest · Defines required runtime packages. · `requirements.txt`
- **HF_TOKENS**: environment secret · Deployment-provided Hugging Face API token.

### Anonymization Pipeline

- [DataAnonymizer](https://github.com/shivammitra/data-anonymization/blob/main/data_anonymizer.py): Python orchestrator · Coordinates text-in/text-out anonymization and supports direct execution. · `data_anonymizer.py`
- **Raw text**: input
- [Remote NER detection](https://github.com/shivammitra/data-anonymization/blob/main/data_anonymizer.py): inference client · Obtains contextual named entities. · `data_anonymizer.py`
- [Regex detection](https://github.com/shivammitra/data-anonymization/blob/main/data_anonymizer.py): local detector · Finds email addresses and phone numbers. · `data_anonymizer.py`
- [Span replacement](https://github.com/shivammitra/data-anonymization/blob/main/data_anonymizer.py): text transformer · Orders overlapping matches and replaces values without corrupting offsets. · `data_anonymizer.py`
- [Anonymized text](https://github.com/shivammitra/data-anonymization/blob/main/data_anonymizer.py): output · Plain text containing semantic placeholders. · `data_anonymizer.py`

### External Service

- **Hugging Face Inference API**: hosted inference service · Network-dependent NER service boundary.
- **BERT CoNLL03 model**: NER model · dbmdz/bert-large-cased-finetuned-conll03-english.

## Connections

- Script or Python consumer → DataAnonymizer: runs or imports
- Application entry point → DataAnonymizer: possible integration
- Python dependencies → DataAnonymizer: provides packages
- Raw text → DataAnonymizer: text input
- DataAnonymizer → Remote NER detection: coordinates
- DataAnonymizer → Regex detection: coordinates
- HF_TOKENS → Remote NER detection: authenticates
- Remote NER detection → Hugging Face Inference API: inference request
- Hugging Face Inference API → BERT CoNLL03 model: serves
- Remote NER detection → Span replacement: NER spans
- Regex detection → Span replacement: email and phone spans
- Span replacement → Anonymized text: semantic placeholders
