Views
No views yet
1from transformers import Qwen2VLForConditionalGeneration, AutoProcessor
2from peft import PeftModel
3import torch
4from PIL import Image
5
6# Load base model and processor
7base_model = Qwen2VLForConditionalGeneration.from_pretrained("Qwen/Qwen2.5-VL-7B-Instruct")
8processor = AutoProcessor.from_pretrained("Qwen/Qwen2.5-VL-7B-Instruct")
9
10# Load LoRA adapter
11model = PeftModel.from_pretrained(base_model, "your-username/arabic-image-captioning-qwen2.5vl")
12
13# Process image and generate caption
14image = Image.open("your_image.jpg")
15prompt = "اكتب وصفاً مختصراً لهذه الصورة باللغة العربية"
16
17inputs = processor(images=image, text=prompt, return_tensors="pt")
18with torch.no_grad():
19 outputs = model.generate(**inputs, max_new_tokens=128)
20
21caption = processor.decode(outputs[0], skip_special_tokens=True)
22print(caption)