Sergidev commited on
Commit
07a8fae
1 Parent(s): d436773
Files changed (1) hide show
  1. app.py +67 -38
app.py CHANGED
@@ -198,27 +198,41 @@ def generate(
198
  # Initialize an empty list to store the generation history
199
  generation_history = []
200
 
201
- # Function to update the history dropdown
202
- def update_history_dropdown():
203
- return [f"{item['prompt']} ({item['timestamp']})" for item in generation_history]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
204
 
205
  # Modify the generate function to add results to the history
206
  def generate_and_update_history(*args, **kwargs):
207
  images, metadata = generate(*args, **kwargs)
208
  timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
209
- generation_history.insert(0, {"prompt": metadata["prompt"], "timestamp": timestamp, "image": images[0], "metadata": metadata})
 
 
 
 
 
210
  if len(generation_history) > 10: # Limit history to 10 items
211
  generation_history.pop()
212
- return images, metadata, update_history_dropdown()
213
-
214
- # Function to display selected history item
215
- def display_history_item(selected_item):
216
- if not selected_item:
217
- return None, None
218
- for item in generation_history:
219
- if f"{item['prompt']} ({item['timestamp']})" == selected_item:
220
- return item['image'], json.dumps(item['metadata'], indent=2)
221
- return None, None
222
 
223
  if torch.cuda.is_available():
224
  pipe = load_pipeline(MODEL)
@@ -307,7 +321,6 @@ with gr.Blocks(css="style.css") as demo:
307
  value=1.5,
308
  visible=False,
309
  )
310
-
311
  sampler = gr.Dropdown(
312
  label="Sampler",
313
  choices=config.sampler_list,
@@ -340,10 +353,12 @@ with gr.Blocks(css="style.css") as demo:
340
  json_input = gr.TextArea(label="Edit/Paste JSON Parameters", placeholder="Paste or edit JSON parameters here")
341
  generate_from_json = gr.Button("Generate from JSON")
342
 
343
- # Add history dropdown
344
- history_dropdown = gr.Dropdown(label="Generation History", choices=[], interactive=True)
345
- history_image = gr.Image(label="Selected Image", interactive=False)
346
- history_metadata = gr.JSON(label="Selected Metadata", show_label=False)
 
 
347
 
348
  gr.Examples(
349
  examples=config.examples,
@@ -381,7 +396,7 @@ with gr.Blocks(css="style.css") as demo:
381
  use_upscaler,
382
  upscaler_strength,
383
  upscale_by,
384
- json_input, # Add JSON input to the list of inputs
385
  ]
386
 
387
  prompt.submit(
@@ -391,11 +406,14 @@ with gr.Blocks(css="style.css") as demo:
391
  queue=False,
392
  api_name=False,
393
  ).then(
394
- fn=generate_and_update_history, # Use the new function
395
  inputs=inputs,
396
- outputs=[result, gr_metadata, history_dropdown], # Add history_dropdown to outputs
397
- api_name="run",
 
 
398
  )
 
399
  negative_prompt.submit(
400
  fn=utils.randomize_seed_fn,
401
  inputs=[seed, randomize_seed],
@@ -403,11 +421,14 @@ with gr.Blocks(css="style.css") as demo:
403
  queue=False,
404
  api_name=False,
405
  ).then(
406
- fn=generate_and_update_history, # Use the new function
407
  inputs=inputs,
408
- outputs=[result, gr_metadata, history_dropdown], # Add history_dropdown to outputs
409
- api_name=False,
 
 
410
  )
 
411
  run_button.click(
412
  fn=utils.randomize_seed_fn,
413
  inputs=[seed, randomize_seed],
@@ -415,25 +436,33 @@ with gr.Blocks(css="style.css") as demo:
415
  queue=False,
416
  api_name=False,
417
  ).then(
418
- fn=generate_and_update_history, # Use the new function
419
  inputs=inputs,
420
- outputs=[result, gr_metadata, history_dropdown], # Add history_dropdown to outputs
421
- api_name=False,
 
 
422
  )
423
 
424
  # Add event handler for generate_from_json button
425
  generate_from_json.click(
426
  fn=generate_and_update_history,
427
  inputs=inputs,
428
- outputs=[result, gr_metadata, history_dropdown],
429
- api_name=False,
 
 
430
  )
431
 
432
- # Add event handler for history_dropdown
433
- history_dropdown.change(
434
- fn=display_history_item,
435
- inputs=[history_dropdown],
436
- outputs=[history_image, history_metadata],
437
- )
 
 
 
 
438
 
439
- demo.queue(max_size=20).launch(debug=IS_COLAB, share=IS_COLAB)
 
198
  # Initialize an empty list to store the generation history
199
  generation_history = []
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
224
  def generate_and_update_history(*args, **kwargs):
225
  images, metadata = generate(*args, **kwargs)
226
  timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
227
+ generation_history.insert(0, {
228
+ "prompt": metadata["prompt"],
229
+ "timestamp": timestamp,
230
+ "image": images[0],
231
+ "metadata": metadata
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)
 
321
  value=1.5,
322
  visible=False,
323
  )
 
324
  sampler = gr.Dropdown(
325
  label="Sampler",
326
  choices=config.sampler_list,
 
353
  json_input = gr.TextArea(label="Edit/Paste JSON Parameters", placeholder="Paste or edit JSON parameters here")
354
  generate_from_json = gr.Button("Generate from JSON")
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,
 
396
  use_upscaler,
397
  upscaler_strength,
398
  upscale_by,
399
+ json_input,
400
  ]
401
 
402
  prompt.submit(
 
406
  queue=False,
407
  api_name=False,
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(
418
  fn=utils.randomize_seed_fn,
419
  inputs=[seed, randomize_seed],
 
421
  queue=False,
422
  api_name=False,
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(
433
  fn=utils.randomize_seed_fn,
434
  inputs=[seed, randomize_seed],
 
436
  queue=False,
437
  api_name=False,
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
458
+ demo.load(None, None, None, _js="""
459
+ function handle_image_click(idx) {
460
+ const images = document.querySelectorAll('#history_list img');
461
+ images.forEach(img => img.style.border = 'none');
462
+ images[idx].style.border = '2px solid blue';
463
+ gradioApp().querySelector('#selected_image img').src = images[idx].src;
464
+ gradioApp().querySelector('#selected_metadata textarea').value = JSON.stringify(generation_history[idx].metadata, null, 2);
465
+ }
466
+ """)
467
 
468
+ demo.queue(max_size=20).launch(debug=IS_COLAB, share=IS_COLAB)