Workpam commited on
Commit
252d936
·
verified ·
1 Parent(s): 75ed356

Update app.py

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