refactor: prompt logic

#5
by LPX55 - opened
Files changed (1) hide show
  1. app_local.py +91 -3
app_local.py CHANGED
@@ -6,7 +6,7 @@ import spaces
6
  from PIL import Image
7
  from diffusers import QwenImageEditPipeline, FlowMatchEulerDiscreteScheduler
8
  from diffusers.utils import is_xformers_available
9
- from presets import PRESETS, get_preset_choices, get_preset_info
10
 
11
  import os
12
  import sys
@@ -426,8 +426,9 @@ def infer(
426
  f"</div>"
427
  )
428
 
429
-
430
  with gr.Blocks(title="Qwen Image Edit - Fast Lightning Mode w/ Batch") as demo:
 
 
431
  gr.Markdown("""
432
  <div style="text-align: center; background: linear-gradient(to right, #3a7bd5, #00d2ff); color: white; padding: 20px; border-radius: 8px;">
433
  <h1 style="margin-bottom: 5px;">⚡️ Qwen-Image-Edit Lightning</h1>
@@ -459,6 +460,16 @@ with gr.Blocks(title="Qwen Image Edit - Fast Lightning Mode w/ Batch") as demo:
459
  label="Preset Batch Generation",
460
  interactive=True
461
  )
 
 
 
 
 
 
 
 
 
 
462
  rewrite_toggle = gr.Checkbox(
463
  label="Enable Prompt Enhancement",
464
  value=True,
@@ -506,12 +517,14 @@ with gr.Blocks(title="Qwen Image Edit - Fast Lightning Mode w/ Batch") as demo:
506
  step=1,
507
  value=4
508
  )
 
509
  num_images_per_prompt = gr.Slider(
510
  label="Output Count (Manual)",
511
  minimum=1,
512
  maximum=4,
513
  step=1,
514
- value=1
 
515
  )
516
 
517
  # Output Column
@@ -527,7 +540,75 @@ with gr.Blocks(title="Qwen Image Edit - Fast Lightning Mode w/ Batch") as demo:
527
  value="<div style='padding:15px; margin-top:15px'>"
528
  "Prompt details will appear after generation. Ability to edit Preset Prompts on the fly will be implemented shortly.</div>"
529
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
530
 
 
 
 
 
 
 
 
 
 
 
 
531
  # Set up prompt preview update
532
  preset_dropdown.change(
533
  fn=update_prompt_preview,
@@ -541,6 +622,13 @@ with gr.Blocks(title="Qwen Image Edit - Fast Lightning Mode w/ Batch") as demo:
541
  outputs=prompt_preview
542
  )
543
 
 
 
 
 
 
 
 
544
  # Set up processing
545
  inputs = [
546
  input_image,
 
6
  from PIL import Image
7
  from diffusers import QwenImageEditPipeline, FlowMatchEulerDiscreteScheduler
8
  from diffusers.utils import is_xformers_available
9
+ from presets import PRESETS, get_preset_choices, get_preset_info, update_preset_prompt
10
 
11
  import os
12
  import sys
 
426
  f"</div>"
427
  )
428
 
 
429
  with gr.Blocks(title="Qwen Image Edit - Fast Lightning Mode w/ Batch") as demo:
430
+ preset_prompts_state = gr.State(value=["", "", "", ""])
431
+
432
  gr.Markdown("""
433
  <div style="text-align: center; background: linear-gradient(to right, #3a7bd5, #00d2ff); color: white; padding: 20px; border-radius: 8px;">
434
  <h1 style="margin-bottom: 5px;">⚡️ Qwen-Image-Edit Lightning</h1>
 
460
  label="Preset Batch Generation",
461
  interactive=True
462
  )
463
+ # Add editable preset prompts (initially hidden)
464
+ preset_editor = gr.Group(visible=False)
465
+ with preset_editor:
466
+ gr.Markdown("### 🎨 Edit Preset Prompts")
467
+ preset_prompt_1 = gr.Textbox(label="Prompt 1", lines=1)
468
+ preset_prompt_2 = gr.Textbox(label="Prompt 2", lines=1)
469
+ preset_prompt_3 = gr.Textbox(label="Prompt 3", lines=1)
470
+ preset_prompt_4 = gr.Textbox(label="Prompt 4", lines=1)
471
+ update_preset_button = gr.Button("Update Preset", variant="secondary")
472
+
473
  rewrite_toggle = gr.Checkbox(
474
  label="Enable Prompt Enhancement",
475
  value=True,
 
517
  step=1,
518
  value=4
519
  )
520
+ ### CHANGED ### - Added interactive parameter
521
  num_images_per_prompt = gr.Slider(
522
  label="Output Count (Manual)",
523
  minimum=1,
524
  maximum=4,
525
  step=1,
526
+ value=1,
527
+ interactive=True ### NEW ### - Default to interactive
528
  )
