nami0342 commited on
Commit
104bf05
·
1 Parent(s): a07c712

Add quick tryon function for API

Browse files
Files changed (3) hide show
  1. Dockerfile +1 -1
  2. app.py +12 -6
  3. requirements.txt +3 -1
Dockerfile CHANGED
@@ -27,4 +27,4 @@ ENV CUDA_VISIBLE_DEVICES=0
27
  EXPOSE 7860
28
 
29
  # Command to run the application
30
- CMD ["python", "app.py"]
 
27
  EXPOSE 7860
28
 
29
  # Command to run the application
30
+ CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
app.py CHANGED
@@ -1,10 +1,15 @@
1
  import spaces
 
 
2
  from fastapi import FastAPI, File, UploadFile
3
  import pickle
4
  import uvicorn
5
  import pandas as pd
6
  import io
 
 
7
  # import gradio as gr
 
8
  from PIL import Image
9
  from src.tryon_pipeline import StableDiffusionXLInpaintPipeline as TryonPipeline
10
  from src.unet_hacked_garmnet import UNet2DConditionModel as UNet2DConditionModel_ref
@@ -30,6 +35,7 @@ from preprocess.openpose.run_openpose import OpenPose
30
  from detectron2.data.detection_utils import convert_PIL_to_numpy,_apply_exif_orientation
31
  from torchvision.transforms.functional import to_pil_image
32
 
 
33
  # device = 'cuda' if torch.cuda.is_available() else 'cpu'
34
 
35
  def pil_to_binary_mask(pil_image, threshold=0):
@@ -46,7 +52,7 @@ def pil_to_binary_mask(pil_image, threshold=0):
46
  return output_mask
47
 
48
 
49
- base_path = 'yisol/IDM-VTON'
50
  example_path = os.path.join(os.path.dirname(__file__), 'example')
51
 
52
  unet = UNet2DConditionModel.from_pretrained(
@@ -128,7 +134,7 @@ pipe = TryonPipeline.from_pretrained(
128
  pipe.unet_encoder = UNet_Encoder
129
 
130
  @spaces.GPU
131
- def start_tryon(dict,garm_img,garment_des,is_checked,is_checked_crop,denoise_steps,seed):
132
  device = "cuda"
133
 
134
  openpose_model.preprocessor.body_estimation.model.to(device)
@@ -136,7 +142,7 @@ def start_tryon(dict,garm_img,garment_des,is_checked,is_checked_crop,denoise_ste
136
  pipe.unet_encoder.to(device)
137
 
138
  garm_img= garm_img.convert("RGB").resize((768,1024))
139
- human_img_orig = dict["background"].convert("RGB")
140
 
141
  if is_checked_crop:
142
  width, height = human_img_orig.size
@@ -153,7 +159,7 @@ def start_tryon(dict,garm_img,garment_des,is_checked,is_checked_crop,denoise_ste
153
  human_img = human_img_orig.resize((768,1024))
154
 
155
 
156
- if is_checked:
157
  keypoints = openpose_model(human_img.resize((384,512)))
158
  model_parse, _ = parsing_model(human_img.resize((384,512)))
159
  mask, mask_gray = get_mask_location('hd', "upper_body", model_parse, keypoints)
@@ -288,7 +294,7 @@ async def vton_run(
288
  upload_human: UploadFile = File(...),
289
  upload_cloth: UploadFile = File(...),
290
  input_prompt: str = None,
291
- is_checked: bool = True,
292
  is_checked_crop: bool = True,
293
  denoise_steps: int = 30,
294
  seed: int = 42
@@ -296,7 +302,7 @@ async def vton_run(
296
  target_human = Image.open(io.BytesIO(await upload_human.read()))
297
  target_cloth = Image.open(io.BytesIO(await upload_cloth.read()))
298
 
299
- results = start_tryon(target_human, target_cloth, input_prompt, is_checked, is_checked_crop, denoise_steps, seed)
300
  return results[0]
301
 
302
 
 
1
  import spaces
2
+
3
+ # For fast API
4
  from fastapi import FastAPI, File, UploadFile
5
  import pickle
6
  import uvicorn
7
  import pandas as pd
8
  import io
9
+
10
+ # Remove gradio
11
  # import gradio as gr
12
+
13
  from PIL import Image
14
  from src.tryon_pipeline import StableDiffusionXLInpaintPipeline as TryonPipeline
15
  from src.unet_hacked_garmnet import UNet2DConditionModel as UNet2DConditionModel_ref
 
35
  from detectron2.data.detection_utils import convert_PIL_to_numpy,_apply_exif_orientation
36
  from torchvision.transforms.functional import to_pil_image
37
 
38
+ # For use ZeroGPU
39
  # device = 'cuda' if torch.cuda.is_available() else 'cpu'
40
 
41
  def pil_to_binary_mask(pil_image, threshold=0):
 
52
  return output_mask
53
 
54
 
55
+ base_path = 'nami0342/GenAI_VTON_API'
56
  example_path = os.path.join(os.path.dirname(__file__), 'example')
57
 
58
  unet = UNet2DConditionModel.from_pretrained(
 
134
  pipe.unet_encoder = UNet_Encoder
135
 
136
  @spaces.GPU
137
+ def start_tryon(dict,garm_img,garment_des,is_automaskchecked,is_checked_crop,denoise_steps,seed):
138
  device = "cuda"
139
 
140
  openpose_model.preprocessor.body_estimation.model.to(device)
 
142
  pipe.unet_encoder.to(device)
143
 
144
  garm_img= garm_img.convert("RGB").resize((768,1024))
145
+ human_img_orig = dict["background"].convert("RGB")
146
 
147
  if is_checked_crop:
148
  width, height = human_img_orig.size
 
159
  human_img = human_img_orig.resize((768,1024))
160
 
161
 
162
+ if is_automaskchecked:
163
  keypoints = openpose_model(human_img.resize((384,512)))
164
  model_parse, _ = parsing_model(human_img.resize((384,512)))
165
  mask, mask_gray = get_mask_location('hd', "upper_body", model_parse, keypoints)
 
294
  upload_human: UploadFile = File(...),
295
  upload_cloth: UploadFile = File(...),
296
  input_prompt: str = None,
297
+ is_automaskchecked: bool = True,
298
  is_checked_crop: bool = True,
299
  denoise_steps: int = 30,
300
  seed: int = 42
 
302
  target_human = Image.open(io.BytesIO(await upload_human.read()))
303
  target_cloth = Image.open(io.BytesIO(await upload_cloth.read()))
304
 
305
+ results = start_tryon(target_human, target_cloth, input_prompt, is_automaskchecked, is_checked_crop, denoise_steps, seed)
306
  return results[0]
307
 
308
 
requirements.txt CHANGED
@@ -20,4 +20,6 @@ av==10.0.0
20
  fvcore==0.1.5.post20221221
21
  cloudpickle==2.2.1
22
  omegaconf==2.3.0
23
- pycocotools==2.0.7
 
 
 
20
  fvcore==0.1.5.post20221221
21
  cloudpickle==2.2.1
22
  omegaconf==2.3.0
23
+ pycocotools==2.0.7
24
+ fastapi==0.99.1
25
+ uvicorn