Sergidev commited on
Commit
e92b65a
1 Parent(s): 0a5ee6c
Files changed (1) hide show
  1. app.py +29 -14
app.py CHANGED
@@ -219,26 +219,34 @@ def generate(
219
  pipe.scheduler = backup_scheduler
220
  utils.free_memory()
221
 
222
- generation_history = []
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
223
 
224
  def update_history_list():
225
- return [item["image"] for item in generation_history]
226
 
227
  def handle_image_click(evt: gr.SelectData):
228
- selected = generation_history[evt.index]
229
  return selected["image"], json.dumps(selected["metadata"], indent=2)
230
 
231
  def generate_and_update_history(*args, **kwargs):
232
  images, metadata = generate(*args, **kwargs)
233
- timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
234
- generation_history.insert(0, {
235
- "prompt": metadata["prompt"],
236
- "timestamp": timestamp,
237
- "image": images[0],
238
- "metadata": metadata
239
- })
240
- if len(generation_history) > 20:
241
- generation_history.pop()
242
  return images[0], json.dumps(metadata, indent=2), update_history_list()
243
 
244
  with open('characterfull.txt', 'r') as f:
@@ -372,12 +380,13 @@ with gr.Blocks(css="style.css") as demo:
372
  generate_from_json = gr.Button("Generate from JSON")
373
 
374
  with gr.Accordion("Generation History", open=False) as history_accordion:
 
375
  history_gallery = gr.Gallery(
376
  label="History",
377
  show_label=False,
378
  elem_id="history_gallery",
379
  columns=5,
380
- rows=2,
381
  height="auto"
382
  )
383
  with gr.Row():
@@ -483,5 +492,11 @@ with gr.Blocks(css="style.css") as demo:
483
  inputs=[],
484
  outputs=[selected_image, selected_metadata]
485
  )
486
-
 
 
 
 
 
 
487
  demo.queue(max_size=20).launch(debug=IS_COLAB, share=IS_COLAB)
 
219
  pipe.scheduler = backup_scheduler
220
  utils.free_memory()
221
 
222
+ def fetch_recent_images(num_images=20):
223
+ image_files = [f for f in os.listdir(OUTPUT_DIR) if f.endswith('.png')]
224
+ image_files.sort(key=lambda x: os.path.getmtime(os.path.join(OUTPUT_DIR, x)), reverse=True)
225
+ recent_images = []
226
+
227
+ for img_file in image_files[:num_images]:
228
+ img_path = os.path.join(OUTPUT_DIR, img_file)
229
+ img = Image.open(img_path)
230
+ metadata = utils.get_image_metadata(img_path)
231
+ recent_images.append({
232
+ "image": img_path,
233
+ "prompt": metadata.get("prompt", ""),
234
+ "timestamp": datetime.fromtimestamp(os.path.getmtime(img_path)).strftime("%Y-%m-%d %H:%M:%S"),
235
+ "metadata": metadata
236
+ })
237
+
238
+ return recent_images
239
 
240
  def update_history_list():
241
+ return fetch_recent_images()
242
 
243
  def handle_image_click(evt: gr.SelectData):
244
+ selected = fetch_recent_images()[evt.index]
245
  return selected["image"], json.dumps(selected["metadata"], indent=2)
246
 
247
  def generate_and_update_history(*args, **kwargs):
248
  images, metadata = generate(*args, **kwargs)
249
+ utils.save_image(images[0], metadata, OUTPUT_DIR)
 
 
 
 
 
 
 
 
250
  return images[0], json.dumps(metadata, indent=2), update_history_list()
251
 
252
  with open('characterfull.txt', 'r') as f:
 
380
  generate_from_json = gr.Button("Generate from JSON")
381
 
382
  with gr.Accordion("Generation History", open=False) as history_accordion:
383
+ update_gallery_button = gr.Button("Update Gallery")
384
  history_gallery = gr.Gallery(
385
  label="History",
386
  show_label=False,
387
  elem_id="history_gallery",
388
  columns=5,
389
+ rows=4,
390
  height="auto"
391
  )
392
  with gr.Row():
 
492
  inputs=[],
493
  outputs=[selected_image, selected_metadata]
494
  )
495
+
496
+ update_gallery_button.click(
497
+ fn=update_history_list,
498
+ inputs=[],
499
+ outputs=[history_gallery]
500
+ )
501
+
502
  demo.queue(max_size=20).launch(debug=IS_COLAB, share=IS_COLAB)