File size: 25,145 Bytes
2144a09
 
 
 
 
 
 
8f0eb46
2144a09
610364e
 
8f0eb46
2144a09
 
9823bb5
610364e
9823bb5
610364e
 
 
8f0eb46
610364e
 
8f0eb46
 
 
 
99f4fb0
2144a09
610364e
8f0eb46
610364e
9823bb5
610364e
8f0eb46
 
 
50fcfc1
8f0eb46
9823bb5
8f0eb46
 
 
 
 
 
 
9823bb5
610364e
8f0eb46
 
9823bb5
 
610364e
8f0eb46
9823bb5
 
8f0eb46
9823bb5
 
8f0eb46
 
 
 
610364e
8f0eb46
610364e
8f0eb46
610364e
8f0eb46
 
 
610364e
 
8f0eb46
 
 
 
9823bb5
 
8f0eb46
 
 
610364e
8f0eb46
9823bb5
 
 
8f0eb46
 
 
 
 
 
9823bb5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8f0eb46
 
 
 
 
a39d07d
8f0eb46
 
 
610364e
 
8f0eb46
 
 
 
a39d07d
 
 
 
8f0eb46
 
 
 
9823bb5
 
 
610364e
8f0eb46
9823bb5
8f0eb46
 
 
 
 
9823bb5
8f0eb46
 
610364e
 
8f0eb46
9823bb5
610364e
 
9823bb5
 
8f0eb46
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
610364e
8f0eb46
 
 
 
 
 
 
 
 
 
 
9823bb5
8f0eb46
 
 
 
 
 
9823bb5
8f0eb46
9823bb5
 
 
 
8f0eb46
a39d07d
9823bb5
 
8f0eb46
 
 
9823bb5
8f0eb46
a39d07d
 
 
 
9823bb5
8f0eb46
a39d07d
9823bb5
8f0eb46
a39d07d
8f0eb46
 
610364e
8f0eb46
610364e
8f0eb46
610364e
8f0eb46
 
 
 
610364e
8f0eb46
 
 
 
610364e
8f0eb46
 
a39d07d
 
 
 
8f0eb46
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9823bb5
a39d07d
8f0eb46
 
a39d07d
 
 
 
 
 
 
 
 
8f0eb46
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9823bb5
8f0eb46
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9823bb5
8f0eb46
 
 
 
 
 
9823bb5
 
 
 
8f0eb46
9823bb5
 
8f0eb46
 
 
 
 
 
 
9823bb5
 
 
 
 
 
 
 
 
8f0eb46
9823bb5
8f0eb46
 
 
 
 
 
 
9823bb5
 
8f0eb46
 
9823bb5
 
8f0eb46
9823bb5
8f0eb46
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9823bb5
8f0eb46
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
610364e
8f0eb46
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
---
title: Prompt Reverse Engineer
emoji: πŸ”
colorFrom: gray
colorTo: blue
sdk: gradio
sdk_version: 6.31.0
python_version: "3.12"
app_file: app.py
pinned: true
license: apache-2.0
suggested_hardware: zero-a10g
models:
  - Qwen/Qwen3-VL-8B-Instruct
  - black-forest-labs/FLUX.2-klein-4B
  - black-forest-labs/FLUX.1-schnell
  - facebook/dinov2-base
library_name: transformers
tags:
  - image-to-prompt
  - computer-vision
  - captioning
  - qwen
  - flux
  - dino
  - vqa
  - zero-gpu
short_description: Turn any image into a verified text-to-image prompt.
---

# Prompt Reverse Engineer

Turn an image into a verified text-to-image prompt. A closed-loop pipeline extracts a constrained scene graph, grounds every fact on the original, renders with a proxy model, scores each predicate with VQAScore and layout with DINOv2, and rewrites only what fails β€” until both gates clear or iterations run out.

