Update app.py
Browse files
app.py
CHANGED
|
@@ -1,86 +1,51 @@
|
|
| 1 |
# app.py
|
| 2 |
import gradio as gr
|
| 3 |
-
import
|
| 4 |
from PIL import Image
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
# --- Device & Model Setup ---
|
| 8 |
-
device = "cuda:0" if torch.cuda.is_available() else "cpu"
|
| 9 |
-
torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
trust_remote_code=True
|
| 15 |
-
).to(device)
|
| 16 |
|
| 17 |
-
|
| 18 |
-
"
|
| 19 |
-
|
|
|
|
|
|
|
| 20 |
)
|
| 21 |
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
# --- Alt‑Text Generation Function ---
|
| 25 |
def generate_alt_text(files, max_new_tokens):
|
| 26 |
captions = []
|
| 27 |
for f in files or []:
|
| 28 |
-
# 1) Load and preprocess
|
| 29 |
img = Image.open(f.name).convert("RGB")
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
input_ids=inputs["input_ids"],
|
| 39 |
-
pixel_values=inputs["pixel_values"],
|
| 40 |
-
max_new_tokens=int(max_new_tokens),
|
| 41 |
-
num_beams=3,
|
| 42 |
-
do_sample=False
|
| 43 |
)
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
generated_text = processor.batch_decode(
|
| 47 |
-
generated_ids,
|
| 48 |
-
skip_special_tokens=False
|
| 49 |
-
)[0]
|
| 50 |
-
parsed = processor.post_process_generation(
|
| 51 |
-
generated_text,
|
| 52 |
-
task=PROMPT,
|
| 53 |
-
image_size=(img.width, img.height)
|
| 54 |
-
)
|
| 55 |
-
caption = parsed.get(PROMPT, "").strip().capitalize()
|
| 56 |
-
captions.append(caption)
|
| 57 |
-
|
| 58 |
return "\n\n".join(captions)
|
| 59 |
|
| 60 |
-
|
| 61 |
-
# --- Gradio Interface ---
|
| 62 |
with gr.Blocks(title="Florence‑2‑large Alt‑Text Generator") as demo:
|
| 63 |
-
gr.Markdown("# Florence‑2‑large Alt‑Text Generator")
|
| 64 |
-
gr.Markdown("Drop/upload images
|
| 65 |
|
| 66 |
with gr.Row():
|
| 67 |
-
file_input = gr.File(
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
)
|
| 72 |
-
slider = gr.Slider(
|
| 73 |
-
minimum=64,
|
| 74 |
-
maximum=4096,
|
| 75 |
-
step=64,
|
| 76 |
-
value=512,
|
| 77 |
-
label="Max New Tokens"
|
| 78 |
-
)
|
| 79 |
-
run_btn = gr.Button("Generate Alt‑Text")
|
| 80 |
-
clear_btn = gr.Button("Clear All")
|
| 81 |
|
| 82 |
gallery = gr.Gallery(label="Image Preview", columns=4)
|
| 83 |
-
output = gr.Textbox(label="Generated Alt‑Text", lines=
|
| 84 |
|
| 85 |
# Show previews immediately
|
| 86 |
file_input.change(
|
|
@@ -89,7 +54,7 @@ with gr.Blocks(title="Florence‑2‑large Alt‑Text Generator") as demo:
|
|
| 89 |
outputs=gallery
|
| 90 |
)
|
| 91 |
|
| 92 |
-
# Generate captions
|
| 93 |
run_btn.click(
|
| 94 |
fn=generate_alt_text,
|
| 95 |
inputs=[file_input, slider],
|
|
|
|
| 1 |
# app.py
|
| 2 |
import gradio as gr
|
| 3 |
+
from transformers import pipeline
|
| 4 |
from PIL import Image
|
| 5 |
+
import torch
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
+
# --- Pipeline Setup ---
|
| 8 |
+
# Use GPU 0 if available, otherwise CPU.
|
| 9 |
+
device = 0 if torch.cuda.is_available() else -1
|
|
|
|
|
|
|
| 10 |
|
| 11 |
+
captioner = pipeline(
|
| 12 |
+
"image-to-text",
|
| 13 |
+
model="microsoft/Florence-2-large",
|
| 14 |
+
trust_remote_code=True,
|
| 15 |
+
device=device
|
| 16 |
)
|
| 17 |
|
| 18 |
+
# --- Generation Function ---
|
|
|
|
|
|
|
| 19 |
def generate_alt_text(files, max_new_tokens):
|
| 20 |
captions = []
|
| 21 |
for f in files or []:
|
|
|
|
| 22 |
img = Image.open(f.name).convert("RGB")
|
| 23 |
+
# Pass everything via generate_kwargs
|
| 24 |
+
out = captioner(
|
| 25 |
+
img,
|
| 26 |
+
generate_kwargs={
|
| 27 |
+
"max_new_tokens": int(max_new_tokens),
|
| 28 |
+
"num_beams": 3,
|
| 29 |
+
"do_sample": False
|
| 30 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
)
|
| 32 |
+
text = out[0].get("generated_text", "").strip().capitalize()
|
| 33 |
+
captions.append(text)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
return "\n\n".join(captions)
|
| 35 |
|
| 36 |
+
# --- Gradio UI ---
|
|
|
|
| 37 |
with gr.Blocks(title="Florence‑2‑large Alt‑Text Generator") as demo:
|
| 38 |
+
gr.Markdown("## Florence‑2‑large Alt‑Text Generator")
|
| 39 |
+
gr.Markdown("Drop/upload images and get detailed, `<OD>`‑style descriptions.")
|
| 40 |
|
| 41 |
with gr.Row():
|
| 42 |
+
file_input = gr.File(label="Upload Images", file_count="multiple", file_types=["image"])
|
| 43 |
+
slider = gr.Slider(64, 2048, value=512, step=64, label="Max New Tokens")
|
| 44 |
+
run_btn = gr.Button("Generate Alt‑Text")
|
| 45 |
+
clear_btn = gr.Button("Clear All")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
gallery = gr.Gallery(label="Image Preview", columns=4)
|
| 48 |
+
output = gr.Textbox(label="Generated Alt‑Text", lines=6, show_copy_button=True)
|
| 49 |
|
| 50 |
# Show previews immediately
|
| 51 |
file_input.change(
|
|
|
|
| 54 |
outputs=gallery
|
| 55 |
)
|
| 56 |
|
| 57 |
+
# Generate captions
|
| 58 |
run_btn.click(
|
| 59 |
fn=generate_alt_text,
|
| 60 |
inputs=[file_input, slider],
|