Spaces:
Saad0KH
/
Running on Zero

Saad0KH commited on
Commit
9ed418d
·
verified ·
1 Parent(s): a37ae3f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +64 -4
app.py CHANGED
@@ -1,5 +1,5 @@
1
  import os
2
- from flask import Flask, request, jsonify
3
  from PIL import Image
4
  from io import BytesIO
5
  import torch
@@ -9,6 +9,8 @@ import logging
9
  import gradio as gr
10
  import numpy as np
11
  import spaces
 
 
12
  from src.tryon_pipeline import StableDiffusionXLInpaintPipeline as TryonPipeline
13
  from src.unet_hacked_garmnet import UNet2DConditionModel as UNet2DConditionModel_ref
14
  from src.unet_hacked_tryon import UNet2DConditionModel
@@ -123,6 +125,15 @@ def pil_to_binary_mask(pil_image, threshold=0):
123
  output_mask = Image.fromarray(mask)
124
  return output_mask
125
 
 
 
 
 
 
 
 
 
 
126
 
127
  def decode_image_from_base64(base64_str):
128
  try:
@@ -143,6 +154,11 @@ def encode_image_to_base64(img):
143
  logging.error(f"Error encoding image: {e}")
144
  raise
145
 
 
 
 
 
 
146
  @spaces.GPU
147
  def start_tryon(dict, garm_img, garment_des, is_checked, is_checked_crop, denoise_steps, seed, categorie = 'upper_body'):
148
  device = "cuda"
@@ -255,11 +271,18 @@ def clear_gpu_memory():
255
  torch.cuda.empty_cache()
256
  torch.cuda.synchronize()
257
 
 
 
 
 
 
 
 
258
  @app.route('/tryon', methods=['POST'])
259
  def tryon():
260
  data = request.json
261
- human_image = decode_image_from_base64(data['human_image'])
262
- garment_image = decode_image_from_base64(data['garment_image'])
263
  description = data.get('description')
264
  use_auto_mask = data.get('use_auto_mask', True)
265
  use_auto_crop = data.get('use_auto_crop', False)
@@ -283,6 +306,32 @@ def tryon():
283
  'mask_image': mask_base64
284
  })
285
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
286
  @spaces.GPU
287
  def generate_mask(human_img, categorie='upper_body'):
288
  device = "cuda"
@@ -316,7 +365,7 @@ def generate_mask_api():
316
  categorie = data.get('categorie', 'upper_body')
317
 
318
  # Décodage de l'image à partir de base64
319
- human_img = decode_image_from_base64(base64_image)
320
 
321
  # Appeler la fonction pour générer le masque
322
  mask_resized = generate_mask(human_img, categorie)
@@ -331,6 +380,17 @@ def generate_mask_api():
331
  logging.error(f"Error generating mask: {e}")
332
  return jsonify({'error': str(e)}), 500
333
 
 
 
 
 
 
 
 
 
 
 
 
334
 
335
  if __name__ == "__main__":
336
  app.run(debug=True, host="0.0.0.0", port=7860)
 
1
  import os
2
+ from flask import Flask, request, jsonify,send_file
3
  from PIL import Image
4
  from io import BytesIO
5
  import torch
 
9
  import gradio as gr
10
  import numpy as np
11
  import spaces
12
+ import uuid
13
+ import random
14
  from src.tryon_pipeline import StableDiffusionXLInpaintPipeline as TryonPipeline
15
  from src.unet_hacked_garmnet import UNet2DConditionModel as UNet2DConditionModel_ref
16
  from src.unet_hacked_tryon import UNet2DConditionModel
 
125
  output_mask = Image.fromarray(mask)
126
  return output_mask
127
 
128
+ def get_image_from_url(url):
129
+ try:
130
+ response = requests.get(url)
131
+ response.raise_for_status() # Vérifie les erreurs HTTP
132
+ img = Image.open(BytesIO(response.content))
133
+ return img
134
+ except Exception as e:
135
+ logging.error(f"Error fetching image from URL: {e}")
136
+ raise
137
 
