Update app.py
Browse files
app.py
CHANGED
|
@@ -1,69 +1,88 @@
|
|
| 1 |
# app.py
|
| 2 |
import gradio as gr
|
|
|
|
|
|
|
| 3 |
from PIL import Image
|
| 4 |
-
from
|
| 5 |
|
| 6 |
-
#
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
def generate_alt_text(files, max_new_tokens):
|
| 10 |
captions = []
|
| 11 |
for f in files or []:
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
|
|
|
|
|
|
| 24 |
)
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
text = text.strip().capitalize()
|
| 33 |
-
captions.append(text)
|
| 34 |
-
|
| 35 |
return "\n\n".join(captions)
|
| 36 |
|
| 37 |
# --- Gradio UI ---
|
| 38 |
with gr.Blocks(title="Florence‑2‑large Alt‑Text Generator") as demo:
|
| 39 |
-
gr.Markdown("#
|
| 40 |
-
gr.Markdown("
|
| 41 |
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
clear_btn = gr.Button("Clear All")
|
| 47 |
|
| 48 |
-
gallery = gr.Gallery(label="
|
| 49 |
-
output = gr.Textbox(label="Generated Alt‑Text", lines=
|
| 50 |
|
| 51 |
-
# Preview immediately
|
| 52 |
file_input.change(
|
| 53 |
fn=lambda files: [Image.open(f.name).convert("RGB") for f in (files or [])],
|
| 54 |
inputs=file_input,
|
| 55 |
outputs=gallery
|
| 56 |
)
|
| 57 |
-
|
| 58 |
-
# Generate captions
|
| 59 |
gen_btn.click(
|
| 60 |
fn=generate_alt_text,
|
| 61 |
inputs=[file_input, slider],
|
| 62 |
outputs=output,
|
| 63 |
queue=True
|
| 64 |
)
|
| 65 |
-
|
| 66 |
-
# Clear all
|
| 67 |
clear_btn.click(
|
| 68 |
fn=lambda: ([], 512, [], ""),
|
| 69 |
outputs=[file_input, slider, gallery, output]
|
|
|
|
| 1 |
# app.py
|
| 2 |
import gradio as gr
|
| 3 |
+
import torch
|
| 4 |
+
import torch.nn as nn
|
| 5 |
from PIL import Image
|
| 6 |
+
from transformers import AutoProcessor, AutoModelForCausalLM
|
| 7 |
|
| 8 |
+
# --- Monkey‑patch to bypass SDPA check ---
|
| 9 |
+
# Whenever a model asks for `_supports_sdpa`, return False instead of raising.
|
| 10 |
+
_orig_getattr = nn.Module.__getattr__
|
| 11 |
+
def _patched_getattr(self, name):
|
| 12 |
+
if name == "_supports_sdpa":
|
| 13 |
+
return False
|
| 14 |
+
return _orig_getattr(self, name)
|
| 15 |
+
nn.Module.__getattr__ = _patched_getattr
|
| 16 |
|
| 17 |
+
# --- Device & Model Setup ---
|
| 18 |
+
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
|
| 19 |
+
torch_dtype = torch.float16 if device.type == "cuda" else torch.float32
|
| 20 |
+
|
| 21 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 22 |
+
"microsoft/Florence-2-large",
|
| 23 |
+
torch_dtype=torch_dtype,
|
| 24 |
+
trust_remote_code=True
|
| 25 |
+
).to(device)
|
| 26 |
+
|
| 27 |
+
processor = AutoProcessor.from_pretrained(
|
| 28 |
+
"microsoft/Florence-2-large",
|
| 29 |
+
trust_remote_code=True
|
| 30 |
+
)
|
| 31 |
+
|
| 32 |
+
PROMPT = "<OD>"
|
| 33 |
+
|
| 34 |
+
# --- Alt‑Text Generation Function ---
|
| 35 |
def generate_alt_text(files, max_new_tokens):
|
| 36 |
captions = []
|
| 37 |
for f in files or []:
|
| 38 |
+
img = Image.open(f.name).convert("RGB")
|
| 39 |
+
# Preprocess
|
| 40 |
+
inputs = processor(
|
| 41 |
+
text=PROMPT,
|
| 42 |
+
images=img,
|
| 43 |
+
return_tensors="pt"
|
| 44 |
+
).to(device, torch_dtype)
|
| 45 |
+
# Generate
|
| 46 |
+
outputs = model.generate(
|
| 47 |
+
input_ids=inputs["input_ids"],
|
| 48 |
+
pixel_values=inputs["pixel_values"],
|
| 49 |
+
max_new_tokens=int(max_new_tokens),
|
| 50 |
+
num_beams=3,
|
| 51 |
+
do_sample=False
|
| 52 |
)
|
| 53 |
+
# Decode & post-process
|
| 54 |
+
raw = processor.batch_decode(outputs, skip_special_tokens=False)[0]
|
| 55 |
+
parsed = processor.post_process_generation(
|
| 56 |
+
raw, task=PROMPT, image_size=(img.width, img.height)
|
| 57 |
+
)
|
| 58 |
+
cap = parsed.get(PROMPT, "").strip().capitalize()
|
| 59 |
+
captions.append(cap)
|
|
|
|
|
|
|
|
|
|
| 60 |
return "\n\n".join(captions)
|
| 61 |
|
| 62 |
# --- Gradio UI ---
|
| 63 |
with gr.Blocks(title="Florence‑2‑large Alt‑Text Generator") as demo:
|
| 64 |
+
gr.Markdown("# Florence‑2‑large Alt‑Text Generator")
|
| 65 |
+
gr.Markdown("Upload images to get `<OD>`‑style descriptions from Florence‑2‑large.")
|
| 66 |
|
| 67 |
+
file_input = gr.File(label="Images", file_count="multiple", file_types=["image"])
|
| 68 |
+
slider = gr.Slider(64, 4096, step=64, value=512, label="Max New Tokens")
|
| 69 |
+
gen_btn = gr.Button("Generate Alt‑Text")
|
| 70 |
+
clear_btn = gr.Button("Clear All")
|
|
|
|
| 71 |
|
| 72 |
+
gallery = gr.Gallery(label="Preview", columns=4)
|
| 73 |
+
output = gr.Textbox(label="Generated Alt‑Text", lines=5, show_copy_button=True)
|
| 74 |
|
|
|
|
| 75 |
file_input.change(
|
| 76 |
fn=lambda files: [Image.open(f.name).convert("RGB") for f in (files or [])],
|
| 77 |
inputs=file_input,
|
| 78 |
outputs=gallery
|
| 79 |
)
|
|
|
|
|
|
|
| 80 |
gen_btn.click(
|
| 81 |
fn=generate_alt_text,
|
| 82 |
inputs=[file_input, slider],
|
| 83 |
outputs=output,
|
| 84 |
queue=True
|
| 85 |
)
|
|
|
|
|
|
|
| 86 |
clear_btn.click(
|
| 87 |
fn=lambda: ([], 512, [], ""),
|
| 88 |
outputs=[file_input, slider, gallery, output]
|