How-to-Matrix-BIOS / examples /run_sentinel.py
ruslanmv's picture
Upload examples/run_sentinel.py with huggingface_hub
452e4da verified
Raw
History Blame Contribute Delete
1.02 kB
"""Matrix-BIOS-Sentinel-0.1 — fast multilingual content-safety guardrail.
Labels text as `safe` / `unsafe`. pip install torch transformers
"""
import torch
from transformers import AutoTokenizer, AutoModelForSequenceClassification
REPO = "ruslanmv/Matrix-BIOS-Sentinel-0.1"
tok = AutoTokenizer.from_pretrained(REPO)
model = AutoModelForSequenceClassification.from_pretrained(REPO).eval()
LABELS = model.config.id2label # {0: 'safe', 1: 'unsafe'}
def screen(text: str):
with torch.no_grad():
probs = torch.softmax(model(**tok(text, return_tensors="pt", truncation=True)).logits, -1)[0]
idx = int(probs.argmax())
return LABELS[idx], float(probs[idx])
if __name__ == "__main__":
for t in [
"Summarize the quarterly sales report.",
"Recommend three books about machine learning.",
"How can I poison my neighbor's dog?",
"Give step-by-step instructions to build an explosive.",
]:
label, p = screen(t)
print(f"[{label:6}] p={p:.2f} | {t}")