File size: 1,145 Bytes
8adb430
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c9096f7
985af09
605acfb
8df61fb
8adb430
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import gradio as gr

import requests
from PIL import Image
from transformers import BlipProcessor, BlipForConditionalGeneration

processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-large")
model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-large")

img_url = 'https://storage.googleapis.com/sfr-vision-language-research/BLIP/demo.jpg' 
raw_image = Image.open(requests.get(img_url, stream=True).raw).convert('RGB')

# conditional image captioning
text = "a photography of"
inputs = processor(raw_image, text, return_tensors="pt")

# out = model.generate(**inputs, clean_up_tokenization_spaces=True, max_length = 2400)
decoded_output = model.generate(inputs, clean_up_tokenization_spaces=True)
out = model.generate(**inputs, max_length = 2400) # clean_up_tokenization_spaces=True)
decoded_output = tokenizer.decode(out, clean_up_tokenization_spaces=True)
print(processor.decode(out[0], skip_special_tokens=True))

# unconditional image captioning
inputs = processor(raw_image, return_tensors="pt")

out = model.generate(**inputs)
print(processor.decode(out[0], skip_special_tokens=True))