138
  def decode_image_from_base64(base64_str):
139
  try:
 
154
  logging.error(f"Error encoding image: {e}")
155
  raise
156
 
157
+ def save_image(img):
158
+ unique_name = str(uuid.uuid4()) + ".webp"
159
+ img.save(unique_name, format="WEBP", lossless=True)
160
+ return unique_name
161
+
162
  @spaces.GPU
163
  def start_tryon(dict, garm_img, garment_des, is_checked, is_checked_crop, denoise_steps, seed, categorie = 'upper_body'):
164
  device = "cuda"
 
271
  torch.cuda.empty_cache()
272
  torch.cuda.synchronize()
273
 
274
+ def process_image(image_data):
275
+ # Vérifie si l'image est en base64 ou URL
276
+ if image_data.startswith('http://') or image_data.startswith('https://'):
277
+ return get_image_from_url(image_data) # Télécharge l'image depuis l'URL
278
+ else:
279
+ return decode_image_from_base64(image_data) # Décode l'image base64
280
+
281
  @app.route('/tryon', methods=['POST'])
282
  def tryon():
283
  data = request.json
284
+ human_image = process_image(data['human_image'])
285
+ garment_image = process_image(data['garment_image'])
286
  description = data.get('description')
287
  use_auto_mask = data.get('use_auto_mask', True)
288
  use_auto_crop = data.get('use_auto_crop', False)
 
306
  'mask_image': mask_base64
307
  })
308
 
309
+ @app.route('/tryon-v2', methods=['POST'])
310
+ def tryon():
311
+ data = request.json
312
+ human_image = process_image(data['human_image'])
313
+ garment_image = process_image(data['garment_image'])
314
+ mask_image = process_image(data['mask_image'])
315
+ description = data.get('description')
316
+ use_auto_mask = data.get('use_auto_mask', True)
317
+ use_auto_crop = data.get('use_auto_crop', False)
318
+ denoise_steps = int(data.get('denoise_steps', 30))
319
+ seed = int(data.get('seed', random.randint(0, 9999999)))
320
+ categorie = data.get('categorie' , 'upper_body')
321
+ human_dict = {
322
+ 'background': human_image,
323
+ 'layers': [mask_image] if not use_auto_mask else None,
324
+ 'composite': None
325
+ }
326
+ output_image, mask_image = start_tryon(human_dict, garment_image, description, use_auto_mask, use_auto_crop, denoise_steps, seed , categorie)
327
+
328
+ output_id =
329
+ mask_base64 = encode_image_to_base64(mask_image)
330
+
331
+ return jsonify({
332
+ 'image_id': save_image(output_image)
333
+ })
334
+
335
  @spaces.GPU
336
  def generate_mask(human_img, categorie='upper_body'):
337
  device = "cuda"
 
365
  categorie = data.get('categorie', 'upper_body')
366
 
367
  # Décodage de l'image à partir de base64
368
+ human_img = process_image(base64_image)
369
 
370
  # Appeler la fonction pour générer le masque
371
  mask_resized = generate_mask(human_img, categorie)
 
380
  logging.error(f"Error generating mask: {e}")
381
  return jsonify({'error': str(e)}), 500
382
 
383
+ # Route pour récupérer l'image générée
384
+ @app.route('/api/get_image/<image_id>', methods=['GET'])
385
+ def get_image(image_id):
386
+ # Construire le chemin complet de l'image
387
+ image_path = image_id # Assurez-vous que le nom de fichier correspond à celui que vous avez utilisé lors de la sauvegarde
388
+
389
+ # Renvoyer l'image
390
+ try:
391
+ return send_file(image_path, mimetype='image/webp')
392
+ except FileNotFoundError:
393
+ return jsonify({'error': 'Image not found'}), 404
394
 
395
  if __name__ == "__main__":
396
  app.run(debug=True, host="0.0.0.0", port=7860)