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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"cells": [
|
3 |
+
{
|
4 |
+
"cell_type": "code",
|
5 |
+
"execution_count": 1,
|
6 |
+
"metadata": {},
|
7 |
+
"outputs": [],
|
8 |
+
"source": [
|
9 |
+
"from PIL import Image\n",
|
10 |
+
"import requests\n",
|
11 |
+
"import torch\n",
|
12 |
+
"from torchvision import io\n",
|
13 |
+
"from typing import Dict\n",
|
14 |
+
"from transformers import Qwen2VLForConditionalGeneration, AutoTokenizer, AutoProcessor"
|
15 |
+
]
|
16 |
+
},
|
17 |
+
{
|
18 |
+
"cell_type": "code",
|
19 |
+
"execution_count": 2,
|
20 |
+
"metadata": {},
|
21 |
+
"outputs": [
|
22 |
+
{
|
23 |
+
"data": {
|
24 |
+
"application/vnd.jupyter.widget-view+json": {
|
25 |
+
"model_id": "29ac356cdb05492d8a2da9bceea03b37",
|
26 |
+
"version_major": 2,
|
27 |
+
"version_minor": 0
|
28 |
+
},
|
29 |
+
"text/plain": [
|
30 |
+
"config.json: 0%| | 0.00/1.20k [00:00<?, ?B/s]"
|
31 |
+
]
|
32 |
+
},
|
33 |
+
"metadata": {},
|
34 |
+
"output_type": "display_data"
|
35 |
+
},
|
36 |
+
{
|
37 |
+
"name": "stderr",
|
38 |
+
"output_type": "stream",
|
39 |
+
"text": [
|
40 |
+
"c:\\Users\\Akhil PC\\AppData\\Local\\Programs\\Python\\Python312\\Lib\\site-packages\\huggingface_hub\\file_download.py:157: UserWarning: `huggingface_hub` cache-system uses symlinks by default to efficiently store duplicated files but your machine does not support them in C:\\Users\\Akhil PC\\.cache\\huggingface\\hub\\models--Qwen--Qwen2-VL-2B-Instruct. Caching files will still work but in a degraded version that might require more space on your disk. This warning can be disabled by setting the `HF_HUB_DISABLE_SYMLINKS_WARNING` environment variable. For more details, see https://huggingface.co/docs/huggingface_hub/how-to-cache#limitations.\n",
|
41 |
+
"To support symlinks on Windows, you either need to activate Developer Mode or to run Python as an administrator. In order to see activate developer mode, see this article: https://docs.microsoft.com/en-us/windows/apps/get-started/enable-your-device-for-development\n",
|
42 |
+
" warnings.warn(message)\n",
|
43 |
+
"Unrecognized keys in `rope_scaling` for 'rope_type'='default': {'mrope_section'}\n"
|
44 |
+
]
|
45 |
+
},
|
46 |
+
{
|
47 |
+
"data": {
|
48 |
+
"application/vnd.jupyter.widget-view+json": {
|
49 |
+
"model_id": "3ca08388cd3a4bc58b5b3c84b57fcd7f",
|
50 |
+
"version_major": 2,
|
51 |
+
"version_minor": 0
|
52 |
+
},
|
53 |
+
"text/plain": [
|
54 |
+
"model.safetensors.index.json: 0%| | 0.00/56.4k [00:00<?, ?B/s]"
|
55 |
+
]
|
56 |
+
},
|
57 |
+
"metadata": {},
|
58 |
+
"output_type": "display_data"
|
59 |
+
},
|
60 |
+
{
|
61 |
+
"data": {
|
62 |
+
"application/vnd.jupyter.widget-view+json": {
|
63 |
+
"model_id": "7f667bff4c014fce85cb222f40508c78",
|
64 |
+
"version_major": 2,
|
65 |
+
"version_minor": 0
|
66 |
+
},
|
67 |
+
"text/plain": [
|
68 |
+
"Downloading shards: 0%| | 0/2 [00:00<?, ?it/s]"
|
69 |
+
]
|
70 |
+
},
|
71 |
+
"metadata": {},
|
72 |
+
"output_type": "display_data"
|
73 |
+
},
|
74 |
+
{
|
75 |
+
"data": {
|
76 |
+
"application/vnd.jupyter.widget-view+json": {
|
77 |
+
"model_id": "c4289d2bd8f0466586d20564fb8fef84",
|
78 |
+
"version_major": 2,
|
79 |
+
"version_minor": 0
|
80 |
+
},
|
81 |
+
"text/plain": [
|
82 |
+
"model-00001-of-00002.safetensors: 0%| | 0.00/3.99G [00:00<?, ?B/s]"
|
83 |
+
]
|
84 |
+
},
|
85 |
+
"metadata": {},
|
86 |
+
"output_type": "display_data"
|
87 |
+
},
|
88 |
+
{
|
89 |
+
"data": {
|
90 |
+
"application/vnd.jupyter.widget-view+json": {
|
91 |
+
"model_id": "47d67996509f431abb0f99bab97a03d6",
|
92 |
+
"version_major": 2,
|
93 |
+
"version_minor": 0
|
94 |
+
},
|
95 |
+
"text/plain": [
|
96 |
+
"model-00002-of-00002.safetensors: 0%| | 0.00/429M [00:00<?, ?B/s]"
|
97 |
+
]
|
98 |
+
},
|
99 |
+
"metadata": {},
|
100 |
+
"output_type": "display_data"
|
101 |
+
},
|
102 |
+
{
|
103 |
+
"name": "stderr",
|
104 |
+
"output_type": "stream",
|
105 |
+
"text": [
|
106 |
+
"`Qwen2VLRotaryEmbedding` can now be fully parameterized by passing the model config through the `config` argument. All other arguments will be removed in v4.46\n"
|
107 |
+
]
|
108 |
+
},
|
109 |
+
{
|
110 |
+
"data": {
|
111 |
+
"application/vnd.jupyter.widget-view+json": {
|
112 |
+
"model_id": "d3e49e52f64147e2b5043c76d9a507e6",
|
113 |
+
"version_major": 2,
|
114 |
+
"version_minor": 0
|
115 |
+
},
|
116 |
+
"text/plain": [
|
117 |
+
"Loading checkpoint shards: 0%| | 0/2 [00:00<?, ?it/s]"
|
118 |
+
]
|
119 |
+
},
|
120 |
+
"metadata": {},
|
121 |
+
"output_type": "display_data"
|
122 |
+
},
|
123 |
+
{
|
124 |
+
"data": {
|
125 |
+
"application/vnd.jupyter.widget-view+json": {
|
126 |
+
"model_id": "5060e7d44d5b40fd8ca2d7e90542be21",
|
127 |
+
"version_major": 2,
|
128 |
+
"version_minor": 0
|
129 |
+
},
|
130 |
+
"text/plain": [
|
131 |
+
"generation_config.json: 0%| | 0.00/272 [00:00<?, ?B/s]"
|
132 |
+
]
|
133 |
+
},
|
134 |
+
"metadata": {},
|
135 |
+
"output_type": "display_data"
|
136 |
+
},
|
137 |
+
{
|
138 |
+
"data": {
|
139 |
+
"application/vnd.jupyter.widget-view+json": {
|
140 |
+
"model_id": "ac0500d6289442d88db22065e94c6df2",
|
141 |
+
"version_major": 2,
|
142 |
+
"version_minor": 0
|
143 |
+
},
|
144 |
+
"text/plain": [
|
145 |
+
"preprocessor_config.json: 0%| | 0.00/347 [00:00<?, ?B/s]"
|
146 |
+
]
|
147 |
+
},
|
148 |
+
"metadata": {},
|
149 |
+
"output_type": "display_data"
|
150 |
+
},
|
151 |
+
{
|
152 |
+
"data": {
|
153 |
+
"application/vnd.jupyter.widget-view+json": {
|
154 |
+
"model_id": "99ff45911ba848f2bd3ccd3f57029641",
|
155 |
+
"version_major": 2,
|
156 |
+
"version_minor": 0
|
157 |
+
},
|
158 |
+
"text/plain": [
|
159 |
+
"tokenizer_config.json: 0%| | 0.00/4.19k [00:00<?, ?B/s]"
|
160 |
+
]
|
161 |
+
},
|
162 |
+
"metadata": {},
|
163 |
+
"output_type": "display_data"
|
164 |
+
},
|
165 |
+
{
|
166 |
+
"data": {
|
167 |
+
"application/vnd.jupyter.widget-view+json": {
|
168 |
+
"model_id": "9d484f67779348d7b242a12de0505324",
|
169 |
+
"version_major": 2,
|
170 |
+
"version_minor": 0
|
171 |
+
},
|
172 |
+
"text/plain": [
|
173 |
+
"vocab.json: 0%| | 0.00/2.78M [00:00<?, ?B/s]"
|
174 |
+
]
|
175 |
+
},
|
176 |
+
"metadata": {},
|
177 |
+
"output_type": "display_data"
|
178 |
+
},
|
179 |
+
{
|
180 |
+
"data": {
|
181 |
+
"application/vnd.jupyter.widget-view+json": {
|
182 |
+
"model_id": "b0e6345cf4cd4b61b7d6b10ab7ae6f23",
|
183 |
+
"version_major": 2,
|
184 |
+
"version_minor": 0
|
185 |
+
},
|
186 |
+
"text/plain": [
|
187 |
+
"merges.txt: 0%| | 0.00/1.67M [00:00<?, ?B/s]"
|
188 |
+
]
|
189 |
+
},
|
190 |
+
"metadata": {},
|
191 |
+
"output_type": "display_data"
|
192 |
+
},
|
193 |
+
{
|
194 |
+
"data": {
|
195 |
+
"application/vnd.jupyter.widget-view+json": {
|
196 |
+
"model_id": "c108ffe24eab4d82a8aa8d5bda088bf7",
|
197 |
+
"version_major": 2,
|
198 |
+
"version_minor": 0
|
199 |
+
},
|
200 |
+
"text/plain": [
|
201 |
+
"tokenizer.json: 0%| | 0.00/7.03M [00:00<?, ?B/s]"
|
202 |
+
]
|
203 |
+
},
|
204 |
+
"metadata": {},
|
205 |
+
"output_type": "display_data"
|
206 |
+
},
|
207 |
+
{
|
208 |
+
"data": {
|
209 |
+
"application/vnd.jupyter.widget-view+json": {
|
210 |
+
"model_id": "c9385ab1782f49fcb59fbe2aa73a81c5",
|
211 |
+
"version_major": 2,
|
212 |
+
"version_minor": 0
|
213 |
+
},
|
214 |
+
"text/plain": [
|
215 |
+
"chat_template.json: 0%| | 0.00/1.05k [00:00<?, ?B/s]"
|
216 |
+
]
|
217 |
+
},
|
218 |
+
"metadata": {},
|
219 |
+
"output_type": "display_data"
|
220 |
+
}
|
221 |
+
],
|
222 |
+
"source": [
|
223 |
+
"# Load the model in half-precision on the available device(s)\n",
|
224 |
+
"model = Qwen2VLForConditionalGeneration.from_pretrained(\"Qwen/Qwen2-VL-2B-Instruct\", device_map=\"cpu\", torch_dtype=torch.float16)\n",
|
225 |
+
"processor = AutoProcessor.from_pretrained(\"Qwen/Qwen2-VL-2B-Instruct\")"
|
226 |
+
]
|
227 |
+
},
|
228 |
+
{
|
229 |
+
"cell_type": "code",
|
230 |
+
"execution_count": 3,
|
231 |
+
"metadata": {},
|
232 |
+
"outputs": [],
|
233 |
+
"source": [
|
234 |
+
"# Image\n",
|
235 |
+
"url = \"https://qianwen-res.oss-cn-beijing.aliyuncs.com/Qwen-VL/assets/demo.jpeg\"\n",
|
236 |
+
"image = Image.open(requests.get(url, stream=True).raw)\n",
|
237 |
+
"\n",
|
238 |
+
"conversation = [\n",
|
239 |
+
" {\n",
|
240 |
+
" \"role\":\"user\",\n",
|
241 |
+
" \"content\":[\n",
|
242 |
+
" {\n",
|
243 |
+
" \"type\":\"image\",\n",
|
244 |
+
" },\n",
|
245 |
+
" {\n",
|
246 |
+
" \"type\":\"text\",\n",
|
247 |
+
" \"text\":\"Describe this image.\"\n",
|
248 |
+
" }\n",
|
249 |
+
" ]\n",
|
250 |
+
" }\n",
|
251 |
+
"]"
|
252 |
+
]
|
253 |
+
},
|
254 |
+
{
|
255 |
+
"cell_type": "code",
|
256 |
+
"execution_count": 4,
|
257 |
+
"metadata": {},
|
258 |
+
"outputs": [],
|
259 |
+
"source": [
|
260 |
+
"# Preprocess the inputs\n",
|
261 |
+
"text_prompt = processor.apply_chat_template(conversation, add_generation_prompt=True)"
|
262 |
+
]
|
263 |
+
},
|
264 |
+
{
|
265 |
+
"cell_type": "code",
|
266 |
+
"execution_count": 5,
|
267 |
+
"metadata": {},
|
268 |
+
"outputs": [],
|
269 |
+
"source": [
|
270 |
+
"inputs = processor(text=[text_prompt], images=[image], padding=True, return_tensors=\"pt\")\n",
|
271 |
+
"# inputs = inputs.to('cuda')"
|
272 |
+
]
|
273 |
+
},
|
274 |
+
{
|
275 |
+
"cell_type": "code",
|
276 |
+
"execution_count": null,
|
277 |
+
"metadata": {},
|
278 |
+
"outputs": [],
|
279 |
+
"source": [
|
280 |
+
"# Inference: Generation of the output\n",
|
281 |
+
"output_ids = model.generate(**inputs, max_new_tokens=128)\n",
|
282 |
+
"generated_ids = [output_ids[len(input_ids):] for input_ids, output_ids in zip(inputs.input_ids, output_ids)]"
|
283 |
+
]
|
284 |
+
},
|
285 |
+
{
|
286 |
+
"cell_type": "code",
|
287 |
+
"execution_count": null,
|
288 |
+
"metadata": {},
|
289 |
+
"outputs": [],
|
290 |
+
"source": [
|
291 |
+
"output_text = processor.batch_decode(generated_ids, skip_special_tokens=True, clean_up_tokenization_spaces=True)\n",
|
292 |
+
"print(output_text)"
|
293 |
+
]
|
294 |
+
},
|
295 |
+
{
|
296 |
+
"cell_type": "code",
|
297 |
+
"execution_count": null,
|
298 |
+
"metadata": {},
|
299 |
+
"outputs": [],
|
300 |
+
"source": []
|
301 |
+
}
|
302 |
+
],
|
303 |
+
"metadata": {
|
304 |
+
"kernelspec": {
|
305 |
+
"display_name": "Python 3",
|
306 |
+
"language": "python",
|
307 |
+
"name": "python3"
|
308 |
+
},
|
309 |
+
"language_info": {
|
310 |
+
"codemirror_mode": {
|
311 |
+
"name": "ipython",
|
312 |
+
"version": 3
|
313 |
+
},
|
314 |
+
"file_extension": ".py",
|
315 |
+
"mimetype": "text/x-python",
|
316 |
+
"name": "python",
|
317 |
+
"nbconvert_exporter": "python",
|
318 |
+
"pygments_lexer": "ipython3",
|
319 |
+
"version": "3.12.0"
|
320 |
+
}
|
321 |
+
},
|
322 |
+
"nbformat": 4,
|
323 |
+
"nbformat_minor": 2
|
324 |
+
}
|