Workpam commited on
Commit
d75d03d
·
verified ·
1 Parent(s): 00dfcd0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -20
app.py CHANGED
@@ -3,63 +3,61 @@ from transformers import pipeline
3
  from PIL import Image
4
  import torch
5
 
6
- # --- Device & Model Setup ---
7
  device = "cuda" if torch.cuda.is_available() else "cpu"
8
  captioner = pipeline(
9
  "image-to-text",
10
  model="Salesforce/blip2-flan-t5-xl",
11
- torch_dtype=torch.float16 if device=="cuda" else torch.float32,
12
  device_map="auto",
13
- trust_remote_code=True, # required for BLIP-2
14
  )
15
 
16
- # --- Caption Generation Function ---
17
  def generate_alt_text(files, max_new_tokens):
18
  captions = []
19
  for f in files or []:
20
  img = Image.open(f.name).convert("RGB")
21
- # Funnel all generation args through generate_kwargs
22
- outputs = captioner(
23
  img,
24
  generate_kwargs={
25
  "max_new_tokens": int(max_new_tokens),
26
  "num_beams": 4
27
  }
28
  )
29
- text = outputs[0]["generated_text"].strip().capitalize()
30
  captions.append(text)
31
  return "\n\n".join(captions)
32
 
33
- # --- Gradio App ---
34
- with gr.Blocks(title="BLIP‑2 XL Alt‑Text Generator") as demo:
35
  gr.Markdown("# BLIP‑2 XL Alt‑Text Generator")
36
- gr.Markdown("Model: **Salesforce/blip2‑flan‑t5‑xl**\n\nUses `generate_kwargs` to set `max_new_tokens` & `num_beams`.")
37
 
38
  with gr.Row():
39
- file_input = gr.File(label="Upload Images", file_count="multiple", file_types=["image"])
40
- slider = gr.Slider(16, 256, value=64, step=16, label="Max New Tokens")
41
- run_btn = gr.Button("Generate Alt‑Text")
42
- clear_btn = gr.Button("Clear All")
43
 
44
  gallery = gr.Gallery(label="Image Preview", columns=4)
45
  output = gr.Textbox(label="Generated Alt‑Text", lines=10, show_copy_button=True)
46
 
47
- file_input.change(
48
- fn=lambda files: [Image.open(f.name).convert("RGB") for f in (files or [])],
49
- inputs=file_input,
50
- outputs=gallery,
51
  )
52
 
53
  run_btn.click(
54
  fn=generate_alt_text,
55
- inputs=[file_input, slider],
56
  outputs=output,
57
  queue=True
58
  )
59
 
60
  clear_btn.click(
61
  fn=lambda: ([], 64, [], ""),
62
- outputs=[file_input, slider, gallery, output]
63
  )
64
 
65
  demo.launch()
 
3
  from PIL import Image
4
  import torch
5
 
6
+ # --- Setup device & pipeline ---
7
  device = "cuda" if torch.cuda.is_available() else "cpu"
8
  captioner = pipeline(
9
  "image-to-text",
10
  model="Salesforce/blip2-flan-t5-xl",
11
+ trust_remote_code=True,
12
  device_map="auto",
13
+ torch_dtype=torch.float16 if device == "cuda" else torch.float32
14
  )
15
 
16
+ # --- Caption generation ---
17
  def generate_alt_text(files, max_new_tokens):
18
  captions = []
19
  for f in files or []:
20
  img = Image.open(f.name).convert("RGB")
21
+ out = captioner(
 
22
  img,
23
  generate_kwargs={
24
  "max_new_tokens": int(max_new_tokens),
25
  "num_beams": 4
26
  }
27
  )
28
+ text = out[0]["generated_text"].strip().capitalize()
29
  captions.append(text)
30
  return "\n\n".join(captions)
31
 
32
+ # --- Gradio UI ---
33
+ with gr.Blocks(title="BLIP‑2 XL Alt‑Text") as demo:
34
  gr.Markdown("# BLIP‑2 XL Alt‑Text Generator")
35
+ gr.Markdown("Model: Salesforce/blip2‑flan‑t5‑xl via `device_map=auto` (requires accelerate)")
36
 
37
  with gr.Row():
38
+ files = gr.File(label="Upload Images", file_count="multiple", file_types=["image"])
39
+ slider = gr.Slider(16, 256, value=64, step=16, label="Max New Tokens")
40
+ run_btn = gr.Button("Generate Alt‑Text")
41
+ clear_btn = gr.Button("Clear All")
42
 
43
  gallery = gr.Gallery(label="Image Preview", columns=4)
44
  output = gr.Textbox(label="Generated Alt‑Text", lines=10, show_copy_button=True)
45
 
46
+ files.change(
47
+ fn=lambda fs: [Image.open(f.name).convert("RGB") for f in (fs or [])],
48
+ inputs=files, outputs=gallery
 
49
  )
50
 
51
  run_btn.click(
52
  fn=generate_alt_text,
53
+ inputs=[files, slider],
54
  outputs=output,
55
  queue=True
56
  )
57
 
58
  clear_btn.click(
59
  fn=lambda: ([], 64, [], ""),
60
+ outputs=[files, slider, gallery, output]
61
  )
62
 
63
  demo.launch()