jadechoghari
commited on
Update README.md
Browse files
README.md
CHANGED
@@ -3,159 +3,49 @@ library_name: transformers
|
|
3 |
pipeline_tag: image-text-to-text
|
4 |
---
|
5 |
|
6 |
-
|
|
|
|
|
7 |
|
8 |
-
Please download and save `builder.py`, `conversation.py` locally.
|
9 |
|
10 |
-
|
11 |
-
import torch
|
12 |
-
from PIL import Image
|
13 |
-
from conversation import conv_templates
|
14 |
-
from builder import load_pretrained_model # Assuming this is your custom model loader
|
15 |
-
from functools import partial
|
16 |
-
import numpy as np
|
17 |
-
|
18 |
-
# define the task categories
|
19 |
-
box_in_tasks = ['widgetcaptions', 'taperception', 'ocr', 'icon_recognition', 'widget_classification', 'example_0']
|
20 |
-
box_out_tasks = ['widget_listing', 'find_text', 'find_icons', 'find_widget', 'conversation_interaction']
|
21 |
-
no_box_tasks = ['screen2words', 'detailed_description', 'conversation_perception', 'gpt4']
|
22 |
-
|
23 |
-
# function to generate the mask
|
24 |
-
def generate_mask_for_feature(coor, raw_w, raw_h, mask=None):
|
25 |
-
"""
|
26 |
-
Generates a region mask based on provided coordinates.
|
27 |
-
Handles both point and box input.
|
28 |
-
"""
|
29 |
-
if mask is not None:
|
30 |
-
assert mask.shape[0] == raw_w and mask.shape[1] == raw_h
|
31 |
-
coor_mask = np.zeros((raw_w, raw_h))
|
32 |
-
|
33 |
-
# if it's a point (2 coordinates)
|
34 |
-
if len(coor) == 2:
|
35 |
-
span = 5 # Define the span for the point
|
36 |
-
x_min = max(0, coor[0] - span)
|
37 |
-
x_max = min(raw_w, coor[0] + span + 1)
|
38 |
-
y_min = max(0, coor[1] - span)
|
39 |
-
y_max = min(raw_h, coor[1] + span + 1)
|
40 |
-
coor_mask[int(x_min):int(x_max), int(y_min):int(y_max)] = 1
|
41 |
-
assert (coor_mask == 1).any(), f"coor: {coor}, raw_w: {raw_w}, raw_h: {raw_h}"
|
42 |
|
43 |
-
|
44 |
-
elif len(coor) == 4:
|
45 |
-
coor_mask[coor[0]:coor[2]+1, coor[1]:coor[3]+1] = 1
|
46 |
-
if mask is not None:
|
47 |
-
coor_mask = coor_mask * mask
|
48 |
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
return coor_mask
|
54 |
-
```
|
55 |
-
### Now, define the infer function
|
56 |
-
```python
|
57 |
-
def infer_single_prompt(image_path, prompt, model_path, region=None, model_name="ferret_gemma", conv_mode="ferret_gemma_instruct"):
|
58 |
-
img = Image.open(image_path).convert('RGB')
|
59 |
-
|
60 |
-
# this loads the model, image processor and tokenizer
|
61 |
-
tokenizer, model, image_processor, context_len = load_pretrained_model(model_path, None, model_name)
|
62 |
-
|
63 |
-
# define the image size (e.g., 224x224 or 336x336)
|
64 |
-
image_size = {"height": 336, "width": 336}
|
65 |
-
|
66 |
-
# process the image
|
67 |
-
image_tensor = image_processor.preprocess(
|
68 |
-
img,
|
69 |
-
return_tensors='pt',
|
70 |
-
do_resize=True,
|
71 |
-
do_center_crop=False,
|
72 |
-
size=(image_size['height'], image_size['width'])
|
73 |
-
)['pixel_values'][0].unsqueeze(0)
|
74 |
-
|
75 |
-
image_tensor = image_tensor.half().cuda()
|
76 |
-
|
77 |
-
# generate the prompt per template requirement
|
78 |
-
conv = conv_templates[conv_mode].copy()
|
79 |
-
conv.append_message(conv.roles[0], prompt)
|
80 |
-
conv.append_message(conv.roles[1], None)
|
81 |
-
prompt_input = conv.get_prompt()
|
82 |
-
|
83 |
-
# tokenize prompt
|
84 |
-
input_ids = tokenizer(prompt_input, return_tensors='pt')['input_ids'].cuda()
|
85 |
-
|
86 |
-
# region mask logic (if region is provided)
|
87 |
-
region_masks = None
|
88 |
-
if region is not None:
|
89 |
-
raw_w, raw_h = img.size
|
90 |
-
region_masks = generate_mask_for_feature(region, raw_w, raw_h).unsqueeze(0).cuda().half()
|
91 |
-
region_masks = [[region_masks]] # Wrap the mask in lists as expected by the model
|
92 |
-
|
93 |
-
# generate model output
|
94 |
-
with torch.inference_mode():
|
95 |
-
# Use region_masks in model's forward call
|
96 |
-
model.orig_forward = model.forward
|
97 |
-
model.forward = partial(
|
98 |
-
model.orig_forward,
|
99 |
-
region_masks=region_masks
|
100 |
-
)
|
101 |
-
output_ids = model.generate(
|
102 |
-
input_ids,
|
103 |
-
images=image_tensor,
|
104 |
-
max_new_tokens=1024,
|
105 |
-
num_beams=1,
|
106 |
-
region_masks=region_masks, # pass the region mask to the model
|
107 |
-
image_sizes=[img.size]
|
108 |
-
)
|
109 |
-
model.forward = model.orig_forward
|
110 |
-
|
111 |
-
# we decode the output
|
112 |
-
output_text = tokenizer.batch_decode(output_ids, skip_special_tokens=True)[0]
|
113 |
-
return output_text.strip()
|
114 |
-
```
|
115 |
-
|
116 |
-
# We also define a task-specific inference function
|
117 |
-
```python
|
118 |
-
def infer_ui_task(image_path, prompt, model_path, task, region=None):
|
119 |
-
"""
|
120 |
-
Handles task types: box_in_tasks, box_out_tasks, no_box_tasks.
|
121 |
-
"""
|
122 |
-
if task in box_in_tasks and region is None:
|
123 |
-
raise ValueError(f"Task {task} requires a bounding box region.")
|
124 |
-
|
125 |
-
if task in box_in_tasks:
|
126 |
-
print(f"Processing {task} with bounding box region.")
|
127 |
-
return infer_single_prompt(image_path, prompt, model_path, region)
|
128 |
-
|
129 |
-
elif task in box_out_tasks:
|
130 |
-
print(f"Processing {task} without bounding box region.")
|
131 |
-
return infer_single_prompt(image_path, prompt, model_path)
|
132 |
-
|
133 |
-
elif task in no_box_tasks:
|
134 |
-
print(f"Processing {task} without image or bounding box.")
|
135 |
-
return infer_single_prompt(image_path, prompt, model_path)
|
136 |
-
|
137 |
-
else:
|
138 |
-
raise ValueError(f"Unknown task type: {task}")
|
139 |
```
|
140 |
|
141 |
### Usage:
|
142 |
```python
|
143 |
-
|
|
|
144 |
image_path = 'image.jpg'
|
145 |
-
model_path = 'jadechoghari/
|
|
|
146 |
|
147 |
-
|
148 |
-
task
|
|
|
|
|
149 |
region = (50, 50, 200, 200)
|
150 |
result = infer_ui_task(image_path, "Describe the contents of the box.", model_path, task, region=region)
|
151 |
print("Result:", result)
|
|
|
152 |
|
153 |
-
|
|
|
|
|
154 |
task = 'conversation_interaction'
|
155 |
result = infer_ui_task(image_path, "How do I navigate to the Games tab?", model_path, task)
|
156 |
print("Result:", result)
|
|
|
157 |
|
158 |
-
|
|
|
|
|
159 |
task = 'detailed_description'
|
160 |
result = infer_ui_task(image_path, "Please describe the screen in detail.", model_path, task)
|
161 |
print("Result:", result)
|
|
|
3 |
pipeline_tag: image-text-to-text
|
4 |
---
|
5 |
|
6 |
+
Ferret-UI is the first UI-centric multimodal large language model (MLLM) designed for referring, grounding, and reasoning tasks.
|
7 |
+
Built on Gemma-2B and Llama-3-8B, it is capable of executing complex UI tasks.
|
8 |
+
This is the Gemma-2B version of ferret-ui. It follows from [this paper](https://arxiv.org/pdf/2404.05719) by Apple.
|
9 |
|
|
|
10 |
|
11 |
+
## How to Use the *Ferret-UI-Gemma2b* Model
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
|
13 |
+
You will need first to download `builder.py`, `conversation.py`, and `inference.py` locally.
|
|
|
|
|
|
|
|
|
14 |
|
15 |
+
```bash
|
16 |
+
wget https://huggingface.co/jadechoghari/ferret-gemma/raw/main/conversation.py
|
17 |
+
wget https://huggingface.co/jadechoghari/ferret-gemma/raw/main/builder.py
|
18 |
+
wget https://huggingface.co/jadechoghari/ferret-gemma/raw/main/inference.py
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
```
|
20 |
|
21 |
### Usage:
|
22 |
```python
|
23 |
+
from inference import infer_ui_task
|
24 |
+
# Pass an image and the online model path
|
25 |
image_path = 'image.jpg'
|
26 |
+
model_path = 'jadechoghari/Ferret-UI-Gemma2b'
|
27 |
+
```
|
28 |
|
29 |
+
### Task requiring bounding box
|
30 |
+
Choose a task from ['widgetcaptions', 'taperception', 'ocr', 'icon_recognition', 'widget_classification', 'example_0']
|
31 |
+
```python
|
32 |
+
task = 'widgetcaptions'
|
33 |
region = (50, 50, 200, 200)
|
34 |
result = infer_ui_task(image_path, "Describe the contents of the box.", model_path, task, region=region)
|
35 |
print("Result:", result)
|
36 |
+
```
|
37 |
|
38 |
+
### Task not requiring bounding box
|
39 |
+
Choose a task from ['widget_listing', 'find_text', 'find_icons', 'find_widget', 'conversation_interaction']
|
40 |
+
```python
|
41 |
task = 'conversation_interaction'
|
42 |
result = infer_ui_task(image_path, "How do I navigate to the Games tab?", model_path, task)
|
43 |
print("Result:", result)
|
44 |
+
```
|
45 |
|
46 |
+
### Task with no image processing
|
47 |
+
Choose a task from ['screen2words', 'detailed_description', 'conversation_perception', 'gpt4']
|
48 |
+
```python
|
49 |
task = 'detailed_description'
|
50 |
result = infer_ui_task(image_path, "Please describe the screen in detail.", model_path, task)
|
51 |
print("Result:", result)
|