[![License](https://img.shields.io/github/license/glarrainv/Prompt-Reverse-Engineer?style=for-the-badge)](LICENSE)
[![Stars](https://img.shields.io/github/stars/glarrainv/Prompt-Reverse-Engineer?style=for-the-badge)](https://github.com/glarrainv/Prompt-Reverse-Engineer/stargazers)
[![Hugging Face Space](https://img.shields.io/badge/%F0%9F%A4%97%20Hugging%20Face-Space-blue?style=for-the-badge)](https://huggingface.co/spaces/glarr/Prompt-Reverse-Engineer)

[![Python](https://img.shields.io/badge/python-3.10%2B-blue?style=for-the-badge&logo=python)](https://www.python.org/)
[![Gradio](https://img.shields.io/badge/Gradio-6.31-orange?style=for-the-badge&logo=gradio)](https://gradio.app/)
[![PyTorch](https://img.shields.io/badge/PyTorch-2.13-red?style=for-the-badge&logo=pytorch)](https://pytorch.org/)
[![Transformers](https://img.shields.io/badge/Transformers-5.22-yellow?style=for-the-badge&logo=huggingface)](https://huggingface.co/docs/transformers)

---

## Overview

**Prompt Reverse Engineer** inverts text-to-image generation: instead of prompting a model and hoping the output matches your vision, you provide an image and get back a prompt whose claims are checked, one by one, against a proxy render of that prompt. The pipeline runs four stages in a loop:

1. **Extract** a Davidsonian Scene Graph (DSG) β€” entities, attributes, relations, and medium/optics on a 3Γ—3 grid
2. **Ground** every predicate on the original image; drop or re-localize facts the scorer rejects
3. **Render** the linearized prompt with a fast proxy model (FLUX.2-klein-4B by default; FLUX.1-schnell is supported)
4. **Verify** with VQAScore (predicate agreement) and DINOv2 (layout similarity); **rewrite** only failed facts

**What makes this different:**

- Grounding on the original: facts the scorer rejects on the original image are removed (or, for grid positions, re-localized) before rendering, and rewrites cannot introduce era/style buzzwords that the grounded prompt does not already contain. Grounding is only as good as the scorer: with the local backend the same model extracts and scores, so shared blind spots remain (use `SCORER_BACKEND=api` for an independent check)
- Targeted diff rewrites: only failed predicates are edited, preserving the rest
- Dual-gate stopping: both predicate agreement (S_VQA) and layout similarity (S_DINO) must clear configurable thresholds; if they never do, the best iteration is returned and the report says `max_iters`
- Two deployment pathways: local GPUs (Qwen3-VL + FLUX + DINOv2) or API-only (OpenRouter + FAL + gpt-4o)
- Gradio UI for Hugging Face Spaces (ZeroGPU-ready) and Python CLI for batch runs
- Full JSON report with every iteration, scores, predicates, and proxy renders

---

## Quickstart

### Option A: Hugging Face Space (zero setup)

[Open the Space](https://huggingface.co/spaces/glarr/Prompt-Reverse-Engineer) β†’ upload an image β†’ adjust gates if needed β†’ click **Invert image**. The verified prompt, proxy renders, per-fact scores, and a downloadable JSON report stream in live.

### Option B: Local (Windows / macOS / Linux)

```bash
# 1. Clone
git clone https://github.com/glarrainv/Prompt-Reverse-Engineer.git
cd Prompt-Reverse-Engineer

# 2. Create venv and install (requirements pin torch 2.13.0; on Windows, install the CUDA build
#    of torch 2.13.0 from pytorch.org first, since the PyPI wheel there is CPU-only)
python -m venv .venv
source .venv/bin/activate  # Windows: .venv\Scripts\activate
pip install --upgrade pip
pip install -r requirements.txt

# 3. Nothing is gated with the default models. Only if you set
#    PROXY_MODEL_ID=black-forest-labs/FLUX.1-schnell: accept its license on the model page, then log in
hf auth login  # or export HF_TOKEN=hf_...

# 4. Run the Gradio UI
python app.py
# β†’ opens at http://localhost:7860
```

> **GPU memory (local pathway).** All three models stay loaded. In bf16 the default weights are ~33.9 GB (Qwen3-VL-8B 17.5 GB, FLUX.2-klein-4B 16.0 GB, DINOv2-B 0.35 GB), plus activations, so a 48 GB GPU is the practical minimum. Ways to fit smaller cards, or to use the larger proxy (estimates from weight sizes, not benchmarks):
>
> | Setting | Weights on GPU | Fits |
> | --- | --- | --- |
> | default (klein-4B, bf16) | ~33.9 GB | 48 GB GPU |
> | `PROXY_FP8=1` | ~30 GB | 40 GB GPU |
> | `PROXY_CPU_OFFLOAD=1` | ~18 GB + the active FLUX component (≀ 8 GB) | 24 GB GPU; slower |
> | `PROXY_BACKEND=fal` | ~18 GB | 24 GB GPU; FLUX runs on fal.ai (paid, needs `FAL_KEY`) |
> | `PROXY_MODEL_ID=black-forest-labs/FLUX.1-schnell` | ~51.6 GB | 80 GB GPU |
> | schnell + `PROXY_FP8=1` | ~40 GB | 48 GB GPU |
>
> `PROXY_FP8=1` stores the FLUX transformer in float8 and computes in bf16; proxy renders change slightly.

### Choosing the proxy model

The proxy renderer is the forward model the loop optimizes the prompt against, so the verified prompt is specific to it. `PROXY_MODEL_ID` selects it; the pipeline class, step count and guidance scale are read from the checkpoint:

| `PROXY_MODEL_ID` | Weights | Steps / guidance | License and access | Needs |
| --- | --- | --- | --- | --- |
| `black-forest-labs/FLUX.2-klein-4B` (default) | 16.0 GB (transformer 7.75, Qwen3 text encoder 8.0) | 4 / 1.0 | Apache-2.0, ungated | diffusers β‰₯ 0.37 |
| `black-forest-labs/FLUX.1-schnell` | 33.7 GB (transformer 23.8, T5-XXL 9.5) | 4 / 0.0 | Apache-2.0, gated (accept on the model page) | `HF_TOKEN` |

- Only step-distilled checkpoints work. `FLUX.2-klein-base-*` are rejected at startup (their `model_index.json` lacks `is_distilled: true`), and other pipelines raise an error listing the supported ones.
- With the default klein-4B no model in the stack is gated, so no `HF_TOKEN` is needed; FLUX.1-schnell requires one.
- Every run report records the models and proxy settings used under `"models"`. Keep one proxy for a whole stimulus bank: scores and prompts are not comparable across proxies.
- klein-4B is verified structurally (pipeline selection, 4 steps, guidance 1.0, fp8 casting, deterministic seeds) on a small random-weight pipeline; render quality on your stimuli has not been measured. Compare S_VQA and S_DINO against schnell on a few images before relying on either in a study.

### Option C: Python CLI (batch processing)

```bash
# Invert a folder of images
python cli.py invert --input stimuli/ --out runs/ --max-iters 3 --vqa-gate 0.82 --dino-gate 0.65

# Select DINO medoid render per condition folder
python cli.py medoid --input renders/ --out medoids.json --key patch --copy-to medoids/
```

| Flag | Effect | Default |
|------|--------|---------|
| `--input PATH` | Image file or directory | required |
| `--out DIR` | Output root for runs/ | `runs/` |
| `--max-iters N` | Max rewrite iterations | `3` |
| `--vqa-gate F` | S_VQA convergence gate | `0.82` |
| `--dino-gate F` | S_DINO convergence gate (0 = disable) | `0.65` |
| `--fail-threshold F` | Predicate failure threshold for rewrite | `0.70` |
| `--w-vqa F` | Weight on S_VQA for best-iteration selection | `0.7` |
| `--seed N` | Base seed (proxy seed = base + t) | `42` |
| `--no-ground` | Skip grounding on original (not recommended) | off |

### Option D: Docker (GPU)

The repo has no Dockerfile; this one is a starting point and has not been tested.

```dockerfile
FROM pytorch/pytorch:2.13.0-cuda12.6-cudnn9-runtime
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
EXPOSE 7860
ENV GRADIO_SERVER_NAME=0.0.0.0
CMD ["python", "app.py"]
```

```bash
docker build -t prompt-reverse-engineer .
docker run --gpus all -p 7860:7860 -e HF_TOKEN=hf_... prompt-reverse-engineer
```

The local pathway is not practical on CPU (an 8B VLM plus a 12B diffusion transformer); without a GPU, use the API pathway below.

### Option E: API-only pathway (no local GPU)

Set these environment variables before launching:

```bash
export VLM_BACKEND=api
export VLM_API_KEY=your_openrouter_key       # or OPENAI_API_KEY compatible endpoint
export VLM_API_MODEL=qwen/qwen3-vl-32b-instruct
export VLM_API_BASE_URL=https://openrouter.ai/api/v1

export SCORER_BACKEND=api
export SCORER_API_KEY=your_openai_key        # gpt-4o for VQAScore logprobs
export SCORER_API_MODEL=gpt-4o

export PROXY_BACKEND=fal
export FAL_KEY=your_fal_key
export FAL_ENDPOINT=fal-ai/flux/schnell

export DINO_MODEL_ID=facebook/dinov2-small   # small, CPU-friendly
```

Then run `python app.py` or `python cli.py ...` as above.

---

## Configuration

All model backends and runtime knobs are controlled via environment variables (see `inversion/backends.py`). The Gradio UI also exposes the most common gates as sliders.

| Variable | Meaning | Default | Notes |
|----------|---------|---------|-------|
| `VLM_BACKEND` | `local` or `api` | `local` | VLM for DSG extraction + rewrite |
| `VLM_MODEL_ID` | HF repo (local) | `Qwen/Qwen3-VL-8B-Instruct` | ~17.5 GB in bf16 (no quantization) |
| `VLM_API_MODEL` | Model name (api) | `qwen/qwen3-vl-32b-instruct` | OpenRouter format |
| `VLM_API_BASE_URL` | API base URL | `https://openrouter.ai/api/v1` | |
| `VLM_API_KEY` | API key | β€” | Required if `VLM_BACKEND=api` |
| `SCORER_BACKEND` | `local` or `api` | auto (matches VLM) | VQAScore backend |
| `SCORER_API_MODEL` | API model for scoring | `gpt-4o` | Needs logprobs |
| `SCORER_API_KEY` | API key for scorer | `$OPENAI_API_KEY` | |
| `SCORER_API_BASE_URL` | API base URL for scorer | OpenAI | |
| `PROXY_BACKEND` | `flux` or `fal` | `flux` | Proxy renderer |
| `PROXY_MODEL_ID` | HF repo (flux) | `black-forest-labs/FLUX.2-klein-4B` | Also supports `black-forest-labs/FLUX.1-schnell`; see "Choosing the proxy model" |
| `PROXY_CPU_OFFLOAD` | Keep FLUX on CPU and move each component to GPU only while it runs | `0` | Saves ~10–24 GB of GPU memory; slower. Local GPUs only, not ZeroGPU |
| `PROXY_FP8` | Store the FLUX transformer in float8, compute in bf16 | `0` | Saves ~3.9 GB of resident weights with klein-4B (~33.9 β†’ ~30 GB) and ~12 GB with schnell (~51.6 β†’ ~40 GB); slightly changes proxy renders |
| `ZEROGPU_SIZE` | `auto`, `large` or `xlarge` | `auto` | Size of every `@spaces.GPU` call; `auto` picks `xlarge` when resident weights exceed 42 GB (logged at startup) |
| `FAL_ENDPOINT` | FAL endpoint | `fal-ai/flux/schnell` | If `PROXY_BACKEND=fal` |
| `DINO_MODEL_ID` | DINO model | `facebook/dinov2-base` | DINOv2-B (ungated). Any DINOv2/DINOv3 checkpoint works; `facebook/dinov3-*` is gated with manual approval, and the `dino_gate` value is not transferable between backbones |
| `DINO_DEVICE` | `cuda` or `cpu` | auto (CUDA if available) | |
| `VQA_MODE` | `normalized` or `yes` | `normalized` | `normalized` = P(yes) / (P(yes) + P(no)); `yes` = the original VQAScore, P("Yes") only (thresholds not comparable) |

**EngineConfig** (Python API / CLI flags) β€” runtime gates per run:

| Key | Meaning | Default | UI range |
|-----|---------|---------|-------|
| `max_iters` | Max proxy renders (render β†’ verify β†’ rewrite rounds) | `3` | 1–8 |
| `vqa_gate` | S_VQA convergence threshold | `0.82` | 0.5–1.0 |
| `dino_gate` | S_DINO convergence threshold (`0` = stop on S_VQA alone) | `0.65` | 0.0–1.0 |
| `fail_threshold` | Predicates scoring below this are sent to the rewriter | `0.70` | 0.3–0.95 |
| `parent_gate` | DSG rule: children of an entity scoring below this score 0 | `0.5` | β€” |
| `ground` | Enable grounding on original | `True` | β€” |
| `grounding_threshold` | P(yes) on the original below which a predicate is pruned; also the minimum for re-localizing a grid cell | `0.65` | β€” |
| `w_vqa` / `w_dino` | Weights of the joint score used to pick the best iteration | `0.7` / `0.3` | 0.0–1.0 |
| `base_seed` | Base seed (proxy seed = base + t) | `42` | β€” |
| `proxy_long_side` | Proxy render long side (px) | `512` | β€” |

---

## Usage / API

### Python API (single image)

```python
from PIL import Image
from inversion import EngineConfig, invert_image_to_verified_prompt
from inversion.backends import BackendSettings, load_components

# Load models (respects env vars)
settings = BackendSettings()
components = load_components(settings)
backends = components.backends()

# Configure run
cfg = EngineConfig(
    max_iters=3,
    vqa_gate=0.82,
    dino_gate=0.65,
    fail_threshold=0.70,
    w_vqa=0.7,
    base_seed=42,
    ground=True,
)

# Run
og = Image.open("my_image.png").convert("RGB")
run = invert_image_to_verified_prompt(og, backends, cfg)

# Results
print("Verified prompt:", run.best_prompt)
print(f"Best iteration: t={run.best.t}")
print(f"S_VQA: {run.best.mean_vqa:.3f}, S_DINO: {run.best.dino_patch:.3f}, Joint: {run.best.joint:.3f}")

# Save full report
from inversion.engine import save_run
save_run(run, "runs/my_image", og)
```

### Gradio UI (app.py)

The Space UI exposes:

- Image upload (drag-drop, file picker, clipboard paste). **Invert image** stays disabled until an image is uploaded and becomes **Run again** after a run
- Stopping rule sliders: max iterations, S_VQA gate, S_DINO gate
- Advanced accordion: failure threshold, S_VQA weight, base seed, grounding toggle
- A **run explorer** that walks through the run one stage at a time: *Extract scene graph β†’ Ground on original β†’ (Render proxy β†’ Verify β†’ Rewrite failures) per iteration β†’ Verified prompt*
  - While a run streams, it follows the newest stage. Navigating back pauses following, and **Play** resumes it. After the run, **Replay** steps through every stage.
  - Extract: medium, palette, nodes outlined in their grid cells, raw scene-graph JSON. Ground: candidates / removed / moved / kept, with re-localized cells marked
  - Render: original next to the render and the exact prompt it came from. Verify: S_VQA, S_DINO and joint tiles, green or orange against their gates, each with a `?` explanation, and every predicate (failed first)
  - Rewrite: what was sent to the rewriter, each edit (applied, added or skipped with its reason), and the next prompt with changes highlighted
  - Result: the verified prompt (Copy), every render with the best marked, the run summary, and the JSON report
- API (`gradio_client`): `/invert` still returns the verified prompt string and the report file (hidden components), alongside the explorer HTML

The explorer is rendered by `inversion/explorer.py` (pure HTML, no Gradio), driven by `app/explorer.js` and styled by `app/explorer.css`. Each run's images and report are written to a temporary folder served as a Gradio static path, and folders are pruned after 6 hours.

---

## Output / Data Flow

```
Input image
    β”‚
    β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ Module 1: Constrained DSG extractionβ”‚  β†’ DSGSceneSchema (JSON)
β”‚   Qwen3-VL β†’ entities, attrs, rels  β”‚     β€’ medium_and_optics[]
β”‚   3Γ—3 grid, ≀12 nodes               β”‚     β€’ background
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜     β€’ color_palette[]
    β”‚                                       β€’ nodes[] (id, grid_cell, entity, attrs, rels)
    β–Ό                                       β€’ extra_checks[]
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ Module 2: Grounding on original     β”‚  β†’ predicates[] (linearized)
β”‚   VQAScore each predicate on OG     β”‚     β€’ dropped[] (hallucinations removed)
β”‚   Parent-gate dependency blocking   β”‚     β€’ relocalized[] (grid cell corrections)
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
    β”‚
    β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ Module 2b: Canonical linearization  β”‚  β†’ canonical_prompt (string)
β”‚   Deterministic template from graph β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
    β”‚
    β–Ό (loop max_iters times)
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ Module 3: Proxy render (FLUX)       β”‚  β†’ proxy_image (PIL)
β”‚   seed = base_seed + t              β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
    β”‚
    β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ Module 4: Dual verification         β”‚  β†’ scored[] (Predicate + raw/effective)
β”‚   VQAScore each predicate on proxy  β”‚     β€’ mean_vqa (S_VQA)
β”‚   DINOv2 patch/cls similarity       β”‚     β€’ dino_patch (S_DINO), dino_cls
β”‚   Joint = w_vqaΒ·S_VQA + w_dinoΒ·S_DINOβ”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
    β”‚
    β”œβ”€ Gates met? β†’ STOP (converged)
    β”‚
    β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ Targeted diff rewrite               β”‚  β†’ RewritePatch (edits + additions)
β”‚   VLM rewrites only failed facts    β”‚     Applied β†’ new prompt for next iter
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
```

**Report schema** (`report.json` per run):
```json
{
  "config": { ...EngineConfig... },
  "models": { "vlm_model", "scorer", "vqa_mode", "dino_model", "proxy_model", "proxy_pipeline", "proxy_steps", "proxy_guidance_scale", "proxy_fp8" },
  "stop_reason": "converged" | "max_iters",
  "best_prompt": "verified prompt string",
  "best_iteration": 3,
  "best_scores": { "S_VQA": 0.92, "S_DINO_patch": 0.84, "joint": 0.88 },
  "canonical_prompt": "initial linearization",
  "proxy_size": [1024, 768],
  "raw_graph": { ...DSGSceneSchema as extracted... },
  "graph": { ...DSGSceneSchema after grounding... },
  "predicates": [ { "id", "kind", "statement", "parents", "ref" }, ... ],
  "grounding_dropped": [ { "id", "kind", "statement", "parents", "p_yes", "effective", "blocked_by" }, ... ],
  "grounding_relocalized": [ { "node", "entity", "from", "to", "p_yes" }, ... ],
  "iterations": [ { "t", "seed", "prompt", "S_VQA", "S_DINO_patch", "S_DINO_cls", "joint",
                    "failed", "rewrite_notes", "patch", "rewrite_error", "predicates" }, ... ]
}
```

---

## Performance

No runtime benchmarks have been measured yet. What is known:

- **Downloads.** The local pathway fetches ~33.9 GB of weights on first run with the default proxy (Qwen3-VL-8B 17.5 GB, FLUX.2-klein-4B diffusers components 16.0 GB, DINOv2-B 0.35 GB), or ~51.6 GB with FLUX.1-schnell (33.7 GB); later runs use the cache. A fresh Space container downloads them at every cold start.
- **GPU memory.** See the table under Option B.
- **Calls per image.** One extraction, one grounding pass on the original (plus one re-localization pass if a grid cell is rejected), then per iteration one render, one VQA pass, two DINO embeddings, and a rewrite unless it is the last iteration. On ZeroGPU each of these is its own `@spaces.GPU` allocation.

### Hugging Face Space (ZeroGPU)

ZeroGPU copies all loaded weights onto the GPU at the start of every `@spaces.GPU` call, so they must fit the slice with room for activations. With the default FLUX.2-klein-4B the weights are ~33.9 GB, which fits a `large` slice (50.87 GB) in bf16, so no variables are needed. `ZEROGPU_SIZE=auto` reads the loaded weight size at startup and requests `xlarge` (the full GPU, double quota cost per second) only above 42 GB; the chosen size is logged as `[startup] resident model weights: … -> ZeroGPU size '…'`, and with the defaults it should read `'large'`. Nothing in the default stack is gated, so no `HF_TOKEN` is needed.

To run the Space on FLUX.1-schnell instead, set the variables `PROXY_MODEL_ID=black-forest-labs/FLUX.1-schnell` and `PROXY_FP8=1` (~40 GB, fits `large`; without fp8 it is ~51.6 GB and `auto` requests `xlarge`), accept its license on the model page, and add `HF_TOKEN` as a Space secret. `PROXY_FP8=1` also applies to klein-4B (~30 GB), so remove it to render in bf16.

---

## Troubleshooting

| Symptom | Cause | Fix |
|---------|-------|-----|
| `CUDA out of memory` | VRAM exhausted | Set `PROXY_FP8=1` or `PROXY_CPU_OFFLOAD=1`; reduce `proxy_long_side`; use `PROXY_BACKEND=fal` or the API pathway |
| ZeroGPU: `memory allocation failed with OOM ... total: 50868518912` then `NVML_SUCCESS == r INTERNAL ASSERT FAILED` in `pack_to_cuda` | ZeroGPU copies all resident weights onto the GPU at the start of every call; with FLUX.1-schnell in bf16 they are ~51.6 GB and a `large` slice holds 50.87 GB | Use the default FLUX.2-klein-4B (~33.9 GB), or schnell with `PROXY_FP8=1` (~40 GB, fits `large`), or keep bf16 with `ZEROGPU_SIZE=auto`, which then requests `xlarge`; check the `[startup] ... ZeroGPU size` log line |
| `VLM_API_KEY not set` | API pathway selected without key | Export `VLM_API_KEY` (OpenRouter) or switch to `VLM_BACKEND=local` |
| `gpt-4o logprobs unavailable` | Scorer API key lacks logprobs access | Use OpenAI key with logprobs; or set `SCORER_BACKEND=local` |
| `Access denied` / 401 / 403 for FLUX.1-schnell (only if you set it as `PROXY_MODEL_ID`) | Gated repo | Accept the license on the model page, then `hf auth login` locally or add `HF_TOKEN` as a Space secret; or use the ungated default FLUX.2-klein-4B |
| `Access denied` for `facebook/dinov3-*` | Gated with manual approval | Use the default `facebook/dinov2-base` until access is granted |
| Gradio UI won't load | Port 7860 in use | `GRADIO_SERVER_PORT=7861 python app.py` |
| Space slow to start | Cold start downloads ~34 GB of weights (~52 GB with schnell), then ZeroGPU packs them | Normal; the first run after startup also waits for a GPU allocation |

**Logs:** Gradio console output shows per-stage status. CLI prints iteration status to stdout. Full run data in `runs/<stem>/report.json`.

---

## Attribution & License

**Author:** Gaspar Dario Larrain Varas  
**Space:** https://huggingface.co/spaces/glarr/Prompt-Reverse-Engineer  
**Repository:** https://github.com/glarrainv/Prompt-Reverse-Engineer  
**License:** Apache-2.0

This work builds on the following research:

```bibtex
@inproceedings{cho2024dsg,
  title={Davidsonian Scene Graph: Improving Reliability in Fine-grained Evaluation for Text-to-Image Generation},
  author={Cho, Jaemin and Hu, Yushi and Garg, Roopal and Anderson, Peter and Krishna, Ranjay and Baldridge, Jason and Bansal, Mohit and Pont-Tuset, Jordi and Wang, Su},
  booktitle={International Conference on Learning Representations (ICLR)},
  year={2024}
}

@inproceedings{lin2024vqascore,
  title={Evaluating Text-to-Visual Generation with Image-to-Text Generation},
  author={Lin, Zhiqiu and Pathak, Deepak and Li, Baiqi and Li, Jiayao and Xia, Xide and Neubig, Graham and Zhang, Pengchuan and Ramanan, Deva},
  booktitle={European Conference on Computer Vision (ECCV)},
  year={2024}
}

@article{oquab2024dinov2,
  title={DINOv2: Learning Robust Visual Features without Supervision},
  author={Oquab, Maxime and Darcet, Timoth{\'e}e and Moutakanni, Th{\'e}o and others},
  journal={Transactions on Machine Learning Research (TMLR)},
  year={2024}
}

@article{simeoni2025dinov3,
  title={DINOv3},
  author={Sim{\'e}oni, Oriane and others},
  journal={arXiv preprint arXiv:2508.10104},
  year={2025}
}

@article{bai2025qwen25vl,
  title={Qwen2.5-VL Technical Report},
  author={Bai, Shuai and Chen, Keqin and Liu, Xuejing and others},
  journal={arXiv preprint arXiv:2502.13923},
  year={2025}
}

@misc{flux2024,
  title={FLUX},
  author={{Black Forest Labs}},
  year={2024},
  howpublished={https://github.com/black-forest-labs/flux}
}

@software{larrain2026promptreverse,
  author = {Larrain Varas, Gaspar Dario},
  title = {Prompt Reverse Engineer},
  year = {2026},
  url = {https://github.com/glarrainv/Prompt-Reverse-Engineer},
  license = {Apache-2.0}
}
```

**Model credits:** Qwen Team (Qwen3-VL), Black Forest Labs (FLUX.1), Meta AI (DINOv2/DINOv3), OpenAI (gpt-4o for VQAScore API pathway).