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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -49
app.py CHANGED
@@ -85,14 +85,8 @@ def create_zip_file(selected_image_paths):
85
  zipf.write(image_path, arcname)
86
  return ZIP_FILE
87
 
88
- def image_to_base64(image_path):
89
- """Convert an image file to a base64 string for HTML embedding."""
90
- with open(image_path, "rb") as image_file:
91
- encoded_string = base64.b64encode(image_file.read()).decode("utf-8")
92
- return f"data:image/jpeg;base64,{encoded_string}"
93
-
94
  def process_and_display(gender, num_images):
95
- """Fetch and download images, then display them as thumbnails with toggles."""
96
  num_images = int(num_images) # Convert dropdown selection to integer
97
  if num_images > 25:
98
  num_images = 25 # Limit to 25 images
@@ -107,42 +101,10 @@ def process_and_display(gender, num_images):
107
  if downloaded_count == 0:
108
  return "No images were successfully downloaded.", None, None, None, None
109
 
110
- # Prepare thumbnail gallery with JavaScript to update hidden checkboxes
111
- gallery_html = """
112
- <div style='display: flex; flex-wrap: wrap; gap: 10px;'>
113
- """
114
- checkbox_states = []
115
- for idx, image_path in enumerate(image_paths, 1):
116
- base64_image = image_to_base64(image_path)
117
- # Create a thumbnail that opens full-size image in a new tab
118
- thumbnail_html = f"""
119
- <div style='text-align: center;'>
120
- <a href='file://{image_path}' target='_blank'>
121
- <img src='{base64_image}' style='width: 100px; height: 100px; object-fit: cover;' />
122
- </a>
123
- <div>
124
- <input type='checkbox' id='checkbox_{idx}' name='checkbox_{idx}' checked onchange='updateCheckbox({idx}, this.checked)'>
125
- <label for='checkbox_{idx}'>Include in ZIP</label>
126
- </div>
127
- </div>
128
- """
129
- gallery_html += thumbnail_html
130
- checkbox_states.append(True) # Default to all images selected
131
- gallery_html += """
132
- </div>
133
- <script>
134
- function updateCheckbox(index, isChecked) {
135
- // Find the hidden Gradio checkbox input by index and update its value
136
- var gradioCheckbox = document.querySelector(`input[type='checkbox'][data-index='${index}']`);
137
- if (gradioCheckbox) {
138
- gradioCheckbox.checked = isChecked;
139
- gradioCheckbox.dispatchEvent(new Event('change')); // Trigger Gradio update
140
- }
141
- }
142
- </script>
143
- """
144
 
145
- return f"Successfully downloaded {downloaded_count}/{num_images} images. Select images to include in ZIP below.", None, None, gallery_html, image_paths
146
 
147
  def process_zip_submission(image_paths, *checkbox_states):
148
  """Create a ZIP file based on the selected images."""
@@ -181,31 +143,35 @@ with gr.Blocks(title="Image Downloader") as demo:
181
  zip_output = gr.File(label="Download ZIP", visible=False)
182
 
183
  gr.Markdown("### Image Gallery (Click Thumbnails to View Full Size)")
184
- gallery_output = gr.HTML(label="Gallery")
185
  image_paths_state = gr.State() # Store image paths for later use
186
 
187
- # Hidden checkbox components to track toggle states, with data-index for JavaScript
188
- checkbox_outputs = [gr.Checkbox(visible=False, elem_id=f"checkbox_hidden_{i}", label=f"Hidden Checkbox {i}") for i in range(25)] # Max 25 images
189
 
190
  gr.Markdown("### Submit Selections")
191
  submit_button = gr.Button("Create ZIP of Selected Images")
192
 
193
  def on_download(gender, num_images):
194
- status, zip_path, image_path, gallery_html, image_paths = process_and_display(gender, num_images)
195
  if image_paths:
 
 
 
 
196
  return {
197
  status_output: status,
198
  zip_output: gr.File(value=None, visible=False), # Hide ZIP initially
199
- gallery_output: gallery_html,
200
  image_paths_state: image_paths,
201
- **{checkbox_outputs[i]: gr.Checkbox(value=True, visible=False, elem_id=f"checkbox_hidden_{i}") for i in range(len(image_paths))}
202
  }
203
  return {
204
  status_output: status,
205
  zip_output: gr.File(visible=False),
206
  gallery_output: None,
207
  image_paths_state: None,
208
- **{checkbox_outputs[i]: gr.Checkbox(value=False, visible=False, elem_id=f"checkbox_hidden_{i}") for i in range(25)}
209
  }
210
 
211
  def on_submit(image_paths, *checkbox_states):
 
85
  zipf.write(image_path, arcname)
86
  return ZIP_FILE
87
 
 
 
 
 
 
 
88
  def process_and_display(gender, num_images):
89
+ """Fetch and download images, then prepare data for display."""
90
  num_images = int(num_images) # Convert dropdown selection to integer
91
  if num_images > 25:
92
  num_images = 25 # Limit to 25 images
 
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
  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
 
177
  def on_submit(image_paths, *checkbox_states):