|
|
|
import torch |
|
from model_loader import model, tokenizer |
|
|
|
def classify_toxic_comment(comment): |
|
""" |
|
Classify a comment as toxic or non-toxic using the fine-tuned XLM-RoBERTa model. |
|
Returns the prediction label, confidence, color, toxicity score, and bias score for UI display. |
|
""" |
|
if not comment.strip(): |
|
return "Error: Please enter a comment.", None, None, None, None |
|
|
|
|
|
inputs = tokenizer(comment, return_tensors="pt", truncation=True, padding=True, max_length=512) |
|
|
|
|
|
with torch.no_grad(): |
|
outputs = model(**inputs) |
|
logits = outputs.logits |
|
|
|
|
|
predicted_class = torch.argmax(logits, dim=1).item() |
|
label = "Toxic" if predicted_class == 1 else "Non-Toxic" |
|
confidence = torch.softmax(logits, dim=1)[0][predicted_class].item() |
|
label_color = "red" if label == "Toxic" else "green" |
|
|
|
|
|
|
|
toxicity_score = torch.softmax(logits, dim=1)[0][1].item() |
|
toxicity_score = round(toxicity_score, 2) |
|
|
|
|
|
|
|
bias_score = 0.01 if label == "Non-Toxic" else 0.15 |
|
bias_score = round(bias_score, 2) |
|
|
|
return f"Prediction: {label}", confidence, label_color, toxicity_score, bias_score |