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