balaji4991512 commited on
Commit
97aa4d9
·
verified ·
1 Parent(s): 562ca32

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -0
app.py ADDED
@@ -0,0 +1,42 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import gradio as gr
3
+ from transformers import pipeline
4
+
5
+ # Load the ViT+GPT2 image-to-text pipeline with bfloat16 precision
6
+ captioner = pipeline(
7
+ "image-to-text",
8
+ model="nlpconnect/vit-gpt2-image-captioning",
9
+ torch_dtype=torch.bfloat16
10
+ )
11
+
12
+ def generate_caption(image):
13
+ """
14
+ Takes a PIL image and returns a generated caption.
15
+ """
16
+ outputs = captioner(image)
17
+ return outputs[0]["generated_text"]
18
+
19
+ # Build the Gradio interface
20
+ with gr.Blocks(theme=gr.themes.Default()) as demo:
21
+ gr.Markdown(
22
+ """
23
+ # 🖼️ Image Caption Generator
24
+ Upload an image to generate a descriptive caption using ViT+GPT2.
25
+ """
26
+ )
27
+
28
+ with gr.Row():
29
+ input_image = gr.Image(type="pil", label="Upload Image")
30
+ caption_output = gr.Textbox(label="Generated Caption", lines=2)
31
+
32
+ generate_btn = gr.Button("Generate Caption")
33
+ generate_btn.click(fn=generate_caption, inputs=input_image, outputs=caption_output)
34
+
35
+ gr.Markdown(
36
+ """
37
+ ---
38
+ Built with 🤗 Transformers (`nlpconnect/vit-gpt2-image-captioning`) and 🚀 Gradio
39
+ """
40
+ )
41
+
42
+ demo.launch()