sahadev10 commited on
Commit
be7fb10
·
verified ·
1 Parent(s): 382f307

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -28
app.py CHANGED
@@ -63,14 +63,13 @@
63
 
64
  # iface.launch()
65
 
66
-
67
  import gradio as gr
68
  import torch
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,12 +77,10 @@ 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,7 +101,6 @@ def mix_styles(image1_path, image2_path, styles_to_mix):
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,14 +112,14 @@ def style_mixing_interface(image1, image2, mix_value):
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."
123
 
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,10 +130,15 @@ def send_to_backend(image_buffer, user_id):
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,30 +152,15 @@ with gr.Blocks(title="Style Mixing for Clothing Design") as iface:
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
 
 
63
 
64
  # iface.launch()
65
 
 
66
  import gradio as gr
67
  import torch
68
  import numpy as np
69
  from PIL import Image
70
  import os
71
+ import legacy
72
+ import torch_utils
73
  import requests
74
  import io
75
  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
  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
  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://361d-103-40-74-78.ngrok-free.app/customisation/upload/{user_id}"
122
+
123
  response = requests.post(url, files=files)
124
 
125
  if response.status_code == 201:
 
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])
139
+ def on_load(request: gr.Request):
140
+ user_id = request.query_params.get('user_id', '')
141
+ return user_id
142
 
143
  gr.Markdown("## Style Mixing for Clothing Design\nUpload two projected clothing images and mix their styles.")
144
 
 
152
  output_image = gr.Image(label="Mixed Clothing Design")
153
  save_button = gr.Button("Download & Save to Database")
154
 
155
+ image_buffer = gr.State()
156
  save_status = gr.Textbox(label="Save Status", interactive=False)
157
 
 
158
  def mix_and_store(image1, image2, mix_value):
159
  result_image, buffer = style_mixing_interface(image1, image2, mix_value)
160
  return result_image, buffer
161
 
162
+ mix_slider.change(mix_and_store, inputs=[image1_input, image2_input, mix_slider], outputs=[output_image, image_buffer])
163
+ save_button.click(send_to_backend, inputs=[image_buffer, user_id_state], outputs=[save_status])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
164
 
165
  iface.launch()
166