File size: 4,401 Bytes
72f90b1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#!/usr/bin/env python3
"""
Test script for the demo functionality
"""

from demo_page import initialize_models, detect_with_all_models, create_results_dataframe, run_demo_tests, DEMO_SAMPLES

def test_model_initialization():
    """Test that all models can be initialized."""
    print("πŸ”„ Testing model initialization...")
    models = initialize_models()
    
    print(f"βœ… Initialized {len(models)} models:")
    for model_key, model_info in models.items():
        status_icon = "βœ…" if model_info["status"] == "Ready" else "❌"
        print(f"  {status_icon} {model_info['name']}: {model_info['status']}")
    
    return models

def test_single_detection():
    """Test detection with a single text across all models."""
    print("\nπŸ”„ Testing single text detection...")
    
    models = initialize_models()
    test_text = "Hello, how are you today?"
    
    results = detect_with_all_models(test_text, models)
    
    print(f"Text: '{test_text}'")
    print("Results:")
    for model_key, result in results.items():
        print(f"  {model_key}: {result['language_code']} ({result['confidence']:.3f}) - {result['status']}")
    
    return results

def test_category_samples():
    """Test a few samples from each category."""
    print("\nπŸ”„ Testing category samples...")
    
    models = initialize_models()
    
    for category, samples in DEMO_SAMPLES.items():
        print(f"\nπŸ“Š Category: {category}")
        # Test first sample from each category
        text, expected, description = samples[0]
        results = detect_with_all_models(text, models)
        
        print(f"  Text: '{text}' (Expected: {expected})")
        print(f"  Description: {description}")
        for model_key, result in results.items():
            match_icon = "βœ…" if result['language_code'] == expected or expected in ['ambiguous', 'mix', 'transliteration'] else "❌"
            print(f"    {model_key}: {result['language_code']} ({result['confidence']:.3f}) {match_icon}")

def test_dataframe_creation():
    """Test DataFrame creation with sample data."""
    print("\nπŸ”„ Testing DataFrame creation...")
    
    models = initialize_models()
    
    # Test with a few samples
    test_texts = [
        "Hello world",
        "Bonjour le monde", 
        "Hola mundo"
    ]
    expected_langs = ["en", "fr", "es"]
    categories = ["Custom", "Custom", "Custom"]
    
    all_results = []
    for text in test_texts:
        results = detect_with_all_models(text, models)
        all_results.append(results)
    
    df = create_results_dataframe(test_texts, all_results, expected_langs, categories)
    
    print("DataFrame shape:", df.shape)
    print("Columns:", list(df.columns))
    print("\nFirst few rows:")
    print(df.head())
    
    return df

def test_demo_workflow():
    """Test the complete demo workflow."""
    print("\nπŸ”„ Testing complete demo workflow...")
    
    models = initialize_models()
    
    # Test with selected categories and custom text
    selected_categories = ["Easy/Obvious", "Short Text"]
    custom_texts = "Hello world\nBonjour\nδ½ ε₯½"
    
    summary, df = run_demo_tests(selected_categories, custom_texts, models)
    
    print(f"Summary: {summary}")
    if df is not None:
        print(f"Results DataFrame shape: {df.shape}")
        print("Sample results:")
        print(df.head())
    else:
        print("❌ No DataFrame returned")
    
    return summary, df

def main():
    """Run all tests."""
    print("πŸš€ Starting demo functionality tests...\n")
    
    try:
        # Test 1: Model initialization
        models = test_model_initialization()
        
        # Test 2: Single detection
        single_results = test_single_detection()
        
        # Test 3: Category samples
        test_category_samples()
        
        # Test 4: DataFrame creation
        df = test_dataframe_creation()
        
        # Test 5: Complete workflow
        summary, demo_df = test_demo_workflow()
        
        print("\nβœ… All tests completed successfully!")
        print(f"πŸ“Š Total categories available: {len(DEMO_SAMPLES)}")
        print(f"πŸ“ Total sample texts: {sum(len(samples) for samples in DEMO_SAMPLES.values())}")
        
    except Exception as e:
        print(f"\n❌ Test failed with error: {e}")
        import traceback
        traceback.print_exc()

if __name__ == "__main__":
    main()