Views
No views yet
1from transformers import Qwen2_5_VLForConditionalGeneration, Qwen2_5_VLProcessor
2from qwen_vl_utils import process_vision_info
3from PIL import Image
4import torch, re
5
6model = Qwen2_5_VLForConditionalGeneration.from_pretrained(
7 "miketes/wave-ui-7b", torch_dtype=torch.bfloat16, device_map="auto"
8)
9processor = Qwen2_5_VLProcessor.from_pretrained("miketes/wave-ui-7b")
10image = Image.open("screenshot.png").convert("RGB")
11
12messages = [{
13 "role": "user",
14 "content": [
15 {"type": "image", "image": image},
16 {"type": "text", "text": 'Where is the "login button"? Return the bounding box.'},
17 ],
18}]
19
20text = processor.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
21image_inputs, _ = process_vision_info(messages)
22inputs = processor(text=[text], images=image_inputs, return_tensors="pt").to(model.device)
23
24with torch.inference_mode():
25 out = model.generate(**inputs, max_new_tokens=64, do_sample=False)
26
27result = processor.decode(out[0][inputs["input_ids"].shape[1]:], skip_special_tokens=True)
28coords = re.findall(r'\d+', result)
29bbox = [int(x) for x in coords[:4]] if len(coords) >= 4 else None
30print(bbox) # [678, 99, 772, 138]