Spaces:
Sleeping
Sleeping
File size: 5,297 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 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
#!/usr/bin/env python3
"""
Script to find the correct API endpoint
"""
import requests
import json
def try_different_endpoints():
"""Try different possible API endpoints"""
print("π Trying different API endpoints...")
base_url = "https://hanszhu-dense-captioning-platform.hf.space"
# Different possible endpoints
endpoints = [
"/api/predict",
"/predict",
"/api/run/predict",
"/run/predict",
"/api/0",
"/0",
"/api/1",
"/1",
"/api/2",
"/2",
"/api/3",
"/3",
"/api/4",
"/4",
"/api/5",
"/5"
]
test_data = {
"data": ["https://raw.githubusercontent.com/gradio-app/gradio/main/test/test_files/bus.png"]
}
for endpoint in endpoints:
print(f"\nTrying POST to: {endpoint}")
try:
response = requests.post(
f"{base_url}{endpoint}",
json=test_data,
headers={"Content-Type": "application/json"}
)
print(f" Status: {response.status_code}")
print(f" Content-Type: {response.headers.get('content-type', 'unknown')}")
if response.status_code == 200:
print(" β
SUCCESS! Found working endpoint!")
print(f" Response: {response.text[:200]}...")
return endpoint
elif response.status_code == 405:
print(" β οΈ Method not allowed (endpoint exists but wrong method)")
elif response.status_code == 404:
print(" β Not found")
else:
print(f" β Unexpected status: {response.text[:100]}...")
except Exception as e:
print(f" β Error: {e}")
return None
def try_get_endpoints():
"""Try GET requests to find API info"""
print("\nπ Trying GET requests to find API info...")
base_url = "https://hanszhu-dense-captioning-platform.hf.space"
get_endpoints = [
"/api",
"/api/",
"/api/predict",
"/api/predict/",
"/api/run/predict",
"/api/run/predict/",
"/api/0",
"/api/1",
"/api/2",
"/api/3",
"/api/4",
"/api/5"
]
for endpoint in get_endpoints:
print(f"\nTrying GET: {endpoint}")
try:
response = requests.get(f"{base_url}{endpoint}")
print(f" Status: {response.status_code}")
print(f" Content-Type: {response.headers.get('content-type', 'unknown')}")
if response.status_code == 200:
content = response.text[:200]
print(f" Content: {content}...")
# Check if it's JSON
if response.headers.get('content-type', '').startswith('application/json'):
print(" β
JSON response - this might be the API info!")
try:
data = response.json()
print(f" API Info: {json.dumps(data, indent=2)}")
except:
pass
except Exception as e:
print(f" β Error: {e}")
def try_gradio_client_different_ways():
"""Try gradio_client with different approaches"""
print("\nπ Trying 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}")
if __name__ == "__main__":
print("π Finding the correct API endpoint")
print("=" * 60)
# Try different POST endpoints
working_endpoint = try_different_endpoints()
# Try GET endpoints for API info
try_get_endpoints()
# Try gradio_client with different approaches
working_api_name = try_gradio_client_different_ways()
print("\n" + "=" * 60)
print("π Endpoint discovery completed!")
if working_endpoint:
print(f"β
Found working POST endpoint: {working_endpoint}")
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 still be loading or need different configuration") |