File size: 855 Bytes
7dc0229
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1989b7d
 
7dc0229
 
 
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
import gradio as gr
from transformers import AutoProcessor, AutoModelForImageTextToText
from PIL import Image
import torch

# Load once
processor = AutoProcessor.from_pretrained("RSRathore/ny-image-captioning")
model = AutoModelForImageTextToText.from_pretrained("RSRathore/ny-image-captioning")
model.eval()

def generate_caption(image):
    inputs = processor(images=image, return_tensors="pt").to(model.device)
    with torch.no_grad():
        outputs = model.generate(**inputs)
    caption = processor.batch_decode(outputs, skip_special_tokens=True)[0]
    return caption

demo = gr.Interface(
    fn=generate_caption,
    inputs=gr.Image(type="pil"),
    outputs="text",
    title="🖼️ Comical Caption Generator",
    description="Upload an image (png or jpg) and get a caption using the `RSRathore/ny-image-captioning` model."
)

demo.launch()