Update app.py
Browse files
app.py
CHANGED
|
@@ -1,108 +1,79 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from transformers import
|
| 3 |
from PIL import Image
|
| 4 |
-
import torch
|
| 5 |
-
import easyocr
|
| 6 |
|
| 7 |
-
# ---
|
| 8 |
-
|
| 9 |
-
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
)
|
| 16 |
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
ocr_reader = easyocr.Reader(['en'], gpu=(device == "cuda"))
|
| 25 |
-
detector = pipeline(
|
| 26 |
-
"object-detection",
|
| 27 |
-
model="facebook/detr-resnet-50",
|
| 28 |
-
device=0 if device == "cuda" else -1
|
| 29 |
-
)
|
| 30 |
-
|
| 31 |
-
# --- Core Generation Function ---
|
| 32 |
-
def generate_alt_text(files, max_tokens):
|
| 33 |
captions = []
|
| 34 |
for f in files or []:
|
| 35 |
-
#
|
| 36 |
img = Image.open(f.name).convert("RGB")
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
visible_text = ' '.join(txts).strip()
|
| 42 |
-
except Exception:
|
| 43 |
-
visible_text = ''
|
| 44 |
-
|
| 45 |
-
# object context
|
| 46 |
-
try:
|
| 47 |
-
objs = detector(img)
|
| 48 |
-
labels = list({o['label'] for o in objs[:5]})
|
| 49 |
-
objects_text = ', '.join(labels)
|
| 50 |
-
except Exception:
|
| 51 |
-
objects_text = ''
|
| 52 |
-
|
| 53 |
-
# build prompt
|
| 54 |
-
prompt = "Generate a concise, descriptive alt text for this image."
|
| 55 |
-
if visible_text:
|
| 56 |
-
prompt += f" Include visible text: '{visible_text}'."
|
| 57 |
-
if objects_text:
|
| 58 |
-
prompt += f" Mention objects: {objects_text}."
|
| 59 |
-
|
| 60 |
-
# BLIP‑2 captioning
|
| 61 |
-
inputs = processor(images=img, text=prompt, return_tensors="pt").to(device)
|
| 62 |
-
outputs = model.generate(
|
| 63 |
-
**inputs,
|
| 64 |
-
max_new_tokens=int(max_tokens),
|
| 65 |
-
num_beams=4,
|
| 66 |
-
no_repeat_ngram_size=2
|
| 67 |
-
)
|
| 68 |
-
caption = processor.decode(outputs[0], skip_special_tokens=True).strip().capitalize()
|
| 69 |
-
captions.append(caption)
|
| 70 |
-
|
| 71 |
return "\n\n".join(captions)
|
| 72 |
|
| 73 |
# --- Gradio Interface ---
|
| 74 |
-
with gr.Blocks(title="
|
| 75 |
-
gr.Markdown("#
|
| 76 |
-
gr.Markdown("
|
| 77 |
|
| 78 |
with gr.Row():
|
| 79 |
-
file_input = gr.File(
|
| 80 |
-
|
| 81 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
clear_btn = gr.Button("Clear All")
|
| 83 |
|
| 84 |
gallery = gr.Gallery(label="Image Preview", columns=4)
|
| 85 |
-
output = gr.Textbox(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
|
| 87 |
-
#
|
| 88 |
file_input.change(
|
| 89 |
fn=lambda files: [Image.open(f.name).convert("RGB") for f in (files or [])],
|
| 90 |
inputs=file_input,
|
| 91 |
outputs=gallery
|
| 92 |
)
|
| 93 |
|
|
|
|
| 94 |
generate_btn.click(
|
| 95 |
fn=generate_alt_text,
|
| 96 |
-
inputs=[file_input,
|
| 97 |
outputs=output,
|
| 98 |
queue=True
|
| 99 |
)
|
| 100 |
|
|
|
|
| 101 |
clear_btn.click(
|
| 102 |
fn=lambda: ([], 64, [], ""),
|
| 103 |
-
outputs=[file_input,
|
| 104 |
)
|
| 105 |
|
| 106 |
-
demo.
|
| 107 |
-
|
| 108 |
-
demo.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
| 3 |
from PIL import Image
|
|
|
|
|
|
|
| 4 |
|
| 5 |
+
# --- Model Setup ---
|
| 6 |
+
# Swap in any image-to-text model from Hugging Face
|
| 7 |
+
MODEL_NAME = "nlpconnect/vit-gpt2-image-captioning"
|
| 8 |
|
| 9 |
+
# Create the pipeline (no trust_remote_code needed)
|
| 10 |
+
captioner = pipeline(
|
| 11 |
+
"image-to-text",
|
| 12 |
+
model=MODEL_NAME
|
| 13 |
)
|
| 14 |
|
| 15 |
+
# --- Core Function ---
|
| 16 |
+
def generate_alt_text(files, max_length):
|
| 17 |
+
"""
|
| 18 |
+
Generate captions for a list of uploaded image files.
|
| 19 |
+
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
captions = []
|
| 21 |
for f in files or []:
|
| 22 |
+
# Load and preprocess image
|
| 23 |
img = Image.open(f.name).convert("RGB")
|
| 24 |
+
# Run the pipeline with a max token length
|
| 25 |
+
result = captioner(img, max_length=int(max_length))
|
| 26 |
+
text = result[0]["generated_text"].strip().capitalize()
|
| 27 |
+
captions.append(text)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
return "\n\n".join(captions)
|
| 29 |
|
| 30 |
# --- Gradio Interface ---
|
| 31 |
+
with gr.Blocks(title="Image-to-Text Captioning Demo") as demo:
|
| 32 |
+
gr.Markdown("# Image-to-Text Captioning Demo")
|
| 33 |
+
gr.Markdown(f"Using **{MODEL_NAME}** model from Hugging Face.")
|
| 34 |
|
| 35 |
with gr.Row():
|
| 36 |
+
file_input = gr.File(
|
| 37 |
+
label="Upload Images",
|
| 38 |
+
file_count="multiple",
|
| 39 |
+
file_types=["image"]
|
| 40 |
+
)
|
| 41 |
+
max_length = gr.Slider(
|
| 42 |
+
minimum=16,
|
| 43 |
+
maximum=128,
|
| 44 |
+
step=16,
|
| 45 |
+
value=64,
|
| 46 |
+
label="Max Caption Length"
|
| 47 |
+
)
|
| 48 |
+
generate_btn = gr.Button("Generate Captions")
|
| 49 |
clear_btn = gr.Button("Clear All")
|
| 50 |
|
| 51 |
gallery = gr.Gallery(label="Image Preview", columns=4)
|
| 52 |
+
output = gr.Textbox(
|
| 53 |
+
label="Generated Captions",
|
| 54 |
+
lines=10,
|
| 55 |
+
show_copy_button=True
|
| 56 |
+
)
|
| 57 |
|
| 58 |
+
# Show previews in gallery when files are selected
|
| 59 |
file_input.change(
|
| 60 |
fn=lambda files: [Image.open(f.name).convert("RGB") for f in (files or [])],
|
| 61 |
inputs=file_input,
|
| 62 |
outputs=gallery
|
| 63 |
)
|
| 64 |
|
| 65 |
+
# Generate captions on button click
|
| 66 |
generate_btn.click(
|
| 67 |
fn=generate_alt_text,
|
| 68 |
+
inputs=[file_input, max_length],
|
| 69 |
outputs=output,
|
| 70 |
queue=True
|
| 71 |
)
|
| 72 |
|
| 73 |
+
# Clear all inputs and outputs
|
| 74 |
clear_btn.click(
|
| 75 |
fn=lambda: ([], 64, [], ""),
|
| 76 |
+
outputs=[file_input, max_length, gallery, output]
|
| 77 |
)
|
| 78 |
|
| 79 |
+
demo.launch()
|
|
|
|
|
|