Workpam commited on
Commit
25ad490
·
verified ·
1 Parent(s): 955b6a9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -52
app.py CHANGED
@@ -2,23 +2,24 @@
2
  import gradio as gr
3
  from transformers import pipeline
4
  from PIL import Image
5
- import torch
6
-
7
- # --- Pipeline Setup ---
8
- # Use GPU 0 if available, else CPU (-1)
9
- device = 0 if torch.cuda.is_available() else -1
10
 
 
 
11
  captioner = pipeline(
12
  "image-to-text",
13
- model="microsoft/git-base-coco", # 0.2 B params :contentReference[oaicite:0]{index=0}
14
- device=device
 
15
  )
16
 
17
- # --- Generation Function ---
18
  def generate_alt_text(files, max_new_tokens):
 
19
  captions = []
20
- for f in files or []:
21
- img = Image.open(f.name).convert("RGB")
 
 
22
  out = captioner(
23
  img,
24
  generate_kwargs={
@@ -27,59 +28,33 @@ def generate_alt_text(files, max_new_tokens):
27
  "no_repeat_ngram_size": 2
28
  }
29
  )
30
- text = out[0].get("generated_text", "").strip().capitalize()
31
  captions.append(text)
32
- return "\n\n".join(captions)
33
 
34
- # --- Gradio UI ---
35
- with gr.Blocks(title="Fast & Accurate Alt‑Text with GIT") as demo:
36
- gr.Markdown("## Fast & Accurate Alt‑Text Generator")
37
- gr.Markdown(
38
- "Model: **microsoft/git-base-coco** (~0.2 B parameters) for sub‑second, high‑fidelity captions. :contentReference[oaicite:1]{index=1}"
39
- )
40
 
41
- with gr.Row():
42
- file_input = gr.File(
43
- label="Upload Images",
44
- file_count="multiple",
45
- file_types=["image"]
46
- )
47
- slider = gr.Slider(
48
- minimum=16,
49
- maximum=128,
50
- step=16,
51
- value=64,
52
- label="Max Tokens"
53
- )
54
- run_btn = gr.Button("Generate Alt‑Text")
55
- clear_btn = gr.Button("Clear All")
56
 
57
- gallery = gr.Gallery(label="Preview", columns=4)
58
- output = gr.Textbox(
59
- label="Generated Alt‑Text",
60
- lines=6,
61
- show_copy_button=True
62
  )
63
 
64
- # Show previews
65
- file_input.change(
66
- fn=lambda files: [Image.open(f.name).convert("RGB") for f in (files or [])],
67
- inputs=file_input,
68
- outputs=gallery
69
- )
70
 
71
- # Generate captions
72
- run_btn.click(
73
  fn=generate_alt_text,
74
  inputs=[file_input, slider],
75
  outputs=output,
76
  queue=True
77
  )
78
 
79
- # Clear the UI
80
- clear_btn.click(
81
- fn=lambda: ([], 64, [], ""),
82
- outputs=[file_input, slider, gallery, output]
83
- )
84
-
85
  demo.launch()
 
2
  import gradio as gr
3
  from transformers import pipeline
4
  from PIL import Image
 
 
 
 
 
5
 
6
+ # --- 1) Set up the BLIP-2 FLAN-T5 XL pipeline ---
7
+ # Runs on GPU 0 if available, otherwise CPU (-1).
8
  captioner = pipeline(
9
  "image-to-text",
10
+ model="Salesforce/blip2-flan-t5-xl",
11
+ trust_remote_code=True,
12
+ device=0 # change to -1 to force CPU
13
  )
14
 
15
+ # --- 2) Caption generation function ---
16
  def generate_alt_text(files, max_new_tokens):
17
+ # Gradio gives us a list of numpy arrays by default; convert to PIL
18
  captions = []
19
+ for img in files or []:
20
+ if not isinstance(img, Image.Image):
21
+ img = Image.fromarray(img)
22
+ # Pass all generation args via generate_kwargs
23
  out = captioner(
24
  img,
25
  generate_kwargs={
 
28
  "no_repeat_ngram_size": 2
29
  }
30
  )
31
+ text = out[0]["generated_text"].strip().capitalize()
32
  captions.append(text)
33
+ return captions # return a list for Gradio to display
34
 
35
+ # --- 3) Gradio UI ---
36
+ with gr.Blocks(title="BLIP‑2 FLAN‑T5 XL Alt‑Text Generator") as demo:
37
+ gr.Markdown("## BLIP‑2 FLAN‑T5 XL Alt‑Text Generator")
38
+ gr.Markdown("High‑quality, fast captions with **Salesforce/blip2‑flan‑t5-xl**.")
 
 
39
 
40
+ file_input = gr.Gallery(
41
+ label="Upload or Drag & Drop Images",
42
+ show_label=False,
43
+ elem_id="gallery"
44
+ ).style(grid=[4], height="auto")
 
 
 
 
 
 
 
 
 
 
45
 
46
+ slider = gr.Slider(
47
+ minimum=16, maximum=128, step=16, value=64, label="Max New Tokens"
 
 
 
48
  )
49
 
50
+ btn = gr.Button("Generate Captions")
51
+ output = gr.Gallery(label="Generated Alt‑Text").style(grid=[1], height="auto")
 
 
 
 
52
 
53
+ btn.click(
 
54
  fn=generate_alt_text,
55
  inputs=[file_input, slider],
56
  outputs=output,
57
  queue=True
58
  )
59
 
 
 
 
 
 
 
60
  demo.launch()