File size: 5,122 Bytes
eb4d305
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""
Comprehensive API endpoint testing
"""

import requests
import json

def test_all_possible_endpoints():
    """Test all possible API endpoint combinations"""
    print("πŸ” Testing all possible API endpoints...")
    
    base_url = "https://hanszhu-dense-captioning-platform.hf.space"
    test_url = "https://raw.githubusercontent.com/gradio-app/gradio/main/test/test_files/bus.png"
    
    # Different endpoint patterns
    endpoints = [
        "/api/predict",
        "/predict", 
        "/api/run/predict",
        "/run/predict",
        "/api/0",
        "/0",
        "/api/1", 
        "/1",
        "/api/2",
        "/2"
    ]
    
    # Different request formats
    request_formats = [
        {"data": [test_url]},
        {"data": [test_url], "fn_index": 0},
        {"data": [test_url], "fn_index": 1},
        {"data": test_url},
        {"data": [test_url], "session_hash": "test123"}
    ]
    
    for endpoint in endpoints:
        print(f"\nπŸ” Testing endpoint: {endpoint}")
        
        for i, format_data in enumerate(request_formats):
            print(f"  Format {i+1}: {format_data}")
            
            try:
                response = requests.post(
                    f"{base_url}{endpoint}",
                    json=format_data,
                    headers={"Content-Type": "application/json"},
                    timeout=10
                )
                
                print(f"    Status: {response.status_code}")
                
                if response.status_code == 200:
                    print("    βœ… SUCCESS!")
                    print(f"    Response: {response.text[:200]}...")
                    return endpoint, format_data
                elif response.status_code == 405:
                    print("    ⚠️ Method not allowed (endpoint exists)")
                elif response.status_code == 404:
                    print("    ❌ Not found")
                else:
                    print(f"    ❌ Unexpected: {response.text[:100]}...")
                    
            except Exception as e:
                print(f"    ❌ Error: {e}")
    
    return None, None

def test_gradio_client_different_ways():
    """Test gradio_client with different approaches"""
    print("\nπŸ” Testing gradio_client with different approaches...")
    
    try:
        from gradio_client import Client
        
        print("Creating client...")
        client = Client("hanszhu/Dense-Captioning-Platform")
        
        print("Trying different API names...")
        
        api_names = ["/predict", "/run/predict", "0", "1", "2", "3", "4", "5"]
        
        for api_name in api_names:
            print(f"\nTrying api_name: {api_name}")
            
            try:
                test_url = "https://raw.githubusercontent.com/gradio-app/gradio/main/test/test_files/bus.png"
                result = client.predict(test_url, api_name=api_name)
                print(f"  βœ… SUCCESS with api_name={api_name}!")
                print(f"  Result: {result}")
                return api_name
                
            except Exception as e:
                print(f"  ❌ Failed: {e}")
                
    except Exception as e:
        print(f"❌ gradio_client error: {e}")

def check_space_status():
    """Check if the space is running and accessible"""
    print("\nπŸ” Checking space status...")
    
    try:
        response = requests.get("https://hanszhu-dense-captioning-platform.hf.space/", timeout=10)
        print(f"Space status: {response.status_code}")
        
        if response.status_code == 200:
            print("βœ… Space is running")
            
            # Check for API-related content
            content = response.text.lower()
            if "api" in content:
                print("βœ… API-related content found")
            if "predict" in content:
                print("βœ… Predict-related content found")
            if "gradio" in content:
                print("βœ… Gradio content found")
                
        else:
            print("❌ Space is not accessible")
            
    except Exception as e:
        print(f"❌ Error checking space: {e}")

if __name__ == "__main__":
    print("πŸš€ Comprehensive API Endpoint Testing")
    print("=" * 60)
    
    # Check space status
    check_space_status()
    
    # Test all endpoints
    working_endpoint, working_format = test_all_possible_endpoints()
    
    # Test gradio_client
    working_api_name = test_gradio_client_different_ways()
    
    print("\n" + "=" * 60)
    print("🏁 Testing completed!")
    
    if working_endpoint and working_format:
        print(f"βœ… Found working combination:")
        print(f"   Endpoint: {working_endpoint}")
        print(f"   Format: {working_format}")
    if working_api_name:
        print(f"βœ… Found working gradio_client api_name: {working_api_name}")
    
    if not working_endpoint and not working_api_name:
        print("❌ No working endpoints found")
        print("The space might need different configuration or the API is not properly exposed")