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