Spaces:
Sleeping
Sleeping
#!/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") |