File size: 5,089 Bytes
40a2927
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
231dc34
 
 
 
40a2927
231dc34
 
 
 
 
 
 
40a2927
231dc34
40a2927
 
 
231dc34
 
40a2927
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
231dc34
40a2927
 
231dc34
 
40a2927
231dc34
 
 
 
 
 
 
 
 
 
 
40a2927
231dc34
40a2927
 
 
231dc34
 
40a2927
 
231dc34
 
 
40a2927
231dc34
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40a2927
 
231dc34
 
40a2927
 
 
 
 
 
 
 
231dc34
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40a2927
 
 
 
 
 
 
 
231dc34
 
40a2927
 
 
 
231dc34
40a2927
 
231dc34
40a2927
 
 
 
 
 
 
 
 
 
 
 
 
 
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#!/usr/bin/env python3
"""Test script for Router API endpoints."""

import requests
import json
import time
import sys

BASE_URL = "https://Alovestocode-router-router-zero.hf.space"

def test_healthcheck():
    """Test the health check endpoint."""
    print("Testing GET /health...")
    try:
        response = requests.get(f"{BASE_URL}/health", timeout=10)
        print(f"Status: {response.status_code}")
        print(f"Headers: {dict(response.headers)}")
        print(f"Content-Type: {response.headers.get('content-type', 'unknown')}")
        print(f"Response length: {len(response.text)} bytes")
        
        if response.status_code == 200:
            try:
                data = response.json()
                print(f"Response: {json.dumps(data, indent=2)}")
                return True
            except json.JSONDecodeError:
                print(f"Not JSON. First 200 chars: {response.text[:200]}")
                return False
        else:
            print(f"Error: {response.text[:200]}")
            return False
    except Exception as e:
        print(f"Exception: {e}")
        import traceback
        traceback.print_exc()
        return False

def test_generate():
    """Test the generate endpoint."""
    print("\nTesting POST /v1/generate...")
    try:
        payload = {
            "prompt": "You are a router agent. User query: What is 2+2?",
            "max_new_tokens": 100,
            "temperature": 0.2,
            "top_p": 0.9
        }
        response = requests.post(
            f"{BASE_URL}/v1/generate",
            json=payload,
            headers={"Content-Type": "application/json"},
            timeout=120  # Longer timeout for model loading
        )
        print(f"Status: {response.status_code}")
        print(f"Headers: {dict(response.headers)}")
        
        if response.status_code == 200:
            try:
                result = response.json()
                print(f"Response keys: {list(result.keys())}")
                if "text" in result:
                    print(f"Generated text (first 300 chars): {result['text'][:300]}...")
                else:
                    print(f"Full response: {json.dumps(result, indent=2)}")
                return True
            except json.JSONDecodeError:
                print(f"Not JSON. First 200 chars: {response.text[:200]}")
                return False
        else:
            print(f"Error: {response.text[:500]}")
            return False
    except Exception as e:
        print(f"Exception: {e}")
        import traceback
        traceback.print_exc()
        return False

def test_gradio_api():
    """Test Gradio's built-in API endpoint."""
    print("\nTesting Gradio API /api/predict...")
    try:
        # Gradio creates /api/predict endpoints automatically
        # We need to find the function index - usually 0 for the first function
        payload = {
            "data": [
                "You are a router agent. User query: What is 2+2?",
                100,
                0.2,
                0.9
            ],
            "fn_index": 0
        }
        response = requests.post(
            f"{BASE_URL}/api/predict",
            json=payload,
            headers={"Content-Type": "application/json"},
            timeout=120
        )
        print(f"Status: {response.status_code}")
        if response.status_code == 200:
            result = response.json()
            print(f"Gradio API Response: {json.dumps(result, indent=2)[:500]}...")
            return True
        else:
            print(f"Error: {response.text[:200]}")
            return False
    except Exception as e:
        print(f"Exception: {e}")
        return False

def test_root():
    """Test the root endpoint."""
    print("\nTesting GET / (root)...")
    try:
        response = requests.get(f"{BASE_URL}/", timeout=10)
        print(f"Status: {response.status_code}")
        print(f"Content-Type: {response.headers.get('content-type', 'unknown')}")
        print(f"Response length: {len(response.text)} chars")
        if response.status_code == 200:
            return True
        return False
    except Exception as e:
        print(f"Exception: {e}")
        return False

def main():
    """Run all API tests."""
    print("=" * 60)
    print("Router API Test Suite")
    print("=" * 60)
    print(f"Base URL: {BASE_URL}\n")
    
    # Wait a moment for Space to be ready
    print("Waiting 3 seconds for Space to be ready...")
    time.sleep(3)
    
    results = []
    
    # Test endpoints
    results.append(("Root", test_root()))
    results.append(("Health Check", test_healthcheck()))
    results.append(("Generate", test_generate()))
    results.append(("Gradio API", test_gradio_api()))
    
    # Summary
    print("\n" + "=" * 60)
    print("Test Summary")
    print("=" * 60)
    for name, passed in results:
        status = "✅ PASS" if passed else "❌ FAIL"
        print(f"{name}: {status}")
    
    all_passed = all(result[1] for result in results)
    sys.exit(0 if all_passed else 1)

if __name__ == "__main__":
    main()