| 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') |
|
|
| def caption(img): |
| raw_image = Image.open(img).convert('RGB') |
| |
| inputs = processor(raw_image, return_tensors="pt") |
| |
| out = model.generate(**inputs, min_length=30, max_length=1000) |
| return processor.decode(out[0], skip_special_tokens=True) |
|
|
| def greet(img): |
| return caption(img) |
|
|
| iface = gr.Interface(fn=greet, inputs=gr.Image(type='filepath'), outputs="text") |
| iface.launch() |