Spaces:
Running on Zero
Running on Zero
Rework click-grounding demo: real MolmoWeb examples, Gradio 6 theme/css, crosshair marker, diagnostics
Browse files- .gitattributes +4 -0
- README.md +32 -13
- app.py +162 -100
- examples/{screenshot_1.png β allrecipes_home.png} +0 -0
- examples/{screenshot_2.png β allrecipes_hummus.png} +0 -0
- examples/{screenshot_3.png β allrecipes_search.png} +0 -0
- examples/amazon_backpacks.png +3 -0
- requirements.txt +1 -1
.gitattributes
CHANGED
|
@@ -36,3 +36,7 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
| 36 |
examples/screenshot_1.png filter=lfs diff=lfs merge=lfs -text
|
| 37 |
examples/screenshot_2.png filter=lfs diff=lfs merge=lfs -text
|
| 38 |
examples/screenshot_3.png filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
examples/screenshot_1.png filter=lfs diff=lfs merge=lfs -text
|
| 37 |
examples/screenshot_2.png filter=lfs diff=lfs merge=lfs -text
|
| 38 |
examples/screenshot_3.png filter=lfs diff=lfs merge=lfs -text
|
| 39 |
+
examples/allrecipes_home.png filter=lfs diff=lfs merge=lfs -text
|
| 40 |
+
examples/allrecipes_hummus.png filter=lfs diff=lfs merge=lfs -text
|
| 41 |
+
examples/allrecipes_search.png filter=lfs diff=lfs merge=lfs -text
|
| 42 |
+
examples/amazon_backpacks.png filter=lfs diff=lfs merge=lfs -text
|
README.md
CHANGED
|
@@ -9,25 +9,44 @@ app_file: app.py
|
|
| 9 |
short_description: Predict where to click on a screenshot from text
|
| 10 |
python_version: "3.12"
|
| 11 |
startup_duration_timeout: 30m
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
---
|
| 13 |
|
| 14 |
# Muse-Glimmer Click Grounding
|
| 15 |
|
| 16 |
-
|
| 17 |
-
a
|
| 18 |
-
|
|
|
|
| 19 |
|
| 20 |
-
## How it
|
| 21 |
|
| 22 |
-
|
| 23 |
-
2. Enter a natural language instruction (e.g., "search for strawberry pretzel salad")
|
| 24 |
-
3. The model returns a JSON object with reasoning and click coordinates (as percentages of image width/height)
|
| 25 |
-
4. A red dot is drawn on the screenshot at the predicted location
|
| 26 |
|
| 27 |
-
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
-
|
| 31 |
|
| 32 |
-
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
short_description: Predict where to click on a screenshot from text
|
| 10 |
python_version: "3.12"
|
| 11 |
startup_duration_timeout: 30m
|
| 12 |
+
models:
|
| 13 |
+
- merve/muse-glimmer-ft-clicking-v2
|
| 14 |
+
- meta-models/Muse-Glimmer-30B
|
| 15 |
+
datasets:
|
| 16 |
+
- merve/MolmoWeb-mini
|
| 17 |
---
|
| 18 |
|
| 19 |
# Muse-Glimmer Click Grounding
|
| 20 |
|
| 21 |
+
Demo of [merve/muse-glimmer-ft-clicking-v2](https://huggingface.co/merve/muse-glimmer-ft-clicking-v2),
|
| 22 |
+
a QLoRA adapter for [meta-models/Muse-Glimmer-30B](https://huggingface.co/meta-models/Muse-Glimmer-30B)
|
| 23 |
+
that points at UI elements in web screenshots: given a screenshot and an instruction such as
|
| 24 |
+
*"filter by MATEIN brand"*, it returns a click point as a percentage of image width/height.
|
| 25 |
|
| 26 |
+
## How it runs
|
| 27 |
|
| 28 |
+
Following the adapter's model card exactly:
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
+
- base model in 4-bit NF4 (bitsandbytes, double quant, bf16 compute), vision tower and
|
| 31 |
+
`lm_head` left unquantized β quantizing the vision tower degrades grounding;
|
| 32 |
+
- LoRA adapter applied with PEFT, `model.eval()` (train mode leaves LoRA dropout active);
|
| 33 |
+
- screenshot resized to a max side of 512px (what it was trained on; the answer is a
|
| 34 |
+
percentage, so the resize is coordinate-invariant);
|
| 35 |
+
- greedy decoding, reasoning strength `low`;
|
| 36 |
+
- only the `to=user` channel is read β coordinates in the `to=self` reasoning channel are
|
| 37 |
+
ones the model already discarded.
|
| 38 |
|
| 39 |
+
The app draws the predicted point on the original, full-resolution screenshot.
|
| 40 |
|
| 41 |
+
## Limitations
|
| 42 |
+
|
| 43 |
+
Per the model card: ~42% strict in-box accuracy on held-out MolmoWeb screenshots, so treat it
|
| 44 |
+
as "roughly the right region" rather than a drop-in click agent. Trained only on desktop web
|
| 45 |
+
screenshots at β€512px; mobile layouts, desktop applications and dense spreadsheets are out of
|
| 46 |
+
distribution. Single click grounding only β no typing, scrolling or multi-step trajectories.
|
| 47 |
+
|
| 48 |
+
## Example screenshots
|
| 49 |
+
|
| 50 |
+
The example screenshots and their instructions are unmodified rows from
|
| 51 |
+
[merve/MolmoWeb-mini](https://huggingface.co/datasets/merve/MolmoWeb-mini), the dataset this
|
| 52 |
+
adapter was fine-tuned on (`filter by MATEIN brand` is the example quoted in the model card).
|
app.py
CHANGED
|
@@ -3,16 +3,25 @@ import os
|
|
| 3 |
os.environ.setdefault("PYTORCH_CUDA_ALLOC_CONF", "expandable_segments:True")
|
| 4 |
|
| 5 |
import spaces # MUST come before any CUDA-touching import
|
|
|
|
| 6 |
import json
|
|
|
|
|
|
|
|
|
|
| 7 |
import torch
|
| 8 |
from PIL import Image, ImageDraw
|
| 9 |
-
import gradio as gr
|
| 10 |
-
from transformers import AutoProcessor, AutoModelForMultimodalLM, BitsAndBytesConfig
|
| 11 |
from peft import PeftModel
|
|
|
|
| 12 |
|
| 13 |
BASE_MODEL = "meta-models/Muse-Glimmer-30B"
|
| 14 |
ADAPTER_MODEL = "merve/muse-glimmer-ft-clicking-v2"
|
| 15 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
PROMPT = (
|
| 17 |
"You are looking at a screenshot of a webpage. Follow this instruction: {question}\n\n"
|
| 18 |
"Respond with a single JSON object of the form "
|
|
@@ -22,22 +31,19 @@ PROMPT = (
|
|
| 22 |
"Output only the JSON object."
|
| 23 |
)
|
| 24 |
|
| 25 |
-
# ββ Model loading at module scope ββββ
|
| 26 |
|
| 27 |
-
print(f"Loading processor from {BASE_MODEL}...")
|
| 28 |
processor = AutoProcessor.from_pretrained(BASE_MODEL)
|
| 29 |
|
| 30 |
-
print(f"Loading base model {BASE_MODEL} in 4-bit
|
| 31 |
bnb_config = BitsAndBytesConfig(
|
| 32 |
load_in_4bit=True,
|
| 33 |
bnb_4bit_compute_dtype=torch.bfloat16,
|
| 34 |
bnb_4bit_use_double_quant=True,
|
| 35 |
bnb_4bit_quant_type="nf4",
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
"model.vision_adapter",
|
| 39 |
-
"lm_head",
|
| 40 |
-
],
|
| 41 |
)
|
| 42 |
model = AutoModelForMultimodalLM.from_pretrained(
|
| 43 |
BASE_MODEL,
|
|
@@ -45,61 +51,110 @@ model = AutoModelForMultimodalLM.from_pretrained(
|
|
| 45 |
quantization_config=bnb_config,
|
| 46 |
device_map="cuda",
|
| 47 |
)
|
| 48 |
-
|
|
|
|
| 49 |
model = PeftModel.from_pretrained(model, ADAPTER_MODEL, torch_device="cpu")
|
|
|
|
| 50 |
model.eval()
|
| 51 |
-
print("Model loaded successfully.")
|
| 52 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
-
|
| 55 |
-
|
|
|
|
| 56 |
annotated = image.copy().convert("RGB")
|
| 57 |
draw = ImageDraw.Draw(annotated)
|
| 58 |
w, h = annotated.size
|
| 59 |
-
cx = int(x_pct / 100 * w)
|
| 60 |
-
cy = int(y_pct / 100 * h)
|
| 61 |
-
radius = max(
|
| 62 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
draw.ellipse(
|
| 64 |
(cx - radius, cy - radius, cx + radius, cy + radius),
|
| 65 |
-
fill=(220,
|
| 66 |
outline=(255, 255, 255),
|
| 67 |
-
width=
|
| 68 |
)
|
| 69 |
return annotated
|
| 70 |
|
| 71 |
|
| 72 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
|
| 74 |
|
| 75 |
-
@spaces.GPU(duration=
|
| 76 |
-
def predict_click(
|
| 77 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
|
| 79 |
Args:
|
| 80 |
-
image: A screenshot of a
|
| 81 |
-
instruction:
|
| 82 |
|
| 83 |
Returns:
|
| 84 |
-
|
| 85 |
-
|
| 86 |
"""
|
| 87 |
if image is None:
|
| 88 |
-
|
| 89 |
-
if not instruction.strip():
|
| 90 |
-
|
|
|
|
|
|
|
| 91 |
|
| 92 |
original = image.convert("RGB")
|
| 93 |
-
# Resize to max 512px for model input (as in the reference implementation)
|
| 94 |
small = original.copy()
|
| 95 |
-
small.thumbnail((
|
| 96 |
|
| 97 |
messages = [
|
| 98 |
{
|
| 99 |
"role": "user",
|
| 100 |
"content": [
|
| 101 |
{"type": "image", "image": small},
|
| 102 |
-
{"type": "text", "text": PROMPT.format(question=instruction)},
|
| 103 |
],
|
| 104 |
}
|
| 105 |
]
|
|
@@ -116,96 +171,103 @@ def predict_click(image, instruction, progress=gr.Progress(track_tqdm=True)):
|
|
| 116 |
add_special_tokens=False,
|
| 117 |
).to(model.device)
|
| 118 |
|
|
|
|
| 119 |
with torch.inference_mode():
|
| 120 |
-
out = model.generate(
|
| 121 |
-
|
| 122 |
-
max_new_tokens=256,
|
| 123 |
-
do_sample=False,
|
| 124 |
-
)
|
| 125 |
|
| 126 |
-
generated = out[0, inputs["input_ids"].shape[-1]:].tolist()
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
generated,
|
| 130 |
-
prefix=tok("<|start|>assistant", add_special_tokens=False)["input_ids"],
|
| 131 |
-
)["content"]
|
| 132 |
|
|
|
|
| 133 |
try:
|
| 134 |
parsed = json.loads(answer)
|
| 135 |
-
action = parsed
|
| 136 |
-
x_pct =
|
| 137 |
-
y_pct =
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
return
|
| 141 |
-
|
| 142 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 143 |
|
| 144 |
|
| 145 |
-
# ββ
|
| 146 |
|
| 147 |
CSS = """
|
| 148 |
-
#col-container { max-width:
|
| 149 |
.dark .gradio-container { color: var(--body-text-color); }
|
| 150 |
"""
|
| 151 |
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
)
|
| 161 |
|
|
|
|
| 162 |
with gr.Column(elem_id="col-container"):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 163 |
with gr.Row():
|
| 164 |
-
with gr.Column(
|
| 165 |
-
input_image = gr.Image(
|
| 166 |
-
label="Screenshot",
|
| 167 |
-
type="pil",
|
| 168 |
-
height=400,
|
| 169 |
-
)
|
| 170 |
instruction = gr.Textbox(
|
| 171 |
label="Instruction",
|
| 172 |
-
placeholder="
|
| 173 |
lines=2,
|
| 174 |
)
|
| 175 |
-
run_btn = gr.Button("
|
|
|
|
|
|
|
|
|
|
|
|
|
| 176 |
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
],
|
| 196 |
-
inputs=[input_image, instruction],
|
| 197 |
-
outputs=[output_image, output_json, raw_output],
|
| 198 |
-
fn=predict_click,
|
| 199 |
-
cache_examples=True,
|
| 200 |
-
cache_mode="lazy",
|
| 201 |
-
)
|
| 202 |
|
| 203 |
-
|
|
|
|
| 204 |
fn=predict_click,
|
| 205 |
inputs=[input_image, instruction],
|
| 206 |
-
outputs=[output_image, output_json, raw_output],
|
| 207 |
api_name="predict_click",
|
| 208 |
)
|
| 209 |
|
| 210 |
if __name__ == "__main__":
|
| 211 |
-
demo.launch(mcp_server=True)
|
|
|
|
| 3 |
os.environ.setdefault("PYTORCH_CUDA_ALLOC_CONF", "expandable_segments:True")
|
| 4 |
|
| 5 |
import spaces # MUST come before any CUDA-touching import
|
| 6 |
+
|
| 7 |
import json
|
| 8 |
+
import time
|
| 9 |
+
|
| 10 |
+
import gradio as gr
|
| 11 |
import torch
|
| 12 |
from PIL import Image, ImageDraw
|
|
|
|
|
|
|
| 13 |
from peft import PeftModel
|
| 14 |
+
from transformers import AutoModelForMultimodalLM, AutoProcessor, BitsAndBytesConfig
|
| 15 |
|
| 16 |
BASE_MODEL = "meta-models/Muse-Glimmer-30B"
|
| 17 |
ADAPTER_MODEL = "merve/muse-glimmer-ft-clicking-v2"
|
| 18 |
|
| 19 |
+
# The adapter was trained on screenshots resized to a max side of 512px; the answer is a
|
| 20 |
+
# percentage of width/height, so the resize is coordinate-invariant.
|
| 21 |
+
MAX_SIDE = 512
|
| 22 |
+
MAX_NEW_TOKENS = 256
|
| 23 |
+
|
| 24 |
+
# Prompt is verbatim from the adapter's model card β the format it was fine-tuned on.
|
| 25 |
PROMPT = (
|
| 26 |
"You are looking at a screenshot of a webpage. Follow this instruction: {question}\n\n"
|
| 27 |
"Respond with a single JSON object of the form "
|
|
|
|
| 31 |
"Output only the JSON object."
|
| 32 |
)
|
| 33 |
|
| 34 |
+
# ββ Model loading at module scope (ZeroGPU packs the weights and streams them to VRAM) ββββ
|
| 35 |
|
| 36 |
+
print(f"Loading processor from {BASE_MODEL}...", flush=True)
|
| 37 |
processor = AutoProcessor.from_pretrained(BASE_MODEL)
|
| 38 |
|
| 39 |
+
print(f"Loading base model {BASE_MODEL} in 4-bit NF4...", flush=True)
|
| 40 |
bnb_config = BitsAndBytesConfig(
|
| 41 |
load_in_4bit=True,
|
| 42 |
bnb_4bit_compute_dtype=torch.bfloat16,
|
| 43 |
bnb_4bit_use_double_quant=True,
|
| 44 |
bnb_4bit_quant_type="nf4",
|
| 45 |
+
# The vision tower shares weight names with the LLM; quantizing it degrades grounding.
|
| 46 |
+
llm_int8_skip_modules=["model.vision_tower", "model.vision_adapter", "lm_head"],
|
|
|
|
|
|
|
|
|
|
| 47 |
)
|
| 48 |
model = AutoModelForMultimodalLM.from_pretrained(
|
| 49 |
BASE_MODEL,
|
|
|
|
| 51 |
quantization_config=bnb_config,
|
| 52 |
device_map="cuda",
|
| 53 |
)
|
| 54 |
+
|
| 55 |
+
print(f"Applying PEFT adapter {ADAPTER_MODEL}...", flush=True)
|
| 56 |
model = PeftModel.from_pretrained(model, ADAPTER_MODEL, torch_device="cpu")
|
| 57 |
+
# .eval() is not optional: in train mode LoRA dropout stays active and generation degrades.
|
| 58 |
model.eval()
|
|
|
|
| 59 |
|
| 60 |
+
_LORA_MODULES = sum(1 for name, _ in model.named_modules() if name.endswith("lora_A.default"))
|
| 61 |
+
print(f"Model ready. LoRA-adapted attention projections: {_LORA_MODULES}", flush=True)
|
| 62 |
+
|
| 63 |
+
_LOGGED_DIAGNOSTICS = False
|
| 64 |
+
|
| 65 |
+
|
| 66 |
+
def _log_adapter_diagnostics() -> None:
|
| 67 |
+
"""One-time check, on the GPU worker, that the LoRA weights really are live."""
|
| 68 |
+
global _LOGGED_DIAGNOSTICS
|
| 69 |
+
if _LOGGED_DIAGNOSTICS:
|
| 70 |
+
return
|
| 71 |
+
_LOGGED_DIAGNOSTICS = True
|
| 72 |
+
for name, param in model.named_parameters():
|
| 73 |
+
if "lora_A" in name:
|
| 74 |
+
print(
|
| 75 |
+
f"[diagnostics] {name}: device={param.device} dtype={param.dtype} "
|
| 76 |
+
f"norm={param.detach().float().norm().item():.4f} "
|
| 77 |
+
f"active_adapters={model.active_adapters}",
|
| 78 |
+
flush=True,
|
| 79 |
+
)
|
| 80 |
+
break
|
| 81 |
|
| 82 |
+
|
| 83 |
+
def _marker(image: Image.Image, x_pct: float, y_pct: float) -> Image.Image:
|
| 84 |
+
"""Draw a crosshair + dot at the predicted click point."""
|
| 85 |
annotated = image.copy().convert("RGB")
|
| 86 |
draw = ImageDraw.Draw(annotated)
|
| 87 |
w, h = annotated.size
|
| 88 |
+
cx = int(round(max(0.0, min(100.0, x_pct)) / 100 * w))
|
| 89 |
+
cy = int(round(max(0.0, min(100.0, y_pct)) / 100 * h))
|
| 90 |
+
radius = max(9, int(min(w, h) * 0.016))
|
| 91 |
+
line = max(2, int(min(w, h) * 0.003))
|
| 92 |
+
|
| 93 |
+
draw.line((cx - radius * 3, cy, cx + radius * 3, cy), fill=(255, 255, 255), width=line + 2)
|
| 94 |
+
draw.line((cx, cy - radius * 3, cx, cy + radius * 3), fill=(255, 255, 255), width=line + 2)
|
| 95 |
+
draw.line((cx - radius * 3, cy, cx + radius * 3, cy), fill=(220, 40, 40), width=line)
|
| 96 |
+
draw.line((cx, cy - radius * 3, cx, cy + radius * 3), fill=(220, 40, 40), width=line)
|
| 97 |
draw.ellipse(
|
| 98 |
(cx - radius, cy - radius, cx + radius, cy + radius),
|
| 99 |
+
fill=(220, 40, 40),
|
| 100 |
outline=(255, 255, 255),
|
| 101 |
+
width=line + 1,
|
| 102 |
)
|
| 103 |
return annotated
|
| 104 |
|
| 105 |
|
| 106 |
+
def _read_user_channel(generated_ids: list[int]) -> str:
|
| 107 |
+
"""Read ONLY the `to=user` channel.
|
| 108 |
+
|
| 109 |
+
The model also emits a `to=self` reasoning channel; numbers scraped from it are
|
| 110 |
+
coordinates it already discarded. The tokenizer carries a `response_template`, so let
|
| 111 |
+
transformers do the parsing.
|
| 112 |
+
"""
|
| 113 |
+
tok = processor.tokenizer
|
| 114 |
+
try:
|
| 115 |
+
prefix = tok("<|start|>assistant", add_special_tokens=False)["input_ids"]
|
| 116 |
+
return tok.parse_response(generated_ids, prefix=prefix)["content"]
|
| 117 |
+
except Exception as exc: # noqa: BLE001 - fall back to a plain decode
|
| 118 |
+
print(f"[warn] parse_response failed ({exc}); falling back to decode", flush=True)
|
| 119 |
+
return tok.decode(generated_ids, skip_special_tokens=True)
|
| 120 |
+
|
| 121 |
+
|
| 122 |
+
# ββ Inference βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 123 |
|
| 124 |
|
| 125 |
+
@spaces.GPU(duration=90)
|
| 126 |
+
def predict_click(
|
| 127 |
+
image: Image.Image,
|
| 128 |
+
instruction: str,
|
| 129 |
+
progress=gr.Progress(track_tqdm=True),
|
| 130 |
+
) -> tuple[Image.Image, dict, str, str]:
|
| 131 |
+
"""Predict where to click on a web screenshot to carry out an instruction.
|
| 132 |
|
| 133 |
Args:
|
| 134 |
+
image: A screenshot of a web page.
|
| 135 |
+
instruction: What to do, e.g. "filter by MATEIN brand".
|
| 136 |
|
| 137 |
Returns:
|
| 138 |
+
The screenshot annotated with the predicted click point, the model's parsed JSON
|
| 139 |
+
action, a short summary of the coordinates, and the raw model answer.
|
| 140 |
"""
|
| 141 |
if image is None:
|
| 142 |
+
raise gr.Error("Please upload a screenshot first.")
|
| 143 |
+
if not instruction or not instruction.strip():
|
| 144 |
+
raise gr.Error("Please describe what should be clicked.")
|
| 145 |
+
|
| 146 |
+
_log_adapter_diagnostics()
|
| 147 |
|
| 148 |
original = image.convert("RGB")
|
|
|
|
| 149 |
small = original.copy()
|
| 150 |
+
small.thumbnail((MAX_SIDE, MAX_SIDE))
|
| 151 |
|
| 152 |
messages = [
|
| 153 |
{
|
| 154 |
"role": "user",
|
| 155 |
"content": [
|
| 156 |
{"type": "image", "image": small},
|
| 157 |
+
{"type": "text", "text": PROMPT.format(question=instruction.strip())},
|
| 158 |
],
|
| 159 |
}
|
| 160 |
]
|
|
|
|
| 171 |
add_special_tokens=False,
|
| 172 |
).to(model.device)
|
| 173 |
|
| 174 |
+
started = time.perf_counter()
|
| 175 |
with torch.inference_mode():
|
| 176 |
+
out = model.generate(**inputs, max_new_tokens=MAX_NEW_TOKENS, do_sample=False)
|
| 177 |
+
elapsed = time.perf_counter() - started
|
|
|
|
|
|
|
|
|
|
| 178 |
|
| 179 |
+
generated = out[0, inputs["input_ids"].shape[-1] :].tolist()
|
| 180 |
+
answer = _read_user_channel(generated)
|
| 181 |
+
print(f"[inference] {elapsed:.2f}s, {len(generated)} tokens -> {answer}", flush=True)
|
|
|
|
|
|
|
|
|
|
| 182 |
|
| 183 |
+
w, h = original.size
|
| 184 |
try:
|
| 185 |
parsed = json.loads(answer)
|
| 186 |
+
action = parsed["action"]
|
| 187 |
+
x_pct = float(action["x"])
|
| 188 |
+
y_pct = float(action["y"])
|
| 189 |
+
except (json.JSONDecodeError, KeyError, TypeError, ValueError) as exc:
|
| 190 |
+
summary = f"Could not parse the model's answer as a click action ({exc})."
|
| 191 |
+
return original, {"error": str(exc), "raw": answer}, summary, answer
|
| 192 |
+
|
| 193 |
+
annotated = _marker(original, x_pct, y_pct)
|
| 194 |
+
x_px, y_px = int(round(x_pct / 100 * w)), int(round(y_pct / 100 * h))
|
| 195 |
+
summary = (
|
| 196 |
+
f"**Click at ({x_pct:g}%, {y_pct:g}%)** β pixel **({x_px}, {y_px})** "
|
| 197 |
+
f"on the {w}Γ{h} screenshot Β· {elapsed:.1f}s"
|
| 198 |
+
)
|
| 199 |
+
return annotated, parsed, summary, answer
|
| 200 |
|
| 201 |
|
| 202 |
+
# ββ UI ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 203 |
|
| 204 |
CSS = """
|
| 205 |
+
#col-container { max-width: 1200px; margin: 0 auto; }
|
| 206 |
.dark .gradio-container { color: var(--body-text-color); }
|
| 207 |
"""
|
| 208 |
|
| 209 |
+
EXAMPLES = [
|
| 210 |
+
["examples/amazon_backpacks.png", "filter by MATEIN brand"],
|
| 211 |
+
["examples/allrecipes_home.png", "search for strawberry pretzel salad"],
|
| 212 |
+
["examples/allrecipes_home.png", "open the Magazine page"],
|
| 213 |
+
["examples/allrecipes_hummus.png", "open the main menu"],
|
| 214 |
+
["examples/allrecipes_search.png", "open the Authentic Enchiladas Verdes recipe"],
|
| 215 |
+
["examples/amazon_backpacks.png", "filter to 4 stars and up"],
|
| 216 |
+
]
|
|
|
|
| 217 |
|
| 218 |
+
with gr.Blocks() as demo:
|
| 219 |
with gr.Column(elem_id="col-container"):
|
| 220 |
+
gr.Markdown(
|
| 221 |
+
"""
|
| 222 |
+
# π±οΈ Muse-Glimmer Click Grounding
|
| 223 |
+
|
| 224 |
+
[merve/muse-glimmer-ft-clicking-v2](https://huggingface.co/merve/muse-glimmer-ft-clicking-v2)
|
| 225 |
+
β a QLoRA adapter on [Muse-Glimmer-30B](https://huggingface.co/meta-models/Muse-Glimmer-30B)
|
| 226 |
+
that points at UI elements in web screenshots. Give it a screenshot and an
|
| 227 |
+
instruction; it answers with a click point as a percentage of width/height.
|
| 228 |
+
"""
|
| 229 |
+
)
|
| 230 |
+
|
| 231 |
with gr.Row():
|
| 232 |
+
with gr.Column():
|
| 233 |
+
input_image = gr.Image(label="Screenshot", type="pil", height=380)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 234 |
instruction = gr.Textbox(
|
| 235 |
label="Instruction",
|
| 236 |
+
placeholder="filter by MATEIN brand",
|
| 237 |
lines=2,
|
| 238 |
)
|
| 239 |
+
run_btn = gr.Button("Find the click point", variant="primary")
|
| 240 |
+
with gr.Column():
|
| 241 |
+
output_image = gr.Image(label="Predicted click point", type="pil", height=380)
|
| 242 |
+
summary = gr.Markdown()
|
| 243 |
+
output_json = gr.JSON(label="Model action")
|
| 244 |
|
| 245 |
+
with gr.Accordion("Raw model answer (the `to=user` channel)", open=False):
|
| 246 |
+
raw_output = gr.Textbox(label="Raw answer", lines=5)
|
| 247 |
+
gr.Markdown(
|
| 248 |
+
"The base model is loaded in 4-bit NF4 (bitsandbytes) with the vision tower "
|
| 249 |
+
"left unquantized, then the LoRA adapter is applied with PEFT. Decoding is "
|
| 250 |
+
"greedy, reasoning strength `low`, input resized to a max side of 512px β the "
|
| 251 |
+
"recipe from the model card."
|
| 252 |
+
)
|
| 253 |
+
|
| 254 |
+
gr.Examples(
|
| 255 |
+
examples=EXAMPLES,
|
| 256 |
+
inputs=[input_image, instruction],
|
| 257 |
+
outputs=[output_image, output_json, summary, raw_output],
|
| 258 |
+
fn=predict_click,
|
| 259 |
+
cache_examples=True,
|
| 260 |
+
cache_mode="lazy",
|
| 261 |
+
label="Examples (screenshots and instructions from merve/MolmoWeb-mini)",
|
| 262 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 263 |
|
| 264 |
+
gr.on(
|
| 265 |
+
triggers=[run_btn.click, instruction.submit],
|
| 266 |
fn=predict_click,
|
| 267 |
inputs=[input_image, instruction],
|
| 268 |
+
outputs=[output_image, output_json, summary, raw_output],
|
| 269 |
api_name="predict_click",
|
| 270 |
)
|
| 271 |
|
| 272 |
if __name__ == "__main__":
|
| 273 |
+
demo.launch(theme=gr.themes.Citrus(), css=CSS, mcp_server=True)
|
examples/{screenshot_1.png β allrecipes_home.png}
RENAMED
|
File without changes
|
examples/{screenshot_2.png β allrecipes_hummus.png}
RENAMED
|
File without changes
|
examples/{screenshot_3.png β allrecipes_search.png}
RENAMED
|
File without changes
|
examples/amazon_backpacks.png
ADDED
|
Git LFS Details
|
requirements.txt
CHANGED
|
@@ -3,4 +3,4 @@ peft
|
|
| 3 |
bitsandbytes
|
| 4 |
accelerate
|
| 5 |
torchvision
|
| 6 |
-
pillow
|
|
|
|
| 3 |
bitsandbytes
|
| 4 |
accelerate
|
| 5 |
torchvision
|
| 6 |
+
pillow
|