linoyts HF Staff commited on
Commit
6a8deb5
·
verified ·
1 Parent(s): a2cff3a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +54 -1
app.py CHANGED
@@ -20,6 +20,7 @@ import os
20
  import base64
21
  from io import BytesIO
22
  import json
 
23
 
24
  SYSTEM_PROMPT = '''
25
  # Edit Instruction Rewriter
@@ -299,7 +300,25 @@ def next_scene_prompt(original_prompt, img_list):
299
  return original_prompt
300
 
301
 
302
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
303
  def encode_image(pil_image):
304
  import io
305
  buffered = io.BytesIO()
@@ -470,6 +489,20 @@ with gr.Blocks(css=css) as demo:
470
  # Add this button right after the result gallery - initially hidden
471
  use_output_btn = gr.Button("↗️ Use as input", variant="secondary", size="sm", visible=False)
472
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
473
  with gr.Row():
474
  prompt = gr.Text(
475
  label="Prompt",
@@ -546,6 +579,11 @@ with gr.Blocks(css=css) as demo:
546
  rewrite_prompt,
547
  ],
548
  outputs=[result, seed, use_output_btn], # Added use_output_btn to outputs
 
 
 
 
 
549
  )
550
 
551
  # Add the new event handler for the "Use Output as Input" button
@@ -555,6 +593,21 @@ with gr.Blocks(css=css) as demo:
555
  outputs=[input_images]
556
  )
557
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
558
  input_images.change(fn=suggest_next_scene_prompt, inputs=[input_images], outputs=[prompt])
559
 
560
  if __name__ == "__main__":
 
20
  import base64
21
  from io import BytesIO
22
  import json
23
+ import time # Added for history update delay
24
 
25
  SYSTEM_PROMPT = '''
26
  # Edit Instruction Rewriter
 
300
  return original_prompt
301
 
302
 
303
+ def update_history(new_images, history):
304
+ """Updates the history gallery with the new images."""
305
+ time.sleep(0.5) # Small delay to ensure images are ready
306
+ if history is None:
307
+ history = []
308
+ if new_images is not None and len(new_images) > 0:
309
+ if not isinstance(history, list):
310
+ history = list(history) if history else []
311
+ for img in new_images:
312
+ history.insert(0, img)
313
+ history = history[:20] # Keep only last 20 images
314
+ return history
315
+
316
+ def use_history_as_input(evt: gr.SelectData):
317
+ """Sets the selected history image as the new input image."""
318
+ if evt.value is not None:
319
+ return gr.update(value=[(evt.value,)])
320
+ return gr.update()
321
+
322
  def encode_image(pil_image):
323
  import io
324
  buffered = io.BytesIO()
 
489
  # Add this button right after the result gallery - initially hidden
490
  use_output_btn = gr.Button("↗️ Use as input", variant="secondary", size="sm", visible=False)
491
 
492
+ with gr.Row():
493
+ gr.Markdown("### 📜 History")
494
+ clear_history_button = gr.Button("🗑️ Clear History", size="sm", variant="stop")
495
+
496
+ history_gallery = gr.Gallery(
497
+ label="Click any image to use as input",
498
+ columns=4,
499
+ rows=2,
500
+ object_fit="contain",
501
+ height="auto",
502
+ interactive=False,
503
+ show_label=True
504
+ )
505
+
506
  with gr.Row():
507
  prompt = gr.Text(
508
  label="Prompt",
 
579
  rewrite_prompt,
580
  ],
581
  outputs=[result, seed, use_output_btn], # Added use_output_btn to outputs
582
+ ).then(
583
+ fn=update_history,
584
+ inputs=[result, history_gallery],
585
+ outputs=history_gallery,
586
+
587
  )
588
 
589
  # Add the new event handler for the "Use Output as Input" button
 
593
  outputs=[input_images]
594
  )
595
 
596
+ # History gallery event handlers
597
+ history_gallery.select(
598
+ fn=use_history_as_input,
599
+ inputs=None,
600
+ outputs=[input_images],
601
+
602
+ )
603
+
604
+ clear_history_button.click(
605
+ fn=lambda: [],
606
+ inputs=None,
607
+ outputs=history_gallery,
608
+
609
+ )
610
+
611
  input_images.change(fn=suggest_next_scene_prompt, inputs=[input_images], outputs=[prompt])
612
 
613
  if __name__ == "__main__":