Update app.py
Browse files
app.py
CHANGED
|
@@ -1,46 +1,44 @@
|
|
| 1 |
-
import os
|
| 2 |
import gradio as gr
|
| 3 |
-
from
|
| 4 |
from PIL import Image
|
| 5 |
|
| 6 |
-
# ---
|
| 7 |
-
#
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
repo_id=REPO_ID,
|
| 14 |
-
token=HF_TOKEN,
|
| 15 |
)
|
| 16 |
|
| 17 |
-
# ---
|
| 18 |
-
def generate_alt_text(
|
| 19 |
"""
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
"""
|
| 22 |
captions = []
|
| 23 |
-
for
|
| 24 |
-
#
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
inputs=img_bytes,
|
| 30 |
-
params={"max_new_tokens": int(max_new_tokens)},
|
| 31 |
-
request_format="binary"
|
| 32 |
-
)
|
| 33 |
-
# Extract and clean the generated text
|
| 34 |
-
text = response[0].get("generated_text", "").strip()
|
| 35 |
-
if text:
|
| 36 |
-
text = text[0].upper() + text[1:]
|
| 37 |
captions.append(text)
|
| 38 |
return "\n\n".join(captions)
|
| 39 |
|
| 40 |
-
# --- Gradio
|
| 41 |
-
with gr.Blocks(title="
|
| 42 |
-
gr.Markdown("#
|
| 43 |
-
gr.Markdown(
|
|
|
|
|
|
|
| 44 |
|
| 45 |
with gr.Row():
|
| 46 |
file_input = gr.File(
|
|
@@ -48,38 +46,42 @@ with gr.Blocks(title="Qwen2.5-VL-7B-Instruct Alt Text Generator") as demo:
|
|
| 48 |
file_count="multiple",
|
| 49 |
file_types=["image"]
|
| 50 |
)
|
| 51 |
-
|
| 52 |
minimum=16,
|
| 53 |
-
maximum=
|
| 54 |
step=16,
|
| 55 |
-
value=
|
| 56 |
-
label="Max
|
| 57 |
)
|
| 58 |
-
generate_btn = gr.Button("Generate
|
| 59 |
clear_btn = gr.Button("Clear All")
|
| 60 |
|
| 61 |
gallery = gr.Gallery(label="Image Preview", columns=4)
|
| 62 |
-
output = gr.Textbox(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
|
| 64 |
-
#
|
| 65 |
file_input.change(
|
| 66 |
-
fn=lambda
|
| 67 |
inputs=file_input,
|
| 68 |
outputs=gallery
|
| 69 |
)
|
| 70 |
|
| 71 |
-
# Generate
|
| 72 |
generate_btn.click(
|
| 73 |
fn=generate_alt_text,
|
| 74 |
-
inputs=[file_input,
|
| 75 |
outputs=output,
|
| 76 |
queue=True
|
| 77 |
)
|
| 78 |
|
| 79 |
-
# Clear all
|
| 80 |
clear_btn.click(
|
| 81 |
-
fn=lambda: ([],
|
| 82 |
-
outputs=[file_input,
|
| 83 |
)
|
| 84 |
|
| 85 |
demo.launch()
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
| 3 |
from PIL import Image
|
| 4 |
|
| 5 |
+
# --- Setup local image-to-text pipeline ---
|
| 6 |
+
# No authentication or gated models required
|
| 7 |
+
MODEL_NAME = "nlpconnect/vit-gpt2-image-captioning"
|
| 8 |
+
captioner = pipeline(
|
| 9 |
+
"image-to-text",
|
| 10 |
+
model=MODEL_NAME,
|
| 11 |
+
return_full_text=False
|
|
|
|
|
|
|
| 12 |
)
|
| 13 |
|
| 14 |
+
# --- Caption generation function ---
|
| 15 |
+
def generate_alt_text(files, max_length):
|
| 16 |
"""
|
| 17 |
+
Generate concise, descriptive alt text for uploaded images.
|
| 18 |
+
|
| 19 |
+
Args:
|
| 20 |
+
files (List[UploadedFile]): List of uploaded image files
|
| 21 |
+
max_length (int): Maximum length of the generated caption
|
| 22 |
+
|
| 23 |
+
Returns:
|
| 24 |
+
str: Alt text for each image, separated by blank lines
|
| 25 |
"""
|
| 26 |
captions = []
|
| 27 |
+
for f in files or []:
|
| 28 |
+
# Load image
|
| 29 |
+
img = Image.open(f.name).convert("RGB")
|
| 30 |
+
# Generate caption
|
| 31 |
+
result = captioner(img, max_length=int(max_length), num_beams=4)
|
| 32 |
+
text = result[0]["generated_text"].strip().capitalize()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
captions.append(text)
|
| 34 |
return "\n\n".join(captions)
|
| 35 |
|
| 36 |
+
# --- Gradio interface ---
|
| 37 |
+
with gr.Blocks(title="Local Vit-GPT2 Image Captioning") as demo:
|
| 38 |
+
gr.Markdown("# Local Vit-GPT2 Image Captioning Demo")
|
| 39 |
+
gr.Markdown(
|
| 40 |
+
f"Using **{MODEL_NAME}** — no API keys or gated access needed"
|
| 41 |
+
)
|
| 42 |
|
| 43 |
with gr.Row():
|
| 44 |
file_input = gr.File(
|
|
|
|
| 46 |
file_count="multiple",
|
| 47 |
file_types=["image"]
|
| 48 |
)
|
| 49 |
+
max_length = gr.Slider(
|
| 50 |
minimum=16,
|
| 51 |
+
maximum=128,
|
| 52 |
step=16,
|
| 53 |
+
value=64,
|
| 54 |
+
label="Max Caption Length"
|
| 55 |
)
|
| 56 |
+
generate_btn = gr.Button("Generate Captions")
|
| 57 |
clear_btn = gr.Button("Clear All")
|
| 58 |
|
| 59 |
gallery = gr.Gallery(label="Image Preview", columns=4)
|
| 60 |
+
output = gr.Textbox(
|
| 61 |
+
label="Generated Alt Texts",
|
| 62 |
+
lines=10,
|
| 63 |
+
show_copy_button=True
|
| 64 |
+
)
|
| 65 |
|
| 66 |
+
# Update gallery when files are selected
|
| 67 |
file_input.change(
|
| 68 |
+
fn=lambda files: [Image.open(f.name).convert("RGB") for f in (files or [])],
|
| 69 |
inputs=file_input,
|
| 70 |
outputs=gallery
|
| 71 |
)
|
| 72 |
|
| 73 |
+
# Generate captions on button click
|
| 74 |
generate_btn.click(
|
| 75 |
fn=generate_alt_text,
|
| 76 |
+
inputs=[file_input, max_length],
|
| 77 |
outputs=output,
|
| 78 |
queue=True
|
| 79 |
)
|
| 80 |
|
| 81 |
+
# Clear all fields
|
| 82 |
clear_btn.click(
|
| 83 |
+
fn=lambda: ([], 64, [], ""),
|
| 84 |
+
outputs=[file_input, max_length, gallery, output]
|
| 85 |
)
|
| 86 |
|
| 87 |
demo.launch()
|