Spaces:
Sleeping
Sleeping
File size: 1,086 Bytes
abfa5ba 98723e0 abfa5ba 98723e0 abfa5ba 98723e0 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
import torch
from transformers import BlipProcessor, BlipForConditionalGeneration
import gradio as gr
from PIL import Image
# Load the AI Image Captioning Model
device = "cuda" if torch.cuda.is_available() else "cpu"
processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base").to(device)
# Define Caption Generation Function
def generate_caption(image):
if isinstance(image, Image.Image) and image.mode != "RGB":
image = image.convert("RGB")
inputs = processor(image, return_tensors="pt").to(device)
caption = model.generate(**inputs)
return processor.decode(caption[0], skip_special_tokens=True)
# Gradio Interface
iface = gr.Interface(
fn=generate_caption,
inputs=gr.Image(type="pil"),
outputs=gr.Textbox(label="Generated Caption"),
title="🖼️ Auto Image Captioning",
description="Upload an image, and let AI caption it with BLIP!"
)
# Launch Gradio App (no share=True needed for Hugging Face Spaces)
iface.launch() |