File size: 10,986 Bytes
8704f25
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
52717aa
 
8704f25
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
300fe3d
8704f25
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
300fe3d
8704f25
 
 
 
 
 
300fe3d
8704f25
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
300fe3d
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
import os

os.environ.setdefault("PYTORCH_CUDA_ALLOC_CONF", "expandable_segments:True")

import spaces  # noqa: E402  (must be imported before any torch/CUDA usage)
import time  # noqa: E402
from pathlib import Path  # noqa: E402

import gradio as gr  # noqa: E402
import torch  # noqa: E402
from jinja2 import Template  # noqa: E402
from PIL import Image  # noqa: E402
from transformers import (  # noqa: E402
    AutoProcessor,
    Qwen2_5_VLForConditionalGeneration,
)

# ---------------------------------------------------------------------------
# Model — mirrors the authors' reference implementation in
# SpatialBlock/src/demo.py (Apache-2.0). The reason model wraps the question
# in a chain-of-thought prompt and answers between <answer> </answer> tags.
# ---------------------------------------------------------------------------

MODEL_ID = "rsoohyun/SpatialBlock-7B-reason"
MAX_PIXELS = 128 * 28 * 28  # authors' default (demo.py)
MIN_PIXELS = 16 * 28 * 28
MAX_NEW_TOKENS = 1024  # authors' demo.sh default for the reason variant

IMAGE_TOKEN = "<image>"
HERE = Path(__file__).parent
COT_TEMPLATE_FILE = HERE / "rl_cot.jinja"

model = Qwen2_5_VLForConditionalGeneration.from_pretrained(
    MODEL_ID,
    dtype=torch.bfloat16,
    attn_implementation="sdpa",
)
model.eval()
model = model.to("cuda")

processor = AutoProcessor.from_pretrained(
    MODEL_ID,
    use_fast=True,
    max_pixels=MAX_PIXELS,
    min_pixels=MIN_PIXELS,
)


def apply_cot_template(text: str) -> str:
    """Exactly the authors' apply_cot_template() from src/demo.py."""
    format_prompt = COT_TEMPLATE_FILE.read_text(encoding="utf-8")
    format_prompt = Template(format_prompt.strip())
    return format_prompt.render(content=text + "\n")


def build_messages(text: str, images: list) -> list:
    """Exactly the authors' build_messages() from src/demo.py."""
    chunks = text.split(IMAGE_TOKEN)
    content = []
    if len(chunks) == 1:
        content += [{"type": "image", "image": image} for image in images]
        content.append({"type": "text", "text": text})
    else:
        if len(chunks) - 1 != len(images):
            raise ValueError(
                f"{len(chunks) - 1} '{IMAGE_TOKEN}' markers but {len(images)} images"
            )
        for i, chunk in enumerate(chunks):
            if chunk:
                content.append({"type": "text", "text": chunk})
            if i < len(images):
                content.append({"type": "image", "image": images[i]})
    return [{"role": "user", "content": content}]


def _parse_answer_letter(text: str) -> str:
    """Extract the option letter from '<answer>X</answer>' (or a bare letter)."""
    if "<answer>" in text:
        inner = text.split("<answer>")[-1].split("</answer>")[0]
        return inner.strip()
    stripped = text.strip()
    return stripped[:1] if stripped else ""


# ---------------------------------------------------------------------------
# Inference
# ---------------------------------------------------------------------------


@spaces.GPU(duration=40)  # measured 4-7s per example; 40s covers a full
# 1024-token chain-of-thought at conservative decode speed with margin.
def answer(images, question):
    if not images:
        raise gr.Error("Please upload at least one image (or pick an example).")
    if not question or not question.strip():
        raise gr.Error("Please enter a question.")

    # gr.Gallery passes a list of (media, caption) tuples; keep the media.
    if images and not isinstance(images[0], (str, Image.Image)):
        images = [item[0] for item in images]

    pil_images = [Image.open(p).convert("RGB") for p in images]

    text = apply_cot_template(question.strip())
    messages = build_messages(text, pil_images)

    inputs = processor.apply_chat_template(
        messages,
        tokenize=True,
        add_generation_prompt=True,
        return_dict=True,
        return_tensors="pt",
    ).to(model.device, dtype=torch.bfloat16)

    start = time.perf_counter()
    with torch.inference_mode():
        generated_ids = model.generate(
            **inputs,
            max_new_tokens=MAX_NEW_TOKENS,
            do_sample=False,
        )
    elapsed = time.perf_counter() - start

    trimmed = [
        out_ids[len(in_ids):]
        for in_ids, out_ids in zip(inputs["input_ids"], generated_ids)
    ]
    output = processor.batch_decode(
        trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False
    )[0]

    letter = _parse_answer_letter(output)
    timing = f"_{elapsed:.1f}s on GPU"
    return letter, output.strip(), timing


# ---------------------------------------------------------------------------
# UI
# ---------------------------------------------------------------------------

EXAMPLES_DIR = HERE / "examples"

FLIP_QUESTION = (
    "Question : Here is an image of a 3D structure made of block.<image>\n"
    "Based on the images, how does the 3D structure appear when flip vertically?\n"
    "Options:\nA.<image>\nB.<image>\nC.<image>\nD.<image>"
)

Q1_QUESTION = (
    "Question : Here are images of a 3D structure made of block <image>\n"
    "and the direction of views.<image>\n"
    "Based on the images, how does the 3D structure appear when viewed from the front?\n"
    "Options:\nA.<image>\nB.<image>\nC.<image>\nD.<image>"
)

