Update app.py
Browse files
app.py
CHANGED
|
@@ -1,23 +1,19 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
| 3 |
from PIL import Image
|
| 4 |
-
from transformers import
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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
|
| 11 |
-
config = AutoConfig.from_pretrained(
|
| 12 |
-
"microsoft/Florence-2-large",
|
| 13 |
-
trust_remote_code=True
|
| 14 |
-
)
|
| 15 |
-
setattr(config, "_supports_sdpa", False)
|
| 16 |
-
|
| 17 |
-
# Load patched model and processor
|
| 18 |
model = AutoModelForCausalLM.from_pretrained(
|
| 19 |
"microsoft/Florence-2-large",
|
| 20 |
-
config=config,
|
| 21 |
torch_dtype=torch_dtype,
|
| 22 |
trust_remote_code=True
|
| 23 |
).to(device)
|
|
@@ -25,22 +21,14 @@ processor = AutoProcessor.from_pretrained(
|
|
| 25 |
"microsoft/Florence-2-large",
|
| 26 |
trust_remote_code=True
|
| 27 |
)
|
| 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 |
-
|
| 38 |
-
inputs = processor(
|
| 39 |
-
text=PROMPT,
|
| 40 |
-
images=img,
|
| 41 |
-
return_tensors="pt"
|
| 42 |
-
).to(device, torch_dtype)
|
| 43 |
-
# 3) Generate tokens
|
| 44 |
outputs = model.generate(
|
| 45 |
input_ids=inputs["input_ids"],
|
| 46 |
pixel_values=inputs["pixel_values"],
|
|
@@ -48,13 +36,8 @@ def generate_alt_text(files, max_new_tokens):
|
|
| 48 |
num_beams=3,
|
| 49 |
do_sample=False
|
| 50 |
)
|
| 51 |
-
# 4) Decode & post-process
|
| 52 |
raw_text = processor.batch_decode(outputs, skip_special_tokens=False)[0]
|
| 53 |
-
parsed = processor.post_process_generation(
|
| 54 |
-
raw_text,
|
| 55 |
-
task=PROMPT,
|
| 56 |
-
image_size=(img.width, img.height)
|
| 57 |
-
)
|
| 58 |
caption = parsed.get(PROMPT, "").strip().capitalize()
|
| 59 |
captions.append(caption)
|
| 60 |
return "\n\n".join(captions)
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
| 3 |
from PIL import Image
|
| 4 |
+
from transformers import AutoProcessor, AutoModelForCausalLM
|
| 5 |
+
from transformers.modeling_utils import PreTrainedModel
|
| 6 |
+
|
| 7 |
+
# --- Monkey-patch to disable SDPA checks on all models ---
|
| 8 |
+
PreTrainedModel._supports_sdpa = False
|
| 9 |
|
| 10 |
# --- Device & Model Setup ---
|
| 11 |
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
|
| 12 |
torch_dtype = torch.float16 if device.type == "cuda" else torch.float32
|
| 13 |
|
| 14 |
+
# Load Florence-2-large without SDPA errors
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
model = AutoModelForCausalLM.from_pretrained(
|
| 16 |
"microsoft/Florence-2-large",
|
|
|
|
| 17 |
torch_dtype=torch_dtype,
|
| 18 |
trust_remote_code=True
|
| 19 |
).to(device)
|
|
|
|
| 21 |
"microsoft/Florence-2-large",
|
| 22 |
trust_remote_code=True
|
| 23 |
)
|
|
|
|
| 24 |
PROMPT = "<OD>"
|
| 25 |
|
| 26 |
# --- Alt-Text Generation Function ---
|
| 27 |
def generate_alt_text(files, max_new_tokens):
|
| 28 |
captions = []
|
| 29 |
for f in files or []:
|
|
|
|
| 30 |
img = Image.open(f.name).convert("RGB")
|
| 31 |
+
inputs = processor(text=PROMPT, images=img, return_tensors="pt").to(device, torch_dtype)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
outputs = model.generate(
|
| 33 |
input_ids=inputs["input_ids"],
|
| 34 |
pixel_values=inputs["pixel_values"],
|
|
|
|
| 36 |
num_beams=3,
|
| 37 |
do_sample=False
|
| 38 |
)
|
|
|
|
| 39 |
raw_text = processor.batch_decode(outputs, skip_special_tokens=False)[0]
|
| 40 |
+
parsed = processor.post_process_generation(raw_text, task=PROMPT, image_size=(img.width, img.height))
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
caption = parsed.get(PROMPT, "").strip().capitalize()
|
| 42 |
captions.append(caption)
|
| 43 |
return "\n\n".join(captions)
|