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 .

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

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

radlab_pii_classification-0.1.0.tar.gz (20.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

radlab_pii_classification-0.1.0-py3-none-any.whl (31.6 kB view details)

Uploaded Python 3

File details

Details for the file radlab_pii_classification-0.1.0.tar.gz.

File metadata

File hashes

Hashes for radlab_pii_classification-0.1.0.tar.gz
Algorithm Hash digest
SHA256 9d0a7e8db67942cd40a2b04287f510687e6953d188aefdbb67f219027f5a9e83
MD5 d41589cc5e5c7867a627f923c270c781
BLAKE2b-256 4479996c527573020b53f4361aef9755a66ba2aaae47f62bc99f4836622d3dfe

See more details on using hashes here.

File details

Details for the file radlab_pii_classification-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for radlab_pii_classification-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f6823c9ce32a483acee1e8a83d38fcab1531b381fe45f52c58f6cbe207302d7c
MD5 10f7fd83fda45de8d81fd85b6c23fae6
BLAKE2b-256 3cbaf6d46784aca11a183c84e8724142321e5e76a966a973a750f100bbf260bc

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 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