Sergidev commited on
Commit
ad67eb4
·
verified ·
1 Parent(s): 92c4989

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -22
app.py CHANGED
@@ -200,14 +200,24 @@ generation_history = []
200
 
201
  # Function to update the history list
202
  def update_history_list():
203
- return [
204
- gr.Image(value=item["image"], show_label=False, height=100, width=100).style(full_width=False, size="small")
205
- for item in generation_history
206
- ]
 
 
 
 
 
 
 
 
 
 
207
 
208
  # Function to handle image click in history
209
- def handle_image_click(evt: gr.SelectData):
210
- selected = generation_history[evt.index]
211
  return selected["image"], json.dumps(selected["metadata"], indent=2)
212
 
213
  # Modify the generate function to add results to the history
@@ -222,7 +232,7 @@ def generate_and_update_history(*args, **kwargs):
222
  })
223
  if len(generation_history) > 10: # Limit history to 10 items
224
  generation_history.pop()
225
- return images, metadata, gr.update(visible=True), update_history_list()
226
 
227
  if torch.cuda.is_available():
228
  pipe = load_pipeline(MODEL)
@@ -345,10 +355,10 @@ with gr.Blocks(css="style.css") as demo:
345
 
346
  # Add history accordion
347
  with gr.Accordion("Generation History", visible=False) as history_accordion:
348
- history_list = gr.Gallery(show_label=False, columns=5, height="auto", allow_preview=False, elem_id="history_list")
349
  with gr.Row():
350
- selected_image = gr.Image(label="Selected Image", interactive=False, elem_id="selected_image")
351
- selected_metadata = gr.JSON(label="Selected Metadata", show_label=False, elem_id="selected_metadata")
352
 
353
  gr.Examples(
354
  examples=config.examples,
@@ -398,7 +408,10 @@ with gr.Blocks(css="style.css") as demo:
398
  ).then(
399
  fn=generate_and_update_history,
400
  inputs=inputs,
401
- outputs=[result, gr_metadata, history_accordion, history_list],
 
 
 
402
  )
403
 
404
  negative_prompt.submit(
@@ -410,7 +423,10 @@ with gr.Blocks(css="style.css") as demo:
410
  ).then(
411
  fn=generate_and_update_history,
412
  inputs=inputs,
413
- outputs=[result, gr_metadata, history_accordion, history_list],
 
 
 
414
  )
415
 
416
  run_button.click(
@@ -422,21 +438,23 @@ with gr.Blocks(css="style.css") as demo:
422
  ).then(
423
  fn=generate_and_update_history,
424
  inputs=inputs,
425
- outputs=[result, gr_metadata, history_accordion, history_list],
 
 
 
426
  )
427
-
428
- # Add event handler for generate_from_json button
429
  generate_from_json.click(
430
  fn=generate_and_update_history,
431
  inputs=inputs,
432
- outputs=[result, gr_metadata, history_accordion, history_list],
 
 
 
433
  )
434
 
435
- # Add event handler for history_list
436
- history_list.select(
437
- fn=handle_image_click,
438
- inputs=None,
439
- outputs=[selected_image, selected_metadata],
440
- )
441
 
442
  demo.queue(max_size=20).launch(debug=IS_COLAB, share=IS_COLAB)
 
200
 
201
  # Function to update the history list
202
  def update_history_list():
203
+ html = "<div style='display: flex; flex-wrap: wrap;'>"
204
+ for idx, item in enumerate(generation_history):
205
+ html += f"""
206
+ <div style='margin: 10px; text-align: center;'>
207
+ <img src='data:image/png;base64,{utils.image_to_base64(item["image"])}'
208
+ style='width: 100px; height: 100px; object-fit: cover;'
209
+ onclick='handle_image_click({idx})' />
210
+ <p style='width: 100px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;'>
211
+ {item["prompt"]}
212
+ </p>
213
+ </div>
214
+ """
215
+ html += "</div>"
216
+ return html
217
 
218
  # Function to handle image click in history
219
+ def handle_image_click(idx):
220
+ selected = generation_history[idx]
221
  return selected["image"], json.dumps(selected["metadata"], indent=2)
222
 
223
  # Modify the generate function to add results to the history
 
232
  })
233
  if len(generation_history) > 10: # Limit history to 10 items
234
  generation_history.pop()
235
+ return images, metadata, gr.update(visible=True) # Update to show the accordion
236
 
237
  if torch.cuda.is_available():
238
  pipe = load_pipeline(MODEL)
 
355
 
356
  # Add history accordion
357
  with gr.Accordion("Generation History", visible=False) as history_accordion:
358
+ history_list = gr.HTML()
359
  with gr.Row():
360
+ selected_image = gr.Image(label="Selected Image", interactive=False)
361
+ selected_metadata = gr.JSON(label="Selected Metadata", show_label=False)
362
 
363
  gr.Examples(
364
  examples=config.examples,
 
408
  ).then(
409
  fn=generate_and_update_history,
410
  inputs=inputs,
411
+ outputs=[result, gr_metadata, history_accordion],
412
+ ).then(
413
+ fn=update_history_list,
414
+ outputs=history_list
415
  )
416
 
417
  negative_prompt.submit(
 
423
  ).then(
424
  fn=generate_and_update_history,
425
  inputs=inputs,
426
+ outputs=[result, gr_metadata, history_accordion],
427
+ ).then(
428
+ fn=update_history_list,
429
+ outputs=history_list
430
  )
431
 
432
  run_button.click(
 
438
  ).then(
439
  fn=generate_and_update_history,
440
  inputs=inputs,
441
+ outputs=[result, gr_metadata, history_accordion],
442
+ ).then(
443
+ fn=update_history_list,
444
+ outputs=history_list
445
  )
446
+
447
+ # Add event handler for generate_from_json button
448
  generate_from_json.click(
449
  fn=generate_and_update_history,
450
  inputs=inputs,
451
+ outputs=[result, gr_metadata, history_accordion],
452
+ ).then(
453
+ fn=update_history_list,
454
+ outputs=history_list
455
  )
456
 
457
+ # Add JavaScript for handling image clicks (todo)
458
+
 
 
 
 
459
 
460
  demo.queue(max_size=20).launch(debug=IS_COLAB, share=IS_COLAB)