sahadev10 commited on
Commit
0c0e595
·
verified ·
1 Parent(s): 9c4fb87

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +75 -52
app.py CHANGED
@@ -69,6 +69,7 @@
69
 
70
 
71
 
 
72
  # import gradio as gr
73
  # import subprocess
74
  # import os
@@ -113,38 +114,50 @@
113
  # return f"✅ Saved {filename}"
114
 
115
  # with gr.Blocks() as demo:
116
- # gr.Markdown("# 👗 AI-Generated Clothing Designs")
117
 
118
  # generate_btn = gr.Button("✨ Generate New Designs")
119
 
120
  # image_components = []
 
121
  # save_buttons = []
122
  # outputs = []
123
 
124
- # with gr.Column():
125
- # for _ in range(10):
126
- # with gr.Row():
127
- # img = gr.Image()
128
- # btn = gr.Button("💾 Save")
129
- # out = gr.Textbox(visible=False)
 
 
 
 
130
 
131
- # btn.click(fn=save_image, inputs=img, outputs=out)
132
 
133
- # image_components.append(img)
134
- # save_buttons.append(btn)
135
- # outputs.append(out)
136
 
137
  # def update_gallery():
138
  # files = get_random_images()
139
- # return files + [""] * (10 - len(files))
140
-
141
- # generate_btn.click(fn=update_gallery, outputs=image_components)
 
 
 
 
 
 
 
 
142
 
143
  # demo.launch()
144
 
145
 
146
 
147
-
148
  import gradio as gr
149
  import subprocess
150
  import os
@@ -152,14 +165,17 @@ import random
152
  from PIL import Image
153
  import shutil
154
 
 
155
  BASE_DIR = os.path.dirname(os.path.abspath(__file__))
156
  GEN_SCRIPT = os.path.join(BASE_DIR, "stylegan3", "gen_images.py")
157
  OUTPUT_DIR = os.path.join(BASE_DIR, "outputs")
158
  MODEL_PATH = os.path.join(BASE_DIR, "dress.pkl")
 
159
 
160
  os.makedirs(OUTPUT_DIR, exist_ok=True)
161
- os.makedirs(os.path.join(BASE_DIR, "saved_images"), exist_ok=True)
162
 
 
163
  def generate_images():
164
  command = [
165
  "python",
@@ -169,64 +185,71 @@ def generate_images():
169
  "--seeds=3-5,7,9,12-14,16-26,29,31,32,34,40,41",
170
  f"--network={MODEL_PATH}"
171
  ]
 
172
  try:
173
  subprocess.run(command, check=True, capture_output=True, text=True)
174
  except subprocess.CalledProcessError as e:
175
  return f"Error generating images:\n{e.stderr}"
176
 
 
177
  def get_random_images():
178
  image_files = [f for f in os.listdir(OUTPUT_DIR) if f.endswith(".png")]
179
  if len(image_files) < 10:
180
  generate_images()
181
  image_files = [f for f in os.listdir(OUTPUT_DIR) if f.endswith(".png")]
182
- selected_files = random.sample(image_files, min(10, len(image_files)))
183
- return [os.path.join(OUTPUT_DIR, f) for f in selected_files]
184
-
185
- def save_image(image_path):
186
- filename = os.path.basename(image_path)
187
- dest = os.path.join(BASE_DIR, "saved_images", filename)
188
- shutil.copy(image_path, dest)
189
- return f"✅ Saved {filename}"
190
 
 
 
 
 
 
 
 
 
 
 
 
191
  with gr.Blocks() as demo:
192
- gr.Markdown("## 👗 AI-Generated Clothing Designs")
193
 
194
- generate_btn = gr.Button("✨ Generate New Designs")
195
 
196
  image_components = []
197
- file_paths = []
198
  save_buttons = []
199
  outputs = []
 
200
 
201
  with gr.Row():
202
  for row in range(2): # 2 rows
203
  with gr.Column():
204
- for col in range(5): # 5 images per row
205
  index = row * 5 + col
206
  with gr.Column():
207
- img = gr.Image()
208
- file_paths.append(gr.Textbox(visible=False)) # to pass file path
209
- btn = gr.Button("💾 Save")
210
- out = gr.Textbox(visible=False)
211
 
212
- btn.click(fn=save_image, inputs=file_paths[index], outputs=out)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
213
 
214
- image_components.append(img)
215
- save_buttons.append(btn)
216
- outputs.append(out)
217
-
218
- def update_gallery():
219
- files = get_random_images()
220
- updates = []
221
- for i, path in enumerate(files):
222
- updates.append(path) # for img.update(value=...)
223
- updates.append(path) # for file_path.update(value=...)
224
- # Fill remaining slots if less than 10
225
- for i in range(10 - len(files)):
226
- updates.append(None)
227
- updates.append("")
228
- return updates
229
-
230
- generate_btn.click(fn=update_gallery, outputs=sum(zip(image_components, file_paths), ()))
231
-
232
- demo.launch()
 
69
 
70
 
71
 
72
+
73
  # import gradio as gr
74
  # import subprocess
75
  # import os
 
114
  # return f"✅ Saved {filename}"
115
 
116
  # with gr.Blocks() as demo:
117
+ # gr.Markdown("## 👗 AI-Generated Clothing Designs")
118
 
