Keltezaa commited on
Commit
3b770fd
·
verified ·
1 Parent(s): 08ce46a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -17
app.py CHANGED
@@ -31,6 +31,12 @@ longformer_model = LongformerModel.from_pretrained("allenai/longformer-base-4096
31
  df = pd.read_csv('prompts.csv', header=None)
32
  prompt_values = df.values.flatten()
33
 
 
 
 
 
 
 
34
  # Load LoRAs from JSON file
35
  with open('loras.json', 'r') as f:
36
  loras = json.load(f)
@@ -373,35 +379,26 @@ def run_lora(prompt, cfg_scale, steps, selected_indices, lora_scale_1, lora_scal
373
  low_cpu_mem_usage=True,
374
  adapter_name=lora_name
375
  )
376
- # if image_input is not None: pipe_i2i = pipe_to_use
377
- # else: pipe = pipe_to_use
378
  print("Loaded LoRAs:", lora_names)
379
  print("Adapter weights:", lora_weights)
380
- # if image_input is not None:
381
- # pipe_i2i.set_adapters(lora_names, adapter_weights=lora_weights)
382
- # else:
383
  pipe.set_adapters(lora_names, adapter_weights=lora_weights)
384
- # print(pipe.get_active_adapters())
385
- # Set random seed for reproducibility
386
  with calculateDuration("Randomizing seed"):
387
  if randomize_seed:
388
  seed = random.randint(0, MAX_SEED)
389
 
390
- # Generate image
391
- # if image_input is not None:
392
- # final_image = generate_image_to_image(prompt_mash, steps, cfg_scale, width, height, seed)
393
- # yield final_image, seed, gr.update(visible=False)
394
- # else:
395
  image_generator = generate_image(prompt_mash, steps, seed, cfg_scale, width, height, progress)
396
- # Consume the generator to get the final image
397
- # final_image = None
398
  step_counter = 0
399
  for image in image_generator:
400
  step_counter += 1
401
  final_image = image
402
  progress_bar = f'<div class="progress-container"><div class="progress-bar" style="--current: {step_counter}; --total: {steps};"></div></div>'
403
  yield image, seed, gr.update(value=progress_bar, visible=True)
404
- # yield final_image, seed, gr.update(value=progress_bar, visible=False)
 
 
 
 
 
405
 
406
  run_lora.zerogpu = True
407
 
@@ -527,6 +524,9 @@ with gr.Blocks(css=css, delete_cache=(240, 240)) as app:
527
 
528
  with gr.Column(scale=1,min_width=50):
529
  randomize_button = gr.Button("🎲", variant="secondary", scale=1, elem_id="random_btn")
 
 
 
530
 
531
  # with gr.Row(elem_id="loaded_loras"):
532
  # with gr.Column(scale=8):
@@ -598,7 +598,8 @@ with gr.Blocks(css=css, delete_cache=(240, 240)) as app:
598
  gallery.select(
599
  update_selection,
600
  inputs=[selected_indices, loras_state, width, height],
601
- outputs=[prompt, selected_info_1, selected_info_2, selected_indices, lora_scale_1, lora_scale_2, width, height, lora_image_1, lora_image_2])
 
602
  remove_button_1.click(
603
  remove_lora_1,
604
  inputs=[selected_indices, loras_state],
@@ -627,7 +628,7 @@ with gr.Blocks(css=css, delete_cache=(240, 240)) as app:
627
  gr.on(
628
  triggers=[generate_button.click, prompt.submit],
629
  fn=run_lora,
630
- inputs=[prompt, cfg_scale, steps, selected_indices, lora_scale_1, lora_scale_2, randomize_seed, seed, width, height, loras_state],
631
  outputs=[result, seed, progress_bar]
632
  )#.then(
633
  # fn=lambda x, history: update_history(x, history),
 
31
  df = pd.read_csv('prompts.csv', header=None)
32
  prompt_values = df.values.flatten()
33
 
34
+ # Define the folder path for saving images
35
+ SAVE_PATH = "d:/autosave"
36
+
37
+ # Ensure the save path exists
38
+ os.makedirs(SAVE_PATH, exist_ok=True)
39
+
40
  # Load LoRAs from JSON file
41
  with open('loras.json', 'r') as f:
42
  loras = json.load(f)
 
379
  low_cpu_mem_usage=True,
380
  adapter_name=lora_name
381
  )
 
 
382
  print("Loaded LoRAs:", lora_names)
383
  print("Adapter weights:", lora_weights)
 
 
 
384
  pipe.set_adapters(lora_names, adapter_weights=lora_weights)
 
 
385
  with calculateDuration("Randomizing seed"):
386
  if randomize_seed:
387
  seed = random.randint(0, MAX_SEED)
388
 
 
 
 
 
 
389
  image_generator = generate_image(prompt_mash, steps, seed, cfg_scale, width, height, progress)
 
 
390
  step_counter = 0
391
  for image in image_generator:
392
  step_counter += 1
393
  final_image = image
394
  progress_bar = f'<div class="progress-container"><div class="progress-bar" style="--current: {step_counter}; --total: {steps};"></div></div>'
395
  yield image, seed, gr.update(value=progress_bar, visible=True)
396
+
397
+ # Save the final image if enabled
398
+ if enable_save and final_image is not None:
399
+ save_path = os.path.join(SAVE_PATH, f"{seed}.png")
400
+ final_image.save(save_path)
401
+ print(f"Image saved to: {save_path}")
402
 
403
  run_lora.zerogpu = True
404
 
 
524
 
525
  with gr.Column(scale=1,min_width=50):
526
  randomize_button = gr.Button("🎲", variant="secondary", scale=1, elem_id="random_btn")
527
+
528
+ with gr.Row():
529
+ enable_save = gr.Checkbox(label="Enable Auto-Save", value=False)
530
 
531
  # with gr.Row(elem_id="loaded_loras"):
532
  # with gr.Column(scale=8):
 
598
  gallery.select(
599
  update_selection,
600
  inputs=[selected_indices, loras_state, width, height],
601
+ outputs=[prompt, selected_info_1, selected_info_2, selected_indices, lora_scale_1, lora_scale_2, width, height, lora_image_1, lora_image_2]
602
+ )
603
  remove_button_1.click(
604
  remove_lora_1,
605
  inputs=[selected_indices, loras_state],
 
628
  gr.on(
629
  triggers=[generate_button.click, prompt.submit],
630
  fn=run_lora,
631
+ inputs=[prompt, cfg_scale, steps, selected_indices, lora_scale_1, lora_scale_2, randomize_seed, seed, width, height, loras_state, enable_save],
632
  outputs=[result, seed, progress_bar]
633
  )#.then(
634
  # fn=lambda x, history: update_history(x, history),