Dense-Captioning-Platform / find_api_endpoint.py
hanszhu's picture
build(space): initial Docker Space with Gradio app, MMDet, SAM integration
eb4d305
#!/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")