Q3_QUESTION = (
    "Question : Here are images of two 3D structures made of block.\n"
    "Structure 1 : <image>\n"
    "Structure 2 : <image>\n"
    "Based on the images, if we move Structure 2 so that its orange and purple "
    "blocks overlap with the matching ones in Structure 1, which 3D structure "
    "can be formed?\n"
    "Options:\nA.<image>\nB.<image>\nC.<image>\nD.<image>"
)


def _ex(question: str, files: list):
    return [[str(EXAMPLES_DIR / f) for f in files], question]


EXAMPLES = [
    _ex(
        FLIP_QUESTION,
        ["image1.png", "image2.png", "image3.png", "image4.png", "image5.png"],
    ),
    _ex(
        Q1_QUESTION,
        ["q1_img1.jpg", "q1_img2.jpg", "q1_opt_a.jpg", "q1_opt_b.jpg",
         "q1_opt_c.jpg", "q1_opt_d.jpg"],
    ),
    _ex(
        Q3_QUESTION,
        ["q3_struct1.jpg", "q3_struct2.jpg", "q3_opt_a.jpg", "q3_opt_b.jpg",
         "q3_opt_c.jpg", "q3_opt_d.jpg"],
    ),
]

with gr.Blocks(title="SpatialBlock") as demo:
    gr.HTML(
        """
        <div style="display: flex; align-items: center; gap: 12px; flex-wrap: wrap;">
          <span style="font-size: 36px;">🧱</span>
          <h1 style="margin: 0; font-size: 26px;">SpatialBlock — Spatial Intelligence in LVLMs</h1>
        </div>
        <p style="margin-top: 4px;">
          <em>SpatialBlock: Enhancing Spatial Intelligence in LVLMs via Synthetic
          Block-Stacking Problem</em> — vision-language models fine-tuned on
          15,000 synthetic block-stacking problems to reason about the 3D
          structure behind 2D images.
          <a href="https://huggingface.co/papers/2609.07064">Paper</a> ·
          <a href="https://github.com/rsoohyun/SpatialBlock">GitHub</a> ·
          <a href="https://huggingface.co/rsoohyun/SpatialBlock-7B-reason">Model</a> ·
          <a href="https://huggingface.co/datasets/rsoohyun/SpatialBlock-15k">Dataset</a>
        </p>
        """
    )
    gr.Markdown(
        "Upload the images for a spatial multiple-choice question — one image per "
        "`<image>` marker in the question — and ask. The model "
        "(`SpatialBlock-7B-reason`) thinks step by step, then gives its final "
        "answer between `<answer> </answer>` tags. "
        "**Try the examples below** to see it in action."
    )

    with gr.Row():
        with gr.Column():
            images_in = gr.Gallery(
                label="Images (one per <image> marker, in order)",
                file_types=["image"],
                columns=3,
                height=240,
                type="filepath",
                interactive=True,
            )
            question_in = gr.Textbox(
                label="Question (use <image> where each uploaded image goes)",
                placeholder=(
                    "Question : Here are images of a 3D structure made of block <image>\n"
                    "and the direction of views.<image>\n..."
                ),
                lines=9,
                max_lines=14,
            )
            submit_btn = gr.Button("Answer", variant="primary")

        with gr.Column():
            answer_letter = gr.Textbox(
                label="Answer",
                interactive=False,
                buttons=["copy"],
            )
            reasoning_out = gr.Textbox(
                label="Step-by-step reasoning",
                lines=14,
                max_lines=24,
                interactive=False,
                buttons=["copy"],
            )
            timing_out = gr.Markdown()

    submit_btn.click(
        fn=answer,
        inputs=[images_in, question_in],
        outputs=[answer_letter, reasoning_out, timing_out],
    )

    gr.Examples(
        examples=EXAMPLES,
        inputs=[images_in, question_in],
        fn=answer,
        outputs=[answer_letter, reasoning_out, timing_out],
        cache_examples=True,
        cache_mode="lazy",
        label="Examples from SpatialBlock-15k (and the repo's flip demo)",
    )

    gr.Markdown(
        """
        <details>
        <summary><b>How this works</b></summary>

        The question text contains `<image>` markers; each uploaded image is
        substituted for one marker, in upload order. This is exactly the
        interface of the authors' `src/demo.py` reference implementation.

        - The question is wrapped in the authors' reasoning prompt, and the
          model produces a numbered reasoning sequence ending with
          `<answer>X</answer>`.
        - The paper also trains a **direct** model that predicts the option
          letter immediately
          ([SpatialBlock-7B-direct](https://huggingface.co/rsoohyun/SpatialBlock-7B-direct));
          this demo runs the reasoning variant.

        The checkpoint is a Qwen2.5-VL-7B-Instruct fine-tune on
        [SpatialBlock-15k](https://huggingface.co/datasets/rsoohyun/SpatialBlock-15k),
        a fully synthetic set of block-stacking problems covering 3D-to-2D
        projection, viewpoint transformation, and structural combination.
        </details>

        <details>
        <summary><b>Citation</b></summary>

        ```bibtex
        @misc{ryu2026spatialblockenhancingspatialintelligence,
              title={SpatialBlock: Enhancing Spatial Intelligence in LVLMs via Synthetic Block-Stacking Problem},
              author={Soohyun Ryu and Sohee Kim and Eunho Yang},
              year={2026},
              eprint={2609.07064},
              archivePrefix={arXiv},
              primaryClass={cs.CV},
              url={https://arxiv.org/abs/2609.07064},
        }
        ```
        </details>
        """
    )

if __name__ == "__main__":
    demo.launch(theme=gr.themes.Citrus())