humy65 commited on
Commit
80fe507
·
1 Parent(s): c196fe9

Add debug version to diagnose model loading issues

Browse files
Files changed (1) hide show
  1. app.py +154 -208
app.py CHANGED
@@ -1,232 +1,178 @@
1
  """
2
- Gradio Demo for Hebrew Intent Classification
3
- Deploy this as a Hugging Face Space
4
  """
5
 
6
  import gradio as gr
7
- from transformers import AutoTokenizer, AutoModelForSequenceClassification
8
- import torch
9
-
10
-
11
- class HebrewIntentClassifier:
12
- def __init__(self):
13
- # Use your deployed model
 
 
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):
31
- """Predict intent for Hebrew text"""
32
- if not text.strip():
33
- return "Please enter some Hebrew text", {}, "אנא הכנס טקסט בעברית"
34
-
35
- try:
36
- # Tokenize input
37
- inputs = self.tokenizer(
38
- text,
39
- return_tensors="pt",
40
- padding=True,
41
- truncation=True,
42
- max_length=128
43
- )
44
-
45
- # Get prediction
46
- with torch.no_grad():
47
- outputs = self.model(**inputs)
48
- logits = outputs.logits
49
- probabilities = torch.softmax(logits, dim=-1)
50
-
51
- # Get all predictions
52
- all_scores = {}
53
- for i, prob in enumerate(probabilities[0]):
54
- intent_name = self.model.config.id2label[i]
55
- all_scores[intent_name] = float(prob)
56
-
57
- # Get top prediction
58
- predicted_id = torch.argmax(logits, dim=-1).item()
59
- predicted_label = self.model.config.id2label[predicted_id]
60
- confidence = probabilities[0][predicted_id].item()
61
-
62
- # Format results in Hebrew and English
63
- intent_translations = {
64
- "ביטול מנוי": "Cancel Subscription",
65
- "שאלה כללית": "General Question",
66
- "שכחת סיסמה": "Password Reset",
67
- "תמיכה טכנית": "Technical Support"
68
- }
69
-
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
- """
77
-
78
- # Sort by confidence and show all
79
- sorted_scores = sorted(
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
96
- try:
97
- classifier = HebrewIntentClassifier()
98
- except Exception as e:
99
- print(f"Failed to initialize classifier: {e}")
100
- classifier = None
101
-
102
 
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
-
113
-
114
- def load_example(example_text):
115
- """Load example text into the input"""
116
- return example_text
117
-
118
-
119
- # Create the Gradio interface
120
- with gr.Blocks(
121
- title="🇮🇱 Hebrew Intent Classification",
122
- theme=gr.themes.Soft(),
123
- css=".rtl { direction: rtl; text-align: right; }"
124
- ) as demo:
125
-
126
- gr.Markdown("""
127
- # 🇮🇱 מסווג כוונות עברית / Hebrew Intent Classification
128
-
129
- ## מה זה עושה? / What does this do?
130
 
131
- מסווג טקסט עברית לפי כוונת הלקוח לאחת מ-4 קטגוריות:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
132
 
133
- **Classifies Hebrew customer service text into 4 categories:**
134
 
