SumantBobade's picture
Removed the question part
03c2202 verified
import gradio as gr
from transformers import BlipProcessor, BlipForConditionalGeneration
from PIL import Image
# Load BLIP model and processor
processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base")
def caption_image(image):
"""
Generates a caption for the given image.
"""
try:
# Prepare inputs for image captioning
inputs = processor(images=image, return_tensors="pt")
# Generate caption
outputs = model.generate(**inputs)
caption = processor.decode(outputs[0], skip_special_tokens=True)
return caption
except Exception as e:
return f"An error occurred: {str(e)}"
# Gradio Interface
iface = gr.Interface(
fn=caption_image,
inputs=gr.Image(type="pil"),
outputs="text",
title="Image Captioning with BLIP",
description="Upload an image to generate a descriptive caption."
)
iface.launch()