Workpam commited on
Commit
f6cedd5
·
verified ·
1 Parent(s): 2556988

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +57 -33
app.py CHANGED
@@ -1,41 +1,65 @@
1
  import gradio as gr
2
- from transformers import pipeline
 
3
  from PIL import Image
4
 
5
- # --- Model Setup ---
 
 
6
  MODEL_NAME = "microsoft/Florence-2-large"
7
- # Florence uses custom code under the hood, so we set trust_remote_code=True
8
- captioner = pipeline(
9
- "image-to-text",
10
- model=MODEL_NAME,
11
- trust_remote_code=True,
12
  )
 
 
 
 
 
13
 
14
- # --- Caption Generation ---
15
  def generate_alt_text(files, max_new_tokens):
16
- """
17
- files: list of uploaded image files
18
- max_new_tokens: how many new tokens to generate
19
- """
20
  captions = []
21
  for f in files or []:
22
- img = Image.open(f.name).convert("RGB")
23
- # Pass all generation params in one dict
24
- result = captioner(
25
- img,
26
- generate_kwargs={
27
- "max_new_tokens": int(max_new_tokens),
28
- "num_beams": 4,
29
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30
  )
31
- text = result[0]["generated_text"].strip().capitalize()
32
- captions.append(text)
33
  return "\n\n".join(captions)
34
 
35
  # --- Gradio UI ---
36
- with gr.Blocks(title="Florence‑2‑large Alt‑Text Generator") as demo:
37
- gr.Markdown("# Florence‑2‑large Alt‑Text Generator")
38
- gr.Markdown("Using Microsoft/Florence‑2‑large via `image-to-text`, with `generate_kwargs`.")
39
 
40
  with gr.Row():
41
  file_input = gr.File(
@@ -47,23 +71,23 @@ with gr.Blocks(title="Florence‑2‑large Alt‑Text Generator") as demo:
47
  minimum=16,
48
  maximum=512,
49
  step=16,
50
- value=64,
51
  label="Max New Tokens"
52
  )
53
  generate_btn = gr.Button("Generate Alt Text")
54
- clear_btn = gr.Button("Clear All")
55
 
56
  gallery = gr.Gallery(label="Image Preview", columns=4)
57
- output = gr.Textbox(label="Generated Alt Texts", lines=10, show_copy_button=True)
58
 
59
- # Preview uploaded images
60
  file_input.change(
61
  fn=lambda files: [Image.open(f.name).convert("RGB") for f in (files or [])],
62
  inputs=file_input,
63
- outputs=gallery,
64
  )
65
 
66
- # Generate captions
67
  generate_btn.click(
68
  fn=generate_alt_text,
69
  inputs=[file_input, max_new_tokens],
@@ -71,9 +95,9 @@ with gr.Blocks(title="Florence‑2‑large Alt‑Text Generator") as demo:
71
  queue=True
72
  )
73
 
74
- # Clear everything
75
  clear_btn.click(
76
- fn=lambda: ([], 64, [], ""),
77
  outputs=[file_input, max_new_tokens, gallery, output]
78
  )
79
 
 
1
  import gradio as gr
2
+ import torch
3
+ from transformers import AutoProcessor, AutoModelForCausalLM
4
  from PIL import Image
5
 
6
+ # --- Configuration ---
7
+ device = "cuda" if torch.cuda.is_available() else "cpu"
8
+ torch_dtype = torch.float16 if device == "cuda" else torch.float32
9
  MODEL_NAME = "microsoft/Florence-2-large"
10
+
11
+ # Load model and processor with remote code trust
12
+ processor = AutoProcessor.from_pretrained(
13
+ MODEL_NAME,
14
+ trust_remote_code=True
15
  )
16
+ model = AutoModelForCausalLM.from_pretrained(
17
+ MODEL_NAME,
18
+ trust_remote_code=True,
19
+ torch_dtype=torch_dtype
20
+ ).to(device)
21
 
22
+ # --- Alt Text Generation ---
23
  def generate_alt_text(files, max_new_tokens):
 
 
 
 
24
  captions = []
25
  for f in files or []:
26
+ # Load image
27
+ image = Image.open(f.name).convert("RGB")
28
+ # Use caption prompt
29
+ prompt = "<CAPTION>"
30
+ # Preprocess inputs
31
+ inputs = processor(
32
+ text=prompt,
33
+ images=image,
34
+ return_tensors="pt"
35
+ ).to(device)
36
+ # Generate tokens
37
+ generated_ids = model.generate(
38
+ input_ids=inputs["input_ids"],
39
+ pixel_values=inputs["pixel_values"],
40
+ max_new_tokens=int(max_new_tokens),
41
+ num_beams=3,
42
+ do_sample=False
43
+ )
44
+ # Decode text
45
+ generated_text = processor.batch_decode(
46
+ generated_ids,
47
+ skip_special_tokens=True
48
+ )[0]
49
+ # Post-process for caption task
50
+ result = processor.post_process_generation(
51
+ generated_text,
52
+ task=prompt,
53
+ image_size=(image.width, image.height)
54
  )
55
+ caption = result.get(prompt, "").strip().capitalize()
56
+ captions.append(caption)
57
  return "\n\n".join(captions)
58
 
59
  # --- Gradio UI ---
60
+ with gr.Blocks(title="Florence-2-large Alt Text Generator") as demo:
61
+ gr.Markdown("# Florence-2-large Alt Text Generator")
62
+ gr.Markdown("Using microsoft/Florence-2-large with manual model & processor loading.")
63
 
64
  with gr.Row():
65
  file_input = gr.File(
 
71
  minimum=16,
72
  maximum=512,
73
  step=16,
74
+ value=128,
75
  label="Max New Tokens"
76
  )
77
  generate_btn = gr.Button("Generate Alt Text")
78
+ clear_btn = gr.Button("Clear All")
79
 
80
  gallery = gr.Gallery(label="Image Preview", columns=4)
81
+ output = gr.Textbox(label="Generated Alt Texts", lines=10, show_copy_button=True)
82
 
83
+ # Preview images
84
  file_input.change(
85
  fn=lambda files: [Image.open(f.name).convert("RGB") for f in (files or [])],
86
  inputs=file_input,
87
+ outputs=gallery
88
  )
89
 
90
+ # Generate captions on click
91
  generate_btn.click(
92
  fn=generate_alt_text,
93
  inputs=[file_input, max_new_tokens],
 
95
  queue=True
96
  )
97
 
98
+ # Clear all fields
99
  clear_btn.click(
100
+ fn=lambda: ([], 128, [], ""),
101
  outputs=[file_input, max_new_tokens, gallery, output]
102
  )
103