Update app.py
Browse files
app.py
CHANGED
|
@@ -3,16 +3,21 @@ from transformers import pipeline
|
|
| 3 |
from PIL import Image
|
| 4 |
import torch
|
| 5 |
|
| 6 |
-
#
|
| 7 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
|
|
|
|
|
|
| 8 |
captioner = pipeline(
|
| 9 |
task="image-to-text",
|
| 10 |
model="nlpconnect/vit-gpt2-image-captioning",
|
| 11 |
-
device=0 if device=="cuda" else -1
|
| 12 |
)
|
| 13 |
|
| 14 |
-
# Core generation function
|
| 15 |
def generate_alt_text(files):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
captions = []
|
| 17 |
for f in files:
|
| 18 |
path = f.name if hasattr(f, 'name') else f
|
|
@@ -25,38 +30,44 @@ def generate_alt_text(files):
|
|
| 25 |
captions.append(f"Error processing {path}: {str(e)}")
|
| 26 |
return "\n".join([f"{i+1}. {cap}" for i, cap in enumerate(captions)])
|
| 27 |
|
| 28 |
-
# Build Gradio interface
|
| 29 |
-
|
|
|
|
| 30 |
gr.Markdown("# Reliable Alt Text Generator")
|
| 31 |
gr.Markdown(
|
| 32 |
-
"Upload multiple images, preview them, and
|
| 33 |
)
|
| 34 |
-
# File upload
|
| 35 |
file_input = gr.File(
|
| 36 |
label="Upload Images",
|
| 37 |
file_count="multiple",
|
| 38 |
file_types=["image"]
|
| 39 |
)
|
| 40 |
-
|
| 41 |
-
|
|
|
|
| 42 |
with gr.Row():
|
| 43 |
generate_btn = gr.Button("Generate Alt Text")
|
| 44 |
clear_btn = gr.Button("Clear All")
|
| 45 |
copy_btn = gr.Button("Copy to Clipboard")
|
| 46 |
-
# Output
|
| 47 |
output = gr.Textbox(
|
| 48 |
label="Generated Alt Texts",
|
| 49 |
lines=10,
|
| 50 |
elem_id="output_box"
|
| 51 |
)
|
| 52 |
-
|
|
|
|
| 53 |
file_input.upload(lambda files: files, inputs=file_input, outputs=preview)
|
| 54 |
generate_btn.click(fn=generate_alt_text, inputs=file_input, outputs=output)
|
| 55 |
clear_btn.click(lambda: ([], [], ""), inputs=None, outputs=[file_input, preview, output])
|
| 56 |
-
copy_btn.click(
|
| 57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
demo.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
|
| 3 |
from PIL import Image
|
| 4 |
import torch
|
| 5 |
|
| 6 |
+
# Set up device (GPU if available)
|
| 7 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 8 |
+
|
| 9 |
+
# Initialize a reliable image-to-text captioning pipeline
|
| 10 |
captioner = pipeline(
|
| 11 |
task="image-to-text",
|
| 12 |
model="nlpconnect/vit-gpt2-image-captioning",
|
| 13 |
+
device=0 if device == "cuda" else -1 # GPU=0, CPU=-1
|
| 14 |
)
|
| 15 |
|
|
|
|
| 16 |
def generate_alt_text(files):
|
| 17 |
+
"""
|
| 18 |
+
Takes a list of uploaded image files, generates alt-text captions,
|
| 19 |
+
and handles errors gracefully.
|
| 20 |
+
"""
|
| 21 |
captions = []
|
| 22 |
for f in files:
|
| 23 |
path = f.name if hasattr(f, 'name') else f
|
|
|
|
| 30 |
captions.append(f"Error processing {path}: {str(e)}")
|
| 31 |
return "\n".join([f"{i+1}. {cap}" for i, cap in enumerate(captions)])
|
| 32 |
|
| 33 |
+
# Build the Gradio interface
|
| 34 |
+
demo = gr.Blocks(title="Reliable Alt‑Text Generator")
|
| 35 |
+
with demo:
|
| 36 |
gr.Markdown("# Reliable Alt Text Generator")
|
| 37 |
gr.Markdown(
|
| 38 |
+
"Upload multiple images, preview them below, and generate accurate alt-text captions."
|
| 39 |
)
|
| 40 |
+
# File upload for multiple images
|
| 41 |
file_input = gr.File(
|
| 42 |
label="Upload Images",
|
| 43 |
file_count="multiple",
|
| 44 |
file_types=["image"]
|
| 45 |
)
|
| 46 |
+
# Preview gallery (4 columns)
|
| 47 |
+
preview = gr.Gallery(label="Image Preview", columns=4)
|
| 48 |
+
# Buttons
|
| 49 |
with gr.Row():
|
| 50 |
generate_btn = gr.Button("Generate Alt Text")
|
| 51 |
clear_btn = gr.Button("Clear All")
|
| 52 |
copy_btn = gr.Button("Copy to Clipboard")
|
| 53 |
+
# Output textbox
|
| 54 |
output = gr.Textbox(
|
| 55 |
label="Generated Alt Texts",
|
| 56 |
lines=10,
|
| 57 |
elem_id="output_box"
|
| 58 |
)
|
| 59 |
+
|
| 60 |
+
# Event bindings
|
| 61 |
file_input.upload(lambda files: files, inputs=file_input, outputs=preview)
|
| 62 |
generate_btn.click(fn=generate_alt_text, inputs=file_input, outputs=output)
|
| 63 |
clear_btn.click(lambda: ([], [], ""), inputs=None, outputs=[file_input, preview, output])
|
| 64 |
+
copy_btn.click(
|
| 65 |
+
fn=None,
|
| 66 |
+
inputs=None,
|
| 67 |
+
outputs=None,
|
| 68 |
+
js="() => { navigator.clipboard.writeText(document.getElementById('output_box').value); }"
|
| 69 |
+
)
|
| 70 |
|
| 71 |
+
# Enable concurrency and launch
|
| 72 |
+
demo.queue(concurrency_count=3, max_size=10)
|
| 73 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|
|
|