|
|
""" |
|
|
Simple API test script using requests |
|
|
""" |
|
|
|
|
|
import requests |
|
|
import json |
|
|
|
|
|
|
|
|
API_URL = "https://sematech-sema-api.hf.space" |
|
|
|
|
|
|
|
|
def test_health(): |
|
|
"""Test basic health check""" |
|
|
print("π₯ Testing health check...") |
|
|
|
|
|
response = requests.get(f"{API_URL}/status") |
|
|
print(f"Status: {response.status_code}") |
|
|
|
|
|
if response.status_code == 200: |
|
|
data = response.json() |
|
|
print(f"β
API is healthy") |
|
|
print(f"Version: {data['version']}") |
|
|
print(f"Models loaded: {data['models_loaded']}") |
|
|
else: |
|
|
print(f"β Health check failed") |
|
|
|
|
|
print("-" * 50) |
|
|
|
|
|
def test_translation(): |
|
|
"""Test basic translation""" |
|
|
print("π Testing translation...") |
|
|
|
|
|
|
|
|
data = { |
|
|
"text": "Habari ya asubuhi", |
|
|
"target_language": "eng_Latn" |
|
|
} |
|
|
|
|
|
response = requests.post( |
|
|
f"{API_URL}/translate", |
|
|
headers={"Content-Type": "application/json"}, |
|
|
json=data |
|
|
) |
|
|
|
|
|
print(f"Status: {response.status_code}") |
|
|
|
|
|
if response.status_code == 200: |
|
|
result = response.json() |
|
|
print(f"β
Translation successful") |
|
|
print(f"Original: {data['text']}") |
|
|
print(f"Translation: {result['translated_text']}") |
|
|
print(f"Source language: {result['source_language']}") |
|
|
print(f"Inference time: {result['inference_time']:.3f}s") |
|
|
else: |
|
|
print(f"β Translation failed") |
|
|
print(f"Status code: {response.status_code}") |
|
|
try: |
|
|
error_data = response.json() |
|
|
print(f"Error details: {error_data}") |
|
|
except: |
|
|
print(f"Error text: {response.text}") |
|
|
|
|
|
print("-" * 50) |
|
|
|
|
|
def test_languages(): |
|
|
"""Test language endpoints""" |
|
|
print("π£οΈ Testing language endpoints...") |
|
|
|
|
|
|
|
|
response = requests.get(f"{API_URL}/languages") |
|
|
if response.status_code == 200: |
|
|
data = response.json() |
|
|
print(f"β
Found {data['total_count']} supported languages") |
|
|
else: |
|
|
print(f"β Failed to get languages") |
|
|
|
|
|
|
|
|
response = requests.get(f"{API_URL}/languages/popular") |
|
|
if response.status_code == 200: |
|
|
data = response.json() |
|
|
print(f"β
Found {data['total_count']} popular languages") |
|
|
else: |
|
|
print(f"β Failed to get popular languages") |
|
|
|
|
|
|
|
|
response = requests.get(f"{API_URL}/languages/swh_Latn") |
|
|
if response.status_code == 200: |
|
|
data = response.json() |
|
|
print(f"β
Swahili info: {data['name']} ({data['native_name']})") |
|
|
else: |
|
|
print(f"β Failed to get Swahili info") |
|
|
|
|
|
print("-" * 50) |
|
|
|
|
|
def test_search(): |
|
|
"""Test language search""" |
|
|
print("π Testing language search...") |
|
|
|
|
|
response = requests.get(f"{API_URL}/languages/search?q=Swahili") |
|
|
|
|
|
if response.status_code == 200: |
|
|
data = response.json() |
|
|
print(f"β
Search found {data['total_count']} results") |
|
|
for code, info in data['languages'].items(): |
|
|
print(f" {code}: {info['name']} ({info['native_name']})") |
|
|
else: |
|
|
print(f"β Search failed") |
|
|
|
|
|
print("-" * 50) |
|
|
|
|
|
def run_all_tests(): |
|
|
"""Run all tests""" |
|
|
print(f"π§ͺ Testing API at: {API_URL}") |
|
|
print("=" * 50) |
|
|
|
|
|
test_health() |
|
|
test_translation() |
|
|
test_languages() |
|
|
test_search() |
|
|
|
|
|
print("π All tests completed!") |
|
|
|
|
|
if __name__ == "__main__": |
|
|
run_all_tests() |
|
|
|