danhtran2mind commited on
Commit
7543b6f
·
verified ·
1 Parent(s): 198128f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -16
app.py CHANGED
@@ -17,28 +17,59 @@ def get_examples(examples_dir: str = "assets/examples/ghibli-fine-tuned-sd-2.1")
17
  Each example is a subdirectory containing a config.json and an image file.
18
  Returns a list of [prompt, height, width, num_inference_steps, guidance_scale, seed, image_path].
19
  """
20
- examples = Path(examples_dir)
 
 
 
 
 
 
 
21
  ans = []
22
- for example in examples.iterdir():
23
- if not example.is_dir():
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24
  continue
25
- with open(example / "config.json") as f:
26
- example_dict = json.load(f)
27
 
 
28
  required_keys = ["prompt", "height", "width", "num_inference_steps", "guidance_scale", "seed", "image"]
29
  if not all(key in example_dict for key in required_keys):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30
  continue
31
-
32
- example_list = [
33
- example_dict["prompt"],
34
- example_dict["height"],
35
- example_dict["width"],
36
- example_dict["num_inference_steps"],
37
- example_dict["guidance_scale"],
38
- example_dict["seed"],
39
- str(example / example_dict["image"]) # Path to the image file
40
- ]
41
- ans.append(example_list)
42
 
43
  if not ans:
44
  ans = [
 
17
  Each example is a subdirectory containing a config.json and an image file.
18
  Returns a list of [prompt, height, width, num_inference_steps, guidance_scale, seed, image_path].
19
  """
20
+ # Check if the directory exists
21
+ if not os.path.exists(examples_dir) or not os.path.isdir(examples_dir):
22
+ raise ValueError(f"Directory {examples_dir} does not exist or is not a directory")
23
+
24
+ # Get list of subfolder paths (e.g., 1, 2, etc.)
25
+ all_examples_dir = [os.path.join(examples_dir, d) for d in os.listdir(examples_dir)
26
+ if os.path.isdir(os.path.join(examples_dir, d))]
27
+
28
  ans = []
29
+ for example_dir in all_examples_dir:
30
+ config_path = os.path.join(example_dir, "config.json")
31
+ image_path = os.path.join(example_dir, "result.png")
32
+
33
+ # Check if config.json and result.png exist
34
+ if not os.path.isfile(config_path):
35
+ print(f"Warning: config.json not found in {example_dir}")
36
+ continue
37
+ if not os.path.isfile(image_path):
38
+ print(f"Warning: result.png not found in {example_dir}")
39
+ continue
40
+
41
+ try:
42
+ with open(config_path, 'r') as f:
43
+ example_dict = json.load(f)
44
+ except (json.JSONDecodeError, IOError) as e:
45
+ print(f"Error reading or parsing {config_path}: {e}")
46
  continue
 
 
47
 
48
+ # Required keys for the config
49
  required_keys = ["prompt", "height", "width", "num_inference_steps", "guidance_scale", "seed", "image"]
50
  if not all(key in example_dict for key in required_keys):
51
+ print(f"Warning: Missing required keys in {config_path}")
52
+ continue
53
+
54
+ # Verify that the image key in config.json matches 'result.png'
55
+ if example_dict["image"] != "result.png":
56
+ print(f"Warning: Image key in {config_path} does not match 'result.png'")
57
+ continue
58
+
59
+ try:
60
+ example_list = [
61
+ example_dict["prompt"],
62
+ example_dict["height"],
63
+ example_dict["width"],
64
+ example_dict["num_inference_steps"],
65
+ example_dict["guidance_scale"],
66
+ example_dict["seed"],
67
+ image_path # Use verified image path
68
+ ]
69
+ ans.append(example_list)
70
+ except KeyError as e:
71
+ print(f"Error processing {config_path}: Missing key {e}")
72
  continue
 
 
 
 
 
 
 
 
 
 
 
73
 
74
  if not ans:
75
  ans = [