File size: 3,501 Bytes
be6b137
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
"""
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()