Views
No views yet
1from transformers import Qwen2_5_VLForConditionalGeneration, AutoProcessor
2from qwen_vl_utils import process_vision_info
3from PIL import Image
4import torch
5
6model_name = "BLR2/qwen2.5-vl-3b-ui-grounding-step-2000"
7
8processor = AutoProcessor.from_pretrained(model_name)
9model = Qwen2_5_VLForConditionalGeneration.from_pretrained(
10 model_name,
11 torch_dtype=torch.bfloat16,
12 device_map="auto",
13)
14
15image = Image.open("screenshot.png").convert("RGB")
16
17messages = [
18 {
19 "role": "user",
20 "content": [
21 {"type": "image", "image": image},
22 {"type": "text", "text": "Given this UI screenshot, predict the center of: 'Submit button'."},
23 ],
24 },
25]
26
27text = processor.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
28img, vid = process_vision_info(messages)
29
30inputs = processor(text=[text], images=img, videos=None, padding=True, return_tensors="pt")
31inputs = {k: v.to(model.device) for k, v in inputs.items()}
32
33with torch.no_grad():
34 generated_ids = model.generate(**inputs, max_new_tokens=128, do_sample=False)
35
36generated_ids_trimmed = generated_ids[0][len(inputs["input_ids"][0]):]
37response = processor.decode(generated_ids_trimmed, skip_special_tokens=True)
38print(response) # Output: "0.7532 0.8921" (x, y coordinates)x y where both values are in the range [0, 1]:x: horizontal position (0 = left, 1 = right)y: vertical position (0 = top, 1 = bottom)