Spaces:
Sleeping
Sleeping
File size: 8,643 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 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 |
# π Dense Captioning Platform API Documentation
## Overview
The Dense Captioning Platform provides comprehensive chart analysis through a Gradio-based API. It can classify chart types, detect chart elements, and segment data points from uploaded images.
## API Access
**Base URL:** `https://hanszhu-dense-captioning-platform.hf.space`
**API Type:** Gradio Client API (not RESTful)
## Installation
### Prerequisites
```bash
pip install gradio-client
```
### Quick Start
### Python Client (Recommended)
```python
from gradio_client import Client, handle_file
# Initialize client with direct URL
client = Client("https://hanszhu-dense-captioning-platform.hf.space")
# Analyze a chart image using file path
result = client.predict(
image=handle_file('path/to/your/chart.png'),
fn_index=0
)
print(result)
```
### Using a URL
```python
from gradio_client import Client, handle_file
client = Client("https://hanszhu-dense-captioning-platform.hf.space")
# Use a publicly accessible image URL
result = client.predict(
image=handle_file("https://example.com/chart.png"),
fn_index=0
)
print(result)
```
## Input Parameters
| Parameter | Type | Required | Description |
|-----------|------|----------|-------------|
| `image` | File/URL | Yes | Chart image to analyze (PNG, JPG, JPEG supported) |
## Important Notes
### β
Working Approach
- **Use `fn_index=0`** instead of `api_name="/predict"`
- **Use direct URL** `"https://hanszhu-dense-captioning-platform.hf.space"`
- **Always use `handle_file()`** for both local files and URLs
- **This is a Gradio Client API**, not a RESTful API
### β What Doesn't Work
- Direct HTTP POST requests to `/predict`
- Using `api_name="/predict"` with this setup
- Using `Client("hanszhu/Dense-Captioning-Platform")` (use direct URL instead)
## Output Format
The API returns a JSON object with the following structure:
```json
{
"chart_type_id": 4,
"chart_type_label": "Bar plot",
"element_result": {
"bboxes": [...],
"segments": [...]
},
"datapoint_result": {
"bboxes": [...],
"segments": [...]
},
"status": "Full analysis completed",
"processing_time": 2.345
}
```
### Output Fields
| Field | Type | Description |
|-------|------|-------------|
| `chart_type_id` | int | Numeric identifier for chart type (0-27) |
| `chart_type_label` | string | Human-readable chart type name |
| `element_result` | object/string | Detected chart elements (titles, axes, legends, etc.) |
| `datapoint_result` | object/string | Segmented data points and regions |
| `status` | string | Processing status message |
| `processing_time` | float | Time taken for analysis in seconds |
## Supported Chart Types
The platform can classify 28 different chart types:
| ID | Chart Type | ID | Chart Type |
|----|------------|----|------------|
| 0 | Line graph | 14 | Histogram |
| 1 | Natural image | 15 | Box plot |
| 2 | Table | 16 | Vector plot |
| 3 | 3D object | 17 | Pie chart |
| 4 | Bar plot | 18 | Surface plot |
| 5 | Scatter plot | 19 | Algorithm |
| 6 | Medical image | 20 | Contour plot |
| 7 | Sketch | 21 | Tree diagram |
| 8 | Geographic map | 22 | Bubble chart |
| 9 | Flow chart | 23 | Polar plot |
| 10 | Heat map | 24 | Area chart |
| 11 | Mask | 25 | Pareto chart |
| 12 | Block diagram | 26 | Radar chart |
| 13 | Venn diagram | 27 | Confusion matrix |
## Chart Elements Detected
The element detection model identifies:
- **Titles & Labels**: Chart title, subtitle, axis labels
- **Axes**: X-axis, Y-axis, tick labels
- **Legend**: Legend title, legend items, legend text
- **Data Elements**: Data points, data lines, data bars, data areas
- **Structural Elements**: Grid lines, plot areas
## Error Handling
The API returns error messages in the response fields when issues occur:
```json
{
"chart_type_id": "Error: Model not available",
"chart_type_label": "Error: Model not available",
"element_result": "Error: Invalid image format",
"datapoint_result": "Error: Processing failed",
"status": "Error in chart classification",
"processing_time": 0.0
}
```
## Rate Limits
- **Free Tier**: Limited requests per hour
- **Processing Time**: Typically 2-5 seconds per image
- **Image Size**: Recommended max 10MB
## Complete Working Example
Here's a complete example that demonstrates all the working patterns:
```python
from gradio_client import Client, handle_file
import json
def analyze_chart(image_path_or_url):
"""
Analyze a chart image using the Dense Captioning Platform API
Args:
image_path_or_url (str): Path to local image file or URL to image
Returns:
dict: Analysis results with chart type, elements, and data points
"""
try:
# Initialize client with direct URL
client = Client("https://hanszhu-dense-captioning-platform.hf.space")
# Make prediction using the working approach
result = client.predict(
image=handle_file(image_path_or_url),
fn_index=0
)
return result
except Exception as e:
return {
"error": f"API call failed: {str(e)}",
"status": "Error",
"processing_time": 0.0
}
# Example usage
if __name__ == "__main__":
# Test with a local file
local_result = analyze_chart("path/to/your/chart.png")
print("Local file result:", json.dumps(local_result, indent=2))
# Test with a URL
url_result = analyze_chart("https://example.com/chart.png")
print("URL result:", json.dumps(url_result, indent=2))
```
## Examples
### Example 1: Bar Chart Analysis
```python
from gradio_client import Client, handle_file
client = Client("https://hanszhu-dense-captioning-platform.hf.space")
# Analyze a bar chart
result = client.predict(
image=handle_file('bar_chart.png'),
fn_index=0
)
print(f"Chart Type: {result['chart_type_label']}")
print(f"Processing Time: {result['processing_time']}s")
```
### Example 2: Batch Processing
```python
from gradio_client import Client, handle_file
import os
client = Client("https://hanszhu-dense-captioning-platform.hf.space")
# Process multiple charts
chart_files = ['chart1.png', 'chart2.png', 'chart3.png']
results = []
for chart_file in chart_files:
if os.path.exists(chart_file):
result = client.predict(
image=handle_file(chart_file),
fn_index=0
)
results.append(result)
print(f"Processed {chart_file}: {result['chart_type_label']}")
```
### Example 3: Test with Public Image
```python
from gradio_client import Client, handle_file
client = Client("https://hanszhu-dense-captioning-platform.hf.space")
# Test with a public image URL
result = client.predict(
image=handle_file("https://raw.githubusercontent.com/gradio-app/gradio/main/test/test_files/bus.png"),
fn_index=0
)
print("β
API Test Successful!")
print(f"Chart Type: {result['chart_type_label']}")
print(f"Status: {result['status']}")
```
## Troubleshooting
### Common Issues
1. **"Model not available"**: The models are still loading, wait a moment and retry
2. **"Invalid image format"**: Ensure the image is in PNG, JPG, or JPEG format
3. **"Processing failed"**: The image might be corrupted or too large
4. **"Expecting value: line 1 column 1"**: Use `fn_index=0` instead of `api_name="/predict"`
5. **"Cannot find a function with api_name"**: Use direct URL and `fn_index=0`
### Best Practices
1. **Image Quality**: Use clear, high-resolution images for best results
2. **Format**: PNG or JPG formats work best
3. **Size**: Keep images under 10MB for faster processing
4. **Client Setup**: Always use direct URL and `fn_index=0`
5. **File Handling**: Always use `handle_file()` for both local files and URLs
6. **Retry Logic**: Implement retry logic for failed requests
### Quick Test
To verify the API is working, run this test:
```python
from gradio_client import Client, handle_file
try:
client = Client("https://hanszhu-dense-captioning-platform.hf.space")
result = client.predict(
image=handle_file("https://raw.githubusercontent.com/gradio-app/gradio/main/test/test_files/bus.png"),
fn_index=0
)
print("β
API is working!")
print(f"Chart Type: {result['chart_type_label']}")
except Exception as e:
print(f"β API test failed: {e}")
```
## Support
For issues or questions:
- Check the [Hugging Face Space](https://huggingface.co/spaces/hanszhu/Dense-Captioning-Platform)
- Review the error messages in the API response
- Ensure your image format and size are within limits |