#!/usr/bin/env python3 """ Test script for the Dense Captioning Platform API """ import requests import json from PIL import Image import io import base64 def test_api_with_url(): """Test the API using a URL""" print("๐Ÿงช Testing API with URL...") # Test URL (a simple chart image) test_url = "https://raw.githubusercontent.com/gradio-app/gradio/main/test/test_files/bus.png" # API endpoint api_url = "https://hanszhu-dense-captioning-platform.hf.space/predict" try: # Make the request response = requests.post( api_url, json={"data": [test_url]}, headers={"Content-Type": "application/json"} ) print(f"Status Code: {response.status_code}") print(f"Response: {response.text[:500]}...") if response.status_code == 200: result = response.json() print("โœ… API test successful!") print(f"Chart Type: {result.get('data', [{}])[0].get('chart_type_label', 'Unknown')}") else: print("โŒ API test failed!") except Exception as e: print(f"โŒ Error testing API: {e}") def test_api_with_gradio_client(): """Test the API using gradio_client""" print("\n๐Ÿงช Testing API with gradio_client...") try: from gradio_client import Client # Initialize client client = Client("hanszhu/Dense-Captioning-Platform") # Test with a URL result = client.predict( "https://raw.githubusercontent.com/gradio-app/gradio/main/test/test_files/bus.png", api_name="/predict" ) print("โœ… gradio_client test successful!") print(f"Result: {result}") except Exception as e: print(f"โŒ Error with gradio_client: {e}") def test_api_endpoints(): """Test available API endpoints""" print("\n๐Ÿงช Testing API endpoints...") base_url = "https://hanszhu-dense-captioning-platform.hf.space" endpoints = [ "/", "/api", "/api/predict", "/predict" ] for endpoint in endpoints: try: response = requests.get(f"{base_url}{endpoint}") print(f"{endpoint}: {response.status_code}") except Exception as e: print(f"{endpoint}: Error - {e}") if __name__ == "__main__": print("๐Ÿš€ Testing Dense Captioning Platform API") print("=" * 50) # Test different approaches test_api_endpoints() test_api_with_url() test_api_with_gradio_client() print("\n" + "=" * 50) print("๐Ÿ API testing completed!")