File size: 9,101 Bytes
077be11
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
"""

XGBoost Busy Detector - Hugging Face Inference Endpoint Handler

Custom handler for HF Inference Endpoints.



Loads XGBoost model, applies normalization, runs evidence accumulation scoring,

and returns busy_score + confidence + recommendation.



Derived from: src/normalization.py, src/scoring_engine.py, src/model.py

"""

from typing import Dict, Any, Tuple
import json
import math
import numpy as np
import pickle
from pathlib import Path


class EndpointHandler:
    """HF Inference Endpoint handler for XGBoost busy detection."""

    def __init__(self, path: str = "."):
        model_dir = Path(path)

        # --- Load XGBoost model ---
        model_path = None
        for candidate in [
            model_dir / "model.pkl",
            model_dir / "busy_detector_v1.pkl",
            model_dir / "busy_detector_5k.pkl",
        ]:
            if candidate.exists():
                model_path = candidate
                break

        if model_path is None:
            raise FileNotFoundError(
                f"No model file found in {model_dir}. "
                "Expected model.pkl, busy_detector_v1.pkl, or busy_detector_5k.pkl"
            )

        with open(model_path, "rb") as f:
            saved = pickle.load(f)

        # Handle both raw model and dict-wrapped model
        if isinstance(saved, dict):
            self.model = saved.get("model") or saved.get("booster")
            self.feature_names = saved.get("feature_names")
        else:
            self.model = saved
            self.feature_names = None

        print(f"✓ XGBoost model loaded from {model_path}")

        # --- Load feature ranges ---
        ranges_path = model_dir / "feature_ranges.json"
        with open(ranges_path) as f:
            ranges_data = json.load(f)

        self.voice_ranges = ranges_data["voice_ranges"]
        self.text_ranges = ranges_data["text_ranges"]
        self.voice_order = ranges_data["voice_feature_order"]
        self.text_order = ranges_data["text_feature_order"]

        # --- Load scoring rules ---
        rules_path = model_dir / "scoring_rules.json"
        with open(rules_path) as f:
            self.scoring = json.load(f)

        self.weights = self.scoring["weights"]
        self.thresholds = self.scoring["thresholds"]
        print("✓ Feature ranges and scoring rules loaded")

    # --------------------------------------------------------------------- #
    # Public interface
    # --------------------------------------------------------------------- #

    def __call__(self, data: Dict[str, Any]) -> Dict[str, Any]:
        """

        Entrypoint for HF Inference Endpoints.



        Expected input (JSON):

        {

            "inputs": {

                "audio_features": { "v1_snr": 15.0, ... },

                "text_features":  { "t1_explicit_busy": 0.8, ... }

            }

        }



        Returns:

        {

            "busy_score": 0.72,

            "confidence": 0.85,

            "recommendation": "EXIT",

            "ml_probability": 0.65,

            "evidence_details": [...]

        }

        """
        # HF wraps payload in "inputs"
        inputs = data.get("inputs", data)
        audio_features = inputs.get("audio_features", {})
        text_features = inputs.get("text_features", {})

        # 1. Normalize
        normalized = self._normalize_features(audio_features, text_features)

        # 2. XGBoost inference
        import xgboost as xgb

        dmatrix = xgb.DMatrix(normalized.reshape(1, -1))
        ml_prob = float(self.model.predict(dmatrix)[0])

        # 3. Evidence accumulation scoring
        final_score, confidence, details = self._score_with_evidence(
            ml_prob, audio_features, text_features
        )

        # 4. Recommendation
        recommendation = self._get_recommendation(final_score)

        return {
            "busy_score": round(final_score, 4),
            "confidence": round(confidence, 4),
            "recommendation": recommendation,
            "ml_probability": round(ml_prob, 4),
            "evidence_details": details,
        }

    # --------------------------------------------------------------------- #
    # Normalization (mirrors src/normalization.py FeatureNormalizer)
    # --------------------------------------------------------------------- #

    def _normalize_value(self, value: float, min_val: float, max_val: float) -> float:
        if max_val == min_val:
            return 0.0
        value = max(min_val, min(max_val, value))
        return (value - min_val) / (max_val - min_val)

    def _normalize_features(

        self,

        audio_features: Dict[str, float],

        text_features: Dict[str, float],

    ) -> np.ndarray:
        """Min-max normalize all 26 features and concatenate."""
        voice_norm = []
        for feat in self.voice_order:
            val = audio_features.get(feat, 0.0)
            lo, hi = self.voice_ranges[feat]
            voice_norm.append(self._normalize_value(val, lo, hi))

        text_norm = []
        for feat in self.text_order:
            val = text_features.get(feat, 0.0)
            lo, hi = self.text_ranges[feat]
            text_norm.append(self._normalize_value(val, lo, hi))

        return np.array(voice_norm + text_norm, dtype=np.float32)

    # --------------------------------------------------------------------- #
    # Evidence scoring (mirrors src/scoring_engine.py ScoringEngine)
    # --------------------------------------------------------------------- #

    @staticmethod
    def _sigmoid(x: float) -> float:
        return 1.0 / (1.0 + math.exp(-x))

    @staticmethod
    def _logit(p: float) -> float:
        p = max(0.01, min(0.99, p))
        return math.log(p / (1.0 - p))

    def _score_with_evidence(

        self,

        ml_prob: float,

        audio_features: Dict[str, float],

        text_features: Dict[str, float],

    ) -> Tuple[float, float, list]:
        """Evidence accumulation scoring exactly matching ScoringEngine.calculate_score."""
        evidence = 0.0
        details = []

        # --- Text evidence ---
        explicit = text_features.get("t1_explicit_busy", 0.0)
        if explicit > 0.5:
            pts = self.weights["explicit_busy"] * explicit
            evidence += pts
            details.append(f"Explicit Intent (+{pts:.1f})")

        explicit_free = text_features.get("t0_explicit_free", 0.0)
        if explicit_free > 0.5:
            pts = self.weights["explicit_free"] * explicit_free
            evidence += pts
            details.append(f"Explicit Free ({pts:.1f})")

        short_ratio = text_features.get("t3_short_ratio", 0.0)
        if short_ratio > 0.3:
            pts = self.weights["short_answers"] * short_ratio
            evidence += pts
            details.append(f"Brief Responses (+{pts:.1f})")

        deflection = text_features.get("t6_deflection", 0.0)
        if deflection > 0.1:
            pts = self.weights["deflection"] * deflection
            evidence += pts
            details.append(f"Deflection (+{pts:.1f})")

        # --- Audio evidence ---
        traffic = audio_features.get("v2_noise_traffic", 0.0)
        if traffic > 0.5:
            pts = self.weights["traffic_noise"] * traffic
            evidence += pts
            details.append(f"Traffic Context (+{pts:.1f})")

        rate = audio_features.get("v3_speech_rate", 0.0)
        if rate > 3.5:
            pts = self.weights["rushed_speech"]
            evidence += pts
            details.append(f"Rushed Speech (+{pts:.1f})")

        pitch_std = audio_features.get("v5_pitch_std", 0.0)
        if pitch_std > 80.0:
            evidence += 0.5
            details.append("Voice Stress (+0.5)")

        emotion_stress = audio_features.get("v11_emotion_stress", 0.0)
        if emotion_stress > 0.6:
            pts = self.weights["emotion_stress"] * emotion_stress
            evidence += pts
            details.append(f"Emotional Stress (+{pts:.1f})")

        emotion_energy = audio_features.get("v12_emotion_energy", 0.0)
        if emotion_energy > 0.7:
            pts = self.weights["emotion_energy"] * emotion_energy
            evidence += pts
            details.append(f"High Energy (+{pts:.1f})")

        # --- ML baseline ---
        ml_evidence = self._logit(ml_prob) * self.weights["ml_model_factor"]
        evidence += ml_evidence
        details.append(f"ML Baseline ({ml_evidence:+.1f})")

        # --- Final ---
        final_score = self._sigmoid(evidence)
        confidence = float(math.tanh(abs(evidence) / 2.0))

        return final_score, confidence, details

    def _get_recommendation(self, score: float) -> str:
        if score < self.thresholds["continue"]:
            return "CONTINUE"
        elif score < self.thresholds["check_in"]:
            return "CHECK_IN"
        else:
            return "EXIT"