Workpam commited on
Commit
9241fc0
·
verified ·
1 Parent(s): 215287d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -14
app.py CHANGED
@@ -10,45 +10,66 @@ captioner = pipeline(
10
  )
11
 
12
  # --- Caption generation function ---
13
- def generate_alt_text(files, max_length):
 
 
 
14
  captions = []
15
  for f in files or []:
16
  img = Image.open(f.name).convert("RGB")
17
- # Only pass max_length and num_beams here
18
- out = captioner(img, max_length=int(max_length), num_beams=4)
19
- text = out[0]["generated_text"].strip().capitalize()
20
  captions.append(text)
21
  return "\n\n".join(captions)
22
 
23
  # --- Gradio interface ---
24
- with gr.Blocks(title="Vit‑GPT2 Image Captioning") as demo:
25
- gr.Markdown("# Vit‑GPT2 Image Captioning Demo")
26
- gr.Markdown(f"Using **{MODEL_NAME}** — no special kwargs on pipeline creation")
27
 
28
  with gr.Row():
29
- file_input = gr.File(label="Upload Images", file_count="multiple", file_types=["image"])
30
- max_length = gr.Slider(16, 128, value=64, step=16, label="Max Caption Length")
 
 
 
 
 
 
 
 
 
 
31
  generate_btn = gr.Button("Generate Captions")
32
- clear_btn = gr.Button("Clear All")
33
 
34
  gallery = gr.Gallery(label="Image Preview", columns=4)
35
- output = gr.Textbox(label="Generated Captions", lines=10, show_copy_button=True)
 
 
 
 
36
 
 
37
  file_input.change(
38
  fn=lambda files: [Image.open(f.name).convert("RGB") for f in (files or [])],
39
- inputs=file_input, outputs=gallery
 
40
  )
41
 
 
42
  generate_btn.click(
43
  fn=generate_alt_text,
44
- inputs=[file_input, max_length],
45
  outputs=output,
46
  queue=True
47
  )
48
 
 
49
  clear_btn.click(
50
  fn=lambda: ([], 64, [], ""),
51
- outputs=[file_input, max_length, gallery, output]
52
  )
53
 
54
  demo.launch()
 
10
  )
11
 
12
  # --- Caption generation function ---
13
+ def generate_alt_text(files, max_new_tokens):
14
+ """
15
+ Generate concise, descriptive alt text for uploaded images.
16
+ """
17
  captions = []
18
  for f in files or []:
19
  img = Image.open(f.name).convert("RGB")
20
+ # Generate caption with specified max_new_tokens and num_beams
21
+ result = captioner(img, max_new_tokens=int(max_new_tokens), num_beams=4)
22
+ text = result[0]["generated_text"].strip().capitalize()
23
  captions.append(text)
24
  return "\n\n".join(captions)
25
 
26
  # --- Gradio interface ---
27
+ with gr.Blocks(title="Local Vit-GPT2 Image Captioning") as demo:
28
+ gr.Markdown("# Local Vit-GPT2 Image Captioning Demo")
29
+ gr.Markdown(f"Using **{MODEL_NAME}** — no API keys or gated access needed")
30
 
31
  with gr.Row():
32
+ file_input = gr.File(
33
+ label="Upload Images",
34
+ file_count="multiple",
35
+ file_types=["image"]
36
+ )
37
+ max_new_tokens = gr.Slider(
38
+ minimum=16,
39
+ maximum=128,
40
+ step=16,
41
+ value=64,
42
+ label="Max New Tokens"
43
+ )
44
  generate_btn = gr.Button("Generate Captions")
45
+ clear_btn = gr.Button("Clear All")
46
 
47
  gallery = gr.Gallery(label="Image Preview", columns=4)
48
+ output = gr.Textbox(
49
+ label="Generated Alt Texts",
50
+ lines=10,
51
+ show_copy_button=True
52
+ )
53
 
54
+ # Preview images in gallery
55
  file_input.change(
56
  fn=lambda files: [Image.open(f.name).convert("RGB") for f in (files or [])],
57
+ inputs=file_input,
58
+ outputs=gallery
59
  )
60
 
61
+ # Generate captions
62
  generate_btn.click(
63
  fn=generate_alt_text,
64
+ inputs=[file_input, max_new_tokens],
65
  outputs=output,
66
  queue=True
67
  )
68
 
69
+ # Clear all fields
70
  clear_btn.click(
71
  fn=lambda: ([], 64, [], ""),
72
+ outputs=[file_input, max_new_tokens, gallery, output]
73
  )
74
 
75
  demo.launch()