Spaces:
Running
Running
import gradio as gr | |
from transformers import BlipProcessor, BlipForConditionalGeneration | |
from PIL import Image | |
import torch | |
# Load the BLIP model and processor | |
processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base") | |
model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base") | |
def generate_caption(image): | |
image = image.convert("RGB") | |
inputs = processor(images=image, return_tensors="pt") | |
with torch.no_grad(): | |
output = model.generate(**inputs) | |
caption = processor.decode(output[0], skip_special_tokens=True) | |
return caption | |
# Gradio interface | |
interface = gr.Interface( | |
fn=generate_caption, | |
inputs=gr.Image(type="pil", label="Upload an Image"), | |
outputs=gr.Textbox(label="Generated Caption"), | |
title="📸 Image Captioning", | |
description="Upload an image and get a descriptive caption using this model.", | |
allow_flagging="never" | |
) | |
if __name__ == "__main__": | |
interface.launch() |