Workpam commited on
Commit
8f619e7
·
verified ·
1 Parent(s): 93daaea

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +6 -6
app.py CHANGED
@@ -13,7 +13,7 @@ captioner = pipeline(
13
  )
14
 
15
  # --- Core Function ---
16
- def generate_alt_text(files, max_length):
17
  """
18
  Generate captions for a list of uploaded image files.
19
  """
@@ -22,7 +22,7 @@ def generate_alt_text(files, max_length):
22
  # Load and preprocess image
23
  img = Image.open(f.name).convert("RGB")
24
  # Run the pipeline with a max token length
25
- result = captioner(img, max_length=int(max_length))
26
  text = result[0]["generated_text"].strip().capitalize()
27
  captions.append(text)
28
  return "\n\n".join(captions)
@@ -38,12 +38,12 @@ with gr.Blocks(title="Image-to-Text Captioning Demo") as demo:
38
  file_count="multiple",
39
  file_types=["image"]
40
  )
41
- max_length = gr.Slider(
42
  minimum=16,
43
  maximum=128,
44
  step=16,
45
  value=64,
46
- label="Max Caption Length"
47
  )
48
  generate_btn = gr.Button("Generate Captions")
49
  clear_btn = gr.Button("Clear All")
@@ -65,7 +65,7 @@ with gr.Blocks(title="Image-to-Text Captioning Demo") as demo:
65
  # Generate captions on button click
66
  generate_btn.click(
67
  fn=generate_alt_text,
68
- inputs=[file_input, max_length],
69
  outputs=output,
70
  queue=True
71
  )
@@ -73,7 +73,7 @@ with gr.Blocks(title="Image-to-Text Captioning Demo") as demo:
73
  # Clear all inputs and outputs
74
  clear_btn.click(
75
  fn=lambda: ([], 64, [], ""),
76
- outputs=[file_input, max_length, gallery, output]
77
  )
78
 
79
  demo.launch()
 
13
  )
14
 
15
  # --- Core Function ---
16
+ def generate_alt_text(files, max_new_tokens):
17
  """
18
  Generate captions for a list of uploaded image files.
19
  """
 
22
  # Load and preprocess image
23
  img = Image.open(f.name).convert("RGB")
24
  # Run the pipeline with a max token length
25
+ result = captioner(img, max_new_tokens=int(max_new_tokens))
26
  text = result[0]["generated_text"].strip().capitalize()
27
  captions.append(text)
28
  return "\n\n".join(captions)
 
38
  file_count="multiple",
39
  file_types=["image"]
40
  )
41
+ max_new_tokens = gr.Slider(
42
  minimum=16,
43
  maximum=128,
44
  step=16,
45
  value=64,
46
+ label="Max New Tokens for Caption"
47
  )
48
  generate_btn = gr.Button("Generate Captions")
49
  clear_btn = gr.Button("Clear All")
 
65
  # Generate captions on button click
66
  generate_btn.click(
67
  fn=generate_alt_text,
68
+ inputs=[file_input, max_new_tokens],
69
  outputs=output,
70
  queue=True
71
  )
 
73
  # Clear all inputs and outputs
74
  clear_btn.click(
75
  fn=lambda: ([], 64, [], ""),
76
+ outputs=[file_input, max_new_tokens, gallery, output]
77
  )
78
 
79
  demo.launch()