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