uhdessai commited on
Commit
a9fa13b
·
verified ·
1 Parent(s): bd5e9c5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -17
app.py CHANGED
@@ -4,45 +4,58 @@ import os
4
  import random
5
  from PIL import Image
6
 
7
- # Paths
8
- OUTPUT_DIR = "outputs"
9
- MODEL_PATH = "dress.pkl" # Adjusted for local Hugging Face repo structure
 
 
10
 
11
- # Ensure the output directory exists
12
  os.makedirs(OUTPUT_DIR, exist_ok=True)
13
 
14
- # Function to generate images using StyleGAN3
15
  def generate_images():
16
- command = f"python stylegan3/gen_images.py --outdir={OUTPUT_DIR} --trunc=1 --seeds='3-5,7,9,12-14,16-26,29,31,32,34,40,41' --network={MODEL_PATH}"
 
 
 
 
 
 
 
 
17
  try:
18
- subprocess.run(command, shell=True, check=True)
 
 
19
  except subprocess.CalledProcessError as e:
20
- return f"Error generating images: {e}"
 
 
21
 
22
- # Function to select 5 random images
23
  def get_random_images():
24
  image_files = [f for f in os.listdir(OUTPUT_DIR) if f.endswith(".png")]
 
25
  if len(image_files) < 5:
26
  generate_images()
27
  image_files = [f for f in os.listdir(OUTPUT_DIR) if f.endswith(".png")]
28
-
29
- if len(image_files) == 0:
30
- return ["No images found. Please try again."]
31
 
32
- random_images = random.sample(image_files, min(5, len(image_files)))
33
- return [os.path.join(OUTPUT_DIR, img) for img in random_images]
34
 
 
 
35
 
36
- # Gradio function
37
  def generate_and_display():
38
  generate_images()
39
  return get_random_images()
40
 
41
- # UI
42
  with gr.Blocks() as demo:
43
  gr.Markdown("# 🎨 AI-Generated Clothing Designs - Dresses")
44
  generate_button = gr.Button("Generate New Designs")
45
- output_gallery = gr.Gallery(label="Generated Designs", columns=5, rows=2)
46
  generate_button.click(fn=generate_and_display, outputs=output_gallery)
47
 
48
  if __name__ == "__main__":
 
4
  import random
5
  from PIL import Image
6
 
7
+ # === Setup Paths ===
8
+ BASE_DIR = os.path.dirname(os.path.abspath(__file__))
9
+ GEN_SCRIPT = os.path.join(BASE_DIR, "stylegan3", "gen_images.py")
10
+ OUTPUT_DIR = os.path.join(BASE_DIR, "outputs")
11
+ MODEL_PATH = os.path.join(BASE_DIR, "dress.pkl")
12
 
13
+ # Ensure output directory exists
14
  os.makedirs(OUTPUT_DIR, exist_ok=True)
15
 
16
+ # === Image Generation Function ===
17
  def generate_images():
18
+ command = [
19
+ "python",
20
+ GEN_SCRIPT,
21
+ f"--outdir={OUTPUT_DIR}",
22
+ "--trunc=1",
23
+ "--seeds=3-5,7,9,12-14,16-26,29,31,32,34,40,41",
24
+ f"--network={MODEL_PATH}"
25
+ ]
26
+
27
  try:
28
+ result = subprocess.run(command, check=True, capture_output=True, text=True)
29
+ print("stdout:\n", result.stdout)
30
+ print("stderr:\n", result.stderr)
31
  except subprocess.CalledProcessError as e:
32
+ print("Image generation failed with error:")
33
+ print(e.stderr)
34
+ return f"Error generating images:\n{e.stderr}"
35
 
36
+ # === Select Random Images from Output Folder ===
37
  def get_random_images():
38
  image_files = [f for f in os.listdir(OUTPUT_DIR) if f.endswith(".png")]
39
+
40
  if len(image_files) < 5:
41
  generate_images()
42
  image_files = [f for f in os.listdir(OUTPUT_DIR) if f.endswith(".png")]
 
 
 
43
 
44
+ if not image_files:
45
+ return [Image.new("RGB", (256, 256), color="gray")] * 5 # fallback
46
 
47
+ random_images = random.sample(image_files, min(5, len(image_files)))
48
+ return [Image.open(os.path.join(OUTPUT_DIR, img)) for img in random_images]
49
 
50
+ # === Gradio Interface ===
51
  def generate_and_display():
52
  generate_images()
53
  return get_random_images()
54
 
 
55
  with gr.Blocks() as demo:
56
  gr.Markdown("# 🎨 AI-Generated Clothing Designs - Dresses")
57
  generate_button = gr.Button("Generate New Designs")
58
+ output_gallery = gr.Gallery(label="Generated Designs", columns=5)
59
  generate_button.click(fn=generate_and_display, outputs=output_gallery)
60
 
61
  if __name__ == "__main__":