Update README.md
Browse files
README.md
CHANGED
@@ -1,13 +1,13 @@
|
|
1 |
-
---
|
2 |
-
library_name: peft
|
3 |
-
license: apache-2.0
|
4 |
-
base_model: HuggingFaceTB/SmolVLM-Base
|
5 |
-
tags:
|
6 |
-
- generated_from_trainer
|
7 |
-
model-index:
|
8 |
-
- name: SmolVLM-Base-vqav2
|
9 |
-
results: []
|
10 |
-
---
|
11 |
|
12 |
<!-- This model card has been generated automatically according to the information the Trainer had access to. You
|
13 |
should probably proofread and complete it, then remove this comment. -->
|
@@ -20,6 +20,87 @@ This model is a fine-tuned version of [HuggingFaceTB/SmolVLM-Base](https://huggi
|
|
20 |
|
21 |
More information needed
|
22 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
## Intended uses & limitations
|
24 |
|
25 |
More information needed
|
|
|
1 |
+
---
|
2 |
+
library_name: peft
|
3 |
+
license: apache-2.0
|
4 |
+
base_model: HuggingFaceTB/SmolVLM-Base
|
5 |
+
tags:
|
6 |
+
- generated_from_trainer
|
7 |
+
model-index:
|
8 |
+
- name: SmolVLM-Base-vqav2
|
9 |
+
results: []
|
10 |
+
---
|
11 |
|
12 |
<!-- This model card has been generated automatically according to the information the Trainer had access to. You
|
13 |
should probably proofread and complete it, then remove this comment. -->
|
|
|
20 |
|
21 |
More information needed
|
22 |
|
23 |
+
```python
|
24 |
+
from transformers import AutoProcessor, AutoModelForImageTextToText
|
25 |
+
from peft import PeftModel
|
26 |
+
import torch
|
27 |
+
from PIL import Image
|
28 |
+
|
29 |
+
|
30 |
+
DEVICE = "cuda:0" if torch.cuda.is_available() else "cpu" ### DEVICE = "cuda:0" instead of DEVICE = "cuda" it fixes flash attention warning!!
|
31 |
+
|
32 |
+
|
33 |
+
model_id = "HuggingFaceTB/SmolVLM-Instruct" # Base Model
|
34 |
+
|
35 |
+
base_model = AutoModelForImageTextToText.from_pretrained(
|
36 |
+
model_id,
|
37 |
+
torch_dtype=torch.bfloat16,
|
38 |
+
_attn_implementation="flash_attention_2" if DEVICE == "cuda" else "eager"
|
39 |
+
).to(DEVICE)
|
40 |
+
|
41 |
+
print(f"Model is on device: {base_model.device}")
|
42 |
+
|
43 |
+
|
44 |
+
# QLoRA adapter
|
45 |
+
adapter_path = r"C:\Users\.....\SmolVLM-Base-vqav2\checkpoint-670"
|
46 |
+
model = PeftModel.from_pretrained(base_model, adapter_path)
|
47 |
+
|
48 |
+
model = model.to(DEVICE) # Check the model device #####################################
|
49 |
+
|
50 |
+
# Load the processor
|
51 |
+
processor = AutoProcessor.from_pretrained(model_id)
|
52 |
+
|
53 |
+
# Functıon for load images from local
|
54 |
+
def load_image_from_file(file_path):
|
55 |
+
try:
|
56 |
+
image = Image.open(file_path)
|
57 |
+
return image
|
58 |
+
except Exception as e:
|
59 |
+
print(f"Error loading image: {e}")
|
60 |
+
return None
|
61 |
+
|
62 |
+
|
63 |
+
image1_path = "C:/Users/.../IMG_4.jpg"
|
64 |
+
image2_path = "C:/Users/.../IMG_35.jpg"
|
65 |
+
|
66 |
+
# Load images
|
67 |
+
image1 = load_image_from_file(image1_path)
|
68 |
+
image2 = load_image_from_file(image2_path)
|
69 |
+
|
70 |
+
# Check the images
|
71 |
+
if image1 and image2:
|
72 |
+
|
73 |
+
# Create message type
|
74 |
+
messages = [
|
75 |
+
{
|
76 |
+
"role": "user",
|
77 |
+
"content": [
|
78 |
+
{"type": "image"},
|
79 |
+
{"type": "image"},
|
80 |
+
{"type": "text", "text": "Can you describe and compare the two images?"}
|
81 |
+
]
|
82 |
+
},
|
83 |
+
]
|
84 |
+
|
85 |
+
# Prepare the Prompt
|
86 |
+
|
87 |
+
prompt = processor.apply_chat_template(messages, add_generation_prompt=True)
|
88 |
+
inputs = processor(text=prompt, images=[image1, image2], return_tensors="pt")
|
89 |
+
inputs = inputs.to(DEVICE)
|
90 |
+
|
91 |
+
# Run the model
|
92 |
+
generated_ids = model.generate(**inputs, max_new_tokens=500)
|
93 |
+
generated_texts = processor.batch_decode(generated_ids, skip_special_tokens=True)
|
94 |
+
|
95 |
+
# Print the result
|
96 |
+
print(generated_texts[0]) # Çıktı
|
97 |
+
|
98 |
+
else:
|
99 |
+
print("Images can not be loaded")
|
100 |
+
|
101 |
+
|
102 |
+
```
|
103 |
+
|
104 |
## Intended uses & limitations
|
105 |
|
106 |
More information needed
|