Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -63,7 +63,6 @@
|
|
| 63 |
|
| 64 |
# iface.launch()
|
| 65 |
|
| 66 |
-
|
| 67 |
import gradio as gr
|
| 68 |
import torch
|
| 69 |
import numpy as np
|
|
@@ -71,90 +70,113 @@ from PIL import Image
|
|
| 71 |
import os
|
| 72 |
import legacy
|
| 73 |
import torch_utils
|
| 74 |
-
import jwt
|
| 75 |
import requests
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
|
| 77 |
-
# Load the pre-trained StyleGAN model
|
| 78 |
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
| 79 |
-
model_path = 'dress_model.pkl'
|
| 80 |
|
| 81 |
-
# Load StyleGAN Generator
|
| 82 |
with open(model_path, 'rb') as f:
|
| 83 |
G = legacy.load_network_pkl(f)['G_ema'].to(device)
|
| 84 |
|
| 85 |
-
# Function to mix styles of two clothing images
|
| 86 |
def mix_styles(image1_path, image2_path, styles_to_mix):
|
| 87 |
-
# Extract image names (without extensions)
|
| 88 |
image1_name = os.path.splitext(os.path.basename(image1_path))[0]
|
| 89 |
image2_name = os.path.splitext(os.path.basename(image2_path))[0]
|
| 90 |
|
| 91 |
-
# Load latent vectors from .npz
|
| 92 |
latent_vector_1 = np.load(os.path.join("projection_results", image1_name, "projected_w.npz"))['w']
|
| 93 |
latent_vector_2 = np.load(os.path.join("projection_results", image2_name, "projected_w.npz"))['w']
|
| 94 |
|
| 95 |
-
# Convert to torch tensors
|
| 96 |
latent_1_tensor = torch.from_numpy(latent_vector_1).to(device)
|
| 97 |
latent_2_tensor = torch.from_numpy(latent_vector_2).to(device)
|
| 98 |
|
| 99 |
-
# Mix layers
|
| 100 |
mixed_latent = latent_1_tensor.clone()
|
| 101 |
mixed_latent[:, styles_to_mix] = latent_2_tensor[:, styles_to_mix]
|
| 102 |
|
| 103 |
-
# Generate image
|
| 104 |
with torch.no_grad():
|
| 105 |
image = G.synthesis(mixed_latent, noise_mode='const')
|
| 106 |
|
| 107 |
-
# Convert to image
|
| 108 |
image = (image.permute(0, 2, 3, 1) * 127.5 + 128).clamp(0, 255).to(torch.uint8).cpu().numpy()
|
| 109 |
mixed_image = Image.fromarray(image[0], 'RGB')
|
| 110 |
return mixed_image
|
| 111 |
|
| 112 |
-
#
|
| 113 |
-
def style_mixing_interface(image1, image2, mix_value
|
| 114 |
if image1 is None or image2 is None:
|
| 115 |
-
return None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 116 |
|
| 117 |
-
# Extract user_id from the JWT token passed via cookies (assuming JWT token is passed as 'cookie' in the request)
|
| 118 |
try:
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
return "Invalid token, please log in again."
|
| 125 |
|
| 126 |
-
|
| 127 |
-
|
|
|
|
| 128 |
|
| 129 |
-
|
| 130 |
-
if user_id:
|
| 131 |
-
upload_url = f"http://localhost:3000/customisation/upload/{user_id}"
|
| 132 |
-
files = {'file': ('mixed_image.png', mixed_image.tobytes(), 'image/png')}
|
| 133 |
-
response = requests.post(upload_url, files=files)
|
| 134 |
|
| 135 |
-
if response.status_code ==
|
| 136 |
-
return "Image uploaded
|
| 137 |
else:
|
| 138 |
-
return f"
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
)
|
| 156 |
-
|
| 157 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 158 |
iface.launch()
|
| 159 |
|
| 160 |
|
|
|
|
|
|
| 63 |
|
| 64 |
# iface.launch()
|
| 65 |
|
|
|
|
| 66 |
import gradio as gr
|
| 67 |
import torch
|
| 68 |
import numpy as np
|
|
|
|
| 70 |
import os
|
| 71 |
import legacy
|
| 72 |
import torch_utils
|
|
|
|
| 73 |
import requests
|
| 74 |
+
import io
|
| 75 |
+
from http.cookies import SimpleCookie
|
| 76 |
+
import jwt # PyJWT
|
| 77 |
+
import warnings
|
| 78 |
+
|
| 79 |
+
# Suppress deprecated torch warnings
|
| 80 |
+
warnings.filterwarnings("ignore")
|
| 81 |
|
| 82 |
+
# --- Load the pre-trained StyleGAN model ---
|
| 83 |
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
| 84 |
+
model_path = 'dress_model.pkl'
|
| 85 |
|
|
|
|
| 86 |
with open(model_path, 'rb') as f:
|
| 87 |
G = legacy.load_network_pkl(f)['G_ema'].to(device)
|
| 88 |
|
|
|
|
| 89 |
def mix_styles(image1_path, image2_path, styles_to_mix):
|
|
|
|
| 90 |
image1_name = os.path.splitext(os.path.basename(image1_path))[0]
|
| 91 |
image2_name = os.path.splitext(os.path.basename(image2_path))[0]
|
| 92 |
|
|
|
|
| 93 |
latent_vector_1 = np.load(os.path.join("projection_results", image1_name, "projected_w.npz"))['w']
|
| 94 |
latent_vector_2 = np.load(os.path.join("projection_results", image2_name, "projected_w.npz"))['w']
|
| 95 |
|
|
|
|
| 96 |
latent_1_tensor = torch.from_numpy(latent_vector_1).to(device)
|
| 97 |
latent_2_tensor = torch.from_numpy(latent_vector_2).to(device)
|
| 98 |
|
|
|
|
| 99 |
mixed_latent = latent_1_tensor.clone()
|
| 100 |
mixed_latent[:, styles_to_mix] = latent_2_tensor[:, styles_to_mix]
|
| 101 |
|
|
|
|
| 102 |
with torch.no_grad():
|
| 103 |
image = G.synthesis(mixed_latent, noise_mode='const')
|
| 104 |
|
|
|
|
| 105 |
image = (image.permute(0, 2, 3, 1) * 127.5 + 128).clamp(0, 255).to(torch.uint8).cpu().numpy()
|
| 106 |
mixed_image = Image.fromarray(image[0], 'RGB')
|
| 107 |
return mixed_image
|
| 108 |
|
| 109 |
+
# --- Step 1: Mixing and preparing the image ---
|
| 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 |
+
# --- Step 2: Extract user_id from JWT cookie and upload image ---
|
| 122 |
+
def send_to_backend(image_buffer, request: gr.Request):
|
| 123 |
+
cookie_header = request.headers.get('cookie', '')
|
| 124 |
+
cookies = SimpleCookie()
|
| 125 |
+
cookies.load(cookie_header)
|
| 126 |
+
|
| 127 |
+
jwt_token_cookie = cookies.get("access_token")
|
| 128 |
+
if not jwt_token_cookie:
|
| 129 |
+
return "❌ JWT token not found in cookies."
|
| 130 |
+
|
| 131 |
+
token = jwt_token_cookie.value
|
| 132 |
|
|
|
|
| 133 |
try:
|
| 134 |
+
# Decode JWT (no verification assuming it's a trusted local token for this case)
|
| 135 |
+
payload = jwt.decode(token, options={"verify_signature": False})
|
| 136 |
+
user_id = payload.get("user_id")
|
| 137 |
+
if not user_id:
|
| 138 |
+
return "❌ user_id not found in JWT."
|
|
|
|
| 139 |
|
| 140 |
+
# Prepare image upload
|
| 141 |
+
files = {'file': ('generated_image.png', image_buffer, 'image/png')}
|
| 142 |
+
url = f"http://localhost:3000/customisation/upload/{user_id}"
|
| 143 |
|
| 144 |
+
response = requests.post(url, files=files)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 145 |
|
| 146 |
+
if response.status_code == 201:
|
| 147 |
+
return "✅ Image uploaded and saved to database!"
|
| 148 |
else:
|
| 149 |
+
return f"❌ Upload failed: {response.status_code} - {response.text}"
|
| 150 |
+
|
| 151 |
+
except jwt.DecodeError:
|
| 152 |
+
return "❌ Invalid JWT token."
|
| 153 |
+
except Exception as e:
|
| 154 |
+
return f"⚠️ Error: {str(e)}"
|
| 155 |
+
|
| 156 |
+
# --- Gradio Interface ---
|
| 157 |
+
with gr.Blocks(title="Style Mixing for Clothing Design") as iface:
|
| 158 |
+
gr.Markdown("## Style Mixing for Clothing Design\nUpload two projected clothing images and mix their styles.")
|
| 159 |
+
|
| 160 |
+
with gr.Row():
|
| 161 |
+
image1_input = gr.Image(label="First Clothing Image", type="filepath")
|
| 162 |
+
image2_input = gr.Image(label="Second Clothing Image", type="filepath")
|
| 163 |
+
|
| 164 |
+
mix_slider = gr.Slider(label="Style Mixing Strength (Layers 0 to N)", minimum=0, maximum=9, step=1, value=5)
|
| 165 |
+
|
| 166 |
+
output_image = gr.Image(label="Mixed Clothing Design")
|
| 167 |
+
image_buffer = gr.State()
|
| 168 |
+
|
| 169 |
+
save_button = gr.Button("Download & Save to Database")
|
| 170 |
+
save_status = gr.Textbox(label="Save Status", interactive=False)
|
| 171 |
+
|
| 172 |
+
def mix_and_store(image1, image2, mix_value):
|
| 173 |
+
result_image, buffer = style_mixing_interface(image1, image2, mix_value)
|
| 174 |
+
return result_image, buffer
|
| 175 |
+
|
| 176 |
+
mix_slider.change(mix_and_store, inputs=[image1_input, image2_input, mix_slider], outputs=[output_image, image_buffer])
|
| 177 |
+
save_button.click(send_to_backend, inputs=[image_buffer], outputs=[save_status])
|
| 178 |
+
|
| 179 |
iface.launch()
|
| 180 |
|
| 181 |
|
| 182 |
+
|