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 only the core dependencies required to run the Flask API (flask_cors, radlab-ml-utils).

If you also need the data processing, training, and reporting tools (CONLL conversion, label generalization, Excel reports), install the optional deps extra:

pip install .[deps]

which additionally installs pandas, xlsxwriter, seqeval, and sacremoses.

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.

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.1

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.1
File Size Uploaded
radlab_pii_classification-0.1.1.tar.gz 20.9 kB Details

Built distribution (wheel)

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

Total release size: 52.7 kB

Release files / radlab_pii_classification-0.1.1.tar.gz

Download URL radlab_pii_classification-0.1.1.tar.gz
Size 20.9 kB
Tags Source
SHA-256 checksum
How to use checksums
03bc5d418fb2db70fab2c6691fddbed31b08428e5bc764b2a43d97e6a22ffe09
BLAKE2b-256 checksum
How to use checksums
5f861773ffd9f2204b0bd7fe79bca6dd919f903efe4d566c095a67d674938c62
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.3

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

Download URL radlab_pii_classification-0.1.1-py3-none-any.whl
Size 31.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
6a8d148aaf8147d882cca7d0c25ef2ad48703df5b6972e867c1852a72d274cee
BLAKE2b-256 checksum
How to use checksums
af168f0d6b0b26445f116f3cf845dd5285c9998232aac6b409754083f3f443db
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.3

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files

0.1.0

2 release files

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