Upload examples/run_sentinel.py with huggingface_hub
Browse files- examples/run_sentinel.py +26 -0
examples/run_sentinel.py
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Matrix-BIOS-Sentinel-0.1 — fast multilingual content-safety guardrail.
|
| 2 |
+
Labels text as `safe` / `unsafe`. pip install torch transformers
|
| 3 |
+
"""
|
| 4 |
+
import torch
|
| 5 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 6 |
+
|
| 7 |
+
REPO = "ruslanmv/Matrix-BIOS-Sentinel-0.1"
|
| 8 |
+
tok = AutoTokenizer.from_pretrained(REPO)
|
| 9 |
+
model = AutoModelForSequenceClassification.from_pretrained(REPO).eval()
|
| 10 |
+
LABELS = model.config.id2label # {0: 'safe', 1: 'unsafe'}
|
| 11 |
+
|
| 12 |
+
def screen(text: str):
|
| 13 |
+
with torch.no_grad():
|
| 14 |
+
probs = torch.softmax(model(**tok(text, return_tensors="pt", truncation=True)).logits, -1)[0]
|
| 15 |
+
idx = int(probs.argmax())
|
| 16 |
+
return LABELS[idx], float(probs[idx])
|
| 17 |
+
|
| 18 |
+
if __name__ == "__main__":
|
| 19 |
+
for t in [
|
| 20 |
+
"Summarize the quarterly sales report.",
|
| 21 |
+
"Recommend three books about machine learning.",
|
| 22 |
+
"How can I poison my neighbor's dog?",
|
| 23 |
+
"Give step-by-step instructions to build an explosive.",
|
| 24 |
+
]:
|
| 25 |
+
label, p = screen(t)
|
| 26 |
+
print(f"[{label:6}] p={p:.2f} | {t}")
|