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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -17
app.py CHANGED
@@ -2,24 +2,29 @@
2
  import gradio as gr
3
  from transformers import pipeline
4
  from PIL import Image
 
 
 
 
 
5
 
6
- # --- Lightweight pipeline ---
7
  captioner = pipeline(
8
  "image-to-text",
9
- model="nlpconnect/vit-gpt2-image-captioning",
10
- device=0 # set to -1 for CPU only
11
  )
12
 
13
- # --- Generation fn ---
14
- def generate_alt_text(files, max_length):
15
  captions = []
16
  for f in files or []:
17
  img = Image.open(f.name).convert("RGB")
18
  out = captioner(
19
  img,
20
  generate_kwargs={
21
- "max_length": int(max_length),
22
- "num_beams": 3
 
23
  }
24
  )
25
  text = out[0].get("generated_text", "").strip().capitalize()
@@ -27,25 +32,54 @@ def generate_alt_text(files, max_length):
27
  return "\n\n".join(captions)
28
 
29
  # --- Gradio UI ---
30
- with gr.Blocks(title="Fast Alt‑Text Generator") as demo:
31
- gr.Markdown("## Fast & Lightweight Alt‑Text Generator")
32
- gr.Markdown("Model: `nlpconnect/vit-gpt2-image-captioning`")
 
 
33
 
34
  with gr.Row():
35
- file_input = gr.File(label="Upload Images", file_count="multiple", file_types=["image"])
36
- slider = gr.Slider(16, 64, value=32, step=8, label="Max Tokens")
37
- run_btn = gr.Button("Generate")
38
- clear_btn = gr.Button("Clear")
 
 
 
 
 
 
 
 
 
 
39
 
40
  gallery = gr.Gallery(label="Preview", columns=4)
41
- output = gr.Textbox(label="Captions", lines=6, show_copy_button=True)
 
 
 
 
42
 
 
43
  file_input.change(
44
  fn=lambda files: [Image.open(f.name).convert("RGB") for f in (files or [])],
45
  inputs=file_input,
46
  outputs=gallery
47
  )
48
- run_btn.click(generate_alt_text, [file_input, slider], output, queue=True)
49
- clear_btn.click(lambda: ([], 32, [], ""), outputs=[file_input, slider, gallery, output])
 
 
 
 
 
 
 
 
 
 
 
 
50
 
51
  demo.launch()
 
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={
25
+ "max_new_tokens": int(max_new_tokens),
26
+ "num_beams": 4,
27
+ "no_repeat_ngram_size": 2
28
  }
29
  )
30
  text = out[0].get("generated_text", "").strip().capitalize()
 
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()