Update app.py
Browse files
app.py
CHANGED
|
@@ -1,40 +1,40 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
|
|
|
| 3 |
|
| 4 |
# Initialize the Hugging Face image-to-text (captioning) pipeline
|
| 5 |
-
# You can choose a different model like "Salesforce/blip-image-captioning-base" or
|
| 6 |
-
# "nlpconnect/vit-gpt2-image-captioning" for potentially better performance.
|
| 7 |
captioner = pipeline(
|
| 8 |
task="image-to-text",
|
| 9 |
model="nlpconnect/vit-gpt2-image-captioning"
|
| 10 |
)
|
| 11 |
|
| 12 |
-
def generate_alt_text(
|
| 13 |
"""
|
| 14 |
-
Generates alt text captions for a list of
|
|
|
|
| 15 |
"""
|
| 16 |
captions = []
|
| 17 |
-
for
|
|
|
|
|
|
|
|
|
|
| 18 |
result = captioner(img)
|
| 19 |
-
# result is a list of dicts: [{'generated_text': '...'}]
|
| 20 |
text = result[0].get('generated_text') or result[0].get('caption')
|
| 21 |
captions.append(text.strip())
|
| 22 |
-
# Format as numbered list
|
| 23 |
return "\n".join([f"{i+1}. {cap}" for i, cap in enumerate(captions)])
|
| 24 |
|
| 25 |
# Build Gradio interface
|
| 26 |
demo = gr.Blocks(title="Multi-Image Alt Text Generator")
|
| 27 |
-
|
| 28 |
with demo:
|
| 29 |
gr.Markdown("# Alt Text Generator for Multiple Images")
|
| 30 |
gr.Markdown(
|
| 31 |
"Upload one or more images below and click **Generate Alt Text** to get descriptive captions."
|
| 32 |
)
|
| 33 |
-
|
|
|
|
| 34 |
label="Upload Images",
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
tool="upload"
|
| 38 |
)
|
| 39 |
output_box = gr.Textbox(
|
| 40 |
label="Generated Alt Texts",
|
|
@@ -47,9 +47,10 @@ with demo:
|
|
| 47 |
copy_btn = gr.Button("Copy to Clipboard")
|
| 48 |
generate_btn.click(
|
| 49 |
fn=generate_alt_text,
|
| 50 |
-
inputs=
|
| 51 |
outputs=output_box
|
| 52 |
)
|
|
|
|
| 53 |
copy_btn.click(
|
| 54 |
fn=None,
|
| 55 |
inputs=None,
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
| 3 |
+
from PIL import Image
|
| 4 |
|
| 5 |
# Initialize the Hugging Face image-to-text (captioning) pipeline
|
|
|
|
|
|
|
| 6 |
captioner = pipeline(
|
| 7 |
task="image-to-text",
|
| 8 |
model="nlpconnect/vit-gpt2-image-captioning"
|
| 9 |
)
|
| 10 |
|
| 11 |
+
def generate_alt_text(files):
|
| 12 |
"""
|
| 13 |
+
Generates alt text captions for a list of uploaded image files.
|
| 14 |
+
Each file is opened with PIL before captioning.
|
| 15 |
"""
|
| 16 |
captions = []
|
| 17 |
+
for f in files:
|
| 18 |
+
# f may be a tempfile with a .name attribute or a file path string
|
| 19 |
+
path = f.name if hasattr(f, 'name') else f
|
| 20 |
+
img = Image.open(path).convert("RGB")
|
| 21 |
result = captioner(img)
|
|
|
|
| 22 |
text = result[0].get('generated_text') or result[0].get('caption')
|
| 23 |
captions.append(text.strip())
|
|
|
|
| 24 |
return "\n".join([f"{i+1}. {cap}" for i, cap in enumerate(captions)])
|
| 25 |
|
| 26 |
# Build Gradio interface
|
| 27 |
demo = gr.Blocks(title="Multi-Image Alt Text Generator")
|
|
|
|
| 28 |
with demo:
|
| 29 |
gr.Markdown("# Alt Text Generator for Multiple Images")
|
| 30 |
gr.Markdown(
|
| 31 |
"Upload one or more images below and click **Generate Alt Text** to get descriptive captions."
|
| 32 |
)
|
| 33 |
+
# Use File component to handle multiple images
|
| 34 |
+
file_input = gr.File(
|
| 35 |
label="Upload Images",
|
| 36 |
+
file_count="multiple",
|
| 37 |
+
file_types=["image"]
|
|
|
|
| 38 |
)
|
| 39 |
output_box = gr.Textbox(
|
| 40 |
label="Generated Alt Texts",
|
|
|
|
| 47 |
copy_btn = gr.Button("Copy to Clipboard")
|
| 48 |
generate_btn.click(
|
| 49 |
fn=generate_alt_text,
|
| 50 |
+
inputs=file_input,
|
| 51 |
outputs=output_box
|
| 52 |
)
|
| 53 |
+
# JS snippet copies the textbox content to clipboard
|
| 54 |
copy_btn.click(
|
| 55 |
fn=None,
|
| 56 |
inputs=None,
|