sahadev10 commited on
Commit
e1ed2a7
·
verified ·
1 Parent(s): 8cf9a64

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +73 -51
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' # Place your .pkl in the same directory or update path
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
- # Function to handle style mixing via Gradio UI
113
- def style_mixing_interface(image1, image2, mix_value, cookie):
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
- decoded_token = jwt.decode(cookie, options={"verify_exp": False}) # Decode token without verifying expiration
120
- user_id = decoded_token.get("user_id", None)
121
- except jwt.ExpiredSignatureError:
122
- return "Session expired, please log in again."
123
- except jwt.InvalidTokenError:
124
- return "Invalid token, please log in again."
125
 
126
- selected_layers = list(range(mix_value + 1))
127
- mixed_image = mix_styles(image1, image2, selected_layers)
 
128
 
129
- # Call backend API to save the image
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 == 200:
136
- return "Image uploaded successfully!"
137
  else:
138
- return f"Failed to upload image: {response.text}"
139
- else:
140
- return "User ID not found in token."
141
-
142
- # Gradio UI
143
- iface = gr.Interface(
144
- fn=style_mixing_interface,
145
- inputs=[
146
- gr.Image(label="First Clothing Image", type="filepath"),
147
- gr.Image(label="Second Clothing Image", type="filepath"),
148
- gr.Slider(label="Style Mixing Strength (Layers 0 to N)", minimum=0, maximum=9, step=1, value=5),
149
- gr.Textbox(label="JWT Token (as cookie)", type="text") # You may pass JWT token here for testing purposes
150
- ],
151
- outputs=gr.Image(label="Mixed Clothing Design"),
152
- live=True,
153
- title="Style Mixing for Clothing Design",
154
- description="Upload two projected images and choose how many early layers to mix. The resulting image will be saved after mixing."
155
- )
156
-
157
- # Launch the Gradio interface directly
 
 
 
 
 
 
 
 
 
 
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
+