Spaces:
Build error
Build error
Initial app code
Browse files- app.py +26 -0
- requirements.txt +4 -0
app.py
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import AutoProcessor, AutoModelForImageTextToText
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import torch
|
| 5 |
+
|
| 6 |
+
# Load once
|
| 7 |
+
processor = AutoProcessor.from_pretrained("RSRathore/ny-image-captioning")
|
| 8 |
+
model = AutoModelForImageTextToText.from_pretrained("RSRathore/ny-image-captioning")
|
| 9 |
+
model.eval()
|
| 10 |
+
|
| 11 |
+
def generate_caption(image):
|
| 12 |
+
inputs = processor(images=image, return_tensors="pt").to(model.device)
|
| 13 |
+
with torch.no_grad():
|
| 14 |
+
outputs = model.generate(**inputs)
|
| 15 |
+
caption = processor.batch_decode(outputs, skip_special_tokens=True)[0]
|
| 16 |
+
return caption
|
| 17 |
+
|
| 18 |
+
demo = gr.Interface(
|
| 19 |
+
fn=generate_caption,
|
| 20 |
+
inputs=gr.Image(type="pil"),
|
| 21 |
+
outputs="text",
|
| 22 |
+
title="🖼️ New Yorker Caption Generator",
|
| 23 |
+
description="Upload an image and get a caption using the `RSRathore/ny-image-captioning` model."
|
| 24 |
+
)
|
| 25 |
+
|
| 26 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
transformers
|
| 2 |
+
torch
|
| 3 |
+
pillow
|
| 4 |
+
gradio
|