Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -69,8 +69,8 @@ import torch
|
|
| 69 |
import numpy as np
|
| 70 |
from PIL import Image
|
| 71 |
import os
|
| 72 |
-
import legacy
|
| 73 |
-
import torch_utils
|
| 74 |
import requests
|
| 75 |
import io
|
| 76 |
import warnings
|
|
@@ -78,10 +78,12 @@ import warnings
|
|
| 78 |
warnings.filterwarnings("ignore")
|
| 79 |
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
| 80 |
|
|
|
|
| 81 |
model_path = 'dress_model.pkl'
|
| 82 |
with open(model_path, 'rb') as f:
|
| 83 |
G = legacy.load_network_pkl(f)['G_ema'].to(device)
|
| 84 |
|
|
|
|
| 85 |
def mix_styles(image1_path, image2_path, styles_to_mix):
|
| 86 |
image1_name = os.path.splitext(os.path.basename(image1_path))[0]
|
| 87 |
image2_name = os.path.splitext(os.path.basename(image2_path))[0]
|
|
@@ -102,6 +104,7 @@ def mix_styles(image1_path, image2_path, styles_to_mix):
|
|
| 102 |
mixed_image = Image.fromarray(image[0], 'RGB')
|
| 103 |
return mixed_image
|
| 104 |
|
|
|
|
| 105 |
def style_mixing_interface(image1, image2, mix_value):
|
| 106 |
if image1 is None or image2 is None:
|
| 107 |
return None, None
|
|
@@ -113,6 +116,7 @@ def style_mixing_interface(image1, image2, mix_value):
|
|
| 113 |
buffer.seek(0)
|
| 114 |
return mixed_img, buffer
|
| 115 |
|
|
|
|
| 116 |
def send_to_backend(image_buffer, user_id):
|
| 117 |
if not user_id:
|
| 118 |
return "❌ user_id not found in URL."
|
|
@@ -120,7 +124,6 @@ def send_to_backend(image_buffer, user_id):
|
|
| 120 |
try:
|
| 121 |
files = {'file': ('generated_image.png', image_buffer, 'image/png')}
|
| 122 |
url = f"https://361d-103-40-74-78.ngrok-free.app/customisation/upload/{user_id}"
|
| 123 |
-
|
| 124 |
response = requests.post(url, files=files)
|
| 125 |
|
| 126 |
if response.status_code == 201:
|
|
@@ -131,8 +134,10 @@ def send_to_backend(image_buffer, user_id):
|
|
| 131 |
except Exception as e:
|
| 132 |
return f"⚠️ Error: {str(e)}"
|
| 133 |
|
|
|
|
| 134 |
with gr.Blocks(title="Style Mixing for Clothing Design") as iface:
|
| 135 |
user_id_state = gr.State()
|
|
|
|
| 136 |
|
| 137 |
gr.Markdown("## Style Mixing for Clothing Design\nUpload two projected clothing images and mix their styles.")
|
| 138 |
|
|
@@ -146,115 +151,30 @@ with gr.Blocks(title="Style Mixing for Clothing Design") as iface:
|
|
| 146 |
output_image = gr.Image(label="Mixed Clothing Design")
|
| 147 |
save_button = gr.Button("Download & Save to Database")
|
| 148 |
|
| 149 |
-
image_buffer = gr.State()
|
| 150 |
save_status = gr.Textbox(label="Save Status", interactive=False)
|
| 151 |
|
|
|
|
| 152 |
def mix_and_store(image1, image2, mix_value):
|
| 153 |
result_image, buffer = style_mixing_interface(image1, image2, mix_value)
|
| 154 |
return result_image, buffer
|
| 155 |
|
| 156 |
mix_slider.change(
|
| 157 |
-
mix_and_store,
|
| 158 |
-
inputs=[image1_input, image2_input, mix_slider],
|
| 159 |
outputs=[output_image, image_buffer]
|
| 160 |
)
|
| 161 |
|
| 162 |
save_button.click(
|
| 163 |
-
send_to_backend,
|
| 164 |
-
inputs=[image_buffer, user_id_state],
|
| 165 |
outputs=[save_status]
|
| 166 |
)
|
| 167 |
|
| 168 |
-
#
|
|
|
|
| 169 |
def init_fn(request: gr.Request):
|
| 170 |
user_id = request.query_params.get("user_id", "")
|
| 171 |
return {user_id_state: user_id}
|
| 172 |
|
| 173 |
-
iface.load(fn=None, inputs=None, outputs=None, preprocess=False, queue=False, show_progress=False)
|
| 174 |
-
iface.launch(initialize=init_fn)
|
| 175 |
-
(model_path, 'rb') as f:
|
| 176 |
-
G = legacy.load_network_pkl(f)['G_ema'].to(device)
|
| 177 |
-
|
| 178 |
-
def mix_styles(image1_path, image2_path, styles_to_mix):
|
| 179 |
-
image1_name = os.path.splitext(os.path.basename(image1_path))[0]
|
| 180 |
-
image2_name = os.path.splitext(os.path.basename(image2_path))[0]
|
| 181 |
-
|
| 182 |
-
latent_vector_1 = np.load(os.path.join("projection_results", image1_name, "projected_w.npz"))['w']
|
| 183 |
-
latent_vector_2 = np.load(os.path.join("projection_results", image2_name, "projected_w.npz"))['w']
|
| 184 |
-
|
| 185 |
-
latent_1_tensor = torch.from_numpy(latent_vector_1).to(device)
|
| 186 |
-
latent_2_tensor = torch.from_numpy(latent_vector_2).to(device)
|
| 187 |
-
|
| 188 |
-
mixed_latent = latent_1_tensor.clone()
|
| 189 |
-
mixed_latent[:, styles_to_mix] = latent_2_tensor[:, styles_to_mix]
|
| 190 |
-
|
| 191 |
-
with torch.no_grad():
|
| 192 |
-
image = G.synthesis(mixed_latent, noise_mode='const')
|
| 193 |
-
|
| 194 |
-
image = (image.permute(0, 2, 3, 1) * 127.5 + 128).clamp(0, 255).to(torch.uint8).cpu().numpy()
|
| 195 |
-
mixed_image = Image.fromarray(image[0], 'RGB')
|
| 196 |
-
return mixed_image
|
| 197 |
-
|
| 198 |
-
def style_mixing_interface(image1, image2, mix_value):
|
| 199 |
-
if image1 is None or image2 is None:
|
| 200 |
-
return None, None
|
| 201 |
-
selected_layers = list(range(mix_value + 1))
|
| 202 |
-
mixed_img = mix_styles(image1, image2, selected_layers)
|
| 203 |
-
|
| 204 |
-
buffer = io.BytesIO()
|
| 205 |
-
mixed_img.save(buffer, format="PNG")
|
| 206 |
-
buffer.seek(0)
|
| 207 |
-
return mixed_img, buffer
|
| 208 |
-
|
| 209 |
-
def send_to_backend(image_buffer, user_id):
|
| 210 |
-
if not user_id:
|
| 211 |
-
return "❌ user_id not found."
|
| 212 |
-
|
| 213 |
-
try:
|
| 214 |
-
files = {'file': ('generated_image.png', image_buffer, 'image/png')}
|
| 215 |
-
url = f"https://361d-103-40-74-78.ngrok-free.app/customisation/upload/{user_id}"
|
| 216 |
-
|
| 217 |
-
response = requests.post(url, files=files)
|
| 218 |
-
|
| 219 |
-
if response.status_code == 201:
|
| 220 |
-
return "✅ Image uploaded and saved to database!"
|
| 221 |
-
else:
|
| 222 |
-
return f"❌ Upload failed: {response.status_code} - {response.text}"
|
| 223 |
-
|
| 224 |
-
except Exception as e:
|
| 225 |
-
return f"⚠️ Error: {str(e)}"
|
| 226 |
-
|
| 227 |
-
# --- Gradio UI ---
|
| 228 |
-
with gr.Blocks(title="Style Mixing for Clothing Design") as iface:
|
| 229 |
-
|
| 230 |
-
user_id_state = gr.State()
|
| 231 |
-
|
| 232 |
-
@iface.load
|
| 233 |
-
def on_load(request: gr.Request):
|
| 234 |
-
user_id = request.query_params.get('user_id', '')
|
| 235 |
-
return user_id
|
| 236 |
-
|
| 237 |
-
gr.Markdown("## Style Mixing for Clothing Design\nUpload two projected clothing images and mix their styles.")
|
| 238 |
-
|
| 239 |
-
with gr.Row():
|
| 240 |
-
image1_input = gr.Image(label="First Clothing Image", type="filepath")
|
| 241 |
-
image2_input = gr.Image(label="Second Clothing Image", type="filepath")
|
| 242 |
-
|
| 243 |
-
mix_slider = gr.Slider(label="Style Mixing Strength (Layers 0 to N)", minimum=0, maximum=9, step=1, value=5)
|
| 244 |
-
|
| 245 |
-
with gr.Row():
|
| 246 |
-
output_image = gr.Image(label="Mixed Clothing Design")
|
| 247 |
-
save_button = gr.Button("Download & Save to Database")
|
| 248 |
-
|
| 249 |
-
image_buffer = gr.State()
|
| 250 |
-
save_status = gr.Textbox(label="Save Status", interactive=False)
|
| 251 |
-
|
| 252 |
-
def mix_and_store(image1, image2, mix_value):
|
| 253 |
-
result_image, buffer = style_mixing_interface(image1, image2, mix_value)
|
| 254 |
-
return result_image, buffer
|
| 255 |
-
|
| 256 |
-
mix_slider.change(mix_and_store, inputs=[image1_input, image2_input, mix_slider], outputs=[output_image, image_buffer])
|
| 257 |
-
save_button.click(send_to_backend, inputs=[image_buffer, user_id_state], outputs=[save_status])
|
| 258 |
-
|
| 259 |
iface.launch()
|
| 260 |
|
|
|
|
| 69 |
import numpy as np
|
| 70 |
from PIL import Image
|
| 71 |
import os
|
| 72 |
+
import legacy # your local module
|
| 73 |
+
import torch_utils # your local module
|
| 74 |
import requests
|
| 75 |
import io
|
| 76 |
import warnings
|
|
|
|
| 78 |
warnings.filterwarnings("ignore")
|
| 79 |
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
| 80 |
|
| 81 |
+
# Load StyleGAN model
|
| 82 |
model_path = 'dress_model.pkl'
|
| 83 |
with open(model_path, 'rb') as f:
|
| 84 |
G = legacy.load_network_pkl(f)['G_ema'].to(device)
|
| 85 |
|
| 86 |
+
# Function to perform style mixing
|
| 87 |
def mix_styles(image1_path, image2_path, styles_to_mix):
|
| 88 |
image1_name = os.path.splitext(os.path.basename(image1_path))[0]
|
| 89 |
image2_name = os.path.splitext(os.path.basename(image2_path))[0]
|
|
|
|
| 104 |
mixed_image = Image.fromarray(image[0], 'RGB')
|
| 105 |
return mixed_image
|
| 106 |
|
| 107 |
+
# Gradio-friendly wrapper
|
| 108 |
def style_mixing_interface(image1, image2, mix_value):
|
| 109 |
if image1 is None or image2 is None:
|
| 110 |
return None, None
|
|
|
|
| 116 |
buffer.seek(0)
|
| 117 |
return mixed_img, buffer
|
| 118 |
|
| 119 |
+
# Upload to backend
|
| 120 |
def send_to_backend(image_buffer, user_id):
|
| 121 |
if not user_id:
|
| 122 |
return "❌ user_id not found in URL."
|
|
|
|
| 124 |
try:
|
| 125 |
files = {'file': ('generated_image.png', image_buffer, 'image/png')}
|
| 126 |
url = f"https://361d-103-40-74-78.ngrok-free.app/customisation/upload/{user_id}"
|
|
|
|
| 127 |
response = requests.post(url, files=files)
|
| 128 |
|
| 129 |
if response.status_code == 201:
|
|
|
|
| 134 |
except Exception as e:
|
| 135 |
return f"⚠️ Error: {str(e)}"
|
| 136 |
|
| 137 |
+
# Gradio Interface
|
| 138 |
with gr.Blocks(title="Style Mixing for Clothing Design") as iface:
|
| 139 |
user_id_state = gr.State()
|
| 140 |
+
image_buffer = gr.State()
|
| 141 |
|
| 142 |
gr.Markdown("## Style Mixing for Clothing Design\nUpload two projected clothing images and mix their styles.")
|
| 143 |
|
|
|
|
| 151 |
output_image = gr.Image(label="Mixed Clothing Design")
|
| 152 |
save_button = gr.Button("Download & Save to Database")
|
| 153 |
|
|
|
|
| 154 |
save_status = gr.Textbox(label="Save Status", interactive=False)
|
| 155 |
|
| 156 |
+
# Mix and update preview image + store in memory buffer
|
| 157 |
def mix_and_store(image1, image2, mix_value):
|
| 158 |
result_image, buffer = style_mixing_interface(image1, image2, mix_value)
|
| 159 |
return result_image, buffer
|
| 160 |
|
| 161 |
mix_slider.change(
|
| 162 |
+
fn=mix_and_store,
|
| 163 |
+
inputs=[image1_input, image2_input, mix_slider],
|
| 164 |
outputs=[output_image, image_buffer]
|
| 165 |
)
|
| 166 |
|
| 167 |
save_button.click(
|
| 168 |
+
fn=send_to_backend,
|
| 169 |
+
inputs=[image_buffer, user_id_state],
|
| 170 |
outputs=[save_status]
|
| 171 |
)
|
| 172 |
|
| 173 |
+
# Extract user_id from URL query
|
| 174 |
+
@iface.load
|
| 175 |
def init_fn(request: gr.Request):
|
| 176 |
user_id = request.query_params.get("user_id", "")
|
| 177 |
return {user_id_state: user_id}
|
| 178 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 179 |
iface.launch()
|
| 180 |
|