529
 
530
  # Output Column
 
540
  value="<div style='padding:15px; margin-top:15px'>"
541
  "Prompt details will appear after generation. Ability to edit Preset Prompts on the fly will be implemented shortly.</div>"
542
  )
543
+
544
+ def toggle_output_count(preset_type):
545
+ if preset_type and preset_type in PRESETS:
546
+ # When preset is selected, disable manual output count
547
+ preset = PRESETS[preset_type]
548
+ return (
549
+ gr.Group(visible=True),
550
+ gr.Slider(interactive=False), # Disable slider
551
+ preset_prompt_1.update(value=preset["prompts"][0] if len(preset["prompts"]) > 0 else ""),
552
+ preset_prompt_2.update(value=preset["prompts"][1] if len(preset["prompts"]) > 1 else ""),
553
+ preset_prompt_3.update(value=preset["prompts"][2] if len(preset["prompts"]) > 2 else ""),
554
+ preset_prompt_4.update(value=preset["prompts"][3] if len(preset["prompts"]) > 3 else "")
555
+ )
556
+ else:
557
+ # When no preset is selected, enable manual output count
558
+ return (
559
+ gr.Group(visible=False),
560
+ gr.Slider(interactive=True), # Enable slider
561
+ gr.Textbox(value=""), gr.Textbox(value=""), gr.Textbox(value=""), gr.Textbox(value="")
562
+ )
563
+
564
+ # Function to show preset editor when a preset is selected
565
+ def show_preset_editor(preset_type):
566
+ if preset_type and preset_type in PRESETS:
567
+ preset = PRESETS[preset_type]
568
+ prompts = preset["prompts"]
569
+ # Pad prompts to 4 items if needed
570
+ while len(prompts) < 4:
571
+ prompts.append("")
572
+ return gr.Group(visible=True), prompts[0], prompts[1], prompts[2], prompts[3]
573
+ return gr.Group(visible=False), "", "", "", ""
574
+ # Function to update prompt preview when preset is selected
575
+ def update_prompt_preview(preset_type, base_prompt):
576
+ """Update the prompt preview display based on selected preset and base prompt"""
577
+ if preset_type and preset_type in PRESETS:
578
+ preset = PRESETS[preset_type]
579
+ preview_text = f"**Preset: {preset_type}**\n\n"
580
+ preview_text += f"*{preset['description']}*\n\n"
581
+ preview_text += "**Generated Prompts:**\n"
582
+
583
+ for i, preset_prompt in enumerate(preset["prompts"], 1):
584
+ full_prompt = f"{base_prompt}, {preset_prompt}"
585
+ preview_text += f"{i}. {full_prompt}\n"
586
+
587
+ return preview_text
588
+ else:
589
+ return "Select a preset above to see how your base prompt will be modified for batch generation."
590
+
591
+ # Function to update individual preset prompts
592
+ def update_preset_prompt_textbox(preset_type, prompt_1, prompt_2, prompt_3, prompt_4):
593
+ """Update preset prompts based on user input"""
594
+ if preset_type and preset_type in PRESETS:
595
+ # Update each prompt in the preset
596
+ new_prompts = [prompt_1, prompt_2, prompt_3, prompt_4]
597
+ for i, new_prompt in enumerate(new_prompts):
598
+ if new_prompt and i < len(PRESETS[preset_type]["prompts"]):
599
+ PRESETS[preset_type]["prompts"][i] = new_prompt
600
 
601
+ # Return updated preset info for preview
602
+ return update_prompt_preview(preset_type, "your subject") # Use placeholder for preview
603
+ return "Select a preset first to edit its prompts."
604
+
605
+
606
+ preset_dropdown.change(
607
+ fn=toggle_output_count, ### CHANGED ### - Using new function
608
+ inputs=preset_dropdown,
609
+ outputs=[preset_editor, num_images_per_prompt, preset_prompt_1, preset_prompt_2, preset_prompt_3, preset_prompt_4]
610
+ )
611
+
612
  # Set up prompt preview update
613
  preset_dropdown.change(
614
  fn=update_prompt_preview,
 
622
  outputs=prompt_preview
623
  )
624
 
625
+ # Set up preset editor update button
626
+ update_preset_button.click(
627
+ fn=update_preset_prompt_textbox,
628
+ inputs=[preset_dropdown, preset_prompt_1, preset_prompt_2, preset_prompt_3, preset_prompt_4],
629
+ outputs=prompt_preview
630
+ )
631
+
632
  # Set up processing
633
  inputs = [
634
  input_image,