An end-to-end NLP project that fine-tunes DistilBERT for Named Entity Recognition (NER) to automatically detect and redact Personally Identifiable Information (PII) from text — built entirely from scratch, from synthetic data generation to model deployment.
Skills demonstrated: NLP · Transformer fine-tuning · Token classification · Synthetic data generation · Hugging Face ecosystem · PyTorch · Apple Silicon (MPS) acceleration
Given a raw log line containing sensitive information, the model identifies and replaces PII with a safe placeholder:
Input : User alice@gmail.com failed login at 10:45pm
Output : User [EMAIL] failed login at 10:45pm
Detection runs with a confidence threshold of 0.90 and handles edge cases such as sub-word tokenization and adjacent entity merging — ensuring high-precision redaction suitable for production-style pipelines.
| Area | Details |
|---|---|
| Model | distilbert-base-uncased fine-tuned for token classification |
| Task | Named Entity Recognition (NER) with IOB2 tagging |
| Training data | 10,000 synthetic log lines generated with Faker (privacy-safe) |
| Train / test split | 90 / 10 |
| Training | 3 epochs · LR 2e-5 · batch size 8 · best-checkpoint selection |
| Evaluation metric | seqeval (precision, recall, F1, accuracy) |
| Hardware | Apple Silicon MPS acceleration supported |
| Data format | Hugging Face datasets (Arrow) |
.
├── data.py # Synthetic PII dataset generation (Faker + IOB2 labelling)
├── train.py # DistilBERT fine-tuning with Hugging Face Trainer
├── test.py # Inference pipeline with confidence filtering & redaction
├── pii_data/ # Saved dataset (train + test splits, Arrow format)
├── my-model/ # Per-epoch training checkpoints
└── my-pii-model/ # Final model + tokenizer ready for inference
For a detailed technical walkthrough of each component, see walkthrough.md.
Requirements: Python 3.9+, PyTorch, Hugging Face stack
pip install torch transformers datasets evaluate faker seqeval# 1. Generate synthetic training data
python data.py
# 2. Fine-tune the model (saves to my-pii-model/)
python train.py
# 3. Run inference and see redaction in action
python test.pyApple Silicon users get MPS GPU acceleration automatically with
torch >= 2.0.
Why synthetic data? Using real PII for training is a privacy risk. Faker generates realistic email patterns that teach the model the structural features of the entity type without exposing any real user data.
Why DistilBERT? It retains ~97% of BERT's performance at 40% fewer parameters and twice the inference speed — a practical choice for a redaction service that needs to run at scale.
Why seqeval? It evaluates span-level entity detection rather than per-token accuracy, which is the correct metric for NER — a model that correctly identifies entity boundaries is rewarded over one that only gets individual tokens right.
Extensibility: The label scheme (B-EMAIL, O) is designed to expand. Adding B-PHONE, B-NAME, or other PII types requires only modifying data.py to generate new entity types and updating label_list in train.py.