""" Hebrew Intent Classification Demo - Debug Version """ import gradio as gr import sys import traceback def test_model_loading(): """Test if model can be loaded""" try: print("šŸ”„ Testing model loading...") from transformers import AutoTokenizer, AutoModelForSequenceClassification model_name = "humy65/hebrew-intent-classifier" print(f"šŸ“” Attempting to load: {model_name}") tokenizer = AutoTokenizer.from_pretrained(model_name) print("āœ… Tokenizer loaded") model = AutoModelForSequenceClassification.from_pretrained(model_name) print("āœ… Model loaded") print(f"šŸ“‹ Labels: {model.config.id2label}") return True, "Model loaded successfully!", model, tokenizer except Exception as e: error_msg = f"āŒ Error: {str(e)}\n\nTraceback:\n{traceback.format_exc()}" print(error_msg) return False, error_msg, None, None def classify_text(text): """Classification function with lazy loading""" if not text or not text.strip(): return "āš ļø Please enter Hebrew text", {} try: # Try to load model on demand success, message, model, tokenizer = test_model_loading() if not success: return f"Model Loading Failed:\n{message}", {} # Perform classification import torch inputs = tokenizer(text, return_tensors="pt", padding=True, truncation=True, max_length=128) with torch.no_grad(): outputs = model(**inputs) logits = outputs.logits probabilities = torch.softmax(logits, dim=-1) # Get results predicted_id = torch.argmax(logits, dim=-1).item() predicted_label = model.config.id2label[predicted_id] confidence = probabilities[0][predicted_id].item() # Create confidence scores for all labels all_scores = {} for i, prob in enumerate(probabilities[0]): intent_name = model.config.id2label[i] all_scores[intent_name] = float(prob) result = f""" šŸŽÆ Predicted Intent: {predicted_label} šŸŽ² Confidence: {confidence:.1%} šŸ“Š All Predictions: """ # Sort and display sorted_scores = sorted( all_scores.items(), key=lambda x: x[1], reverse=True) for intent, score in sorted_scores: bar = "ā–ˆ" * max(1, int(score * 20)) result += f"\n{intent}: {score:.1%} {bar}" return result, all_scores except Exception as e: error_msg = f"Classification Error: {str(e)}\n\nTraceback:\n{traceback.format_exc()}" print(error_msg) return error_msg, {} def test_connection(): """Test Hugging Face connection""" try: from huggingface_hub import HfApi api = HfApi() info = api.model_info("humy65/hebrew-intent-classifier") return f"āœ… Model repository accessible\nModel ID: {info.modelId}\nLast Modified: {info.lastModified}" except Exception as e: return f"āŒ Repository access failed: {str(e)}" def get_training_data(): """Display the training data used for the model""" training_data = [ ("שכחתי את ההיהמה שלי", "שכחת היהמה"), ("איך אני מבטל את המנוי?", "ביטול מנוי"), ("מה ×”×ž×—×™×Ø של התוכנית?", "שאלה ×›×œ×œ×™×Ŗ"), ("האתר לא עובד לי", "×Ŗ×ž×™×›×” ×˜×›× ×™×Ŗ"), ("אני לא מצליח ×œ×”×Ŗ×—×‘×Ø", "×Ŗ×ž×™×›×” ×˜×›× ×™×Ŗ"), ("איך אני משנה את כתובת האימייל?", "שאלה ×›×œ×œ×™×Ŗ"), ("אני רוצה ×œ×©×“×Ø×’ את התוכנית שלי", "שאלה ×›×œ×œ×™×Ŗ"), ("החשבון שלי ננעל", "×Ŗ×ž×™×›×” ×˜×›× ×™×Ŗ"), ("אני לא מקבל מיילים", "×Ŗ×ž×™×›×” ×˜×›× ×™×Ŗ"), ("איך אני רואה את החשבונית שלי?", "שאלה ×›×œ×œ×™×Ŗ"), ("אני רוצה לבטל את השירות", "ביטול מנוי"), ("שכחתי את ×¤×Ø×˜×™ ההתחברות", "שכחת היהמה"), ("איבדתי את ההיהמה", "שכחת היהמה"), ("לא זוכר את ההיהמה", "שכחת היהמה"), ("ההיהמה לא עובדת", "שכחת היהמה"), ("לא מצליח להיכנה עם ההיהמה", "שכחת היהמה"), ("×¦×Ø×™×š לאפה את ההיהמה", "שכחת היהמה"), ("בעיה עם ההיהמה", "שכחת היהמה"), ("ההיהמה שלי לא נכונה", "שכחת היהמה"), ("שכחתי מה ההיהמה", "שכחת היהמה"), ("איך אני ×ž×©×—×–×Ø את ההיהמה", "שכחת היהמה"), ("רוצה ×œ×©× ×•×Ŗ את ההיהמה", "שכחת היהמה"), ("ההיהמה לא ×ž×Ŗ×§×‘×œ×Ŗ", "שכחת היהמה"), ("בעיית התחברות - היהמה", "שכחת היהמה"), ("×¦×Ø×™×š עזרה עם ההיהמה", "שכחת היהמה"), ("לא יודע מה ההיהמה שלי", "שכחת היהמה"), ("רוצה לבטל את השירות", "ביטול מנוי"), ("איך מפהיקים את המנוי", "ביטול מנוי"), ("רוצה להפהיק את ×”×Ŗ×©×œ×•×", "ביטול מנוי"), ("איך יוצאים מהמנוי", "ביטול מנוי"), ("בקשה לביטול מנוי", "ביטול מנוי"), ("לא רוצה יותר את השירות", "ביטול מנוי"), ("איך מבטלים את החשבון", "ביטול מנוי"), ("רוצה ×œ×”×’×•×Ø את החשבון", "ביטול מנוי"), ("עזרה בביטול מנוי", "ביטול מנוי"), ("הליך ביטול המנוי", "ביטול מנוי"), ("מעוניין לבטל", "ביטול מנוי"), ("איך מפהיקים את השירות", "ביטול מנוי"), ("רוצה להפהיק את ×”×”×Ø×©×ž×”", "ביטול מנוי"), ("בקשה ×œ×”×¤×”×§×Ŗ שירות", "ביטול מנוי"), ("מה כולל השירות", "שאלה ×›×œ×œ×™×Ŗ"), ("אילו תוכניות יש לכם", "שאלה ×›×œ×œ×™×Ŗ"), ("כמה עולה החבילה", "שאלה ×›×œ×œ×™×Ŗ"), ("מה ההבדל בין התוכניות", "שאלה ×›×œ×œ×™×Ŗ"), ("איך אני משנה את ×”×¤×Ø×˜×™× שלי", "שאלה ×›×œ×œ×™×Ŗ"), ("איך אפשר ×œ×©×“×Ø×’", "שאלה ×›×œ×œ×™×Ŗ"), ("מה האפשרויות שלכם", "שאלה ×›×œ×œ×™×Ŗ"), ("אני רוצה לעדכן ×¤×Ø×˜×™×", "שאלה ×›×œ×œ×™×Ŗ"), ("איך ×Ø×•××™× את ×”×”×™×”×˜×•×Ø×™×”", "שאלה ×›×œ×œ×™×Ŗ"), ("האפליקציה קורהת", "×Ŗ×ž×™×›×” ×˜×›× ×™×Ŗ"), ("יש באג באתר", "×Ŗ×ž×™×›×” ×˜×›× ×™×Ŗ"), ("הדף לא נטען", "×Ŗ×ž×™×›×” ×˜×›× ×™×Ŗ"), ("שגיאה ×‘×ž×¢×Ø×›×Ŗ", "×Ŗ×ž×™×›×” ×˜×›× ×™×Ŗ"), ("הטוען לא עובד", "×Ŗ×ž×™×›×” ×˜×›× ×™×Ŗ"), ("בעיה ×˜×›× ×™×Ŗ", "×Ŗ×ž×™×›×” ×˜×›× ×™×Ŗ"), ("×”×ž×¢×Ø×›×Ŗ לא מגיבה", "×Ŗ×ž×™×›×” ×˜×›× ×™×Ŗ"), ("שגיאת חיבור", "×Ŗ×ž×™×›×” ×˜×›× ×™×Ŗ"), ("הכפתור לא עובד", "×Ŗ×ž×™×›×” ×˜×›× ×™×Ŗ"), ("×”×Ŗ×ž×•× ×•×Ŗ לא × ×˜×¢× ×•×Ŗ", "×Ŗ×ž×™×›×” ×˜×›× ×™×Ŗ"), ("הווידאו לא ×ž×Ŗ× ×’×Ÿ", "×Ŗ×ž×™×›×” ×˜×›× ×™×Ŗ"), ("××™×˜×™×•×Ŗ באתר", "×Ŗ×ž×™×›×” ×˜×›× ×™×Ŗ") ] # Count examples per category category_counts = {} for _, label in training_data: category_counts[label] = category_counts.get(label, 0) + 1 result = f""" šŸ“Š **Training Data Summary** Total Examples: {len(training_data)} šŸ“ˆ **Examples per Category:** """ # Add category statistics for category, count in sorted(category_counts.items()): percentage = (count / len(training_data)) * 100 result += f"\n• {category}: {count} examples ({percentage:.1f}%)" result += f""" šŸ“ **Sample Training Examples:** šŸ” **שכחת היהמה (Password Reset):** • שכחתי את ההיהמה שלי • לא זוכר את ההיהמה • ההיהמה לא עובדת • ×¦×Ø×™×š לאפה את ההיהמה • איך אני ×ž×©×—×–×Ø את ההיהמה āŒ **ביטול מנוי (Cancel Subscription):** • איך אני מבטל את המנוי? • רוצה להפהיק את ×”×Ŗ×©×œ×•× • לא רוצה יותר את השירות • איך מבטלים את החשבון • בקשה לביטול מנוי ā“ **שאלה ×›×œ×œ×™×Ŗ (General Question):** • מה ×”×ž×—×™×Ø של התוכנית? • כמה עולה החבילה • אילו תוכניות יש לכם • איך אני משנה את ×”×¤×Ø×˜×™× שלי • מה כולל השירות šŸ”§ **×Ŗ×ž×™×›×” ×˜×›× ×™×Ŗ (Technical Support):** • האתר לא עובד לי • האפליקציה קורהת • יש באג באתר • הדף לא נטען • שגיאה ×‘×ž×¢×Ø×›×Ŗ --- šŸ’” **Model was trained with data augmentation techniques:** • Synonym replacement • Paraphrasing • Context variation • Original 12 examples → Enhanced to {len(training_data)} examples """ return result def get_training_data(): """Display the training data used for the model""" training_data = [ ("שכחתי את ההיהמה שלי", "שכחת היהמה"), ("איך אני מבטל את המנוי?", "ביטול מנוי"), ("מה ×”×ž×—×™×Ø של התוכנית?", "שאלה ×›×œ×œ×™×Ŗ"), ("האתר לא עובד לי", "×Ŗ×ž×™×›×” ×˜×›× ×™×Ŗ"), ("אני לא מצליח ×œ×”×Ŗ×—×‘×Ø", "×Ŗ×ž×™×›×” ×˜×›× ×™×Ŗ"), ("איך אני משנה את כתובת האימייל?", "שאלה ×›×œ×œ×™×Ŗ"), ("אני רוצה ×œ×©×“×Ø×’ את התוכנית שלי", "שאלה ×›×œ×œ×™×Ŗ"), ("החשבון שלי ננעל", "×Ŗ×ž×™×›×” ×˜×›× ×™×Ŗ"), ("אני לא מקבל מיילים", "×Ŗ×ž×™×›×” ×˜×›× ×™×Ŗ"), ("איך אני רואה את החשבונית שלי?", "שאלה ×›×œ×œ×™×Ŗ"), ("אני רוצה לבטל את השירות", "ביטול מנוי"), ("שכחתי את ×¤×Ø×˜×™ ההתחברות", "שכחת היהמה"), ("איבדתי את ההיהמה", "שכחת היהמה"), ("לא זוכר את ההיהמה", "שכחת היהמה"), ("ההיהמה לא עובדת", "שכחת היהמה"), ("לא מצליח להיכנה עם ההיהמה", "שכחת היהמה"), ("×¦×Ø×™×š לאפה את ההיהמה", "שכחת היהמה"), ("בעיה עם ההיהמה", "שכחת היהמה"), ("ההיהמה שלי לא נכונה", "שכחת היהמה"), ("שכחתי מה ההיהמה", "שכחת היהמה"), ("איך אני ×ž×©×—×–×Ø את ההיהמה", "שכחת היהמה"), ("רוצה ×œ×©× ×•×Ŗ את ההיהמה", "שכחת היהמה"), ("ההיהמה לא ×ž×Ŗ×§×‘×œ×Ŗ", "שכחת היהמה"), ("בעיית התחברות - היהמה", "שכחת היהמה"), ("×¦×Ø×™×š עזרה עם ההיהמה", "שכחת היהמה"), ("לא יודע מה ההיהמה שלי", "שכחת היהמה"), ("רוצה לבטל את השירות", "ביטול מנוי"), ("איך מפהיקים את המנוי", "ביטול מנוי"), ("רוצה להפהיק את ×”×Ŗ×©×œ×•×", "ביטול מנוי"), ("איך יוצאים מהמנוי", "ביטול מנוי"), ("בקשה לביטול מנוי", "ביטול מנוי"), ("לא רוצה יותר את השירות", "ביטול מנוי"), ("איך מבטלים את החשבון", "ביטול מנוי"), ("רוצה ×œ×”×’×•×Ø את החשבון", "ביטול מנוי"), ("עזרה בביטול מנוי", "ביטול מנוי"), ("הליך ביטול המנוי", "ביטול מנוי"), ("מעוניין לבטל", "ביטול מנוי"), ("איך מפהיקים את השירות", "ביטול מנוי"), ("רוצה להפהיק את ×”×”×Ø×©×ž×”", "ביטול מנוי"), ("בקשה ×œ×”×¤×”×§×Ŗ שירות", "ביטול מנוי"), ("מה כולל השירות", "שאלה ×›×œ×œ×™×Ŗ"), ("אילו תוכניות יש לכם", "שאלה ×›×œ×œ×™×Ŗ"), ("כמה עולה החבילה", "שאלה ×›×œ×œ×™×Ŗ"), ("מה ההבדל בין התוכניות", "שאלה ×›×œ×œ×™×Ŗ"), ("איך אני משנה את ×”×¤×Ø×˜×™× שלי", "שאלה ×›×œ×œ×™×Ŗ"), ("איך אפשר ×œ×©×“×Ø×’", "שאלה ×›×œ×œ×™×Ŗ"), ("מה האפשרויות שלכם", "שאלה ×›×œ×œ×™×Ŗ"), ("אני רוצה לעדכן ×¤×Ø×˜×™×", "שאלה ×›×œ×œ×™×Ŗ"), ("איך ×Ø×•××™× את ×”×”×™×”×˜×•×Ø×™×”", "שאלה ×›×œ×œ×™×Ŗ"), ("האפליקציה קורהת", "×Ŗ×ž×™×›×” ×˜×›× ×™×Ŗ"), ("יש באג באתר", "×Ŗ×ž×™×›×” ×˜×›× ×™×Ŗ"), ("הדף לא נטען", "×Ŗ×ž×™×›×” ×˜×›× ×™×Ŗ"), ("שגיאה ×‘×ž×¢×Ø×›×Ŗ", "×Ŗ×ž×™×›×” ×˜×›× ×™×Ŗ"), ("הטוען לא עובד", "×Ŗ×ž×™×›×” ×˜×›× ×™×Ŗ"), ("בעיה ×˜×›× ×™×Ŗ", "×Ŗ×ž×™×›×” ×˜×›× ×™×Ŗ"), ("×”×ž×¢×Ø×›×Ŗ לא מגיבה", "×Ŗ×ž×™×›×” ×˜×›× ×™×Ŗ"), ("שגיאת חיבור", "×Ŗ×ž×™×›×” ×˜×›× ×™×Ŗ"), ("הכפתור לא עובד", "×Ŗ×ž×™×›×” ×˜×›× ×™×Ŗ"), ("×”×Ŗ×ž×•× ×•×Ŗ לא × ×˜×¢× ×•×Ŗ", "×Ŗ×ž×™×›×” ×˜×›× ×™×Ŗ"), ("הווידאו לא ×ž×Ŗ× ×’×Ÿ", "×Ŗ×ž×™×›×” ×˜×›× ×™×Ŗ"), ("××™×˜×™×•×Ŗ באתר", "×Ŗ×ž×™×›×” ×˜×›× ×™×Ŗ") ] # Count examples per category category_counts = {} for _, label in training_data: category_counts[label] = category_counts.get(label, 0) + 1 result = f""" šŸ“Š **Training Data Summary** Total Examples: {len(training_data)} šŸ“ˆ **Examples per Category:** """ # Add category statistics for category, count in sorted(category_counts.items()): percentage = (count / len(training_data)) * 100 result += f"\n• {category}: {count} examples ({percentage:.1f}%)" result += f""" šŸ“ **Sample Training Examples:** šŸ” **שכחת היהמה (Password Reset):** • שכחתי את ההיהמה שלי • לא זוכר את ההיהמה • ההיהמה לא עובדת • ×¦×Ø×™×š לאפה את ההיהמה • איך אני ×ž×©×—×–×Ø את ההיהמה āŒ **ביטול מנוי (Cancel Subscription):** • איך אני מבטל את המנוי? • רוצה להפהיק את ×”×Ŗ×©×œ×•× • לא רוצה יותר את השירות • איך מבטלים את החשבון • בקשה לביטול מנוי ā“ **שאלה ×›×œ×œ×™×Ŗ (General Question):** • מה ×”×ž×—×™×Ø של התוכנית? • כמה עולה החבילה • אילו תוכניות יש לכם • איך אני משנה את ×”×¤×Ø×˜×™× שלי • מה כולל השירות šŸ”§ **×Ŗ×ž×™×›×” ×˜×›× ×™×Ŗ (Technical Support):** • האתר לא עובד לי • האפליקציה קורהת • יש באג באתר • הדף לא נטען • שגיאה ×‘×ž×¢×Ø×›×Ŗ --- šŸ’” **Model was trained with data augmentation techniques:** • Synonym replacement • Paraphrasing • Context variation • Original 12 examples → Enhanced to {len(training_data)} examples """ return result # Create interface with gr.Blocks(title="Hebrew Intent Classification - Debug") as demo: gr.Markdown("# šŸ‡®šŸ‡± Hebrew Intent Classification - Debug Version 2.1 šŸ“Š") gr.Markdown("### šŸ”¢ Version 2.1 - Training Data Display Added (Aug 12, 2025) āœ…") with gr.Tab("Classification"): with gr.Row(): with gr.Column(): text_input = gr.Textbox( label="Hebrew Text:", placeholder="שכחתי את ההיהמה שלי", lines=3 ) classify_btn = gr.Button("Classify", variant="primary") # Quick examples gr.Markdown("### Examples:") examples = [ "שכחתי את ההיהמה שלי", "רוצה לבטל את המנוי", "כמה עולה החבילה", "האתר לא עובד" ] for example in examples: gr.Button(example, size="sm").click( lambda x=example: x, outputs=text_input ) with gr.Column(): result_output = gr.Textbox( label="Result:", lines=12, interactive=False ) confidence_output = gr.Label( label="Confidence Scores", num_top_classes=4 ) with gr.Tab("Debug"): gr.Markdown("### Debug Information") with gr.Row(): with gr.Column(): test_btn = gr.Button("Test Model Loading") debug_output = gr.Textbox( label="Debug Output:", lines=15, interactive=False ) test_btn.click( lambda: test_model_loading()[1], outputs=debug_output ) conn_btn = gr.Button("Test Repository Connection") conn_output = gr.Textbox( label="Connection Test:", lines=5, interactive=False ) conn_btn.click( test_connection, outputs=conn_output ) with gr.Column(): data_btn = gr.Button("Show Training Data") training_output = gr.Textbox( label="Training Data:", lines=20, interactive=False ) data_btn.click( get_training_data, outputs=training_output ) # Connect classification classify_btn.click( classify_text, inputs=[text_input], outputs=[result_output, confidence_output] ) text_input.submit( classify_text, inputs=[text_input], outputs=[result_output, confidence_output] ) if __name__ == "__main__": demo.launch( share=True, server_name="0.0.0.0", server_port=7860 ) # Connect classification classify_btn.click( classify_text, inputs=[text_input], outputs=[result_output, confidence_output] ) text_input.submit( classify_text, inputs=[text_input], outputs=[result_output, confidence_output] ) if __name__ == "__main__": demo.launch( share=True, server_name="0.0.0.0", server_port=7860 )