fffiloni commited on
Commit
69458a9
·
verified ·
1 Parent(s): 6927d3a

clean outputs directories

Browse files
Files changed (1) hide show
  1. app.py +37 -14
app.py CHANGED
@@ -2,6 +2,7 @@ import gradio as gr
2
  from main import main
3
  from arguments import parse_args
4
  import os
 
5
  import glob
6
 
7
  def list_iter_images(save_dir):
@@ -21,6 +22,27 @@ def list_iter_images(save_dir):
21
 
22
  return image_paths
23
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24
  def generate_image(prompt, model, num_iterations, learning_rate, progress=gr.Progress(track_tqdm=True)):
25
  # Set up arguments
26
  args = parse_args()
@@ -32,6 +54,21 @@ def generate_image(prompt, model, num_iterations, learning_rate, progress=gr.Pro
32
  args.cache_dir = "./HF_model_cache"
33
  args.save_dir = "./outputs"
34
  args.save_all_images = True
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35
 
36
  try:
37
  # Run the main function with progress tracking
@@ -39,20 +76,6 @@ def generate_image(prompt, model, num_iterations, learning_rate, progress=gr.Pro
39
  progress(step / num_iterations, f"Iteration {step}/{num_iterations}")
40
 
41
  best_image, total_init_rewards, total_best_rewards = main(args, progress_callback)
42
-
43
- settings = (
44
- f"{args.model}{'_' + args.prompt if args.task == 't2i-compbench' else ''}"
45
- f"{'_no-optim' if args.no_optim else ''}_{args.seed if args.task != 'geneval' else ''}"
46
- f"_lr{args.lr}_gc{args.grad_clip}_iter{args.n_iters}"
47
- f"_reg{args.reg_weight if args.enable_reg else '0'}"
48
- f"{'_pickscore' + str(args.pickscore_weighting) if args.enable_pickscore else ''}"
49
- f"{'_clip' + str(args.clip_weighting) if args.enable_clip else ''}"
50
- f"{'_hps' + str(args.hps_weighting) if args.enable_hps else ''}"
51
- f"{'_imagereward' + str(args.imagereward_weighting) if args.enable_imagereward else ''}"
52
- f"{'_aesthetic' + str(args.aesthetic_weighting) if args.enable_aesthetic else ''}"
53
- )
54
-
55
- save_dir = f"{args.save_dir}/{args.task}/{settings}/{args.prompt}"
56
 
57
  # Return the path to the generated image
58
  image_path = f"{save_dir}/best_image.png"
 
2
  from main import main
3
  from arguments import parse_args
4
  import os
5
+ import shutil
6
  import glob
7
 
8
  def list_iter_images(save_dir):
 
22
 
23
  return image_paths
24
 
25
+ def clean_dir(save_dir):
26
+ # Check if the directory exists
27
+ if os.path.exists(save_dir):
28
+ # Check if the directory contains any files
29
+ if len(os.listdir(save_dir)) > 0:
30
+ # If it contains files, delete all files in the directory
31
+ for filename in os.listdir(save_dir):
32
+ file_path = os.path.join(save_dir, filename)
33
+ try:
34
+ if os.path.isfile(file_path) or os.path.islink(file_path):
35
+ os.unlink(file_path) # Remove file or symbolic link
36
+ elif os.path.isdir(file_path):
37
+ shutil.rmtree(file_path) # Remove directory and its contents
38
+ except Exception as e:
39
+ print(f"Failed to delete {file_path}. Reason: {e}")
40
+ print(f"All files in {save_dir} have been deleted.")
41
+ else:
42
+ print(f"{save_dir} exists but is empty.")
43
+ else:
44
+ print(f"{save_dir} does not exist.")
45
+
46
  def generate_image(prompt, model, num_iterations, learning_rate, progress=gr.Progress(track_tqdm=True)):
47
  # Set up arguments
48
  args = parse_args()
 
54
  args.cache_dir = "./HF_model_cache"
55
  args.save_dir = "./outputs"
56
  args.save_all_images = True
57
+
58
+ settings = (
59
+ f"{args.model}{'_' + args.prompt if args.task == 't2i-compbench' else ''}"
60
+ f"{'_no-optim' if args.no_optim else ''}_{args.seed if args.task != 'geneval' else ''}"
61
+ f"_lr{args.lr}_gc{args.grad_clip}_iter{args.n_iters}"
62
+ f"_reg{args.reg_weight if args.enable_reg else '0'}"
63
+ f"{'_pickscore' + str(args.pickscore_weighting) if args.enable_pickscore else ''}"
64
+ f"{'_clip' + str(args.clip_weighting) if args.enable_clip else ''}"
65
+ f"{'_hps' + str(args.hps_weighting) if args.enable_hps else ''}"
66
+ f"{'_imagereward' + str(args.imagereward_weighting) if args.enable_imagereward else ''}"
67
+ f"{'_aesthetic' + str(args.aesthetic_weighting) if args.enable_aesthetic else ''}"
68
+ )
69
+
70
+ save_dir = f"{args.save_dir}/{args.task}/{settings}/{args.prompt}"
71
+ clean_dir(save_dir)
72
 
73
  try:
74
  # Run the main function with progress tracking
 
76
  progress(step / num_iterations, f"Iteration {step}/{num_iterations}")
77
 
78
  best_image, total_init_rewards, total_best_rewards = main(args, progress_callback)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
79
 
80
  # Return the path to the generated image
81
  image_path = f"{save_dir}/best_image.png"