File size: 2,691 Bytes
eb4d305
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#!/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!")