linoyts HF Staff commited on
Commit
1e423ca
·
verified ·
1 Parent(s): 5889a9b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +94 -1
app.py CHANGED
@@ -4,6 +4,7 @@ import spaces
4
  import torch
5
  import random
6
  import os
 
7
 
8
  # from diffusers import QwenImageEditInpaintPipeline
9
  from optimization import optimize_pipeline_
@@ -137,6 +138,37 @@ Return only the rewritten instruction text directly, without JSON formatting or
137
  MAX_SEED = np.iinfo(np.int32).max
138
  MAX_IMAGE_SIZE = 2048
139
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
140
  # Initialize Qwen Image Edit pipeline
141
  # Scheduler configuration for Lightning
142
  scheduler_config = {
@@ -230,6 +262,9 @@ css = """
230
  width: 400px;
231
  }
232
  #edit_text{margin-top: -62px !important}
 
 
 
233
  """
234
 
235
 
@@ -278,7 +313,27 @@ with gr.Blocks(css=css) as demo:
278
  )
279
  run_button = gr.Button("Run")
280
 
281
- result = gr.ImageSlider(label="Result", show_label=False)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
282
 
283
  with gr.Accordion("Advanced Settings", open=False):
284
 
@@ -324,11 +379,49 @@ with gr.Blocks(css=css) as demo:
324
  value=True
325
  )
326
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
327
  gr.on(
328
  triggers=[run_button.click, prompt.submit],
 
 
 
 
 
329
  fn = infer,
330
  inputs = [edit_image, prompt, negative_prompt, seed, randomize_seed, strength, num_inference_steps, true_cfg_scale, rewrite_prompt],
331
  outputs = [result, seed]
 
 
 
 
 
 
 
 
 
 
332
  )
333
 
334
  demo.launch()
 
4
  import torch
5
  import random
6
  import os
7
+ import time
8
 
9
  # from diffusers import QwenImageEditInpaintPipeline
10
  from optimization import optimize_pipeline_
 
138
  MAX_SEED = np.iinfo(np.int32).max
139
  MAX_IMAGE_SIZE = 2048
140
 
141
+ # --- Helper functions for reuse feature ---
142
+ def clear_result():
143
+ """Clears the result image."""
144
+ return gr.update(value=None)
145
+
146
+ def update_history(new_image, history):
147
+ """Updates the history gallery with the new image."""
148
+ time.sleep(0.5) # Small delay to ensure image is ready
149
+ if history is None:
150
+ history = []
151
+ if new_image is not None:
152
+ # Convert to list if needed (Gradio sometimes returns tuples)
153
+ if not isinstance(history, list):
154
+ history = list(history) if history else []
155
+ history.insert(0, new_image)
156
+ # Keep only the last 20 images in history
157
+ history = history[:20]
158
+ return history
159
+
160
+ def use_history_as_input(evt: gr.SelectData, history):
161
+ """Sets the selected history image as the new input image."""
162
+ if history and evt.index < len(history):
163
+ return gr.update(value=history[evt.index][0])
164
+ return gr.update()
165
+
166
+ def use_output_as_input(output_image):
167
+ """Sets the generated output as the new input image."""
168
+ if output_image is not None:
169
+ return gr.update(value=output_image)
170
+ return gr.update()
171
+
172
  # Initialize Qwen Image Edit pipeline
173
  # Scheduler configuration for Lightning
174
  scheduler_config = {
 
262
  width: 400px;
263
  }
264
  #edit_text{margin-top: -62px !important}
265
+ .gallery-container {
266
+ margin-top: 20px;
267
+ }
268
  """
269
 
270
 
 
313
  )
314
  run_button = gr.Button("Run")
315
 
316
+ with gr.Column():
317
+ result = gr.ImageSlider(label="Result", show_label=False)
318
+
319
+ use_as_input_button = gr.Button("🔄 Use as Input Image", visible=False, variant="secondary")
320
+
321
+ gr.Markdown("---")
322
+
323
+ with gr.Row():
324
+ gr.Markdown("### 📜 History")
325
+ clear_history_button = gr.Button("🗑️ Clear History", size="sm", variant="stop")
326
+
327
+ history_gallery = gr.Gallery(
328
+ label="Click any image to use as input",
329
+ columns=4,
330
+ rows=2,
331
+ object_fit="contain",
332
+ height="auto",
333
+ interactive=False,
334
+ show_label=True,
335
+ elem_classes=["gallery-container"]
336
+ )
337
 
338
  with gr.Accordion("Advanced Settings", open=False):
339
 
 
379
  value=True
380
  )
381
 
382
+ # Event handlers for reuse functionality
383
+ use_as_input_button.click(
384
+ fn=use_output_as_input,
385
+ inputs=[result],
386
+ outputs=[edit_image],
387
+ show_api=False
388
+ )
389
+
390
+ history_gallery.select(
391
+ fn=use_history_as_input,
392
+ inputs=[history_gallery],
393
+ outputs=[edit_image],
394
+ show_api=False
395
+ )
396
+
397
+ clear_history_button.click(
398
+ fn=lambda: [],
399
+ inputs=None,
400
+ outputs=history_gallery,
401
+ show_api=False
402
+ )
403
+
404
+ # Main generation pipeline with result clearing, history update, and button visibility
405
  gr.on(
406
  triggers=[run_button.click, prompt.submit],
407
+ fn=clear_result,
408
+ inputs=None,
409
+ outputs=result,
410
+ show_api=False
411
+ ).then(
412
  fn = infer,
413
  inputs = [edit_image, prompt, negative_prompt, seed, randomize_seed, strength, num_inference_steps, true_cfg_scale, rewrite_prompt],
414
  outputs = [result, seed]
415
+ ).then(
416
+ fn=lambda: gr.update(visible=True),
417
+ inputs=None,
418
+ outputs=use_as_input_button,
419
+ show_api=False
420
+ ).then(
421
+ fn=update_history,
422
+ inputs=[result, history_gallery],
423
+ outputs=history_gallery,
424
+ show_api=False
425
  )
426
 
427
  demo.launch()