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