Spaces:
Runtime error
Runtime error
| 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)) |