135
- - 🔐 **שכחת סיסמה** (Password Reset) - בעיות התחברות וסיסמאות
136
- - ❌ **ביטול מנוי** (Cancel Subscription) - בקשות לביטול שירות
137
- - ❓ **שאלה כללית** (General Question) - שאלות על מחירים, שירותים, חשבון
138
- - 🔧 **תמיכה טכנית** (Technical Support) - בעיות טכניות, תקלות, באגים
139
- """)
140
-
141
- with gr.Row():
142
- with gr.Column(scale=1):
143
- gr.Markdown("### 📝 הכנס טקסט עברית / Enter Hebrew Text")
144
-
145
- text_input = gr.Textbox(
146
- label="טקסט / Text:",
147
- placeholder="לדוגמה: שכחתי את הסיסמה שלי",
148
- lines=4,
149
- elem_classes=["rtl"],
150
- info="הכנס טקסט בעברית הקשור לשירות לקוחות"
151
- )
152
-
153
- classify_btn = gr.Button(
154
- "🔍 סווג כוונה / Classify Intent",
155
- variant="primary",
156
- size="lg"
157
- )
158
-
159
- gr.Markdown("### 📋 דוגמאות לניסוי / Try These Examples:")
160
-
161
- examples = [
162
- ("שכחתי את הסיסמה שלי", "🔐 שכחת סיסמה"),
163
- ("רוצה לבטל את המנוי", "❌ ביטול מנוי"),
164
- ("כמה עולה החבילה השנתית", "❓ שאלה כללית"),
165
- ("האתר לא עובד לי", "🔧 תמיכה טכנית"),
166
- ("איך אני משנה את האימייל", "❓ שאלה כללית"),
167
- ("יש לי בעיה טכנית באפליקציה", "🔧 תמיכה טכנית"),
168
- ("איך מבטלים את החשבון", "❌ ביטול מנוי"),
169
- ("לא מצליח להיכנס למערכת", "🔐 שכחת סיסמה")
170
- ]
171
-
172
- for text, category in examples:
173
- gr.Button(
174
- f"{category}: {text}",
175
- size="sm"
176
- ).click(
177
- lambda x=text: x,
178
- outputs=text_input
179
  )
180
-
181
- with gr.Column(scale=1):
182
- gr.Markdown("### 📊 תוצאות / Results")
183
-
184
- result_output = gr.Markdown(
185
- value="התוצאות יופיעו כאן / Results will appear here",
186
- elem_classes=["rtl"]
187
- )
188
-
189
- confidence_plot = gr.Label(
190
- label="ציוני ביטחון / Confidence Scores",
191
- num_top_classes=4
192
- )
193
-
194
- predicted_intent = gr.Textbox(
195
- label="כוונה חזויה / Predicted Intent",
196
- interactive=False
197
- )
198
-
199
- # Connect the classification function
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
200
  classify_btn.click(
201
  classify_text,
202
  inputs=[text_input],
203
- outputs=[result_output, confidence_plot, predicted_intent]
204
  )
205
-
206
- # Allow Enter key to trigger classification
207
  text_input.submit(
208
  classify_text,
209
  inputs=[text_input],
210
- outputs=[result_output, confidence_plot, predicted_intent]
211
  )
212
 
213
- gr.Markdown("""
214
- ---
215
- ### 📈 מידע על המודל / Model Information
216
-
217
- - **בסיס / Base Model:** BERT Multilingual
218
- - **נתוני אימון / Training Data:** 135 דוגמאות עברית מתחום שירות לקוחות
219
- - **ביצועים / Performance:** דיוק גבוה >90% על טקסט עברית
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
- """)
228
-
229
- # Launch the demo
230
  if __name__ == "__main__":
231
  demo.launch(
232
  share=True,
 
1
  """
2
+ Hebrew Intent Classification Demo - Debug Version
 
