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)