from PIL import Image import requests from transformers import Blip2Processor, Blip2Model, Blip2ForConditionalGeneration import torch device = "cuda" if torch.cuda.is_available() else "cpu" processor = Blip2Processor.from_pretrained("Salesforce/blip2-flan-t5-xxl", load_in_8bit=True, device_map="auto") model = Blip2ForConditionalGeneration.from_pretrained("Salesforce/blip2-flan-t5-xxl", load_in_8bit=True, device_map="auto") url = "http://images.cocodataset.org/val2017/000000039769.jpg" image = Image.open(requests.get(url, stream=True).raw) inputs = processor(image, return_tensors="pt").to(device, torch.float16) out = model.generate(**inputs, max_length=64, min_length=20) print(i,': ',processor.decode(out[0], skip_special_tokens=True))