File size: 1,114 Bytes
00bd0c6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import joblib
import numpy as np
import os
from sentence_transformers import SentenceTransformer

# Load trained model once at module import
CURRENT_DIR = os.path.dirname(os.path.abspath(__file__))  
BACKEND_DIR = os.path.abspath(os.path.join(CURRENT_DIR, ".."))
MODEL_DIR = os.path.join(BACKEND_DIR, "models", "intent_classification")

classifier = joblib.load(os.path.join(MODEL_DIR, "intent_model2.pkl"))
id_to_label = joblib.load(os.path.join(MODEL_DIR, "id_to_label2.pkl"))

# Load model directly from HuggingFace
embedder = SentenceTransformer("all-MiniLM-L6-v2")

CONFIDENCE_THRESHOLD = 0.55


def classify_intent(text):
    embedding = embedder.encode([text])
    probs = classifier.predict_proba(embedding)[0]

    pred_idx = np.argmax(probs)
    confidence = float(probs[pred_idx])
    label = id_to_label[pred_idx]

    if confidence < CONFIDENCE_THRESHOLD:
        return {
            "status": "UNCERTAIN",
            "label": label,
            "confidence": confidence 
        }
    
    return {
        "status": "READY",
            "label": label,
            "confidence": confidence
    }