Spaces:
Sleeping
Sleeping
import gradio as gr | |
from PIL import Image | |
import requests | |
from transformers import BlipProcessor, BlipForConditionalGeneration | |
processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base") | |
model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base").to("cpu") | |
def generate_caption(image_url): | |
raw_image = Image.open(requests.get(image_url, stream=True).raw).convert('RGB') | |
# Unconditional image captioning | |
inputs = processor(raw_image, return_tensors="pt").to("cpu") | |
out = model.generate(**inputs) | |
caption = processor.decode(out[0], skip_special_tokens=True) | |
return caption | |
# Define the Gradio interface | |
iface = gr.Interface( | |
fn=generate_caption, | |
inputs="text", # URL input | |
outputs="text", # Caption output | |
title="Image Captioning with BLIP", | |
description="Provide an image URL, and the model will generate a caption." | |
) | |
if __name__ == "__main__": | |
iface.launch(share=True) | |