|
|
import gradio as gr |
|
|
from transformers import BlipProcessor, BlipForConditionalGeneration |
|
|
from PIL import Image |
|
|
|
|
|
|
|
|
processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base") |
|
|
model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base") |
|
|
|
|
|
|
|
|
def generate_caption(image): |
|
|
inputs = processor(images=image, return_tensors="pt") |
|
|
out = model.generate(**inputs) |
|
|
caption = processor.decode(out[0], skip_special_tokens=True) |
|
|
return caption |
|
|
|
|
|
|
|
|
with gr.Blocks() as demo: |
|
|
gr.Markdown("## 🖼️ Upload an image to generate a caption using BLIP") |
|
|
image_input = gr.Image(type="pil", label="Image") |
|
|
caption_output = gr.Textbox(label="Caption") |
|
|
btn = gr.Button("Generate") |
|
|
btn.click(fn=generate_caption, inputs=image_input, outputs=caption_output) |
|
|
|
|
|
demo.launch() |
|
|
|