sahadev10 commited on
Commit
e9a7d00
·
verified ·
1 Parent(s): 99826a6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -37
app.py CHANGED
@@ -56,7 +56,6 @@
56
  # if __name__ == "__main__":
57
  # demo.launch()
58
 
59
-
60
  import gradio as gr
61
  import subprocess
62
  import os
@@ -70,7 +69,6 @@ GEN_SCRIPT = os.path.join(BASE_DIR, "stylegan3", "gen_images.py")
70
  OUTPUT_DIR = os.path.join(BASE_DIR, "outputs")
71
  MODEL_PATH = os.path.join(BASE_DIR, "dress.pkl")
72
 
73
- # Ensure output directory exists
74
  os.makedirs(OUTPUT_DIR, exist_ok=True)
75
 
76
  # === Image Generation Function ===
@@ -83,52 +81,52 @@ def generate_images():
83
  "--seeds=3-5,7,9,12-14,16-26,29,31,32,34,40,41",
84
  f"--network={MODEL_PATH}"
85
  ]
86
-
87
  try:
88
- result = subprocess.run(command, check=True, capture_output=True, text=True)
89
- print("stdout:\n", result.stdout)
90
- print("stderr:\n", result.stderr)
91
  except subprocess.CalledProcessError as e:
92
- print("Image generation failed with error:")
93
- print(e.stderr)
94
  return f"Error generating images:\n{e.stderr}"
95
 
96
- # === Select Random Images from Output Folder ===
97
  def get_random_images():
98
  image_files = [f for f in os.listdir(OUTPUT_DIR) if f.endswith(".png")]
99
  if len(image_files) < 10:
100
  generate_images()
101
  image_files = [f for f in os.listdir(OUTPUT_DIR) if f.endswith(".png")]
102
- random_images = random.sample(image_files, min(10, len(image_files)))
103
- return [Image.open(os.path.join(OUTPUT_DIR, img)) for img in random_images], random_images
104
 
105
  # === Save Image Function ===
106
- def save_image(image_filename):
107
- image_path = os.path.join(OUTPUT_DIR, image_filename)
108
- save_path = os.path.join(BASE_DIR, "saved_images", image_filename)
109
- os.makedirs(os.path.dirname(save_path), exist_ok=True)
110
- shutil.copy(image_path, save_path)
111
- return f"Image saved as {save_path}"
112
-
113
- # === Gradio Interface ===
114
- def generate_and_display():
115
- images, filenames = get_random_images()
116
- save_buttons = [gr.Button("Save", elem_id=filename) for filename in filenames]
117
- return images, save_buttons, filenames
118
-
119
- def save_button_clicked(image_filename):
120
- return save_image(image_filename)
121
-
122
  with gr.Blocks() as demo:
123
- gr.Markdown("# 🎨 AI-Generated Clothing Designs - Dresses")
124
- generate_button = gr.Button("Generate New Designs")
125
- output_gallery = gr.Gallery(label="Generated Designs", columns=5)
126
-
127
- generate_button.click(fn=generate_and_display, outputs=[output_gallery, generate_button])
128
 
129
- # Create dynamic save buttons for each generated image
130
- with gr.Row() as button_row:
131
- for filename in []:
132
- gr.Button("Save", elem_id=filename).click(fn=save_button_clicked, inputs=filename, outputs=None)
133
-
 
 
 
 
 
 
 
 
 
 
 
 
134
  demo.launch()
 
 
56
  # if __name__ == "__main__":
57
  # demo.launch()
58
 
 
59
  import gradio as gr
60
  import subprocess
61
  import os
 
69
  OUTPUT_DIR = os.path.join(BASE_DIR, "outputs")
70
  MODEL_PATH = os.path.join(BASE_DIR, "dress.pkl")
71
 
 
72
  os.makedirs(OUTPUT_DIR, exist_ok=True)
73
 
74
  # === Image Generation Function ===
 
81
  "--seeds=3-5,7,9,12-14,16-26,29,31,32,34,40,41",
82
  f"--network={MODEL_PATH}"
83
  ]
 
84
  try:
85
+ subprocess.run(command, check=True, capture_output=True, text=True)
 
 
86
  except subprocess.CalledProcessError as e:
 
 
87
  return f"Error generating images:\n{e.stderr}"
88
 
89
+ # === Get 10 Random Images ===
90
  def get_random_images():
91
  image_files = [f for f in os.listdir(OUTPUT_DIR) if f.endswith(".png")]
92
  if len(image_files) < 10:
93
  generate_images()
94
  image_files = [f for f in os.listdir(OUTPUT_DIR) if f.endswith(".png")]
95
+ selected_files = random.sample(image_files, min(10, len(image_files)))
96
+ return selected_files
97
 
98
  # === Save Image Function ===
99
+ def save_image(filename):
100
+ src_path = os.path.join(OUTPUT_DIR, filename)
101
+ dest_dir = os.path.join(BASE_DIR, "saved_images")
102
+ os.makedirs(dest_dir, exist_ok=True)
103
+ dest_path = os.path.join(dest_dir, filename)
104
+ shutil.copy(src_path, dest_path)
105
+ return f"✅ Saved as {dest_path}"
106
+
107
+ # === UI ===
 
 
 
 
 
 
 
108
  with gr.Blocks() as demo:
109
+ gr.Markdown("# 👗 AI-Generated Clothing Designs (Dresses)")
110
+
111
+ generate_btn = gr.Button(" Generate New Designs")
112
+ image_rows = gr.Column()
 
113
 
114
+ def generate_ui():
115
+ files = get_random_images()
116
+ elements = []
117
+ for f in files:
118
+ img_path = os.path.join(OUTPUT_DIR, f)
119
+ with gr.Row():
120
+ image = gr.Image(img_path, label=f, show_label=False)
121
+ save_btn = gr.Button("💾 Save This Image")
122
+
123
+ # Use a closure to capture the filename
124
+ save_btn.click(fn=save_image, inputs=gr.Textbox(value=f, visible=False), outputs=gr.Textbox())
125
+
126
+ return
127
+
128
+ generate_btn.click(fn=generate_ui, outputs=[])
129
+
130
+ if __name__ == "__main__":
131
  demo.launch()
132
+