mateoluksenberg commited on
Commit
f53527d
Β·
verified Β·
1 Parent(s): 53b03cb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +63 -0
app.py CHANGED
@@ -95,6 +95,69 @@ def run_example(image, text_input=None, model_id="mateoluksenberg/Qwen-modelo-im
95
  output_text = processor.batch_decode(
96
  generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False
97
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
98
 
99
  return output_text[0]
100
 
 
95
  output_text = processor.batch_decode(
96
  generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False
97
  )
98
+
99
+ "---------------"
100
+ from transformers import Qwen2VLForConditionalGeneration, AutoTokenizer, AutoProcessor
101
+ from qwen_vl_utils import process_vision_info
102
+
103
+ # default: Load the model on the available device(s)
104
+ model = Qwen2VLForConditionalGeneration.from_pretrained(
105
+ "mateoluksenberg/Qwen-modelo-image", torch_dtype="auto", device_map="auto"
106
+ )
107
+
108
+ # We recommend enabling flash_attention_2 for better acceleration and memory saving, especially in multi-image and video scenarios.
109
+ # model = Qwen2VLForConditionalGeneration.from_pretrained(
110
+ # "Qwen/Qwen2-VL-2B-Instruct",
111
+ # torch_dtype=torch.bfloat16,
112
+ # attn_implementation="flash_attention_2",
113
+ # device_map="auto",
114
+ # )
115
+
116
+ # default processer
117
+ processor = AutoProcessor.from_pretrained("mateoluksenberg/Qwen-modelo-image")
118
+
119
+ # The default range for the number of visual tokens per image in the model is 4-16384. You can set min_pixels and max_pixels according to your needs, such as a token count range of 256-1280, to balance speed and memory usage.
120
+ # min_pixels = 256*28*28
121
+ # max_pixels = 1280*28*28
122
+ # processor = AutoProcessor.from_pretrained("Qwen/Qwen2-VL-2B-Instruct", min_pixels=min_pixels, max_pixels=max_pixels)
123
+
124
+ messages = [
125
+ {
126
+ "role": "user",
127
+ "content": [
128
+ {
129
+ "type": "image",
130
+ "image": image_path,
131
+ },
132
+ {"type": "text", "text": "Describe this image."},
133
+ ],
134
+ }
135
+ ]
136
+
137
+ # Preparation for inference
138
+ text = processor.apply_chat_template(
139
+ messages, tokenize=False, add_generation_prompt=True
140
+ )
141
+ image_inputs, video_inputs = process_vision_info(messages)
142
+ inputs = processor(
143
+ text=[text],
144
+ images=image_inputs,
145
+ videos=video_inputs,
146
+ padding=True,
147
+ return_tensors="pt",
148
+ )
149
+ inputs = inputs.to("cuda")
150
+
151
+ # Inference: Generation of the output
152
+ generated_ids = model.generate(**inputs, max_new_tokens=128)
153
+ generated_ids_trimmed = [
154
+ out_ids[len(in_ids) :] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)
155
+ ]
156
+ output_text = processor.batch_decode(
157
+ generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False
158
+ )
159
+ print(output_text)
160
+ "---------------"
161
 
162
  return output_text[0]
163