humy65 commited on
Commit
c196fe9
·
1 Parent(s): 8d5cec2

Fix classification errors - improve error handling and debugging

Browse files
Files changed (1) hide show
  1. app.py +17 -8
app.py CHANGED
@@ -14,14 +14,17 @@ class HebrewIntentClassifier:
14
  model_name = "humy65/hebrew-intent-classifier"
15
 
16
  try:
17
- print("Loading Hebrew Intent Classification model...")
18
  self.tokenizer = AutoTokenizer.from_pretrained(model_name)
19
  self.model = AutoModelForSequenceClassification.from_pretrained(
20
  model_name)
21
  self.model.eval()
22
  print("Model loaded successfully!")
 
23
  except Exception as e:
24
  print(f"Error loading model: {e}")
 
 
25
  raise e
26
 
27
  def predict(self, text):
@@ -59,7 +62,7 @@ class HebrewIntentClassifier:
59
  # Format results in Hebrew and English
60
  intent_translations = {
61
  "ביטול מנוי": "Cancel Subscription",
62
- "שאלה כללית": "General Question",
63
  "שכחת סיסמה": "Password Reset",
64
  "תמיכה טכנית": "Technical Support"
65
  }
@@ -67,7 +70,7 @@ class HebrewIntentClassifier:
67
  result_text = f"""
68
  🎯 **כוונה חזויה / Predicted Intent:** {predicted_label}
69
  🎲 **רמת ביטחון / Confidence:** {confidence:.1%}
70
- 🔤 **תרגום / Translation:** {intent_translations.get(predicted_label, "Unknown")}
71
 
72
  📊 **כל התחזיות / All Predictions:**
73
  """
@@ -77,13 +80,16 @@ class HebrewIntentClassifier:
77
  all_scores.items(), key=lambda x: x[1], reverse=True)
78
  for intent, score in sorted_scores:
79
  bar = "█" * int(score * 20)
80
- translation = intent_translations.get(intent, "Unknown")
81
  result_text += f"\\n{intent} ({translation}): {score:.1%} {bar}"
82
 
83
  return result_text, all_scores, predicted_label
84
 
85
  except Exception as e:
86
- return f"Error: {str(e)}", {}, "שגיאה"
 
 
 
87
 
88
 
89
  # Initialize the classifier
@@ -97,7 +103,10 @@ except Exception as e:
97
  def classify_text(text):
98
  """Main classification function"""
99
  if classifier is None:
100
- return "Model not loaded properly", {}, "Model Error"
 
 
 
101
 
102
  return classifier.predict(text)
103
 
@@ -211,8 +220,8 @@ with gr.Blocks(
211
  - **קוד מקור / Source:** [GitHub Repository](https://github.com/your-repo)
212
 
213
  ### 🔗 קישורים / Links
214
- - [Model on Hugging Face](https://huggingface.co/Huggingm1r@n/hebrew-intent-classifier)
215
- - [Documentation](https://huggingface.co/Huggingm1r@n/hebrew-intent-classifier/blob/main/README.md)
216
 
217
  Built with ❤️ using Hugging Face Transformers and Gradio
218
  """)
 
14
  model_name = "humy65/hebrew-intent-classifier"
15
 
16
  try:
17
+ print(f"Loading Hebrew Intent Classification model: {model_name}")
18
  self.tokenizer = AutoTokenizer.from_pretrained(model_name)
19
  self.model = AutoModelForSequenceClassification.from_pretrained(
20
  model_name)
21
  self.model.eval()
22
  print("Model loaded successfully!")
23
+ print(f"Model labels: {self.model.config.id2label}")
24
  except Exception as e:
25
  print(f"Error loading model: {e}")
26
+ import traceback
27
+ traceback.print_exc()
28
  raise e
29
 
30
  def predict(self, text):
 
62
  # Format results in Hebrew and English
63
  intent_translations = {
64
  "ביטול מנוי": "Cancel Subscription",
65
+ "שאלה כללית": "General Question",
66
  "שכחת סיסמה": "Password Reset",
67
  "תמיכה טכנית": "Technical Support"
68
  }
 
70
  result_text = f"""
71
  🎯 **כוונה חזויה / Predicted Intent:** {predicted_label}
72
  🎲 **רמת ביטחון / Confidence:** {confidence:.1%}
73
+ 🔤 **תרגום / Translation:** {intent_translations.get(predicted_label, predicted_label)}
74
 
75
  📊 **כל התחזיות / All Predictions:**
76
  """
 
80
  all_scores.items(), key=lambda x: x[1], reverse=True)
81
  for intent, score in sorted_scores:
82
  bar = "█" * int(score * 20)
83
+ translation = intent_translations.get(intent, intent)
84
  result_text += f"\\n{intent} ({translation}): {score:.1%} {bar}"
85
 
86
  return result_text, all_scores, predicted_label
87
 
88
  except Exception as e:
89
+ print(f"Prediction error: {e}")
90
+ import traceback
91
+ traceback.print_exc()
92
+ return f"Classification Error: {str(e)}", {}, f"Error: {str(e)}"
93
 
94
 
95
  # Initialize the classifier
 
103
  def classify_text(text):
104
  """Main classification function"""
105
  if classifier is None:
106
+ return "Model not loaded properly - please check logs", {}, "Model Error"
107
+
108
+ if not text or not text.strip():
109
+ return "⚠️ Please enter some Hebrew text to classify", {}, "Empty Input"
110
 
111
  return classifier.predict(text)
112
 
 
220
  - **קוד מקור / Source:** [GitHub Repository](https://github.com/your-repo)
221
 
222
  ### 🔗 קישורים / Links
223
+ - [Model on Hugging Face](https://huggingface.co/humy65/hebrew-intent-classifier)
224
+ - [Documentation](https://huggingface.co/humy65/hebrew-intent-classifier/blob/main/README.md)
225
 
226
  Built with ❤️ using Hugging Face Transformers and Gradio
227
  """)