Spaces:
Build error
Build error
| 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() | |