119
  # generate_btn = gr.Button("✨ Generate New Designs")
120
 
121
  # image_components = []
122
+ # file_paths = []
123
  # save_buttons = []
124
  # outputs = []
125
 
126
+ # with gr.Row():
127
+ # for row in range(2): # 2 rows
128
+ # with gr.Column():
129
+ # for col in range(5): # 5 images per row
130
+ # index = row * 5 + col
131
+ # with gr.Column():
132
+ # img = gr.Image()
133
+ # file_paths.append(gr.Textbox(visible=False)) # to pass file path
134
+ # btn = gr.Button("💾 Save")
135
+ # out = gr.Textbox(visible=False)
136
 
137
+ # btn.click(fn=save_image, inputs=file_paths[index], outputs=out)
138
 
139
+ # image_components.append(img)
140
+ # save_buttons.append(btn)
141
+ # outputs.append(out)
142
 
143
  # def update_gallery():
144
  # files = get_random_images()
145
+ # updates = []
146
+ # for i, path in enumerate(files):
147
+ # updates.append(path) # for img.update(value=...)
148
+ # updates.append(path) # for file_path.update(value=...)
149
+ # # Fill remaining slots if less than 10
150
+ # for i in range(10 - len(files)):
151
+ # updates.append(None)
152
+ # updates.append("")
153
+ # return updates
154
+
155
+ # generate_btn.click(fn=update_gallery, outputs=sum(zip(image_components, file_paths), ()))
156
 
157
  # demo.launch()
158
 
159
 
160
 
 
161
  import gradio as gr
162
  import subprocess
163
  import os
 
165
  from PIL import Image
166
  import shutil
167
 
168
+ # === Setup Paths ===
169
  BASE_DIR = os.path.dirname(os.path.abspath(__file__))
170
  GEN_SCRIPT = os.path.join(BASE_DIR, "stylegan3", "gen_images.py")
171
  OUTPUT_DIR = os.path.join(BASE_DIR, "outputs")
172
  MODEL_PATH = os.path.join(BASE_DIR, "dress.pkl")
173
+ SAVE_DIR = os.path.join(BASE_DIR, "saved_images")
174
 
175
  os.makedirs(OUTPUT_DIR, exist_ok=True)
176
+ os.makedirs(SAVE_DIR, exist_ok=True)
177
 
178
+ # === Image Generation Function ===
179
  def generate_images():
180
  command = [
181
  "python",
 
185
  "--seeds=3-5,7,9,12-14,16-26,29,31,32,34,40,41",
186
  f"--network={MODEL_PATH}"
187
  ]
188
+
189
  try:
190
  subprocess.run(command, check=True, capture_output=True, text=True)
191
  except subprocess.CalledProcessError as e:
192
  return f"Error generating images:\n{e.stderr}"
193
 
194
+ # === Select Random Images from Output Folder ===
195
  def get_random_images():
196
  image_files = [f for f in os.listdir(OUTPUT_DIR) if f.endswith(".png")]
197
  if len(image_files) < 10:
198
  generate_images()
199
  image_files = [f for f in os.listdir(OUTPUT_DIR) if f.endswith(".png")]
200
+ random_images = random.sample(image_files, min(10, len(image_files)))
201
+ image_paths = [os.path.join(OUTPUT_DIR, img) for img in random_images]
202
+ return image_paths
 
 
 
 
 
203
 
204
+ # === Save Image Function ===
205
+ def save_image(img_path):
206
+ try:
207
+ filename = os.path.basename(img_path)
208
+ dest_path = os.path.join(SAVE_DIR, filename)
209
+ shutil.copy(img_path, dest_path)
210
+ return f"Saved to: {dest_path}"
211
+ except Exception as e:
212
+ return f"Error saving image: {str(e)}"
213
+
214
+ # === Gradio Interface ===
215
  with gr.Blocks() as demo:
216
+ gr.Markdown("# 🎨 AI-Generated Clothing Designs - Dresses")
217
 
218
+ generate_button = gr.Button("Generate New Designs")
219
 
220
  image_components = []
 
221
  save_buttons = []
222
  outputs = []
223
+ file_paths = []
224
 
225
  with gr.Row():
226
  for row in range(2): # 2 rows
227
  with gr.Column():
228
+ for col in range(5): # 5 columns
229
  index = row * 5 + col
230
  with gr.Column():
231
+ img = gr.Image(width=200, height=200)
232
+ image_components.append(img)
 
 
233
 
234
+ file_path = gr.Textbox(visible=False)
235
+ file_paths.append(file_path)
236
+
237
+ save_btn = gr.Button("💾 Save")
238
+ output = gr.Textbox(visible=False)
239
+ outputs.append(output)
240
+
241
+ save_btn.click(fn=save_image, inputs=file_path, outputs=output)
242
+ save_buttons.append(save_btn)
243
+
244
+ def generate_and_display_images():
245
+ image_paths = get_random_images()
246
+ return image_paths + image_paths # for setting file_paths and images
247
+
248
+ generate_button.click(
249
+ fn=generate_and_display_images,
250
+ outputs=image_components + file_paths
251
+ )
252
+
253
+ if __name__ == "__main__":
254
+ demo.launch()
255