File size: 4,343 Bytes
e2911a9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import torch
import numpy as np
import torch.nn.functional as F
from transformers import AutoTokenizer
from lime.lime_text import LimeTextExplainer
from flask import Flask, request, jsonify
from flask_cors import CORS
import os

app = Flask(__name__)
CORS(app)  # Enable CORS for all routes

# --- 1. Define Your Prediction Function ---
# Note: No 'DistilBertLstmClassifier' class definition is needed!

class EmotionBackend:
    def __init__(self, model_path, tokenizer_name, device_str="cpu"):
        print("Loading backend model...")
        self.device = torch.device(device_str)
        
        # Load the self-contained TorchScript model
        self.model = torch.jit.load(model_path, map_location=self.device)
        self.model.eval() # Set to evaluation mode
        
        print("Loading tokenizer...")
        self.tokenizer = AutoTokenizer.from_pretrained(tokenizer_name)
        self.max_length = 128 # The same max_length used in training
        
        # The same label mapping from your training
        self.id2label = ['sadness', 'joy', 'love', 'anger', 'fear', 'surprise']
        print("Backend is ready.")

    def predict_proba_wrapper(self, text_list):
        inputs = self.tokenizer(
        text_list,
        return_tensors="pt",
        padding=True,
        truncation=True,
        max_length=self.max_length
    )

        input_ids=inputs["input_ids"].to(self.device)
        attention_mask = inputs["attention_mask"].to(self.device)

        with torch.no_grad():
            logits = self.model(input_ids,  attention_mask=attention_mask)

        probabilities = F.softmax(logits, dim=1).cpu().numpy()

        return probabilities

    def predict(self, text):
        # 1. Tokenize the input text
        inputs = self.tokenizer(
            text, 
            return_tensors="pt",  # 'pt' for PyTorch
            padding='max_length', 
            truncation=True, 
            max_length=self.max_length
        )
        
        # 2. Move inputs to the correct device
        input_ids = inputs['input_ids'].to(self.device)
        attention_mask = inputs['attention_mask'].to(self.device)
        
        # 3. Predict (no gradients needed)
        with torch.no_grad():
            # Pass inputs to the traced model
            logits = self.model(input_ids, attention_mask)
        
        # 4. Decode the prediction
        probs = F.softmax(logits, dim=1)
        predicted_class_id = torch.argmax(probs, dim=1).item()
        emotion = self.id2label[predicted_class_id]
        
        return {
            "text": text,
            "emotion": emotion,
            "confidence": probs[0][predicted_class_id].item()
        }
    
    def explanation_lime(self, text_to_explain):
        class_names = self.id2label
        num_classes = len(class_names)
        
        explainer = LimeTextExplainer(class_names = class_names)
        exp = explainer.explain_instance(
            text_to_explain,
            self.predict_proba_wrapper, 
            num_features=10,
            top_labels = 1
        )

        probs = self.predict_proba_wrapper([text_to_explain])
        top_class_index = np.argmax(probs[0])
        top_class_name = class_names[top_class_index]
        confidence = float(probs[0][top_class_index])
        explanation_list = exp.as_list(label=top_class_index)

        return ({
            "text": text_to_explain,
            "emotion": top_class_name,
            "confidence": confidence,
            "explanation": explanation_list, 
        })
        

# Initialize the backend with the model
MODEL_PATH = os.path.join(os.path.dirname(__file__), "emotion_classifier_scripted.pt")
TOKENIZER_NAME = "distilbert-base-uncased"
backend = EmotionBackend(MODEL_PATH, TOKENIZER_NAME, device_str="cpu")

@app.route('/predict', methods=['POST'])
def predict_emotion():
    if not request.is_json:
        return jsonify({"error": "Content-Type must be application/json"}), 400
    
    data = request.get_json()
    text = data.get('text')
    
    if not text:
        return jsonify({"error": "No text provided"}), 400
    
    try:
        result = backend.explanation_lime(text)
        return jsonify(result)
    except Exception as e:
        return jsonify({"error": str(e)}), 500

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5000, debug=True)