Skip to main content

PII Classification Model

This project provides a complete pipeline for Named Entity Recognition (NER) focused on identifying Personally Identifiable Information (PII) in Polish text. It includes utilities for data conversion, label generalization, model training using Hugging Face Transformers, and a deployment-ready Flask API with a simple web interface.

Repository: https://github.com/radlab-dev-group/anonymizer-model

Features

  • Data Processing CLI: Tools to convert CONLL/IOB formats to JSONL, generalize labels, and generate distribution reports.
  • Training Pipeline: A configurable trainer based on AutoModelForTokenClassification with Weights & Biases (W&B) integration.
  • Advanced Inference: A predictor that handles sub-token merging, punctuation cleaning, and gap preservation to return human-readable entities.
  • REST API: A Flask-based service to serve multiple model versions with optional dynamic quantization for faster inference.
  • Web Tester: A lightweight HTML/JS interface for real-time PII detection testing.

Installation

Ensure you have Python $\ge$ 3.9 installed.

git clone https://github.com/radlab-dev-group/anonymizer-model.git
cd anonymizer-model
pip install .

By default, pip install . installs the core dependencies required to run the Flask API and the pii-classifier convert / generalise commands: flask, flask_cors, radlab-ml-utils, numpy, torch, transformers, and datasets.

Optional extras cover the rest of the tooling:

Extra Installs Needed for
train scikit-learn, wandb Training the model (pii_classification/trainer)
report pandas, xlsxwriter pii-classifier report (Excel distribution reports)
test pytest Test tooling
deps train + report Everything except test

For example, to get core dependencies plus training and reporting tools:

pip install .[deps]

Data Preparation

The project is designed to work with datasets like clarin-pl/kpwr-ner.

  1. Download Dataset: Store IOB files in dataset/kpwr/raw/.
  2. Convert to JSONL:
pii-classifier convert \
     -i dataset/kpwr/raw/kpwr-ner-n82-train-tune.iob \
     -o dataset/kpwr/converted/specific/kpwr-ner-n82-train-tune.jsonl


pii-classifier convert \
     -i dataset/kpwr/raw/kpwr-ner-n82-test.iob \
     -o dataset/kpwr/converted/specific/kpwr-ner-n82-test.jsonl
  1. Generalize Labels: Map fine-grained labels to general categories using a mapping file (e.g., config/mappings/kpwr-ner.json).
pii-classifier generalise \
     -i dataset/kpwr/converted/specific/kpwr-ner-n82-train-tune.jsonl \
     dataset/kpwr/converted/specific/kpwr-ner-n82-test.jsonl \
     -m config/mappings/kpwr-ner.json \
     -o dataset/kpwr/converted/generalised/kpwr-ner-general-whole.jsonl
  1. Generate Report: Create an Excel report to analyze class distribution.
pii-classifier report \
     -i dataset/kpwr/converted/generalised/kpwr-ner-general-whole.jsonl \
     -o dataset/kpwr/converted/generalised/kpwr-ner-general-whole-report.xlsx

Training

Training is driven by a JSON configuration file located in config/training/.

To start training, run the training script:

python pii_classification/trainer/train.py

Key Training Features:

  • Configurable: Hyperparameters (learning rate, batch size, epochs) are managed via kpwr-ner-config.json.
  • W&B Integration: Logs metrics and hyperparameters to Weights & Biases.
  • Automatic Export: Saves the best model (based on f1_macro) into a final_model directory.

Inference & API

Running the API

The API allows you to load multiple model versions and perform predictions.

python3 -m pii_classification.api.app

Endpoints:

  • GET /models: Returns a list of available models and the default model.
  • POST /predict: Accepts JSON with text and optional model name. Returns a list of tokens and their predicted PII labels.
  • POST /predict_and_anonymize: Accepts JSON with text, optional model name, and a labels list. Returns the text with the selected PII entities replaced by tags.

Web Interface (under development)

Open pii_classification/ui/index.html in a browser to interact with the API. The UI allows you to select a model, input Polish text, and see highlighted PII entities.

Or simple python-like http server:

cd anonymizer-model/pii_classification/ui
python3 -m http.server

>> Serving HTTP on 0.0.0.0 port 8000 (http://0.0.0.0:8000/) ...

CLI Reference

The pii-classifier command provides the following sub-commands:

Command Description Required Arguments
convert Convert CONLL/IOB file to JSONL -i (input), -o (output)
generalise Map labels using a JSON map -i (input files), -m (mapping), -o (output)
report Generate Excel distribution report -i (input files), -o (output)

Project Structure

├── config/
│   ├── mappings/       # Label mapping JSONs
│   └── training/       # Training hyperparameter configs
├── pii_classification/
│   ├── analysis/       # Label generalization and reporting logic
│   ├── api/            # Flask API implementation
│   ├── cli/            # CLI entry point
│   ├── converters/     # Format conversion utilities
│   ├── inference/      # Model prediction and post-processing logic
│   ├── trainer/        # Training scripts and data processors
│   └── ui/             # Frontend tester (HTML/JS)
└── pyproject.toml      # Project dependencies and metadata

Metadata

Release files for radlab-pii-classification 0.1.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for radlab-pii-classification 0.1.2
File Size Uploaded Provenance Actions
radlab_pii_classification-0.1.2.tar.gz 18.0 kB Without provenance Details for radlab_pii_classification-0.1.2.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for radlab-pii-classification 0.1.2
File Interpreter ABI Platform Actions
radlab_pii_classification-0.1.2-py3-none-any.whl Python 3 none any Details for radlab_pii_classification-0.1.2-py3-none-any.whl

Total release size: 38.0 kB

Release files / radlab_pii_classification-0.1.2.tar.gz

Download URL radlab_pii_classification-0.1.2.tar.gz
Size 18.0 kB
Tags Source
SHA-256 checksum
How to use checksums
02ef5665f85965c871e0262af92884219cef22559411c1adad0e226383249385
BLAKE2b-256 checksum
How to use checksums
5e255d00bcd6d5e56adab2375337b6d5caf83a207b15028269e0f109afcf2f53
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.4

Release files / radlab_pii_classification-0.1.2-py3-none-any.whl

Download URL radlab_pii_classification-0.1.2-py3-none-any.whl
Size 19.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
7abd5ceff2c7392699d8c5ffc1cb6ff876d0f65133265aedf006996c85a6e1e1
BLAKE2b-256 checksum
How to use checksums
35ff11ea49246fc7d17d8d83bf7319493430450b394e66c1a3273ed5ffc0d42b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.4

Security for radlab-pii-classification 0.1.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page