14 lines
602 B
Python
Raw Normal View History

import requests
import torch
from PIL import Image
from transformers import BlipForConditionalGeneration
from transformers import BlipProcessor
2023-02-15 01:38:21 +02:00
processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
2023-02-15 00:03:19 -03:00
model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base", torch_dtype=torch.float32).to("cpu")
def caption_image(raw_image):
2023-02-15 00:03:19 -03:00
inputs = processor(raw_image.convert('RGB'), return_tensors="pt").to("cpu", torch.float32)
out = model.generate(**inputs, max_new_tokens=100)
return processor.decode(out[0], skip_special_tokens=True)