Views
No views yet
pip install transformers peft torch pillow qwen-vl-utils1from transformers import Qwen2VLForConditionalGeneration, AutoProcessor
2from peft import PeftModel
3import torch
4
5# Load base model
6base_model = Qwen2VLForConditionalGeneration.from_pretrained(
7 "Qwen/Qwen3-VL-8B-Instruct",
8 torch_dtype=torch.bfloat16,
9 device_map="auto"
10)
11
12# Load LoRA adapter
13model = PeftModel.from_pretrained(
14 base_model,
15 "openhay/qwen3vl-8b-lora",
16 torch_dtype=torch.bfloat16
17)
18
19# Load processor
20processor = AutoProcessor.from_pretrained("Qwen/Qwen3-VL-8B-Instruct")1from qwen_vl_utils import process_vision_info
2from PIL import Image
3
4# Prepare messages
5messages = [
6 {
7 "role": "user",
8 "content": [
9 {"type": "image", "image": "path/to/image.jpg"},
10 {"type": "text", "text": "Describe this image in detail."},
11 ],
12 }
13]
14
15# Prepare for inference
16text = processor.apply_chat_template(
17 messages, tokenize=False, add_generation_prompt=True
18)
19image_inputs, video_inputs = process_vision_info(messages)
20inputs = processor(
21 text=[text],
22 images=image_inputs,
23 videos=video_inputs,
24 padding=True,
25 return_tensors="pt",
26)
27inputs = inputs.to("cuda")
28
29# Generate
30with torch.no_grad():
31 generated_ids = model.generate(**inputs, max_new_tokens=512)
32
33generated_ids_trimmed = [
34 out_ids[len(in_ids):] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)
35]
36output_text = processor.batch_decode(
37 generated_ids_trimmed,
38 skip_special_tokens=True,
39 clean_up_tokenization_spaces=False
40)
41
42print(output_text[0])1from transformers import Qwen2VLForConditionalGeneration
2from peft import PeftModel
3
4# Load base model and adapter
5base_model = Qwen2VLForConditionalGeneration.from_pretrained(
6 "Qwen/Qwen3-VL-8B-Instruct",
7 torch_dtype=torch.bfloat16,
8 device_map="auto"
9)
10model = PeftModel.from_pretrained(base_model, "openhay/qwen3vl-8b-lora")
11
12# Merge and save
13merged_model = model.merge_and_unload()
14merged_model.save_pretrained("./merged_model")1@misc{qwen3vl_8b_lora,
2 author = {OpenHay},
3 title = {qwen3vl-8b-lora},
4 year = {2025},
5 publisher = {HuggingFace},
6 howpublished = {\url{https://huggingface.co/openhay/qwen3vl-8b-lora}}
7}