|
import gradio as gr |
|
from transformers import BlipProcessor, BlipForConditionalGeneration |
|
from PIL import Image |
|
import torch |
|
|
|
|
|
processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-large") |
|
model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-large") |
|
|
|
def caption_image(image): |
|
|
|
inputs = processor(images=image, return_tensors="pt") |
|
|
|
|
|
out = model.generate(**inputs) |
|
|
|
|
|
caption = processor.decode(out[0], skip_special_tokens=True) |
|
|
|
return caption |
|
|
|
|
|
interface = gr.Interface( |
|
fn=caption_image, |
|
inputs=gr.Image(type="pil"), |
|
outputs="text", |
|
title="Image Captioning", |
|
description="Generate captions for images using the BLIP model." |
|
) |
|
|
|
|
|
interface.launch() |
|
|