Update app.py
Browse files
app.py
CHANGED
@@ -10,17 +10,9 @@ import io
|
|
10 |
import logging
|
11 |
import gradio as gr
|
12 |
import numpy as np
|
|
|
13 |
import uuid
|
14 |
import random
|
15 |
-
if os.environ.get("SPACES_ZERO_GPU") is not None:
|
16 |
-
import spaces
|
17 |
-
else:
|
18 |
-
class spaces:
|
19 |
-
@staticmethod
|
20 |
-
def GPU(func):
|
21 |
-
def wrapper(*args, **kwargs):
|
22 |
-
return func(*args, **kwargs)
|
23 |
-
return wrapper
|
24 |
from src.tryon_pipeline import StableDiffusionXLInpaintPipeline as TryonPipeline
|
25 |
from src.unet_hacked_garmnet import UNet2DConditionModel as UNet2DConditionModel_ref
|
26 |
from src.unet_hacked_tryon import UNet2DConditionModel
|
@@ -74,7 +66,6 @@ UNet_Encoder = UNet2DConditionModel_ref.from_pretrained(base_path, subfolder="un
|
|
74 |
parsing_model = Parsing(0)
|
75 |
openpose_model = OpenPose(0)
|
76 |
|
77 |
-
|
78 |
# Préparation du pipeline Tryon
|
79 |
pipe = TryonPipeline.from_pretrained(
|
80 |
base_path,
|
@@ -98,7 +89,6 @@ tensor_transfrom = transforms.Compose([
|
|
98 |
transforms.Normalize([0.5], [0.5]),
|
99 |
])
|
100 |
|
101 |
-
@spaces.GPU
|
102 |
def pil_to_binary_mask(pil_image, threshold=0):
|
103 |
np_image = np.array(pil_image)
|
104 |
grayscale_image = Image.fromarray(np_image).convert("L")
|
@@ -142,6 +132,7 @@ def save_image(img):
|
|
142 |
img.save(unique_name, format="WEBP", lossless=True)
|
143 |
return unique_name
|
144 |
|
|
|
145 |
def start_tryon(dict, garm_img, garment_des, is_checked, is_checked_crop, denoise_steps, seed, categorie = 'upper_body'):
|
146 |
device = "cuda"
|
147 |
openpose_model.preprocessor.body_estimation.model.to(device)
|
@@ -238,7 +229,7 @@ def start_tryon(dict, garm_img, garment_des, is_checked, is_checked_crop, denois
|
|
238 |
height=768,
|
239 |
width=768,
|
240 |
ip_adapter_image=garm_img.resize((768, 768)),
|
241 |
-
guidance_scale=
|
242 |
)[0]
|
243 |
|
244 |
if is_checked_crop:
|
@@ -248,7 +239,7 @@ def start_tryon(dict, garm_img, garment_des, is_checked, is_checked_crop, denois
|
|
248 |
else:
|
249 |
return images[0], mask_gray , mask
|
250 |
|
251 |
-
|
252 |
@app.route('/tryon-v2', methods=['POST'])
|
253 |
def tryon_v2():
|
254 |
|
@@ -295,7 +286,6 @@ def process_image(image_data):
|
|
295 |
else:
|
296 |
return decode_image_from_base64(image_data) # Décode l'image base64
|
297 |
|
298 |
-
@spaces.GPU
|
299 |
@app.route('/tryon', methods=['POST'])
|
300 |
def tryon():
|
301 |
data = request.json
|
@@ -326,7 +316,6 @@ def tryon():
|
|
326 |
|
327 |
|
328 |
# Route index
|
329 |
-
@spaces.GPU
|
330 |
@app.route('/', methods=['GET'])
|
331 |
def index():
|
332 |
|
@@ -350,6 +339,7 @@ def get_image(image_id):
|
|
350 |
|
351 |
|
352 |
|
|
|
353 |
def generate_mask(human_img, categorie='upper_body'):
|
354 |
device = "cuda"
|
355 |
openpose_model.preprocessor.body_estimation.model.to(device)
|
@@ -373,7 +363,6 @@ def generate_mask(human_img, categorie='upper_body'):
|
|
373 |
logging.error(f"Error generating mask: {e}")
|
374 |
raise e
|
375 |
|
376 |
-
@spaces.GPU
|
377 |
@app.route('/generate_mask', methods=['POST'])
|
378 |
def generate_mask_api():
|
379 |
try:
|
@@ -398,7 +387,10 @@ def generate_mask_api():
|
|
398 |
logging.error(f"Error generating mask: {e}")
|
399 |
return jsonify({'error': str(e)}), 500
|
400 |
|
401 |
-
|
|
|
|
|
402 |
|
403 |
if __name__ == "__main__":
|
|
|
404 |
app.run(debug=False, host="0.0.0.0", port=7860)
|
|
|
10 |
import logging
|
11 |
import gradio as gr
|
12 |
import numpy as np
|
13 |
+
import spaces
|
14 |
import uuid
|
15 |
import random
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
from src.tryon_pipeline import StableDiffusionXLInpaintPipeline as TryonPipeline
|
17 |
from src.unet_hacked_garmnet import UNet2DConditionModel as UNet2DConditionModel_ref
|
18 |
from src.unet_hacked_tryon import UNet2DConditionModel
|
|
|
66 |
parsing_model = Parsing(0)
|
67 |
openpose_model = OpenPose(0)
|
68 |
|
|
|
69 |
# Préparation du pipeline Tryon
|
70 |
pipe = TryonPipeline.from_pretrained(
|
71 |
base_path,
|
|
|
89 |
transforms.Normalize([0.5], [0.5]),
|
90 |
])
|
91 |
|
|
|
92 |
def pil_to_binary_mask(pil_image, threshold=0):
|
93 |
np_image = np.array(pil_image)
|
94 |
grayscale_image = Image.fromarray(np_image).convert("L")
|
|
|
132 |
img.save(unique_name, format="WEBP", lossless=True)
|
133 |
return unique_name
|
134 |
|
135 |
+
@spaces.GPU
|
136 |
def start_tryon(dict, garm_img, garment_des, is_checked, is_checked_crop, denoise_steps, seed, categorie = 'upper_body'):
|
137 |
device = "cuda"
|
138 |
openpose_model.preprocessor.body_estimation.model.to(device)
|
|
|
229 |
height=768,
|
230 |
width=768,
|
231 |
ip_adapter_image=garm_img.resize((768, 768)),
|
232 |
+
guidance_scale=5,
|
233 |
)[0]
|
234 |
|
235 |
if is_checked_crop:
|
|
|
239 |
else:
|
240 |
return images[0], mask_gray , mask
|
241 |
|
242 |
+
|
243 |
@app.route('/tryon-v2', methods=['POST'])
|
244 |
def tryon_v2():
|
245 |
|
|
|
286 |
else:
|
287 |
return decode_image_from_base64(image_data) # Décode l'image base64
|
288 |
|
|
|
289 |
@app.route('/tryon', methods=['POST'])
|
290 |
def tryon():
|
291 |
data = request.json
|
|
|
316 |
|
317 |
|
318 |
# Route index
|
|
|
319 |
@app.route('/', methods=['GET'])
|
320 |
def index():
|
321 |
|
|
|
339 |
|
340 |
|
341 |
|
342 |
+
@spaces.GPU
|
343 |
def generate_mask(human_img, categorie='upper_body'):
|
344 |
device = "cuda"
|
345 |
openpose_model.preprocessor.body_estimation.model.to(device)
|
|
|
363 |
logging.error(f"Error generating mask: {e}")
|
364 |
raise e
|
365 |
|
|
|
366 |
@app.route('/generate_mask', methods=['POST'])
|
367 |
def generate_mask_api():
|
368 |
try:
|
|
|
387 |
logging.error(f"Error generating mask: {e}")
|
388 |
return jsonify({'error': str(e)}), 500
|
389 |
|
390 |
+
@spaces.GPU
|
391 |
+
def warmup_gpu():
|
392 |
+
logging.info("Warmup GPU called to register with Hugging Face Spaces.")
|
393 |
|
394 |
if __name__ == "__main__":
|
395 |
+
warmup_gpu()
|
396 |
app.run(debug=False, host="0.0.0.0", port=7860)
|