""" ml_classifier.py — DistilBERT toxic-comment classifier (Stage 1). Runs on CPU only — no GPU quota consumed. Inference takes ~7 ms per comment, so it is available to every visitor (including guests). """ from typing import Tuple from transformers import pipeline as hf_pipeline from config import ( CLASSIFIER_MODEL_NAME, CONFIDENCE_HIGH, CONFIDENCE_LOW, DECISION_BLOCK, DECISION_ALLOW, ) class MLClassifier: """Wrapper around a HuggingFace text-classification pipeline.""" def __init__(self, model_name: str = CLASSIFIER_MODEL_NAME): self._pipe = hf_pipeline( task="text-classification", model=model_name, tokenizer=model_name, framework="pt", device=-1, # ← force CPU (no GPU quota) truncation=True, max_length=512, ) self._toxic_labels = {"toxic"} print(f"✅ ML Classifier loaded on CPU: {model_name}") def classify(self, text: str) -> Tuple[str, float, bool]: """ Returns (decision, confidence, is_uncertain). decision : DECISION_BLOCK or DECISION_ALLOW confidence : float in [0, 1] is_uncertain : True when confidence is in the grey zone """ result = self._pipe(text)[0] return self._interpret(result) def _interpret(self, result: dict) -> Tuple[str, float, bool]: label = result["label"].lower() score = result["score"] toxic_prob = score if label in self._toxic_labels else (1.0 - score) if toxic_prob >= CONFIDENCE_HIGH: return (DECISION_BLOCK, toxic_prob, False) elif toxic_prob <= CONFIDENCE_LOW: return (DECISION_ALLOW, 1.0 - toxic_prob, False) else: tentative = DECISION_BLOCK if toxic_prob >= 0.5 else DECISION_ALLOW return (tentative, max(toxic_prob, 1.0 - toxic_prob), True)