Token Classification
Transformers
Safetensors
lfm2
liquid
lfm2.5
bidirectional
masked-lm
encoder
pii
ner
privacy
multilingual
custom_code
Run this model with HF Jobs
Run LiquidAI/LFM2.5-Encoder-350M-PII-Detector on Hugging Face's managed compute, monitor its progress, and retrieve the results when it's done.
Paste one of these prompts into your coding agent:
Fine-tune on my dataset
Use HF Jobs to fine-tune https://huggingface.co/LiquidAI/LFM2.5-Encoder-350M-PII-Detector. Ask me which dataset to train on, then push the fine-tuned model to a new model repo. Run `hf jobs --help` first to learn the CLI.
Run batch inference
Use HF Jobs to run batch inference with https://huggingface.co/LiquidAI/LFM2.5-Encoder-350M-PII-Detector on the inputs in a mounted HF Bucket. Ask me which Bucket to use, then write the outputs back to it and keep each input ID. Run `hf jobs --help` first to learn the CLI.
Instructions to use LiquidAI/LFM2.5-Encoder-350M-PII-Detector with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use LiquidAI/LFM2.5-Encoder-350M-PII-Detector with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("token-classification", model="LiquidAI/LFM2.5-Encoder-350M-PII-Detector", trust_remote_code=True)# pip install -U transformers accelerate # Load model directly from transformers import AutoTokenizer, AutoModelForTokenClassification tokenizer = AutoTokenizer.from_pretrained("LiquidAI/LFM2.5-Encoder-350M-PII-Detector", trust_remote_code=True) model = AutoModelForTokenClassification.from_pretrained("LiquidAI/LFM2.5-Encoder-350M-PII-Detector", trust_remote_code=True, device_map="auto") - Notebooks
- Google Colab
- Kaggle
Upload pii_hybrid_decode.py with huggingface_hub
Browse files123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142"""Self-contained hybrid decode for LiquidAI/pii-detect (v7).
The token-classification head locates PII but, like all byte-BPE token classifiers,fragments the boundaries of format-bound entities (e.g. it tags `1969` inside a date,or `charite.de` inside an email). This module adds an inference-time regex layer — thedecode the product is meant to use — which roughly DOUBLES exact-match F1 with no lossof precision/recall on real text:
AUTH types : distinctive, validator-gated formats (email, IBAN, JWT, SSN, MAC, crypto, api_key, private_key, connection_string, ip, url, credit_card, swift, imei, gps). Regex ADDS these and owns their exact boundaries. SNAP types : FP-prone formats (phone, date_of_birth, amount, postal_code). The MODEL must fire; regex only EXPANDS its fragment to the full match (no new FPs).
Everything else (names, addresses, conditions, medications, org, special-category,username, national_id, passport, etc.) is left to the model.
Usage: import torch from transformers import AutoTokenizer, AutoModelForTokenClassification from pii_hybrid_decode import predict tok = AutoTokenizer.from_pretrained("LiquidAI/pii-detect", trust_remote_code=True) model = AutoModelForTokenClassification.from_pretrained("LiquidAI/pii-detect", trust_remote_code=True).eval() spans = predict("Email laura@charite.de or call +49 30 4505 1234.", tok, model) # -> [{'start':6,'end':22,'type':'contact.email','text':'laura@charite.de'}, ...]"""from __future__ import annotationsimport re
def _luhn_ok(num: str) -> bool: ds = [int(c) for c in num if c.isdigit()] if not (12 <= len(ds) <= 19): return False tot, par = 0, len(ds) % 2 for i, d in enumerate(ds): if i % 2 == par: d *= 2; d = d - 9 if d > 9 else d tot += d return tot % 10 == 0
def _iban_ok(s: str) -> bool: s = s.replace(" ", "").upper() if not re.fullmatch(r"[A-Z]{2}\d{2}[A-Z0-9]{11,30}", s): return False r = s[4:] + s[:4] return int("".join(str(int(c, 36)) for c in r)) % 97 == 1
# (type, pattern, validator) — distinctive formats the regex layer ADDS + owns boundaries_AUTH = [ ("contact.email", re.compile(r"\b[A-Za-z0-9._%+\-]+@[A-Za-z0-9.\-]+\.[A-Za-z]{2,}\b"), None), ("credential.jwt", re.compile(r"\beyJ[A-Za-z0-9_\-]+\.eyJ[A-Za-z0-9_\-]+\.[A-Za-z0-9_\-]+"), None), ("credential.api_key", re.compile(r"\b(?:AKIA[0-9A-Z]{16}|sk-(?:proj-)?[A-Za-z0-9]{20,}|sk-ant-api03-[A-Za-z0-9_\-]{20,}|ghp_[A-Za-z0-9]{36}|AIza[0-9A-Za-z_\-]{35}|xox[baprs]-[A-Za-z0-9\-]{10,}|hf_[A-Za-z0-9]{30,})\b"), None), ("credential.private_key", re.compile(r"-----BEGIN (?:RSA |EC |OPENSSH |PGP )?PRIVATE KEY-----"), None), ("credential.connection_string", re.compile(r"\b(?:postgres(?:ql)?|mysql|mongodb(?:\+srv)?|redis|amqp)://[^\s:@/]+:[^\s:@/]+@[^\s/]+"), None), ("financial.iban", re.compile(r"\b[A-Z]{2}\d{2}(?:[ ]?[A-Z0-9]{4}){2,7}[ ]?[A-Z0-9]{1,3}\b"), _iban_ok), ("financial.crypto_wallet", re.compile(r"\b(?:0x[a-fA-F0-9]{40}|bc1[a-z0-9]{25,90}|[13][a-km-zA-HJ-NP-Z1-9]{25,34})\b"), None), ("device.mac_address", re.compile(r"\b(?:[0-9A-Fa-f]{2}[:\-]){5}[0-9A-Fa-f]{2}\b"), None), ("location.gps_coordinates", re.compile(r"[\-+]?\d{1,3}\.\d{3,}\s*,\s*[\-+]?\d{1,3}\.\d{3,}"), None), ("online.url", re.compile(r"\bhttps?://[^\s]+"), None), ("identity.ssn", re.compile(r"\b\d{3}-\d{2}-\d{4}\b"), None), ("contact.ip_address", re.compile(r"\b(?:(?:25[0-5]|2[0-4]\d|1?\d?\d)\.){3}(?:25[0-5]|2[0-4]\d|1?\d?\d)\b"), None), ("financial.credit_card", re.compile(r"\b(?:\d[ \-]?){13,19}\b"), _luhn_ok), ("financial.swift_bic", re.compile(r"\b[A-Z]{4}[A-Z]{2}[A-Z0-9]{2}(?:[A-Z0-9]{3})?\b"), None), ("device.imei", re.compile(r"\b\d{15}\b"), _luhn_ok),]_SNAP = { "contact.phone": re.compile(r"(?<!\d)(?:\+?\d{1,3}[ \-.]?)?(?:\(\d{2,4}\)[ \-.]?)?\d{2,4}[ \-.]?\d{3}[ \-.]?\d{3,4}(?!\d)"), "identity.date_of_birth": re.compile(r"\b(?:\d{1,2}[\/.\-]\d{1,2}[\/.\-]\d{2,4}|\d{4}[\/.\-]\d{1,2}[\/.\-]\d{1,2}|(?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)[a-z]*\.?\s+\d{1,2},?\s+\d{4}|\d{1,2}\s+(?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)[a-z]*\.?\s+\d{4})\b"), "financial.amount": re.compile(r"(?:[$€£¥]\s?\d[\d.,]*(?:\s?[KMB])?|\b(?:USD|EUR|GBP|JPY|CHF|CAD|AUD)\s?\d[\d.,]*(?:\s?[KMB])?\b|\b\d[\d.,]*\s?(?:USD|EUR|GBP|dollars|euros)\b)"), "contact.postal_code": re.compile(r"\b(?:\d{5}(?:-\d{4})?|[A-Z]{1,2}\d[A-Z\d]?\s?\d[A-Z]{2})\b"),}_AUTH_TYPES = {t for t, _, _ in _AUTH}
def hybrid_spans(text: str, model_spans: list[dict]) -> list[dict]: """model_spans: [{'start','end','type'}...] from the token classifier. Returns the hybrid-decoded spans (dicts with start/end/type/text).""" # 1. AUTH regex spans — built INDEPENDENTLY of the model (regex is authoritative # for these distinctive formats; model fragments must not block them). auth, claimed = [], [False] * len(text) for t, pat, val in _AUTH: for mm in pat.finditer(text): s, e = mm.start(), mm.end() if any(claimed[s:e]): continue if val and not val(mm.group(0)): continue for i in range(s, e): claimed[i] = True auth.append({"start": s, "end": e, "type": t, "text": mm.group(0)}) # 2. model spans for non-AUTH types; SNAP types expand to overlapping regex match out = [] for m in model_spans: t = m["type"] if t in _AUTH_TYPES: continue # regex owns these if t in _SNAP: snap = None for mm in _SNAP[t].finditer(text): if min(mm.end(), m["end"]) > max(mm.start(), m["start"]): snap = mm; break if snap: out.append({"start": snap.start(), "end": snap.end(), "type": t, "text": text[snap.start():snap.end()]}); continue out.append({"start": m["start"], "end": m["end"], "type": t, "text": text[m["start"]:m["end"]]}) out.extend(auth) seen, uniq = set(), [] for sp in sorted(out, key=lambda s: (s["start"], s["end"])): k = (sp["start"], sp["end"], sp["type"]) if k not in seen: seen.add(k); uniq.append(sp) return uniq
def model_spans(text: str, tok, model): import torch enc = tok(text, return_offsets_mapping=True, return_tensors="pt", truncation=True, max_length=2048) off = enc.pop("offset_mapping")[0].tolist() enc = {k: v.to(model.device) for k, v in enc.items()} with torch.no_grad(): ids = model(**enc).logits[0].argmax(-1).tolist() id2label = model.config.id2label spans, cur = [], None for (a, b), i in zip(off, ids): lab = id2label[i] if b <= a or lab == "O": if cur: spans.append(cur); cur = None continue typ = lab.split("-", 1)[1] if "-" in lab else lab if lab[:2] in ("B-", "S-") or cur is None or cur["type"] != typ: if cur: spans.append(cur) cur = {"start": a, "end": b, "type": typ} else: cur["end"] = b if cur: spans.append(cur) # trim leading/trailing whitespace for sp in spans: while sp["start"] < sp["end"] and text[sp["start"]].isspace(): sp["start"] += 1 while sp["end"] > sp["start"] and text[sp["end"] - 1].isspace(): sp["end"] -= 1 return [s for s in spans if s["end"] > s["start"]]
def predict(text: str, tok, model, hybrid: bool = True) -> list[dict]: ms = model_spans(text, tok, model) if not hybrid: return [{"start": s["start"], "end": s["end"], "type": s["type"], "text": text[s["start"]:s["end"]]} for s in ms] return hybrid_spans(text, ms)pii_hybrid_decode.pyAdded+142