Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import pipeline
|
3 |
+
from PIL import Image
|
4 |
+
|
5 |
+
# Load model
|
6 |
+
captioner = pipeline("image-to-text", model="nlpconnect/vit-gpt2-image-captioning")
|
7 |
+
|
8 |
+
# Inference function
|
9 |
+
def generate_caption(image):
|
10 |
+
result = captioner(image)
|
11 |
+
return result[0]["generated_text"]
|
12 |
+
|
13 |
+
# Gradio UI
|
14 |
+
iface = gr.Interface(
|
15 |
+
fn=generate_caption,
|
16 |
+
inputs=gr.Image(type="pil"),
|
17 |
+
outputs="text",
|
18 |
+
title="🖼️ Image Caption Generator",
|
19 |
+
description="Upload an image and the model will describe it in a sentence.",
|
20 |
+
)
|
21 |
+
|
22 |
+
iface.launch()
|