Deadmon commited on
Commit
f74ab25
·
verified ·
1 Parent(s): 81b64c1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -20
app.py CHANGED
@@ -94,17 +94,14 @@ def process_and_display(gender, num_images):
94
  # Fetch image URLs
95
  image_urls = fetch_image_urls(gender, num_images)
96
  if not image_urls:
97
- return "Failed to fetch image URLs.", None, None, None, None
98
 
99
  # Download images
100
  downloaded_count, image_paths = download_images(image_urls)
101
  if downloaded_count == 0:
102
- return "No images were successfully downloaded.", None, None, None, None
103
 
104
- # Prepare gallery data (image paths for thumbnails)
105
- gallery_data = [(path, f"Image {i+1}") for i, path in enumerate(image_paths)]
106
-
107
- return f"Successfully downloaded {downloaded_count}/{num_images} images. Select images to include in ZIP below.", None, None, gallery_data, image_paths
108
 
109
  def process_zip_submission(image_paths, *checkbox_states):
110
  """Create a ZIP file based on the selected images."""
@@ -143,34 +140,42 @@ with gr.Blocks(title="Image Downloader") as demo:
143
  zip_output = gr.File(label="Download ZIP", visible=False)
144
 
145
  gr.Markdown("### Image Gallery (Click Thumbnails to View Full Size)")
146
- gallery_output = gr.Gallery(label="Gallery", show_label=True, elem_id="gallery", columns=[5], height="auto")
147
  image_paths_state = gr.State() # Store image paths for later use
148
 
149
- # Checkbox components for toggling inclusion in ZIP
150
- checkbox_outputs = [gr.Checkbox(label=f"Include Image {i+1} in ZIP", value=True, visible=False) for i in range(25)] # Max 25 images
 
 
 
 
 
 
151
 
152
  gr.Markdown("### Submit Selections")
153
  submit_button = gr.Button("Create ZIP of Selected Images")
154
 
155
  def on_download(gender, num_images):
156
- status, zip_path, image_path, gallery_data, image_paths = process_and_display(gender, num_images)
157
  if image_paths:
158
- # Show checkboxes for the downloaded images
159
- checkbox_updates = {checkbox_outputs[i]: gr.Checkbox(value=True, visible=True, label=f"Include Image {i+1} in ZIP") for i in range(len(image_paths))}
160
- # Hide remaining checkboxes
161
- checkbox_updates.update({checkbox_outputs[i]: gr.Checkbox(value=False, visible=False) for i in range(len(image_paths), 25)})
162
- return {
163
  status_output: status,
164
  zip_output: gr.File(value=None, visible=False), # Hide ZIP initially
165
- gallery_output: gallery_data,
166
- image_paths_state: image_paths,
167
- **checkbox_updates
168
  }
 
 
 
 
 
 
 
 
169
  return {
170
  status_output: status,
171
  zip_output: gr.File(visible=False),
172
- gallery_output: None,
173
  image_paths_state: None,
 
174
  **{checkbox_outputs[i]: gr.Checkbox(value=False, visible=False) for i in range(25)}
175
  }
176
 
@@ -184,7 +189,7 @@ with gr.Blocks(title="Image Downloader") as demo:
184
  download_button.click(
185
  fn=on_download,
186
  inputs=[gender_input, num_images_input],
187
- outputs=[status_output, zip_output, gallery_output, image_paths_state] + checkbox_outputs
188
  )
189
 
190
  submit_button.click(
 
94
  # Fetch image URLs
95
  image_urls = fetch_image_urls(gender, num_images)
96
  if not image_urls:
97
+ return "Failed to fetch image URLs.", None, None, None
98
 
99
  # Download images
100
  downloaded_count, image_paths = download_images(image_urls)
101
  if downloaded_count == 0:
102
+ return "No images were successfully downloaded.", None, None, None
103
 
104
+ return f"Successfully downloaded {downloaded_count}/{num_images} images. Select images to include in ZIP below.", None, image_paths, image_paths
 
 
 
105
 
106
  def process_zip_submission(image_paths, *checkbox_states):
107
  """Create a ZIP file based on the selected images."""
 
140
  zip_output = gr.File(label="Download ZIP", visible=False)
141
 
142
  gr.Markdown("### Image Gallery (Click Thumbnails to View Full Size)")
 
143
  image_paths_state = gr.State() # Store image paths for later use
144
 
145
+ # Create a grid layout for images and checkboxes
146
+ image_outputs = []
147
+ checkbox_outputs = []
148
+ with gr.Row():
149
+ for i in range(25): # Max 25 images
150
+ with gr.Column():
151
+ image_outputs.append(gr.Image(label=f"Image {i+1}", visible=False, width=100, height=100))
152
+ checkbox_outputs.append(gr.Checkbox(label=f"Include in ZIP", value=True, visible=False))
153
 
154
  gr.Markdown("### Submit Selections")
155
  submit_button = gr.Button("Create ZIP of Selected Images")
156
 
157
  def on_download(gender, num_images):
158
+ status, zip_path, image_paths, image_paths_for_display = process_and_display(gender, num_images)
159
  if image_paths:
160
+ # Update image and checkbox components for the downloaded images
161
+ updates = {
 
 
 
162
  status_output: status,
163
  zip_output: gr.File(value=None, visible=False), # Hide ZIP initially
164
+ image_paths_state: image_paths
 
 
165
  }
166
+ for i in range(25):
167
+ if i < len(image_paths):
168
+ updates[image_outputs[i]] = gr.Image(value=image_paths[i], visible=True, label=f"Image {i+1}", width=100, height=100)
169
+ updates[checkbox_outputs[i]] = gr.Checkbox(value=True, visible=True, label=f"Include in ZIP")
170
+ else:
171
+ updates[image_outputs[i]] = gr.Image(value=None, visible=False)
172
+ updates[checkbox_outputs[i]] = gr.Checkbox(value=False, visible=False)
173
+ return updates
174
  return {
175
  status_output: status,
176
  zip_output: gr.File(visible=False),
 
177
  image_paths_state: None,
178
+ **{image_outputs[i]: gr.Image(value=None, visible=False) for i in range(25)},
179
  **{checkbox_outputs[i]: gr.Checkbox(value=False, visible=False) for i in range(25)}
180
  }
181
 
 
189
  download_button.click(
190
  fn=on_download,
191
  inputs=[gender_input, num_images_input],
192
+ outputs=[status_output, zip_output, image_paths_state] + image_outputs + checkbox_outputs
193
  )
194
 
195
  submit_button.click(