| | """ |
| | Test script to verify NumPy compatibility fix |
| | """ |
| |
|
| | import requests |
| | import json |
| | import time |
| |
|
| | def test_translation_after_numpy_fix(api_url="https://sematech-sema-api.hf.space"): |
| | """Test translation functionality after NumPy compatibility fix""" |
| | |
| | print("π§ Testing NumPy Compatibility Fix") |
| | print("=" * 50) |
| | |
| | |
| | test_cases = [ |
| | { |
| | "text": "Habari ya asubuhi", |
| | "target_language": "eng_Latn", |
| | "expected_contains": ["morning", "hello", "good"] |
| | }, |
| | { |
| | "text": "Asante sana", |
| | "target_language": "eng_Latn", |
| | "expected_contains": ["thank", "thanks"] |
| | }, |
| | { |
| | "text": "Hello world", |
| | "source_language": "eng_Latn", |
| | "target_language": "swh_Latn", |
| | "expected_contains": ["habari", "dunia", "halo"] |
| | }, |
| | { |
| | "text": "Good morning", |
| | "source_language": "eng_Latn", |
| | "target_language": "fra_Latn", |
| | "expected_contains": ["bonjour", "matin"] |
| | } |
| | ] |
| | |
| | successful_translations = 0 |
| | total_tests = len(test_cases) |
| | |
| | for i, test_case in enumerate(test_cases, 1): |
| | print(f"\nπ§ͺ Test {i}/{total_tests}: '{test_case['text']}'") |
| | |
| | try: |
| | start_time = time.time() |
| | |
| | response = requests.post( |
| | f"{api_url}/translate", |
| | headers={"Content-Type": "application/json"}, |
| | json=test_case, |
| | timeout=30 |
| | ) |
| | |
| | request_time = time.time() - start_time |
| | |
| | if response.status_code == 200: |
| | result = response.json() |
| | translation = result['translated_text'].lower() |
| | |
| | |
| | contains_expected = any( |
| | expected.lower() in translation |
| | for expected in test_case['expected_contains'] |
| | ) |
| | |
| | if contains_expected or len(translation) > 0: |
| | print(f" β
SUCCESS: '{result['translated_text']}'") |
| | print(f" π Source: {result['source_language']}") |
| | print(f" β±οΈ Time: {request_time:.3f}s (inference: {result['inference_time']:.3f}s)") |
| | successful_translations += 1 |
| | else: |
| | print(f" β οΈ UNEXPECTED: '{result['translated_text']}'") |
| | print(f" Expected to contain: {test_case['expected_contains']}") |
| | |
| | else: |
| | print(f" β FAILED: HTTP {response.status_code}") |
| | try: |
| | error_data = response.json() |
| | print(f" Error: {error_data.get('detail', 'Unknown error')}") |
| | except: |
| | print(f" Error: {response.text}") |
| | |
| | except requests.exceptions.Timeout: |
| | print(f" β° TIMEOUT: Request took longer than 30 seconds") |
| | except Exception as e: |
| | print(f" π₯ EXCEPTION: {e}") |
| | |
| | |
| | print("\n" + "=" * 50) |
| | print(f"π SUMMARY:") |
| | print(f" β
Successful: {successful_translations}/{total_tests}") |
| | print(f" π Success Rate: {(successful_translations/total_tests)*100:.1f}%") |
| | |
| | if successful_translations == total_tests: |
| | print(f" π ALL TESTS PASSED! NumPy fix is working!") |
| | return True |
| | elif successful_translations > 0: |
| | print(f" β οΈ PARTIAL SUCCESS: Some translations working") |
| | return False |
| | else: |
| | print(f" β ALL TESTS FAILED: NumPy issue may persist") |
| | return False |
| |
|
| | def test_health_and_languages(api_url="https://sematech-sema-api.hf.space"): |
| | """Test non-translation endpoints to ensure they still work""" |
| | |
| | print("\nπ₯ Testing Other Endpoints") |
| | print("-" * 30) |
| | |
| | |
| | try: |
| | response = requests.get(f"{api_url}/status", timeout=10) |
| | if response.status_code == 200: |
| | data = response.json() |
| | print(f"β
Health: {data['status']} (models: {data['models_loaded']})") |
| | else: |
| | print(f"β Health check failed: {response.status_code}") |
| | except Exception as e: |
| | print(f"β Health check error: {e}") |
| | |
| | |
| | try: |
| | response = requests.get(f"{api_url}/languages/popular", timeout=10) |
| | if response.status_code == 200: |
| | data = response.json() |
| | print(f"β
Languages: {data['total_count']} popular languages loaded") |
| | else: |
| | print(f"β Languages failed: {response.status_code}") |
| | except Exception as e: |
| | print(f"β Languages error: {e}") |
| |
|
| | if __name__ == "__main__": |
| | import sys |
| | |
| | |
| | api_url = "https://sematech-sema-api.hf.space" |
| | if len(sys.argv) > 1: |
| | api_url = sys.argv[1] |
| | |
| | print(f"π― Testing NumPy Fix at: {api_url}") |
| | |
| | |
| | test_health_and_languages(api_url) |
| | |
| | |
| | success = test_translation_after_numpy_fix(api_url) |
| | |
| | if success: |
| | print("\nπ NumPy compatibility fix is working perfectly!") |
| | sys.exit(0) |
| | else: |
| | print("\nβ NumPy compatibility issues may still exist") |
| | sys.exit(1) |
| |
|