Update app.py
Browse files
app.py
CHANGED
|
@@ -10,45 +10,66 @@ captioner = pipeline(
|
|
| 10 |
)
|
| 11 |
|
| 12 |
# --- Caption generation function ---
|
| 13 |
-
def generate_alt_text(files,
|
|
|
|
|
|
|
|
|
|
| 14 |
captions = []
|
| 15 |
for f in files or []:
|
| 16 |
img = Image.open(f.name).convert("RGB")
|
| 17 |
-
#
|
| 18 |
-
|
| 19 |
-
text =
|
| 20 |
captions.append(text)
|
| 21 |
return "\n\n".join(captions)
|
| 22 |
|
| 23 |
# --- Gradio interface ---
|
| 24 |
-
with gr.Blocks(title="Vit
|
| 25 |
-
gr.Markdown("# Vit
|
| 26 |
-
gr.Markdown(f"Using **{MODEL_NAME}** — no
|
| 27 |
|
| 28 |
with gr.Row():
|
| 29 |
-
file_input = gr.File(
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
generate_btn = gr.Button("Generate Captions")
|
| 32 |
-
clear_btn
|
| 33 |
|
| 34 |
gallery = gr.Gallery(label="Image Preview", columns=4)
|
| 35 |
-
output
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
|
|
|
| 37 |
file_input.change(
|
| 38 |
fn=lambda files: [Image.open(f.name).convert("RGB") for f in (files or [])],
|
| 39 |
-
inputs=file_input,
|
|
|
|
| 40 |
)
|
| 41 |
|
|
|
|
| 42 |
generate_btn.click(
|
| 43 |
fn=generate_alt_text,
|
| 44 |
-
inputs=[file_input,
|
| 45 |
outputs=output,
|
| 46 |
queue=True
|
| 47 |
)
|
| 48 |
|
|
|
|
| 49 |
clear_btn.click(
|
| 50 |
fn=lambda: ([], 64, [], ""),
|
| 51 |
-
outputs=[file_input,
|
| 52 |
)
|
| 53 |
|
| 54 |
demo.launch()
|
|
|
|
| 10 |
)
|
| 11 |
|
| 12 |
# --- Caption generation function ---
|
| 13 |
+
def generate_alt_text(files, max_new_tokens):
|
| 14 |
+
"""
|
| 15 |
+
Generate concise, descriptive alt text for uploaded images.
|
| 16 |
+
"""
|
| 17 |
captions = []
|
| 18 |
for f in files or []:
|
| 19 |
img = Image.open(f.name).convert("RGB")
|
| 20 |
+
# Generate caption with specified max_new_tokens and num_beams
|
| 21 |
+
result = captioner(img, max_new_tokens=int(max_new_tokens), num_beams=4)
|
| 22 |
+
text = result[0]["generated_text"].strip().capitalize()
|
| 23 |
captions.append(text)
|
| 24 |
return "\n\n".join(captions)
|
| 25 |
|
| 26 |
# --- Gradio interface ---
|
| 27 |
+
with gr.Blocks(title="Local Vit-GPT2 Image Captioning") as demo:
|
| 28 |
+
gr.Markdown("# Local Vit-GPT2 Image Captioning Demo")
|
| 29 |
+
gr.Markdown(f"Using **{MODEL_NAME}** — no API keys or gated access needed")
|
| 30 |
|
| 31 |
with gr.Row():
|
| 32 |
+
file_input = gr.File(
|
| 33 |
+
label="Upload Images",
|
| 34 |
+
file_count="multiple",
|
| 35 |
+
file_types=["image"]
|
| 36 |
+
)
|
| 37 |
+
max_new_tokens = gr.Slider(
|
| 38 |
+
minimum=16,
|
| 39 |
+
maximum=128,
|
| 40 |
+
step=16,
|
| 41 |
+
value=64,
|
| 42 |
+
label="Max New Tokens"
|
| 43 |
+
)
|
| 44 |
generate_btn = gr.Button("Generate Captions")
|
| 45 |
+
clear_btn = gr.Button("Clear All")
|
| 46 |
|
| 47 |
gallery = gr.Gallery(label="Image Preview", columns=4)
|
| 48 |
+
output = gr.Textbox(
|
| 49 |
+
label="Generated Alt Texts",
|
| 50 |
+
lines=10,
|
| 51 |
+
show_copy_button=True
|
| 52 |
+
)
|
| 53 |
|
| 54 |
+
# Preview images in gallery
|
| 55 |
file_input.change(
|
| 56 |
fn=lambda files: [Image.open(f.name).convert("RGB") for f in (files or [])],
|
| 57 |
+
inputs=file_input,
|
| 58 |
+
outputs=gallery
|
| 59 |
)
|
| 60 |
|
| 61 |
+
# Generate captions
|
| 62 |
generate_btn.click(
|
| 63 |
fn=generate_alt_text,
|
| 64 |
+
inputs=[file_input, max_new_tokens],
|
| 65 |
outputs=output,
|
| 66 |
queue=True
|
| 67 |
)
|
| 68 |
|
| 69 |
+
# Clear all fields
|
| 70 |
clear_btn.click(
|
| 71 |
fn=lambda: ([], 64, [], ""),
|
| 72 |
+
outputs=[file_input, max_new_tokens, gallery, output]
|
| 73 |
)
|
| 74 |
|
| 75 |
demo.launch()
|