Add model usage examples
Browse files
README.md
CHANGED
@@ -1,3 +1,78 @@
|
|
1 |
-
---
|
2 |
-
license: apache-2.0
|
3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
license: apache-2.0
|
3 |
+
language:
|
4 |
+
- zh
|
5 |
+
base_model:
|
6 |
+
- meta-llama/Llama-3.2-11B-Vision-Instruct
|
7 |
+
tags:
|
8 |
+
- llama
|
9 |
+
- lora
|
10 |
+
- chinese
|
11 |
+
- zh
|
12 |
+
---
|
13 |
+
|
14 |
+
# Llama-3.2-Vision-chinese-lora
|
15 |
+
- base model: [meta-llama/Llama-3.2-11B-Vision-Instruct](https://huggingface.co/meta-llama/Llama-3.2-11B-Vision-Instruct)
|
16 |
+
|
17 |
+
## Use with transformers
|
18 |
+
```python
|
19 |
+
import torch
|
20 |
+
from transformers import MllamaForConditionalGeneration, AutoProcessor
|
21 |
+
from peft import PeftModel
|
22 |
+
from PIL import Image
|
23 |
+
|
24 |
+
# Base model ID and LoRA model ID
|
25 |
+
base_model_id = "meta-llama/Llama-3.2-11B-Vision-Instruct"
|
26 |
+
lora_model_id = "Kadins/Llama-3.2-Vision-chinese-lora"
|
27 |
+
|
28 |
+
# Load the processor
|
29 |
+
processor = AutoProcessor.from_pretrained(base_model_id)
|
30 |
+
|
31 |
+
# Load the base model
|
32 |
+
base_model = MllamaForConditionalGeneration.from_pretrained(
|
33 |
+
base_model_id,
|
34 |
+
device_map="auto",
|
35 |
+
torch_dtype=torch.float16 # Use torch.bfloat16 if your hardware supports it
|
36 |
+
).eval()
|
37 |
+
|
38 |
+
# Load the LoRA model and apply it to the base model
|
39 |
+
model = PeftModel.from_pretrained(base_model, lora_model_id)
|
40 |
+
|
41 |
+
# Optionally, merge the LoRA weights with the base model for faster inference
|
42 |
+
model = model.merge_and_unload()
|
43 |
+
|
44 |
+
# Load an example image (replace 'path_to_image.jpg' with your image file)
|
45 |
+
image_path = 'path_to_image.jpg'
|
46 |
+
image = Image.open(image_path)
|
47 |
+
|
48 |
+
# User prompt in Chinese
|
49 |
+
user_prompt = "请描述这张图片。"
|
50 |
+
|
51 |
+
# Prepare the content with the image and text
|
52 |
+
content = [
|
53 |
+
{"type": "image", "image": image},
|
54 |
+
{"type": "text", "text": user_prompt}
|
55 |
+
]
|
56 |
+
|
57 |
+
# Apply the chat template to create the prompt
|
58 |
+
prompt = processor.apply_chat_template(
|
59 |
+
[{"role": "user", "content": content}],
|
60 |
+
add_generation_prompt=True
|
61 |
+
)
|
62 |
+
|
63 |
+
# Prepare the inputs for the model
|
64 |
+
inputs = processor(
|
65 |
+
images=image,
|
66 |
+
text=prompt,
|
67 |
+
return_tensors="pt"
|
68 |
+
).to(model.device)
|
69 |
+
|
70 |
+
# Generate the model's response
|
71 |
+
output = model.generate(**inputs, max_new_tokens=512)
|
72 |
+
|
73 |
+
# Decode the output to get the assistant's response
|
74 |
+
response = processor.decode(output[0], skip_special_tokens=True)
|
75 |
+
|
76 |
+
# Print the assistant's response
|
77 |
+
print("Assistant:", response)
|
78 |
+
```
|