Upload inference.py
Browse files- inference.py +38 -0
inference.py
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""Inference script for HR conversation multi-label classifier."""
|
| 3 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 4 |
+
import torch
|
| 5 |
+
|
| 6 |
+
MODEL_ID = "AurelPx/hr-conversations-classifier"
|
| 7 |
+
|
| 8 |
+
LABELS = [
|
| 9 |
+
"Benefits", "Career Development", "Compliance & Legal", "Contracts",
|
| 10 |
+
"Diversity, Equity & Inclusion", "Expense Management", "Harassment", "Health",
|
| 11 |
+
"IT & Equipment", "Leave & Absence", "Mobility", "Offboarding",
|
| 12 |
+
"Onboarding", "Payroll", "Performance Management", "Recruitment",
|
| 13 |
+
"Safety", "Timetracking", "Training", "Work Arrangements",
|
| 14 |
+
]
|
| 15 |
+
|
| 16 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
|
| 17 |
+
model = AutoModelForSequenceClassification.from_pretrained(MODEL_ID)
|
| 18 |
+
model.eval()
|
| 19 |
+
|
| 20 |
+
def classify(text: str, threshold: float = 0.5):
|
| 21 |
+
inputs = tokenizer(text, return_tensors="pt", truncation=True, max_length=512)
|
| 22 |
+
with torch.no_grad():
|
| 23 |
+
logits = model(**inputs).logits
|
| 24 |
+
probs = torch.sigmoid(logits).numpy()[0]
|
| 25 |
+
predicted = [LABELS[i] for i, p in enumerate(probs) if p >= threshold]
|
| 26 |
+
probs_dict = {LABELS[i]: round(float(p), 3) for i, p in enumerate(probs)}
|
| 27 |
+
return predicted, probs_dict
|
| 28 |
+
|
| 29 |
+
if __name__ == "__main__":
|
| 30 |
+
sample = (
|
| 31 |
+
"USER: I haven't received my payslip for March yet. Could you please check what's going on?\n"
|
| 32 |
+
"AGENT: Good morning. I've checked the payroll system and it appears your March payslip "
|
| 33 |
+
"was generated on the 28th but there was a distribution delay. I've resent it to your "
|
| 34 |
+
"registered email. You should receive it within the next hour."
|
| 35 |
+
)
|
| 36 |
+
preds, probs = classify(sample, threshold=0.5)
|
| 37 |
+
print(f"Predicted: {preds}")
|
| 38 |
+
print(f"Top probs: {sorted(probs.items(), key=lambda x: x[1], reverse=True)[:5]}")
|