Spaces:
Running on Zero
Running on Zero
Download app.py from hugging-apps/vtr-2b: direct link, hf CLI and curl.
- Browser
- Download file 5.79 kB
-
https://huggingface.co/spaces/hugging-apps/vtr-2b/resolve/main/app.py
- Command line
-
hf download hf://spaces/hugging-apps/vtr-2b/app.py
-
curl -L -o app.py https://huggingface.co/spaces/hugging-apps/vtr-2b/resolve/main/app.py
5.79 kB
| """VTR-2B: Visual Token Routing demo. | |
| `splash-vlm/VTR-2B` is a 2B-parameter computer-use VLM built on Qwen3.5-2B | |
| (https://arxiv.org/abs/2611.06854). Given the previous and current | |
| screenshots of a UI plus a task instruction, it predicts the next UI action. | |
| Its token router scores every merged vision patch from a spatio-temporal | |
| window and keeps only the patches that matter, splicing the kept tokens back | |
| in scaled by their keep-probability at the original M-RoPE positions. | |
| """ | |
| import spaces # must precede any torch / transformers import | |
| import time | |
| import gradio as gr | |
| from transformers import AutoProcessor | |
| from vtr_ops import generate_routed, load_vtr_model | |
| MODEL_ID = "l3lab/VTR-2B" | |
| BLOCK_SIZE = 10 | |
| DEFAULT_INSTRUCTION = "What changed on the screen, and what should I click next?" | |
| print("Loading VTR-2B…") | |
| processor = AutoProcessor.from_pretrained(MODEL_ID) | |
| model, router = load_vtr_model(MODEL_ID) # loads to CUDA eagerly, module scope | |
| print("VTR-2B ready.") | |
| def _build_messages(prev_img, curr_img, instruction): | |
| content = [] | |
| if prev_img is not None: | |
| content.append({"type": "image", "image": prev_img}) | |
| content.append({"type": "image", "image": curr_img}) | |
| content.append({"type": "text", "text": instruction or DEFAULT_INSTRUCTION}) | |
| return [{"role": "user", "content": content}] | |
| def predict( | |
| prev_screenshot, | |
| current_screenshot, | |
| instruction=DEFAULT_INSTRUCTION, | |
| max_new_tokens=256, | |
| dense=False, | |
| progress=gr.Progress(track_tqdm=True), | |
| ): | |
| """Predict the next UI action from a screenshot pair and a task instruction. | |
| Parameters | |
| ---------- | |
| prev_screenshot : the previous screen frame (optional but recommended — | |
| routing needs at least two frames to have any effect). | |
| current_screenshot : the current screen frame (required). | |
| instruction : what the agent should do, e.g. "click the File menu". | |
| max_new_tokens : generation budget for the answer. | |
| dense : disable routing and keep every vision token (comparison mode). | |
| """ | |
| if current_screenshot is None: | |
| raise gr.Error("Please provide the current screenshot.") | |
| messages = _build_messages(prev_screenshot, current_screenshot, instruction) | |
| t0 = time.time() | |
| text, stats = generate_routed( | |
| model, | |
| processor, | |
| router, | |
| messages, | |
| max_new_tokens=int(max_new_tokens), | |
| dense=bool(dense), | |
| block_size=BLOCK_SIZE, | |
| ) | |
| elapsed = time.time() - t0 | |
| kept = stats["kept"] | |
| per_frame = " + ".join( | |
| f"{k}/{stats['tokens_per_frame']}" if stats["tokens_per_frame"] else str(k) for k in kept | |
| ) | |
| routing_line = ( | |
| f"Router kept {stats['kept_total']}/{stats['total']} vision tokens " | |
| f"({per_frame}) · prompt {stats['compacted_tokens']} vs {stats['prompt_tokens']} dense " | |
| f"· {elapsed:.1f}s" | |
| ) | |
| if "note" in stats: | |
| routing_line += f" · note: {stats['note']}" | |
| return text, routing_line | |
| CSS = """ | |
| #hero {text-align: center;} | |
| #hero h1 {font-size: 1.6rem; margin-bottom: 0.2rem;} | |
| #hero p {color: var(--body-text-color-subdued); font-size: 0.95rem;} | |
| """ | |
| with gr.Blocks(title="VTR-2B · Visual Token Routing") as demo: | |
| with gr.Column(elem_id="hero"): | |
| gr.Markdown("# VTR-2B · Visual Token Routing") | |
| gr.Markdown( | |
| "A 2B computer-use VLM that predicts the next UI action from your screenshots. " | |
| "Its router keeps only the vision tokens that matter — give it a **previous + current** " | |
| "frame pair to see routing in action." | |
| ) | |
| with gr.Row(): | |
| with gr.Column(): | |
| prev_screenshot = gr.Image( | |
| type="pil", label="Previous screenshot (optional)", height=260 | |
| ) | |
| current_screenshot = gr.Image( | |
| type="pil", label="Current screenshot", height=260 | |
| ) | |
| instruction = gr.Textbox( | |
| label="Instruction", | |
| value=DEFAULT_INSTRUCTION, | |
| placeholder="What changed on the screen, and what should I click next?", | |
| lines=2, | |
| ) | |
| with gr.Accordion("Advanced", open=False): | |
| max_new_tokens = gr.Slider(32, 512, value=256, step=16, label="Max new tokens") | |
| dense = gr.Checkbox(value=False, label="Dense mode (disable routing)") | |
| predict_btn = gr.Button("Predict next action", variant="primary") | |
| with gr.Column(): | |
| output = gr.Textbox(label="Predicted action", lines=10) | |
| routing_info = gr.Textbox(label="Routing", lines=2, interactive=False) | |
| predict_btn.click( | |
| fn=predict, | |
| inputs=[prev_screenshot, current_screenshot, instruction, max_new_tokens, dense], | |
| outputs=[output, routing_info], | |
| api_name="predict", | |
| ) | |
| gr.Examples( | |
| examples=[ | |
| ["examples/candy_prev.jpg", "examples/candy_kept.jpg"], | |
| ["examples/candyfall_prev.jpg", "examples/candyfall_kept.jpg"], | |
| ["examples/dota_prev.jpg", "examples/dota_kept.jpg"], | |
| ["examples/gpredict_prev.jpg", "examples/gpredict_kept.jpg"], | |
| ["examples/gta_prev.jpg", "examples/gta_kept.jpg"], | |
| ["examples/librecad_prev.jpg", "examples/librecad_kept.jpg"], | |
| ["examples/minecraft_prev.jpg", "examples/minecraft_kept.jpg"], | |
| ["examples/psychopy_prev.jpg", "examples/psychopy_kept.jpg"], | |
| ], | |
| inputs=[prev_screenshot, current_screenshot], | |
| fn=predict, | |
| outputs=[output, routing_info], | |
| cache_examples=True, | |
| cache_mode="lazy", | |
| label="Screenshot pairs from the VTR paper (consecutive frames)", | |
| ) | |
| demo.launch(theme=gr.themes.Citrus(), css=CSS, mcp_server=True) | |