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Captions Setup Guide
|
| 2 |
+
|
| 3 |
+
## Overview
|
| 4 |
+
This system uses **Alibaba Qwen VL Max** model to automatically caption cropped UI elements. The captions are added to each detected element in the output, making it easier to understand what each UI component is.
|
| 5 |
+
|
| 6 |
+
## Setup Steps
|
| 7 |
+
|
| 8 |
+
### 1. Get Alibaba Qwen API Key
|
| 9 |
+
- Sign up at: https://dashscope.console.aliyun.com/
|
| 10 |
+
- Navigate to API Keys section
|
| 11 |
+
- Copy your API key (format: `sk-...`)
|
| 12 |
+
|
| 13 |
+
### 2. Set Environment Variable
|
| 14 |
+
|
| 15 |
+
**Local Development:**
|
| 16 |
+
```bash
|
| 17 |
+
export QWEN_API_KEY="sk-YOUR_API_KEY_HERE"
|
| 18 |
+
```
|
| 19 |
+
|
| 20 |
+
**Docker/HF Spaces:**
|
| 21 |
+
Add to your `.env` file or Docker environment:
|
| 22 |
+
```
|
| 23 |
+
QWEN_API_KEY=sk-YOUR_API_KEY_HERE
|
| 24 |
+
```
|
| 25 |
+
|
| 26 |
+
### 3. Update app_hf_spaces_server.py
|
| 27 |
+
The API key is automatically read from the environment. Make sure to set it before running.
|
| 28 |
+
|
| 29 |
+
## Usage
|
| 30 |
+
|
| 31 |
+
### With API Server
|
| 32 |
+
Captions are automatically generated when you analyze an image:
|
| 33 |
+
|
| 34 |
+
```bash
|
| 35 |
+
python ui_element_client.py /path/to/image.png --api http://localhost:7860
|
| 36 |
+
```
|
| 37 |
+
|
| 38 |
+
Response includes `"caption"` field for each element:
|
| 39 |
+
```json
|
| 40 |
+
{
|
| 41 |
+
"elements": [
|
| 42 |
+
{
|
| 43 |
+
"element_id": "crop_0001",
|
| 44 |
+
"caption": "close button",
|
| 45 |
+
"x": 590,
|
| 46 |
+
"y": 21,
|
| 47 |
+
"confidence": 1.0
|
| 48 |
+
}
|
| 49 |
+
]
|
| 50 |
+
}
|
| 51 |
+
```
|
| 52 |
+
|
| 53 |
+
### Manual Captioning
|
| 54 |
+
```bash
|
| 55 |
+
python caption_cropped_images.py --dir /tmp/omoi_cropped_images --output crop_captions.json
|
| 56 |
+
```
|
| 57 |
+
|
| 58 |
+
## Output Format
|
| 59 |
+
|
| 60 |
+
### JSON with Captions
|
| 61 |
+
```json
|
| 62 |
+
{
|
| 63 |
+
"element_id": "crop_0001",
|
| 64 |
+
"caption": "close button",
|
| 65 |
+
"x": 590,
|
| 66 |
+
"y": 21,
|
| 67 |
+
"confidence": 1.0,
|
| 68 |
+
"bbox": { ... },
|
| 69 |
+
"center": { ... }
|
| 70 |
+
}
|
| 71 |
+
```
|
| 72 |
+
|
| 73 |
+
### CSV with Captions
|
| 74 |
+
```csv
|
| 75 |
+
Element_ID,Caption,X,Y,X1,Y1,X2,Y2,Width,Height,Confidence
|
| 76 |
+
crop_0001,close button,590,21,580,5,600,35,20,30,1.0000
|
| 77 |
+
crop_0002,search input,732,23,720,15,750,35,30,20,0.9950
|
| 78 |
+
```
|
| 79 |
+
|
| 80 |
+
## Configuration
|
| 81 |
+
|
| 82 |
+
### Cache System
|
| 83 |
+
- Captions are cached in `crop_captions.json`
|
| 84 |
+
- Avoids re-captioning the same images
|
| 85 |
+
- Use `--refresh` flag to force regeneration:
|
| 86 |
+
```bash
|
| 87 |
+
python caption_cropped_images.py --refresh
|
| 88 |
+
```
|
| 89 |
+
|
| 90 |
+
### Batch Processing
|
| 91 |
+
- Concurrent workers: Default 10, adjust with `--workers`:
|
| 92 |
+
```bash
|
| 93 |
+
python caption_cropped_images.py --workers 20
|
| 94 |
+
```
|
| 95 |
+
|
| 96 |
+
## Pricing
|
| 97 |
+
|
| 98 |
+
Alibaba Qwen VL pricing (as of April 2024):
|
| 99 |
+
- **Qwen VL Max**: ~$0.01-0.02 per image
|
| 100 |
+
- **Batch of 100 images**: ~$1-2
|
| 101 |
+
- Check current pricing: https://dashscope.console.aliyun.com/pricing
|
| 102 |
+
|
| 103 |
+
## Troubleshooting
|
| 104 |
+
|
| 105 |
+
### "QWEN_API_KEY not set"
|
| 106 |
+
```bash
|
| 107 |
+
# Check if environment variable is set
|
| 108 |
+
echo $QWEN_API_KEY
|
| 109 |
+
|
| 110 |
+
# If not, set it
|
| 111 |
+
export QWEN_API_KEY="sk-..."
|
| 112 |
+
```
|
| 113 |
+
|
| 114 |
+
### "API Rate Limit"
|
| 115 |
+
Reduce concurrent workers:
|
| 116 |
+
```bash
|
| 117 |
+
python caption_cropped_images.py --workers 5
|
| 118 |
+
```
|
| 119 |
+
|
| 120 |
+
### "Captions module not available"
|
| 121 |
+
Install dependencies:
|
| 122 |
+
```bash
|
| 123 |
+
pip install openai
|
| 124 |
+
```
|
| 125 |
+
|
| 126 |
+
## Fallback Behavior
|
| 127 |
+
|
| 128 |
+
If Qwen API is not available:
|
| 129 |
+
1. Captions are marked as "unknown"
|
| 130 |
+
2. Output still works without captions
|
| 131 |
+
3. No processing is blocked
|
| 132 |
+
|
| 133 |
+
## Example Output with Captions
|
| 134 |
+
|
| 135 |
+
```
|
| 136 |
+
[API] Analyzing: screenshot.png
|
| 137 |
+
[API] OmniParser: 152 elements detected
|
| 138 |
+
[API] Template matching: 152 matches found
|
| 139 |
+
[API] Adding captions: 152 images captioned
|
| 140 |
+
[API] Results:
|
| 141 |
+
- crop_0001: "close button" @ (590, 21)
|
| 142 |
+
- crop_0002: "search input" @ (732, 23)
|
| 143 |
+
- crop_0003: "maximize button" @ (1136, 20)
|
| 144 |
+
... and 149 more elements
|
| 145 |
+
```
|
| 146 |
+
|
| 147 |
+
## Next Steps
|
| 148 |
+
|
| 149 |
+
1. Set your QWEN_API_KEY environment variable
|
| 150 |
+
2. Deploy to HF Spaces with the updated code
|
| 151 |
+
3. Captions will be automatically generated for all images
|
| 152 |
+
4. Export results with captions in JSON/CSV format
|