sahadev10 commited on
Commit
8e7d931
·
verified ·
1 Parent(s): 1fea16f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +144 -18
app.py CHANGED
@@ -1,44 +1,170 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
  import subprocess
3
  import os
4
  import random
5
  from PIL import Image
 
 
6
 
7
- # Paths
8
- OUTPUT_DIR = "outputs"
9
- MODEL_PATH = "blazer_model.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) < 10:
26
  generate_images()
27
  image_files = [f for f in os.listdir(OUTPUT_DIR) if f.endswith(".png")]
28
  random_images = random.sample(image_files, min(10, len(image_files)))
29
- return [Image.open(os.path.join(OUTPUT_DIR, img)) for img in random_images]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30
 
31
- # Gradio function
32
- def generate_and_display():
33
- generate_images()
34
- return get_random_images()
35
 
36
- # UI
 
 
 
37
  with gr.Blocks() as demo:
38
- gr.Markdown("# 🎨 AI-Generated Clothing Designs - Blazers")
 
39
  generate_button = gr.Button("Generate New Designs")
40
- output_gallery = gr.Gallery(label="Generated Designs", columns=5, rows=2)
41
- generate_button.click(fn=generate_and_display, outputs=output_gallery)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42
 
43
  if __name__ == "__main__":
44
  demo.launch()
 
 
1
+ # import gradio as gr
2
+ # import subprocess
3
+ # import os
4
+ # import random
5
+ # from PIL import Image
6
+
7
+ # # Paths
8
+ # OUTPUT_DIR = "outputs"
9
+ # MODEL_PATH = "blazer_model.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) < 10:
26
+ # generate_images()
27
+ # image_files = [f for f in os.listdir(OUTPUT_DIR) if f.endswith(".png")]
28
+ # random_images = random.sample(image_files, min(10, len(image_files)))
29
+ # return [Image.open(os.path.join(OUTPUT_DIR, img)) for img in random_images]
30
+
31
+ # # Gradio function
32
+ # def generate_and_display():
33
+ # generate_images()
34
+ # return get_random_images()
35
+
36
+ # # UI
37
+ # with gr.Blocks() as demo:
38
+ # gr.Markdown("# 🎨 AI-Generated Clothing Designs - Blazers")
39
+ # generate_button = gr.Button("Generate New Designs")
40
+ # output_gallery = gr.Gallery(label="Generated Designs", columns=5, rows=2)
41
+ # generate_button.click(fn=generate_and_display, outputs=output_gallery)
42
+
43
+ # if __name__ == "__main__":
44
+ # demo.launch()
45
+
46
+
47
+
48
+
49
  import gradio as gr
50
  import subprocess
51
  import os
52
  import random
53
  from PIL import Image
54
+ import shutil
55
+ import requests
56
 
57
+ # === Setup Paths ===
58
+ BASE_DIR = os.path.dirname(os.path.abspath(__file__))
59
+ GEN_SCRIPT = os.path.join(BASE_DIR, "stylegan3", "gen_images.py")
60
+ OUTPUT_DIR = os.path.join(BASE_DIR, "outputs")
61
+ MODEL_PATH = os.path.join(BASE_DIR, "blazer_model.pkl")
62
+ SAVE_DIR = os.path.join(BASE_DIR, "saved_images")
63
 
 
64
  os.makedirs(OUTPUT_DIR, exist_ok=True)
65
+ os.makedirs(SAVE_DIR, exist_ok=True)
66
 
67
+ # === Image Generation Function ===
68
  def generate_images():
69
+ command = [
70
+ "python",
71
+ GEN_SCRIPT,
72
+ f"--outdir={OUTPUT_DIR}",
73
+ "--trunc=1",
74
+ "--seeds=3-5,7,9,12-14,16-26,29,31,32,34,40,41",
75
+ f"--network={MODEL_PATH}"
76
+ ]
77
  try:
78
+ subprocess.run(command, check=True, capture_output=True, text=True)
79
  except subprocess.CalledProcessError as e:
80
+ return f"Error generating images:\n{e.stderr}"
81
 
82
+ # === Select Random Images from Output Folder ===
83
  def get_random_images():
84
  image_files = [f for f in os.listdir(OUTPUT_DIR) if f.endswith(".png")]
85
  if len(image_files) < 10:
86
  generate_images()
87
  image_files = [f for f in os.listdir(OUTPUT_DIR) if f.endswith(".png")]
88
  random_images = random.sample(image_files, min(10, len(image_files)))
89
+ image_paths = [os.path.join(OUTPUT_DIR, img) for img in random_images]
90
+ return image_paths
91
+
92
+ # === Send Image to Backend ===
93
+ def send_to_backend(img_path, user_id):
94
+ if not user_id:
95
+ return "❌ user_id not found in URL."
96
+
97
+ if not img_path or not os.path.exists(img_path):
98
+ return "⚠️ No image selected or image not found."
99
+
100
+ try:
101
+ with open(img_path, 'rb') as f:
102
+ files = {'file': ('generated_image.png', f, 'image/png')}
103
+
104
+ # Your backend endpoint here
105
+ url = f" https://a56e-103-40-74-78.ngrok-free.app/images/upload/{user_id}"
106
+ response = requests.post(url, files=files)
107
 
108
+ if response.status_code == 201:
109
+ return "✅ Image uploaded and saved to database!"
110
+ else:
111
+ return f"❌ Upload failed: {response.status_code} - {response.text}"
112
 
113
+ except Exception as e:
114
+ return f"⚠️ Error: {str(e)}"
115
+
116
+ # === Gradio Interface ===
117
  with gr.Blocks() as demo:
118
+ gr.Markdown("# 🎨 AI-Generated Clothing Designs - Dresses")
119
+
120
  generate_button = gr.Button("Generate New Designs")
121
+ user_id_state = gr.State()
122
+
123
+ @demo.load(inputs=None, outputs=[user_id_state])
124
+ def get_user_id(request: gr.Request):
125
+ return request.query_params.get("user_id", "")
126
+
127
+ image_components = []
128
+ file_paths = []
129
+ save_buttons = []
130
+ outputs = []
131
+
132
+ # Use 3 columns layout
133
+ for row_idx in range(4): # 4 rows (to cover 10 images)
134
+ with gr.Row():
135
+ for col_idx in range(3): # 3 columns
136
+ i = row_idx * 3 + col_idx
137
+ if i >= 10:
138
+ break
139
+ with gr.Column():
140
+ img = gr.Image(width=180, height=180, label=f"Design {i+1}")
141
+ image_components.append(img)
142
+
143
+ file_path = gr.Textbox(visible=False)
144
+ file_paths.append(file_path)
145
+
146
+ save_btn = gr.Button("💾 Save to DB")
147
+ save_buttons.append(save_btn)
148
+
149
+ output = gr.Textbox(label="Status", interactive=False)
150
+ outputs.append(output)
151
+
152
+ save_btn.click(
153
+ fn=send_to_backend,
154
+ inputs=[file_path, user_id_state],
155
+ outputs=output
156
+ )
157
+
158
+ # Generate button logic
159
+ def generate_and_display_images():
160
+ image_paths = get_random_images()
161
+ return image_paths + image_paths # One for display, one for hidden path tracking
162
+
163
+ generate_button.click(
164
+ fn=generate_and_display_images,
165
+ outputs=image_components + file_paths
166
+ )
167
 
168
  if __name__ == "__main__":
169
  demo.launch()
170
+