3
  """
4
 
5
  import gradio as gr
6
+ import sys
7
+ import traceback
8
+
9
+ def test_model_loading():
10
+ """Test if model can be loaded"""
11
+ try:
12
+ print("🔄 Testing model loading...")
13
+ from transformers import AutoTokenizer, AutoModelForSequenceClassification
14
+
15
  model_name = "humy65/hebrew-intent-classifier"
16
+ print(f"📡 Attempting to load: {model_name}")
17
+
18
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
19
+ print("✅ Tokenizer loaded")
20
+
21
+ model = AutoModelForSequenceClassification.from_pretrained(model_name)
22
+ print("✅ Model loaded")
23
+
24
+ print(f"📋 Labels: {model.config.id2label}")
25
+ return True, "Model loaded successfully!", model, tokenizer
26
+
27
+ except Exception as e:
28
+ error_msg = f"❌ Error: {str(e)}\n\nTraceback:\n{traceback.format_exc()}"
29
+ print(error_msg)
30
+ return False, error_msg, None, None
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31
 
32
  def classify_text(text):
33
+ """Classification function with lazy loading"""
 
 
 
34
  if not text or not text.strip():
35
+ return "⚠️ Please enter Hebrew text", {}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36
 
37
+ try:
38
+ # Try to load model on demand
39
+ success, message, model, tokenizer = test_model_loading()
40
+
41
+ if not success:
42
+ return f"Model Loading Failed:\n{message}", {}
43
+
44
+ # Perform classification
45
+ import torch
46
+
47
+ inputs = tokenizer(text, return_tensors="pt", padding=True, truncation=True, max_length=128)
48
+
49
+ with torch.no_grad():
50
+ outputs = model(**inputs)
51
+ logits = outputs.logits
52
+ probabilities = torch.softmax(logits, dim=-1)
53
+
54
+ # Get results
55
+ predicted_id = torch.argmax(logits, dim=-1).item()
56
+ predicted_label = model.config.id2label[predicted_id]
57
+ confidence = probabilities[0][predicted_id].item()
58
+
59
+ # Create confidence scores for all labels
60
+ all_scores = {}
61
+ for i, prob in enumerate(probabilities[0]):
62
+ intent_name = model.config.id2label[i]
63
+ all_scores[intent_name] = float(prob)
64
+
65
+ result = f"""
66
+ 🎯 Predicted Intent: {predicted_label}
67
+ 🎲 Confidence: {confidence:.1%}
68
+
69
+ 📊 All Predictions:
70
+ """
71
+
72
+ # Sort and display
73
+ sorted_scores = sorted(all_scores.items(), key=lambda x: x[1], reverse=True)
74
+ for intent, score in sorted_scores:
75
+ bar = "█" * max(1, int(score * 20))
76
+ result += f"\n{intent}: {score:.1%} {bar}"
77
+
78
+ return result, all_scores
79
+
80
+ except Exception as e:
81
+ error_msg = f"Classification Error: {str(e)}\n\nTraceback:\n{traceback.format_exc()}"
82
+ print(error_msg)
83
+ return error_msg, {}
84
+
85
+ def test_connection():
86
+ """Test Hugging Face connection"""
87
+ try:
88
+ from huggingface_hub import HfApi
89
+ api = HfApi()
90
+ info = api.model_info("humy65/hebrew-intent-classifier")
91
+ return f"✅ Model repository accessible\nModel ID: {info.modelId}\nLast Modified: {info.lastModified}"
92
+ except Exception as e:
93
+ return f"❌ Repository access failed: {str(e)}"
94
+
95
+ # Create interface
96
+ with gr.Blocks(title="Hebrew Intent Classification - Debug") as demo:
97
 
98
+ gr.Markdown("# 🇮🇱 Hebrew Intent Classification - Debug Version")
99
 
100
+ with gr.Tab("Classification"):
101
+ with gr.Row():
102
+ with gr.Column():
103
+ text_input = gr.Textbox(
104
+ label="Hebrew Text:",
105
+ placeholder="שכחתי את הסיסמה שלי",
106
+ lines=3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
107
  )
108
+ classify_btn = gr.Button("Classify", variant="primary")
109
+
110
+ # Quick examples
111
+ gr.Markdown("### Examples:")
112
+ examples = [
113
+ "שכחתי את הסיסמה שלי",
114
+ "רוצה לבטל את המנוי",
115
+ "כמה עולה החבילה",
116
+ "האתר לא עובד"
117
+ ]
118
+
119
+ for example in examples:
120
+ gr.Button(example, size="sm").click(
121
+ lambda x=example: x, outputs=text_input
122
+ )
123
+
124
+ with gr.Column():
125
+ result_output = gr.Textbox(
126
+ label="Result:",
127
+ lines=12,
128
+ interactive=False
129
+ )
130
+
131
+ confidence_output = gr.Label(
132
+ label="Confidence Scores",
133
+ num_top_classes=4
134
+ )
135
+
136
+ with gr.Tab("Debug"):
137
+ gr.Markdown("### Debug Information")
138
+
139
+ test_btn = gr.Button("Test Model Loading")
140
+ debug_output = gr.Textbox(
141
+ label="Debug Output:",
142
+ lines=15,
143
+ interactive=False
144
+ )
145
+
146
+ test_btn.click(
147
+ lambda: test_model_loading()[1],
148
+ outputs=debug_output
149
+ )
150
+
151
+ conn_btn = gr.Button("Test Repository Connection")
152
+ conn_output = gr.Textbox(
153
+ label="Connection Test:",
154
+ lines=5,
155
+ interactive=False
156
+ )
157
+
158
+ conn_btn.click(
159
+ test_connection,
160
+ outputs=conn_output
161
+ )
162
+
163
+ # Connect classification
164
  classify_btn.click(
165
  classify_text,
166
  inputs=[text_input],
167
+ outputs=[result_output, confidence_output]
168
  )
169
+
 
170
  text_input.submit(
171
  classify_text,
172
  inputs=[text_input],
173
+ outputs=[result_output, confidence_output]
174
  )
175
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
176
  if __name__ == "__main__":
177
  demo.launch(
178
  share=True,