|
""" |
|
Test client for the Sema Translation API |
|
""" |
|
|
|
import requests |
|
import json |
|
import time |
|
|
|
def test_api_endpoint(base_url="http://localhost:8000"): |
|
"""Test the translation API endpoints""" |
|
|
|
print("π§ͺ Testing Sema Translation API\n") |
|
|
|
|
|
print("1οΈβ£ Testing health check endpoint...") |
|
try: |
|
response = requests.get(f"{base_url}/") |
|
if response.status_code == 200: |
|
data = response.json() |
|
print(f"β
Health check passed: {data}") |
|
else: |
|
print(f"β Health check failed: {response.status_code}") |
|
return False |
|
except Exception as e: |
|
print(f"β Health check error: {e}") |
|
return False |
|
|
|
|
|
print("\n2οΈβ£ Testing translation with auto-detection...") |
|
test_data = { |
|
"text": "Habari ya asubuhi, ulimwengu", |
|
"target_language": "eng_Latn" |
|
} |
|
|
|
try: |
|
response = requests.post( |
|
f"{base_url}/translate", |
|
headers={"Content-Type": "application/json"}, |
|
data=json.dumps(test_data) |
|
) |
|
|
|
if response.status_code == 200: |
|
data = response.json() |
|
print(f"β
Auto-detection translation successful:") |
|
print(f" π Original: {test_data['text']}") |
|
print(f" π Detected source: {data['source_language']}") |
|
print(f" π― Target: {data['target_language']}") |
|
print(f" β¨ Translation: {data['translated_text']}") |
|
print(f" β±οΈ Inference time: {data['inference_time']:.3f}s") |
|
else: |
|
print(f"β Auto-detection translation failed: {response.status_code}") |
|
print(f" Error: {response.text}") |
|
return False |
|
except Exception as e: |
|
print(f"β Auto-detection translation error: {e}") |
|
return False |
|
|
|
|
|
print("\n3οΈβ£ Testing translation with specified source language...") |
|
test_data_with_source = { |
|
"text": "WΔ© mwega?", |
|
"source_language": "kik_Latn", |
|
"target_language": "eng_Latn" |
|
} |
|
|
|
try: |
|
response = requests.post( |
|
f"{base_url}/translate", |
|
headers={"Content-Type": "application/json"}, |
|
data=json.dumps(test_data_with_source) |
|
) |
|
|
|
if response.status_code == 200: |
|
data = response.json() |
|
print(f"β
Specified source translation successful:") |
|
print(f" π Original: {test_data_with_source['text']}") |
|
print(f" π Source: {data['source_language']}") |
|
print(f" π― Target: {data['target_language']}") |
|
print(f" β¨ Translation: {data['translated_text']}") |
|
print(f" β±οΈ Inference time: {data['inference_time']:.3f}s") |
|
else: |
|
print(f"β Specified source translation failed: {response.status_code}") |
|
print(f" Error: {response.text}") |
|
return False |
|
except Exception as e: |
|
print(f"β Specified source translation error: {e}") |
|
return False |
|
|
|
|
|
print("\n4οΈβ£ Testing error handling (empty text)...") |
|
test_data_empty = { |
|
"text": "", |
|
"target_language": "eng_Latn" |
|
} |
|
|
|
try: |
|
response = requests.post( |
|
f"{base_url}/translate", |
|
headers={"Content-Type": "application/json"}, |
|
data=json.dumps(test_data_empty) |
|
) |
|
|
|
if response.status_code == 400: |
|
print("β
Empty text error handling works correctly") |
|
else: |
|
print(f"β Empty text error handling failed: {response.status_code}") |
|
return False |
|
except Exception as e: |
|
print(f"β Empty text error handling error: {e}") |
|
return False |
|
|
|
|
|
print("\n5οΈβ£ Testing multiple translations for performance...") |
|
test_texts = [ |
|
{"text": "Jambo", "target_language": "eng_Latn"}, |
|
{"text": "Asante sana", "target_language": "eng_Latn"}, |
|
{"text": "Karibu", "target_language": "eng_Latn"}, |
|
{"text": "Pole sana", "target_language": "eng_Latn"}, |
|
{"text": "Tutaonana", "target_language": "eng_Latn"} |
|
] |
|
|
|
total_time = 0 |
|
successful_translations = 0 |
|
|
|
for i, test_data in enumerate(test_texts, 1): |
|
try: |
|
start_time = time.time() |
|
response = requests.post( |
|
f"{base_url}/translate", |
|
headers={"Content-Type": "application/json"}, |
|
data=json.dumps(test_data) |
|
) |
|
end_time = time.time() |
|
|
|
if response.status_code == 200: |
|
data = response.json() |
|
request_time = end_time - start_time |
|
total_time += request_time |
|
successful_translations += 1 |
|
|
|
print(f" {i}. '{test_data['text']}' β '{data['translated_text']}' " |
|
f"({request_time:.3f}s)") |
|
else: |
|
print(f" {i}. Failed: {response.status_code}") |
|
except Exception as e: |
|
print(f" {i}. Error: {e}") |
|
|
|
if successful_translations > 0: |
|
avg_time = total_time / successful_translations |
|
print(f"\nπ Performance Summary:") |
|
print(f" β
Successful translations: {successful_translations}/{len(test_texts)}") |
|
print(f" β±οΈ Average request time: {avg_time:.3f}s") |
|
print(f" π Total time: {total_time:.3f}s") |
|
|
|
return True |
|
|
|
def test_api_documentation(base_url="http://localhost:8000"): |
|
"""Test API documentation endpoints""" |
|
|
|
print("\nπ Testing API documentation...") |
|
|
|
|
|
try: |
|
response = requests.get(f"{base_url}/docs") |
|
if response.status_code == 200: |
|
print("β
OpenAPI docs accessible at /docs") |
|
else: |
|
print(f"β OpenAPI docs failed: {response.status_code}") |
|
except Exception as e: |
|
print(f"β OpenAPI docs error: {e}") |
|
|
|
|
|
try: |
|
response = requests.get(f"{base_url}/openapi.json") |
|
if response.status_code == 200: |
|
print("β
OpenAPI JSON accessible at /openapi.json") |
|
else: |
|
print(f"β OpenAPI JSON failed: {response.status_code}") |
|
except Exception as e: |
|
print(f"β OpenAPI JSON error: {e}") |
|
|
|
if __name__ == "__main__": |
|
import sys |
|
|
|
|
|
base_url = "http://localhost:8000" |
|
if len(sys.argv) > 1: |
|
base_url = sys.argv[1] |
|
|
|
print(f"π― Testing API at: {base_url}") |
|
print("β οΈ Make sure the API server is running before running this test!\n") |
|
|
|
|
|
success = test_api_endpoint(base_url) |
|
test_api_documentation(base_url) |
|
|
|
if success: |
|
print("\nπ All API tests passed!") |
|
else: |
|
print("\nβ Some API tests failed!") |
|
sys.exit(1) |
|
|