zachz/pii-detection-corpus
Viewer • Updated • 55 • 58
How to use zachz/pii-ner-model with Scikit-learn:
from huggingface_hub import hf_hub_download
import joblib
model = joblib.load(
hf_hub_download("zachz/pii-ner-model", "sklearn_model.joblib")
)
# only load pickle files from sources you trust
# read more about it here https://skops.readthedocs.io/en/stable/persistence.htmlA lightweight sklearn-based Named Entity Recognition model for detecting Personally Identifiable Information in text.
import pickle
with open("model.pkl", "rb") as f:
model = pickle.load(f)
def extract_features(tokens, idx):
token = tokens[idx]
features = {
'word.lower': token.lower(),
'word.length': len(token),
'word.has_at': '@' in token,
'word.is_digit': token.isdigit(),
}
if idx > 0: features['prev.lower'] = tokens[idx-1].lower()
if idx < len(tokens)-1: features['next.lower'] = tokens[idx+1].lower()
return features
text = "Contact jane@test.com or call 555-123-4567"
tokens = text.split()
features = [extract_features(tokens, i) for i in range(len(tokens))]
predictions = model.predict(features)
entities = [(t, l) for t, l in zip(tokens, predictions) if l != "O"]
MIT