sema-api / tests /simple_test.py
kamau1's picture
fix(api): resolve SlowAPI request param conflict, update schema_extra naming, enhance tests, and serve Swagger UI at root
be6b137
raw
history blame
3.5 kB
"""
Simple API test script using requests
"""
import requests
import json
# API base URL - change this to test different environments
API_URL = "https://sematech-sema-api.hf.space"
# API_URL = "http://localhost:8000" # For local testing
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...")
# Test data
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...")
# Test all languages
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")
# Test popular 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")
# Test specific language
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()