sahadev10 commited on
Commit
4332791
·
verified ·
1 Parent(s): 7d13bfb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +128 -2
app.py CHANGED
@@ -63,6 +63,126 @@
63
 
64
  # iface.launch()
65
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
66
  import gradio as gr
67
  import torch
68
  import numpy as np
@@ -77,10 +197,12 @@ import warnings
77
  warnings.filterwarnings("ignore")
78
  device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
79
 
 
80
  model_path = 'dress_model.pkl'
81
  with open(model_path, 'rb') as f:
82
  G = legacy.load_network_pkl(f)['G_ema'].to(device)
83
 
 
84
  def mix_styles(image1_path, image2_path, styles_to_mix):
85
  image1_name = os.path.splitext(os.path.basename(image1_path))[0]
86
  image2_name = os.path.splitext(os.path.basename(image2_path))[0]
@@ -101,6 +223,7 @@ def mix_styles(image1_path, image2_path, styles_to_mix):
101
  mixed_image = Image.fromarray(image[0], 'RGB')
102
  return mixed_image
103
 
 
104
  def style_mixing_interface(image1, image2, mix_value):
105
  if image1 is None or image2 is None:
106
  return None, None
@@ -112,12 +235,16 @@ def style_mixing_interface(image1, image2, mix_value):
112
  buffer.seek(0)
113
  return mixed_img, buffer
114
 
 
115
  def send_to_backend(image_buffer, user_id):
116
  if not user_id:
117
  return "❌ user_id not found."
118
 
119
  try:
 
120
  files = {'file': ('generated_image.png', image_buffer, 'image/png')}
 
 
121
  url = f"https://5a4d-103-40-74-78.ngrok-free.app/customisation/upload/{user_id}"
122
 
123
  response = requests.post(url, files=files)
@@ -130,9 +257,8 @@ def send_to_backend(image_buffer, user_id):
130
  except Exception as e:
131
  return f"⚠️ Error: {str(e)}"
132
 
133
- # --- Gradio UI ---
134
  with gr.Blocks(title="Style Mixing for Clothing Design") as iface:
135
-
136
  user_id_state = gr.State()
137
 
138
  @iface.load(inputs=None, outputs=[user_id_state])
 
63
 
64
  # iface.launch()
65
 
