File size: 1,936 Bytes
fb62a11
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
"""
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)