LPX55 commited on
Commit
a40ef4c
·
verified ·
1 Parent(s): ecbdee3

Update app_local.py

Browse files
Files changed (1) hide show
  1. app_local.py +96 -59
app_local.py CHANGED
@@ -242,35 +242,19 @@ try:
242
  except Exception as e:
243
  print(f"VAE Slicing Failed: {e}")
244
 
245
- # Function to update prompt preview when preset is selected
246
- def update_prompt_preview(preset_type, base_prompt):
247
- """Update the prompt preview display based on selected preset and base prompt"""
248
- if preset_type and preset_type in PRESETS:
249
- preset = PRESETS[preset_type]
250
- preview_text = f"**Preset: {preset_type}**\n\n"
251
- preview_text += f"*{preset['description']}*\n\n"
252
- preview_text += "**Generated Prompts:**\n"
253
-
254
- for i, preset_prompt in enumerate(preset["prompts"], 1):
255
- full_prompt = f"{base_prompt}, {preset_prompt}"
256
- preview_text += f"{i}. {full_prompt}\n"
257
-
258
- return preview_text
259
- else:
260
- return "Select a preset above to see how your base prompt will be modified for batch generation."
261
-
262
  def toggle_output_count(preset_type):
263
  """Control output count slider interactivity and show/hide preset editor"""
264
  if preset_type and preset_type in PRESETS:
265
  # When preset is selected, disable manual output count and show editor
266
  preset = PRESETS[preset_type]
267
- prompts = preset["prompts"]
268
  # Pad prompts to 4 items if needed
269
  while len(prompts) < 4:
270
  prompts.append("")
271
  return (
272
  gr.Group(visible=True),
273
- gr.Slider(interactive=False, value=preset["count"]), # Disable slider
274
  prompts[0], prompts[1], prompts[2], prompts[3] # Populate preset prompts
275
  )
276
  else:
@@ -280,8 +264,48 @@ def toggle_output_count(preset_type):
280
  gr.Slider(interactive=True), # Enable slider
281
  "", "", "", "" # Clear preset prompts
282
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
283
 
 
 
 
 
 
 
 
 
 
 
284
 
 
 
 
 
 
 
 
 
 
285
  @spaces.GPU()