66
+
67
+
68
+
69
+
70
+
71
+
72
+ # import gradio as gr
73
+ # import torch
74
+ # import numpy as np
75
+ # from PIL import Image
76
+ # import os
77
+ # import legacy
78
+ # import torch_utils
79
+ # import requests
80
+ # import io
81
+ # import warnings
82
+
83
+ # warnings.filterwarnings("ignore")
84
+ # device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
85
+
86
+ # model_path = 'dress_model.pkl'
87
+ # with open(model_path, 'rb') as f:
88
+ # G = legacy.load_network_pkl(f)['G_ema'].to(device)
89
+
90
+ # def mix_styles(image1_path, image2_path, styles_to_mix):
91
+ # image1_name = os.path.splitext(os.path.basename(image1_path))[0]
92
+ # image2_name = os.path.splitext(os.path.basename(image2_path))[0]
93
+
94
+ # latent_vector_1 = np.load(os.path.join("projection_results", image1_name, "projected_w.npz"))['w']
95
+ # latent_vector_2 = np.load(os.path.join("projection_results", image2_name, "projected_w.npz"))['w']
96
+
97
+ # latent_1_tensor = torch.from_numpy(latent_vector_1).to(device)
98
+ # latent_2_tensor = torch.from_numpy(latent_vector_2).to(device)
99
+
100
+ # mixed_latent = latent_1_tensor.clone()
101
+ # mixed_latent[:, styles_to_mix] = latent_2_tensor[:, styles_to_mix]
102
+
103
+ # with torch.no_grad():
104
+ # image = G.synthesis(mixed_latent, noise_mode='const')
105
+
106
+ # image = (image.permute(0, 2, 3, 1) * 127.5 + 128).clamp(0, 255).to(torch.uint8).cpu().numpy()
107
+ # mixed_image = Image.fromarray(image[0], 'RGB')
108
+ # return mixed_image
109
+
110
+ # def style_mixing_interface(image1, image2, mix_value):
111
+ # if image1 is None or image2 is None:
112
+ # return None, None
113
+ # selected_layers = list(range(mix_value + 1))
114
+ # mixed_img = mix_styles(image1, image2, selected_layers)
115
+
116
+ # buffer = io.BytesIO()
117
+ # mixed_img.save(buffer, format="PNG")
118
+ # buffer.seek(0)
119
+ # return mixed_img, buffer
120
+
121
+ # def send_to_backend(image_buffer, user_id):
122
+ # if not user_id:
123
+ # return "❌ user_id not found."
124
+
125
+ # try:
126
+ # files = {'file': ('generated_image.png', image_buffer, 'image/png')}
127
+ # url = f"https://5a4d-103-40-74-78.ngrok-free.app/customisation/upload/{user_id}"
128
+
129
+ # response = requests.post(url, files=files)
130
+
131
+ # if response.status_code == 201:
132
+ # return "✅ Image uploaded and saved to database!"
133
+ # else:
134
+ # return f"❌ Upload failed: {response.status_code} - {response.text}"
135
+
136
+ # except Exception as e:
137
+ # return f"⚠️ Error: {str(e)}"
138
+
139
+ # # --- Gradio UI ---
140
+ # with gr.Blocks(title="Style Mixing for Clothing Design") as iface:
141
+
142
+ # user_id_state = gr.State()
143
+
144
+ # @iface.load(inputs=None, outputs=[user_id_state])
145
+ # def on_load(request: gr.Request):
146
+ # user_id = request.query_params.get('user_id', '')
147
+ # return user_id
148
+
149
+ # gr.Markdown("## Style Mixing for Clothing Design\nUpload two projected clothing images and mix their styles.")
150
+
151
+ # with gr.Row():
152
+ # image1_input = gr.Image(label="First Clothing Image", type="filepath")
153
+ # image2_input = gr.Image(label="Second Clothing Image", type="filepath")
154
+
155
+ # mix_slider = gr.Slider(label="Style Mixing Strength (Layers 0 to N)", minimum=0, maximum=9, step=1, value=5)
156
+
157
+ # with gr.Row():
158
+ # output_image = gr.Image(label="Mixed Clothing Design")
159
+ # save_button = gr.Button("Download & Save to Database")
160
+
161
+ # image_buffer = gr.State()
162
+ # save_status = gr.Textbox(label="Save Status", interactive=False)
163
+
164
+ # def mix_and_store(image1, image2, mix_value):
165
+ # result_image, buffer = style_mixing_interface(image1, image2, mix_value)
166
+ # return result_image, buffer
167
+
168
+ # mix_slider.change(mix_and_store, inputs=[image1_input, image2_input, mix_slider], outputs=[output_image, image_buffer])
169
+ # save_button.click(send_to_backend, inputs=[image_buffer, user_id_state], outputs=[save_status])
170
+
171
+ # iface.launch()
172
+
173
+
174
+
175
+
176
+
177
+
178
+
179
+
180
+
181
+
182
+
183
+
184
+
185
+
186
  import gradio as gr
187
  import torch
188
  import numpy as np
 
197
  warnings.filterwarnings("ignore")
198
  device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
199
 
200
+ # Load pretrained StyleGAN model
201
  model_path = 'dress_model.pkl'
202
  with open(model_path, 'rb') as f:
203
  G = legacy.load_network_pkl(f)['G_ema'].to(device)
204
 
205
+ # Style mixing between two projected images
206
  def mix_styles(image1_path, image2_path, styles_to_mix):
207
  image1_name = os.path.splitext(os.path.basename(image1_path))[0]
208
  image2_name = os.path.splitext(os.path.basename(image2_path))[0]
 
223
  mixed_image = Image.fromarray(image[0], 'RGB')
224
  return mixed_image
225
 
226
+ # Handles style mixing + output buffer
227
  def style_mixing_interface(image1, image2, mix_value):
228
  if image1 is None or image2 is None:
229
  return None, None
 
235
  buffer.seek(0)
236
  return mixed_img, buffer
237
 
238
+ # Upload to NestJS backend
239
  def send_to_backend(image_buffer, user_id):
240
  if not user_id:
241
  return "❌ user_id not found."
242
 
243
  try:
244
+ # ✅ key 'file' must match the NestJS interceptor
245
  files = {'file': ('generated_image.png', image_buffer, 'image/png')}
246
+
247
+ # ✅ Update URL accordingly
248
  url = f"https://5a4d-103-40-74-78.ngrok-free.app/customisation/upload/{user_id}"
249
 
250
  response = requests.post(url, files=files)
 
257
  except Exception as e:
258
  return f"⚠️ Error: {str(e)}"
259
 
260
+ # Gradio interface
261
  with gr.Blocks(title="Style Mixing for Clothing Design") as iface:
 
262
  user_id_state = gr.State()
263
 
264
  @iface.load(inputs=None, outputs=[user_id_state])