Instructions to use ankitcodes/pii_model with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use ankitcodes/pii_model with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("token-classification", model="ankitcodes/pii_model")# pip install -U transformers accelerate # Load model directly from transformers import AutoTokenizer, AutoModelForTokenClassification tokenizer = AutoTokenizer.from_pretrained("ankitcodes/pii_model") model = AutoModelForTokenClassification.from_pretrained("ankitcodes/pii_model", device_map="auto") - Notebooks
- Google Colab
- Kaggle
pii_model: English PII detection with BERT
ankitcodes/pii_model is a token-classification model that finds personally identifiable information (PII) in English text. It is a fine-tune of google-bert/bert-base-cased (about 0.1B parameters, Safetensors, F32) and predicts 31 entity types, such as names, addresses, emails, phone numbers and card numbers.
Intended use
- Flagging or redacting PII in English text such as emails, chat logs, forms and support tickets, before the text is stored, shared or sent to another system.
- A starting point for fine-tuning on your own PII data.
Not intended for
- Being the only control for GDPR, HIPAA or similar compliance.
- Languages other than English.
- Medical records. The labels are financial and personal; they do not include medical record numbers, patient IDs, hospital names or device IDs.
- Documents longer than 512 tokens without chunking (see below).
Entity labels (31)
| Group | Labels |
|---|---|
| Person | PREFIX, FIRSTNAME, MIDDLENAME, LASTNAME, GENDER, SEX, AGE, DOB, DATE |
| Contact | EMAIL, PHONENUMBER, USERNAME, URL |
| Address | BUILDINGNUMBER, STREET, SECONDARYADDRESS, CITY, COUNTY, STATE, ZIPCODE |
| Financial | ACCOUNTNUMBER, ACCOUNTNAME, IBAN, CREDITCARDNUMBER, CREDITCARDISSUER, CREDITCARDCVV, AMOUNT, PIN |
| Identifiers and secrets | SSN, PASSWORD |
| Organisation | COMPANYNAME |
How to use
Quick start
from transformers import pipeline
ner = pipeline(
"token-classification",
model="ankitcodes/pii_model",
aggregation_strategy="simple", # merge word pieces into whole entities
)
text = "Hi, I'm John Smith. Call me on 555-0134 or email john.smith@example.com."
for ent in ner(text):
print(ent["entity_group"], ent["word"], round(float(ent["score"]), 3))
Redaction example
def redact(text, ner, min_score=0.5):
# Replace from the end so earlier character offsets stay valid.
entities = sorted(ner(text), key=lambda e: e["start"], reverse=True)
for e in entities:
if e["score"] >= min_score:
text = text[: e["start"]] + f"[{e['entity_group']}]" + text[e["end"]:]
return text
print(redact("Hi, I'm John Smith. Call me on 555-0134.", ner))
# Expected shape: "Hi, I'm [FIRSTNAME] [LASTNAME]. Call me on [PHONENUMBER]."
Lower min_score to catch more PII at the cost of more false alarms. For PII, missing an entity usually costs more than flagging one too many.
Long documents
BERT reads at most 512 tokens at a time. Split long text into overlapping chunks, run the model on each chunk, and merge the results.
def ner_long(text, ner, size=1500, overlap=200):
results, start = [], 0
while start < len(text):
chunk = text[start : start + size]
for e in ner(chunk):
e["start"] += start
e["end"] += start
results.append(e)
if start + size >= len(text):
break
start += size - overlap
return results # remove duplicates in the overlap before using
Training
- Base model:
google-bert/bert-base-cased - Training data:
- Procedure and hyperparameters:
- Framework versions: Transformers 4.38.2, PyTorch 2.1.0+cu121, Datasets 2.18.0, Tokenizers 0.15.2
Evaluation
Results on the evaluation split of the training dataset:
| Metric | Score |
|---|---|
| Precision | 0.955 |
| Recall | 0.965 |
| F1 | 0.960 |
| Accuracy | 0.991 |
How to read these numbers:
- They come from the same source as the training data. If that data is synthetic, real-world scores will usually be lower.
- Token accuracy is high partly because most tokens are not PII. Entity-level F1 is more informative.
- Per-entity results are not yet reported. Rare labels (for example PIN, CREDITCARDCVV, COUNTY) are likely weaker than the average.
- The model has not been tested on real-world text such as messy emails, chat logs, scanned-document OCR, or text with typos.
Limitations and risks
- Missed PII. Any PII the model misses stays in your text. Do not rely on it alone.
- False positives. Order numbers, product codes and dates that look like PII may be flagged.
- English only, and trained on a fixed set of 31 label types. Other identifiers (national IDs from other countries, medical record numbers, vehicle plates) are not covered.
- Formatting. Unusual formats, all-lowercase text, typos and OCR errors can reduce accuracy.
- Bias. Names and addresses from under-represented regions or cultures may be detected less reliably. This has not been measured.
- Privacy. Run the model locally or in your own environment when handling sensitive data, so the text is not sent to a third party.
Intended improvements
- Report per-entity metrics and a test on real-world text.
- Try a stronger base model (DeBERTa-v3 or RoBERTa).
- Add healthcare identifiers for de-identification use.
- Publish a quantized or ONNX version for fast CPU use.
License
Apache-2.0 for the model weights and code. Check the licence of the training dataset before using the model commercially.
Citation
@misc{ankitcodes_pii_model,
author = {Ankit Gupta},
title = {pii_model: English PII detection with BERT},
year = {2026},
url = {https://huggingface.co/ankitcodes/pii_model}
}
- Downloads last month
- 43
Model tree for ankitcodes/pii_model
Base model
google-bert/bert-base-cased