wjbmattingly
commited on
Create README.md
Browse files
README.md
ADDED
@@ -0,0 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
datasets:
|
3 |
+
- CATMuS/medieval
|
4 |
+
base_model:
|
5 |
+
- Qwen/Qwen2-VL-2B-Instruct
|
6 |
+
---
|
7 |
+
```
|
8 |
+
import torch
|
9 |
+
from transformers import Qwen2VLForConditionalGeneration, AutoTokenizer, AutoProcessor
|
10 |
+
from qwen_vl_utils import process_vision_info
|
11 |
+
|
12 |
+
|
13 |
+
device = "cuda" if torch.cuda.is_available() else "mps"
|
14 |
+
|
15 |
+
model_dir = "medieval-data/qwen2-vl-2b-catmus"
|
16 |
+
|
17 |
+
|
18 |
+
|
19 |
+
model = Qwen2VLForConditionalGeneration.from_pretrained(
|
20 |
+
model_dir, torch_dtype="auto", device_map="auto"
|
21 |
+
)
|
22 |
+
|
23 |
+
processor = AutoProcessor.from_pretrained("Qwen/Qwen2-VL-2B-Instruct")
|
24 |
+
image_url ="""https://datasets-server.huggingface.co/cached-assets/CATMuS/medieval/--/76c4e4124476cced0b7b487421313450cf646ce8/--/default/test/851/im/image.jpg?Expires=1726188562&Signature=rdeGLGZfuXA0e93VngajlOGZ~4RUz3W6HYe84u27vHd~X502-O0gDiT8y39mJeYyUyQOf9wXs~mlXDaT8ugP62f4gcKEEaqikBHhhbIFHYgCy48NKzJXx4bPRCND1T6JrBotOfY3LUy6XP7PNcv7e5cAXQPeGoEHH4VcU6Bt~~mLg~oD2qYzKwKQ7PcFmIYAk-4igi0MZNUuScw6dpCe9CY2aCgvJeGb3ZZySbb~9Tn7ij7p7ouG2DMVurKCsm8tMIwLrzAAv2UEl4WE0aSVFk9Rm-zPiH3qRwzElLi7FNn6BzRYmm9WPW6wuRdTGweJxDrPjBi3Roy3B~jqk4hryg__&Key-Pair-Id=K3EI6M078Z3AC3"""
|
25 |
+
messages = [
|
26 |
+
{
|
27 |
+
"role": "user",
|
28 |
+
"content": [
|
29 |
+
{
|
30 |
+
"type": "image",
|
31 |
+
"image": image_url,
|
32 |
+
},
|
33 |
+
{"type": "text", "text": "Convert this image to text."},
|
34 |
+
],
|
35 |
+
}
|
36 |
+
]
|
37 |
+
|
38 |
+
# Preparation for inference
|
39 |
+
text = processor.apply_chat_template(
|
40 |
+
messages, tokenize=False, add_generation_prompt=True
|
41 |
+
)
|
42 |
+
image_inputs, video_inputs = process_vision_info(messages)
|
43 |
+
inputs = processor(
|
44 |
+
text=[text],
|
45 |
+
images=image_inputs,
|
46 |
+
videos=video_inputs,
|
47 |
+
padding=True,
|
48 |
+
return_tensors="pt",
|
49 |
+
)
|
50 |
+
inputs = inputs.to("mps")
|
51 |
+
|
52 |
+
# Inference: Generation of the output
|
53 |
+
generated_ids = model.generate(**inputs, max_new_tokens=4000)
|
54 |
+
generated_ids_trimmed = [
|
55 |
+
out_ids[len(in_ids) :] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)
|
56 |
+
]
|
57 |
+
output_text = processor.batch_decode(
|
58 |
+
generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False
|
59 |
+
)
|
60 |
+
|
61 |
+
print(output_text)
|
62 |
+
# Import required libraries if not already imported
|
63 |
+
from IPython.display import display, Image
|
64 |
+
|
65 |
+
# Display the output text
|
66 |
+
print(output_text)
|
67 |
+
|
68 |
+
# Display the image
|
69 |
+
display(Image(url=image_url))
|
70 |
+
```
|