286
  def infer(
287
  image,
@@ -344,16 +368,31 @@ def infer(
344
  # Handle preset batch generation
345
  if preset_type and preset_type in PRESETS:
346
  preset = PRESETS[preset_type]
347
- batch_prompts = [f"{original_prompt}, {preset_prompt}" for preset_prompt in preset["prompts"]]
348
- num_images_per_prompt = preset["count"]
349
- prompt_info = (
350
- f"<div style='margin:10px; padding:15px; border-radius:8px; border-left:4px solid #2196F3; background: #f0f8ff'>"
351
- f"<h4 style='margin-top: 0;'>🎨 Preset: {preset_type}</h4>"
352
- f"<p>{preset['description']}</p>"
353
- f"<p><strong>Base Prompt:</strong> {original_prompt}</p>"
354
- f"</div>"
355
- )
356
- print(f"Using preset: {preset_type} with {len(batch_prompts)} variations")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
357
  else:
358
  batch_prompts = [prompt] # Single prompt in list
359
 
@@ -573,37 +612,13 @@ with gr.Blocks(title="Qwen Image Edit - Fast Lightning Mode w/ Batch") as demo:
573
  prompts.append("")
574
  return gr.Group(visible=True), prompts[0], prompts[1], prompts[2], prompts[3]
575
  return gr.Group(visible=False), "", "", "", ""
576
- # Function to update prompt preview when preset is selected
577
- def update_prompt_preview(preset_type, base_prompt):
578
- """Update the prompt preview display based on selected preset and base prompt"""
579
- if preset_type and preset_type in PRESETS:
580
- preset = PRESETS[preset_type]
581
- preview_text = f"**Preset: {preset_type}**\n\n"
582
- preview_text += f"*{preset['description']}*\n\n"
583
- preview_text += "**Generated Prompts:**\n"
584
-
585
- for i, preset_prompt in enumerate(preset["prompts"], 1):
586
- full_prompt = f"{base_prompt}, {preset_prompt}"
587
- preview_text += f"{i}. {full_prompt}\n"
588
-
589
- return preview_text
590
- else:
591
- return "Select a preset above to see how your base prompt will be modified for batch generation."
592
-
593
- # Function to update individual preset prompts
594
- def update_preset_prompt_textbox(preset_type, prompt_1, prompt_2, prompt_3, prompt_4):
595
- """Update preset prompts based on user input"""
596
  if preset_type and preset_type in PRESETS:
597
- # Update each prompt in the preset
598
- new_prompts = [prompt_1, prompt_2, prompt_3, prompt_4]
599
- for i, new_prompt in enumerate(new_prompts):
600
- if new_prompt and i < len(PRESETS[preset_type]["prompts"]):
601
- PRESETS[preset_type]["prompts"][i] = new_prompt
602
-
603
- # Return updated preset info for preview
604
- return update_prompt_preview(preset_type, "your subject") # Use placeholder for preview
605
- return "Select a preset first to edit its prompts."
606
-
607
 
608
  # Set up preset dropdown change handler - now controls output count slider
609
  preset_dropdown.change(
@@ -619,6 +634,28 @@ with gr.Blocks(title="Qwen Image Edit - Fast Lightning Mode w/ Batch") as demo:
619
  outputs=prompt_preview
620
  )
621
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
622
  prompt.change(
623
  fn=update_prompt_preview,
624
  inputs=[preset_dropdown, prompt],
 
242
  except Exception as e:
243
  print(f"VAE Slicing Failed: {e}")
244
 
245
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
246
  def toggle_output_count(preset_type):
247
  """Control output count slider interactivity and show/hide preset editor"""
248
  if preset_type and preset_type in PRESETS:
249
  # When preset is selected, disable manual output count and show editor
250
  preset = PRESETS[preset_type]
251
+ prompts = preset["prompts"][:4] # Get up to 4 prompts
252
  # Pad prompts to 4 items if needed
253
  while len(prompts) < 4:
254
  prompts.append("")
255
  return (
256
  gr.Group(visible=True),
257
+ gr.Slider(interactive=False, value=len([p for p in prompts if p.strip()])), # Count non-empty prompts
258
  prompts[0], prompts[1], prompts[2], prompts[3] # Populate preset prompts
259
  )
260
  else:
 
264
  gr.Slider(interactive=True), # Enable slider
265
  "", "", "", "" # Clear preset prompts
266
  )
267
+ ### FIXED ### - Updated prompt preview to show only non-empty prompts
268
+ def update_prompt_preview(preset_type, base_prompt):
269
+ """Update the prompt preview display based on selected preset and base prompt"""
270
+ if preset_type and preset_type in PRESETS:
271
+ preset = PRESETS[preset_type]
272
+ non_empty_prompts = [p for p in preset["prompts"] if p.strip()]
273
+
274
+ if not non_empty_prompts:
275
+ return "No prompts defined. Please enter at least one prompt in the editor."
276
+
277
+ preview_text = f"**Preset: {preset_type}**\n\n"
278
+ preview_text += f"*{preset['description']}*\n\n"
279
+ preview_text += f"**Generating {len(non_empty_prompts)} image{'s' if len(non_empty_prompts) > 1 else ''}:**\n"
280
+
281
+ for i, preset_prompt in enumerate(non_empty_prompts, 1):
282
+ full_prompt = f"{base_prompt}, {preset_prompt}"
283
+ preview_text += f"{i}. {full_prompt}\n"
284
+
285
+ return preview_text
286
+ else:
287
+ return "Select a preset above to see how your base prompt will be modified for batch generation."
288
 
289
+ def update_preset_prompt_textbox(preset_type, prompt_1, prompt_2, prompt_3, prompt_4):
290
+ """Update preset prompts based on user input"""
291
+ if preset_type and preset_type in PRESETS:
292
+ # Update each prompt in the preset
293
+ new_prompts = [prompt_1, prompt_2, prompt_3, prompt_4]
294
+ for i, new_prompt in enumerate(new_prompts):
295
+ if i < len(PRESETS[preset_type]["prompts"]):
296
+ PRESETS[preset_type]["prompts"][i] = new_prompt.strip()
297
+ else:
298
+ PRESETS[preset_type]["prompts"].append(new_prompt.strip())
299
 
300
+ # Count non-empty prompts for the slider value
301
+ non_empty_count = len([p for p in new_prompts if p.strip()])
302
+ non_empty_count = max(1, min(4, non_empty_count)) # Ensure between 1-4
303
+
304
+ # Return updated preset info for preview
305
+ return update_prompt_preview(preset_type, "your subject") # Use placeholder for preview
306
+ return "Select a preset first to edit its prompts."
307
+
308
+
309
  @spaces.GPU()
310
  def infer(
311
  image,
 
368
  # Handle preset batch generation
369
  if preset_type and preset_type in PRESETS:
370
  preset = PRESETS[preset_type]
371
+ # Filter out empty prompts
372
+ non_empty_preset_prompts = [p for p in preset["prompts"] if p.strip()]
373
+
374
+ if non_empty_preset_prompts:
375
+ batch_prompts = [f"{original_prompt}, {preset_prompt}" for preset_prompt in non_empty_preset_prompts]
376
+ num_images_per_prompt = len(non_empty_preset_prompts) # Use actual count of non-empty prompts
377
+ prompt_info = (
378
+ f"<div style='margin:10px; padding:15px; border-radius:8px; border-left:4px solid #2196F3; background: #f0f8ff'>"
379
+ f"<h4 style='margin-top: 0;'>🎨 Preset: {preset_type}</h4>"
380
+ f"<p>{preset['description']}</p>"
381
+ f"<p><strong>Base Prompt:</strong> {original_prompt}</p>"
382
+ f"<p>Generating {len(non_empty_preset_prompts)} image{'s' if len(non_empty_preset_prompts) > 1 else ''}</p>"
383
+ f"</div>"
384
+ )
385
+ print(f"Using preset: {preset_type} with {len(batch_prompts)} variations")
386
+ else:
387
+ # Fallback to manual if no valid prompts
388
+ batch_prompts = [prompt]
389
+ prompt_info = (
390
+ f"<div style='margin:10px; padding:15px; border-radius:8px; border-left:4px solid #FF9800; background: #fff8f0'>"
391
+ f"<h4 style='margin-top: 0;'>⚠️ Invalid Preset</h4>"
392
+ f"<p>No valid prompts found. Using manual prompt.</p>"
393
+ f"<p><strong>Prompt:</strong> {original_prompt}</p>"
394
+ f"</div>"
395
+ )
396
  else:
397
  batch_prompts = [prompt] # Single prompt in list
398
 
 
612
  prompts.append("")
613
  return gr.Group(visible=True), prompts[0], prompts[1], prompts[2], prompts[3]
614
  return gr.Group(visible=False), "", "", "", ""
615
+
616
+ def update_preset_count(preset_type, prompt_1, prompt_2, prompt_3, prompt_4):
617
+ """Update the output count slider based on non-empty preset prompts"""
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
618
  if preset_type and preset_type in PRESETS:
619
+ non_empty_count = len([p for p in [prompt_1, prompt_2, prompt_3, prompt_4] if p.strip()])
620
+ return gr.Slider(value=max(1, min(4, non_empty_count)), interactive=False)
621
+ return gr.Slider(interactive=True)
 
 
 
 
 
 
 
622
 
623
  # Set up preset dropdown change handler - now controls output count slider
624
  preset_dropdown.change(
 
634
  outputs=prompt_preview
635
  )
636
 
637
+ # Set up handlers for preset editor changes
638
+ preset_prompt_1.change(
639
+ fn=update_preset_count,
640
+ inputs=[preset_dropdown, preset_prompt_1, preset_prompt_2, preset_prompt_3, preset_prompt_4],
641
+ outputs=num_images_per_prompt
642
+ )
643
+ preset_prompt_2.change(
644
+ fn=update_preset_count,
645
+ inputs=[preset_dropdown, preset_prompt_1, preset_prompt_2, preset_prompt_3, preset_prompt_4],
646
+ outputs=num_images_per_prompt
647
+ )
648
+ preset_prompt_3.change(
649
+ fn=update_preset_count,
650
+ inputs=[preset_dropdown, preset_prompt_1, preset_prompt_2, preset_prompt_3, preset_prompt_4],
651
+ outputs=num_images_per_prompt
652
+ )
653
+ preset_prompt_4.change(
654
+ fn=update_preset_count,
655
+ inputs=[preset_dropdown, preset_prompt_1, preset_prompt_2, preset_prompt_3, preset_prompt_4],
656
+ outputs=num_images_per_prompt
657
+ )
658
+
659
  prompt.change(
660
  fn=update_prompt_preview,
661
  inputs=[preset_dropdown, prompt],