| |
| """ |
| 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}") |
| |
| 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_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() |
| |
| |
| 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: |
| |
| models = test_model_initialization() |
| |
| |
| single_results = test_single_detection() |
| |
| |
| test_category_samples() |
| |
| |
| df = test_dataframe_creation() |
| |
| |
| 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() |