Workpam commited on
Commit
7a711de
·
verified ·
1 Parent(s): f65bd79

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -6
app.py CHANGED
@@ -1,15 +1,23 @@
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)
@@ -26,13 +34,13 @@ def generate_alt_text(files, max_new_tokens):
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"],
@@ -41,9 +49,9 @@ def generate_alt_text(files, max_new_tokens):
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
  )
 
1
  import gradio as gr
2
  import torch
3
  from PIL import Image
4
+ from transformers import AutoConfig, 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 and patch config to disable SDPA checks
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)
 
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 tokens
44
  outputs = model.generate(
45
  input_ids=inputs["input_ids"],
46
  pixel_values=inputs["pixel_values"],
 
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
  )