Workpam commited on
Commit
272422f
·
verified ·
1 Parent(s): aef8a85

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -7
app.py CHANGED
@@ -15,7 +15,7 @@ def generate_alt_text(files):
15
  """
16
  captions = []
17
  for f in files:
18
- # f may be a tempfile with a .name attribute or a file path string
19
  path = f.name if hasattr(f, 'name') else f
20
  img = Image.open(path).convert("RGB")
21
  result = captioner(img)
@@ -23,19 +23,19 @@ def generate_alt_text(files):
23
  captions.append(text.strip())
24
  return "\n".join([f"{i+1}. {cap}" for i, cap in enumerate(captions)])
25
 
26
- # Build Gradio interface
27
- demo = gr.Blocks(title="Multi-Image Alt Text Generator")
28
- with demo:
29
  gr.Markdown("# Alt Text Generator for Multiple Images")
30
  gr.Markdown(
31
  "Upload one or more images below and click **Generate Alt Text** to get descriptive captions."
32
  )
33
- # Use File component to handle multiple images
34
  file_input = gr.File(
35
  label="Upload Images",
36
  file_count="multiple",
37
  file_types=["image"]
38
  )
 
39
  output_box = gr.Textbox(
40
  label="Generated Alt Texts",
41
  placeholder="Your alt text will appear here...",
@@ -45,17 +45,18 @@ with demo:
45
  with gr.Row():
46
  generate_btn = gr.Button("Generate Alt Text")
47
  copy_btn = gr.Button("Copy to Clipboard")
 
48
  generate_btn.click(
49
  fn=generate_alt_text,
50
  inputs=file_input,
51
  outputs=output_box
52
  )
53
- # JS snippet copies the textbox content to clipboard
54
  copy_btn.click(
55
  fn=None,
56
  inputs=None,
57
  outputs=None,
58
- _js="() => { navigator.clipboard.writeText(document.getElementById('output_box').value); }"
59
  )
60
 
61
  # Launch the app
 
15
  """
16
  captions = []
17
  for f in files:
18
+ # f may be a temporary file object or file path
19
  path = f.name if hasattr(f, 'name') else f
20
  img = Image.open(path).convert("RGB")
21
  result = captioner(img)
 
23
  captions.append(text.strip())
24
  return "\n".join([f"{i+1}. {cap}" for i, cap in enumerate(captions)])
25
 
26
+ # Build Gradio Blocks app with custom JS support
27
+ with gr.Blocks(title="Multi-Image Alt Text Generator") as demo:
 
28
  gr.Markdown("# Alt Text Generator for Multiple Images")
29
  gr.Markdown(
30
  "Upload one or more images below and click **Generate Alt Text** to get descriptive captions."
31
  )
32
+ # Use File component to allow multiple image uploads
33
  file_input = gr.File(
34
  label="Upload Images",
35
  file_count="multiple",
36
  file_types=["image"]
37
  )
38
+ # Textbox to display the generated alt texts
39
  output_box = gr.Textbox(
40
  label="Generated Alt Texts",
41
  placeholder="Your alt text will appear here...",
 
45
  with gr.Row():
46
  generate_btn = gr.Button("Generate Alt Text")
47
  copy_btn = gr.Button("Copy to Clipboard")
48
+ # Generate captions on click
49
  generate_btn.click(
50
  fn=generate_alt_text,
51
  inputs=file_input,
52
  outputs=output_box
53
  )
54
+ # Copy to clipboard using the 'js' parameter (no underscore)
55
  copy_btn.click(
56
  fn=None,
57
  inputs=None,
58
  outputs=None,
59
+ js="() => { navigator.clipboard.writeText(document.getElementById('output_box').value); }"
60
  )
61
 
62
  # Launch the app