davidberenstein1957 commited on
Commit
ac90c02
·
1 Parent(s): 8486710

refactor: streamline image generation process and improve metadata handling in generate_images function

Browse files
Files changed (1) hide show
  1. sample.py +61 -52
sample.py CHANGED
@@ -33,32 +33,39 @@ def generate_images(api_type: str, benchmarks: List[str]):
33
  entry = json.loads(line)
34
  existing_metadata[entry["filepath"]] = entry
35
 
36
- for metadata, folder_name in tqdm(
37
- benchmark, desc=f"Generating images for {benchmark_type}", leave=False
38
- ):
39
- sample_path = benchmark_dir / folder_name
40
- samples_path = sample_path / "samples"
41
- samples_path.mkdir(parents=True, exist_ok=True)
42
- image_path = samples_path / "0000.png"
43
-
44
- if image_path.exists():
45
- continue
46
-
47
- try:
48
- inference_time = api.generate_image(metadata["prompt"], image_path)
49
-
50
- metadata_entry = {
51
- "filepath": str(image_path),
52
- "prompt": metadata["prompt"],
53
- "inference_time": inference_time,
54
- }
55
-
56
- existing_metadata[str(image_path)] = metadata_entry
57
-
58
- except Exception as e:
59
- print(f"\nError generating image for prompt: {metadata['prompt']}")
60
- print(f"Error: {str(e)}")
61
- continue
 
 
 
 
 
 
 
62
  else:
63
  benchmark_dir = api_dir / benchmark_type
64
  benchmark_dir.mkdir(parents=True, exist_ok=True)
@@ -71,33 +78,35 @@ def generate_images(api_type: str, benchmarks: List[str]):
71
  entry = json.loads(line)
72
  existing_metadata[entry["filepath"]] = entry
73
 
74
- for prompt, image_path in tqdm(
75
- benchmark, desc=f"Generating images for {benchmark_type}", leave=False
76
- ):
77
- full_image_path = benchmark_dir / image_path
78
-
79
- if full_image_path.exists():
80
- continue
81
-
82
- try:
83
- inference_time = api.generate_image(prompt, full_image_path)
84
-
85
- metadata_entry = {
86
- "filepath": str(image_path),
87
- "prompt": prompt,
88
- "inference_time": inference_time,
89
- }
90
-
91
- existing_metadata[str(image_path)] = metadata_entry
92
-
93
- except Exception as e:
94
- print(f"\nError generating image for prompt: {prompt}")
95
- print(f"Error: {str(e)}")
96
- continue
97
-
98
  with open(metadata_file, "w") as f:
99
- for entry in existing_metadata.values():
100
- f.write(json.dumps(entry) + "\n")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
101
 
102
 
103
  def main():
 
33
  entry = json.loads(line)
34
  existing_metadata[entry["filepath"]] = entry
35
 
36
+ with open(metadata_file, "w") as f:
37
+ for metadata, folder_name in tqdm(
38
+ benchmark,
39
+ desc=f"Generating images for {benchmark_type}",
40
+ leave=False,
41
+ ):
42
+ sample_path = benchmark_dir / folder_name
43
+ samples_path = sample_path / "samples"
44
+ samples_path.mkdir(parents=True, exist_ok=True)
45
+ image_path = samples_path / "0000.png"
46
+
47
+ if image_path.exists():
48
+ continue
49
+
50
+ try:
51
+ inference_time = api.generate_image(
52
+ metadata["prompt"], image_path
53
+ )
54
+
55
+ metadata_entry = {
56
+ "filepath": str(image_path),
57
+ "prompt": metadata["prompt"],
58
+ "inference_time": inference_time,
59
+ }
60
+
61
+ f.write(json.dumps(metadata_entry) + "\n")
62
+
63
+ except Exception as e:
64
+ print(
65
+ f"\nError generating image for prompt: {metadata['prompt']}"
66
+ )
67
+ print(f"Error: {str(e)}")
68
+ continue
69
  else:
70
  benchmark_dir = api_dir / benchmark_type
71
  benchmark_dir.mkdir(parents=True, exist_ok=True)
 
78
  entry = json.loads(line)
79
  existing_metadata[entry["filepath"]] = entry
80
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
81
  with open(metadata_file, "w") as f:
82
+ for prompt, image_path in tqdm(
83
+ benchmark,
84
+ desc=f"Generating images for {benchmark_type}",
85
+ leave=False,
86
+ ):
87
+ if image_path in existing_metadata:
88
+ continue
89
+
90
+ full_image_path = benchmark_dir / image_path
91
+
92
+ if full_image_path.exists():
93
+ continue
94
+
95
+ try:
96
+ inference_time = api.generate_image(prompt, full_image_path)
97
+
98
+ metadata_entry = {
99
+ "filepath": str(image_path),
100
+ "prompt": prompt,
101
+ "inference_time": inference_time,
102
+ }
103
+
104
+ f.write(json.dumps(metadata_entry) + "\n")
105
+
106
+ except Exception as e:
107
+ print(f"\nError generating image for prompt: {prompt}")
108
+ print(f"Error: {str(e)}")
109
+ continue
110
 
111
 
112
  def main():