File size: 3,390 Bytes
1a91148
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
---
tags:
- object-detection
- callout-detection
- architectural-drawings
- wrapper
library_name: custom
task: object-detection
license: apache-2.0
---

# LearningStudio Callout Detection Wrapper

Wrapper for the Lambda-based callout detection pipeline, providing EMCO-compatible API format for LearningStudio integration.

## Overview

This wrapper:
1. Accepts image input in multiple formats (URL, base64, data URL)
2. Gets a presigned S3 URL from API Gateway
3. Uploads the image directly to S3 (avoids API Gateway data transfer costs)
4. Starts the detection job via API Gateway (small JSON payload)
5. Polls for completion
6. Transforms results to EMCO-compatible format

## Architecture

```
HF Wrapper
    β”‚
    β”œβ”€1─▢ GET /upload-url (get presigned S3 URL)
    β”‚
    β”œβ”€2─▢ PUT image directly to S3 (free, bypasses API Gateway)
    β”‚
    β”œβ”€3─▢ POST /detect {"job_id", "s3_url"} (tiny payload)
    β”‚
    └─4─▢ GET /status/{job_id} (poll until complete)
```

## API Format

### Input

Accepts images in multiple formats:

```json
// HTTP URL
{"inputs": "https://example.com/image.jpg"}

// Data URL (base64 encoded)
{"inputs": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUA..."}

// Raw base64
{"inputs": "iVBORw0KGgoAAAANSUhEUgAAAAUA..."}
```

### Output

Returns EMCO-compatible format:

```json
{
  "predictions": [
    {
      "id": 1,
      "label": "callout",
      "class_id": 0,
      "confidence": 0.95,
      "bbox": {
        "x1": 100,
        "y1": 200,
        "x2": 300,
        "y2": 400
      }
    }
  ],
  "total_detections": 1,
  "image": "base64_encoded_image",
  "image_width": 1920,
  "image_height": 1080
}
```

### Bounding Box Format

- **Input from Lambda**: `[x, y, width, height]` (xywh format)
- **Output to LearningStudio**: `{"x1", "y1", "x2", "y2"}` (xyxy format)

The wrapper automatically converts between these formats.

## Configuration

This endpoint requires the following secrets to be configured in HuggingFace Inference Endpoint settings:

| Secret | Description |
|--------|-------------|
| `API_GATEWAY_URL` | Full URL of the API Gateway endpoint (e.g., `https://xxx.execute-api.us-east-1.amazonaws.com/dev`) |
| `API_KEY` | API key for authentication |

## Usage

### Python

```python
import requests

HF_ENDPOINT = "https://your-endpoint.endpoints.huggingface.cloud"
HF_TOKEN = "your-hf-token"

response = requests.post(
    HF_ENDPOINT,
    headers={"Authorization": f"Bearer {HF_TOKEN}"},
    json={"inputs": "https://example.com/architectural-drawing.png"}
)

result = response.json()
print(f"Found {result['total_detections']} callouts")
for pred in result["predictions"]:
    print(f"  Callout {pred['id']}: {pred['bbox']}, confidence={pred['confidence']}")
```

### cURL

```bash
curl -X POST https://your-endpoint.endpoints.huggingface.cloud \
  -H "Authorization: Bearer $HF_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{"inputs": "https://example.com/architectural-drawing.png"}'
```

## Processing Time

Typical processing time is 30-120 seconds depending on image size and complexity. The wrapper polls the backend every 5 seconds with a maximum timeout of 15 minutes.

## Error Handling

Errors are returned in a consistent format:

```json
{
  "error": "Description of the error",
  "predictions": [],
  "total_detections": 0,
  "image": ""
}
```

## License

Apache 2.0