unexperiencedone commited on
Commit
2f23cc4
·
verified ·
1 Parent(s): 36ac218

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -11
app.py CHANGED
@@ -32,12 +32,12 @@ class EmotionBackend:
32
 
33
  def predict_proba_wrapper(self, text_list):
34
  inputs = self.tokenizer(
35
- text_list,
36
- return_tensors="pt",
37
- padding=True,
38
- truncation=True,
39
- max_length=self.max_length
40
- )
41
 
42
  input_ids=inputs["input_ids"].to(self.device)
43
  attention_mask = inputs["attention_mask"].to(self.device)
@@ -84,29 +84,41 @@ class EmotionBackend:
84
  num_classes = len(class_names)
85
 
86
  explainer = LimeTextExplainer(class_names = class_names)
 
 
 
87
  exp = explainer.explain_instance(
88
  text_to_explain,
89
  self.predict_proba_wrapper,
90
  num_features=10,
91
- top_labels = 1
92
  )
93
 
94
  probs = self.predict_proba_wrapper([text_to_explain])
95
  top_class_index = np.argmax(probs[0])
96
  top_class_name = class_names[top_class_index]
97
  confidence = float(probs[0][top_class_index])
 
 
98
  explanation_list = exp.as_list(label=top_class_index)
99
 
 
 
 
 
100
  return ({
101
  "text": text_to_explain,
102
- "emotion": top_class_name,
 
103
  "confidence": confidence,
104
  "explanation": explanation_list,
 
105
  })
106
 
107
 
108
- # Initialize the backend with the model
109
- MODEL_PATH = os.path.join(os.path.dirname(__file__), "emotion_classifier_scripted.pt")
 
110
  TOKENIZER_NAME = "distilbert-base-uncased"
111
  backend = EmotionBackend(MODEL_PATH, TOKENIZER_NAME, device_str="cpu")
112
 
@@ -122,10 +134,13 @@ def predict_emotion():
122
  return jsonify({"error": "No text provided"}), 400
123
 
124
  try:
 
125
  result = backend.explanation_lime(text)
126
  return jsonify(result)
127
  except Exception as e:
128
- return jsonify({"error": str(e)}), 500
 
 
129
 
130
  if __name__ == '__main__':
131
  app.run(host='0.0.0.0', port=5000, debug=True)
 
32
 
33
  def predict_proba_wrapper(self, text_list):
34
  inputs = self.tokenizer(
35
+ text_list,
36
+ return_tensors="pt",
37
+ padding=True,
38
+ truncation=True,
39
+ max_length=self.max_length
40
+ )
41
 
42
  input_ids=inputs["input_ids"].to(self.device)
43
  attention_mask = inputs["attention_mask"].to(self.device)
 
84
  num_classes = len(class_names)
85
 
86
  explainer = LimeTextExplainer(class_names = class_names)
87
+
88
+ # --- CHANGE 1: Explain all classes, not just the top one ---
89
+ # This prevents potential KeyErrors
90
  exp = explainer.explain_instance(
91
  text_to_explain,
92
  self.predict_proba_wrapper,
93
  num_features=10,
94
+ labels=range(num_classes) # Use 'labels' instead of 'top_labels'
95
  )
96
 
97
  probs = self.predict_proba_wrapper([text_to_explain])
98
  top_class_index = np.argmax(probs[0])
99
  top_class_name = class_names[top_class_index]
100
  confidence = float(probs[0][top_class_index])
101
+
102
+ # This is safe to call now
103
  explanation_list = exp.as_list(label=top_class_index)
104
 
105
+ # --- CHANGE 2: Get the fidelity score from the 'exp' object ---
106
+ fidelity_score = exp.score(label=top_class_index)
107
+
108
+ # --- CHANGE 3: Update the returned JSON ---
109
  return ({
110
  "text": text_to_explain,
111
+ # Use 'predicted_class' to match the React app
112
+ "predicted_class": top_class_name,
113
  "confidence": confidence,
114
  "explanation": explanation_list,
115
+ "fidelity_score": fidelity_score # Add the score here
116
  })
117
 
118
 
119
+ # --- CHANGE 4: Use a relative path, not __file__ ---
120
+ # Assumes the model is in the same directory as this script
121
+ MODEL_PATH = "emotion_classifier_scripted.pt"
122
  TOKENIZER_NAME = "distilbert-base-uncased"
123
  backend = EmotionBackend(MODEL_PATH, TOKENIZER_NAME, device_str="cpu")
124
 
 
134
  return jsonify({"error": "No text provided"}), 400
135
 
136
  try:
137
+ # This one function gives us everything we need
138
  result = backend.explanation_lime(text)
139
  return jsonify(result)
140
  except Exception as e:
141
+ # Provide a more detailed error log on the server
142
+ print(f"An error occurred: {e}")
143
+ return jsonify({"error": "An internal server error occurred"}), 500
144
 
145
  if __name__ == '__main__':
146
  app.run(host='0.0.0.0', port=5000, debug=True)