Spaces:
Sleeping

wqerer's picture
Create app.py
3e04ac9 verified
Raw History Blame Contribute Delete
850 Bytes
from transformers import BlipProcessor, BlipForConditionalGeneration
import gradio as gr
from PIL import Image
import torch
# Load model
processor = BlipProcessor.from_pretrained(
"Salesforce/blip-image-captioning-base"
)
model = BlipForConditionalGeneration.from_pretrained(
"Salesforce/blip-image-captioning-base"
)
# Caption function
def generate_caption(image):
inputs = processor(images=image, return_tensors="pt")
output = model.generate(**inputs)
caption = processor.decode(
output[0],
skip_special_tokens=True
)
return caption
# Interface
demo = gr.Interface(
fn=generate_caption,
inputs=gr.Image(type="pil"),
outputs=gr.Textbox(label="Generated Caption"),
title="AI Image Caption Generator",
description="Upload an image and AI will generate a caption."
)
demo.launch()