This is the model card of a 🤗 transformers model that has been pushed on the Hub. This model was fine-tuned from the Microsoft Florence-2-base-ft model to specialize in User Interface (UI) understanding and element description.
Florence-2-Wave-UI is a Vision-Language Model (VLM) fine-tuned to accurately understand and describe UI elements within screenshots. By providing an image and a bounding box coordinate of a specific UI element, the model generates a descriptive caption and categorizes the UI element type (e.g., button, text input, dropdown).
The model was trained using parameter-efficient fine-tuning (PEFT/LoRA) and the weights have been merged back into the base model for easy deployment.
The model is focused exclusively on digital user interface elements. It is not designed for:
Users should be aware that the bounding box coordinates must be normalized relative to the image size (0-1000 scale) using the <loc_X> format for the model to interpret them correctly.
Use the code below to get started with the model.
1import torch
2from PIL import Image
3from transformers import AutoModelForCausalLM, AutoProcessor
4
5device = "cuda" if torch.cuda.is_available() else "cpu"
6repo_id = "minhvn4/florence2-wave-ui-lora"
7
8# Load Processor and Model
9processor = AutoProcessor.from_pretrained(repo_id, trust_remote_code=True)
10model = AutoModelForCausalLM.from_pretrained(repo_id, trust_remote_code=True).to(device)
11model.eval()
12
13# Helper function to normalize bounding boxes
14def normalize_bbox(bbox, img_width: int, img_height: int) -> str:
15 x1, y1, x2, y2 = bbox
16 def _norm(v, dim):
17 return min(999, max(0, int((v / dim) * 1000)))
18 return (f"<loc_{_norm(x1, img_width)}><loc_{_norm(y1, img_height)}>"
19 f"<loc_{_norm(x2, img_width)}><loc_{_norm(y2, img_height)}>")
20
21# Prepare Image & Bounding Box
22image = Image.open("your_screenshot.png").convert("RGB")
23img_w, img_h = image.size
24bbox = [100, 200, 300, 400] # [x1, y1, x2, y2]
25
26# Format prompt
27task_prompt = "<UI_DESCRIBE_REGION>"
28prefix = f"{task_prompt}{normalize_bbox(bbox, img_w, img_h)}"
29
30# Inference
31inputs = processor(text=prefix, images=image, return_tensors="pt").to(device)
32with torch.no_grad():
33 generated_ids = model.generate(
34 input_ids=inputs["input_ids"],
35 pixel_values=inputs["pixel_values"],
36 max_new_tokens=256,
37 num_beams=3,
38 repetition_penalty=1.3,
39 early_stopping=True
40 )
41
42generated_text = processor.batch_decode(generated_ids, skip_special_tokens=False)[0]
43# Format cleanup
44generated_text = generated_text.replace(prefix, "").replace("</s>", "").replace("<s>", "").strip()
45
46print(generated_text)
47# Expected Output format: <caption>Description of element</caption><type>UI_Type</type>