fanboyd13 commited on
Commit
9f4de68
Β·
verified Β·
1 Parent(s): 7edb31d

Upload 7 files

Browse files
Files changed (1) hide show
  1. app.py +15 -26
app.py CHANGED
@@ -14,18 +14,18 @@ pipe = None
14
  def load_model():
15
  global pipe, MODEL_LOADED, LOAD_ERROR
16
  try:
17
- print("πŸ“₯ Loading virtual try-on model...")
18
  from diffusers import StableDiffusionInpaintPipeline
19
  pipe = StableDiffusionInpaintPipeline.from_pretrained(
20
  "runwayml/stable-diffusion-inpainting",
21
- torch_dtype=torch.float32,
22
  safety_checker=None,
23
  requires_safety_checker=False,
24
  )
 
25
  pipe.enable_attention_slicing()
26
- pipe.enable_sequential_cpu_offload() if not torch.cuda.is_available() else pipe.to("cuda")
27
  MODEL_LOADED = True
28
- print("βœ… Model ready!")
29
  except Exception as e:
30
  LOAD_ERROR = str(e)
31
  print(f"❌ {e}")
@@ -37,14 +37,13 @@ def pil_to_b64(img):
37
  img.save(buf, format="PNG")
38
  return base64.b64encode(buf.getvalue()).decode()
39
 
40
- def make_upper_body_mask(size):
41
  w, h = size
42
  mask = Image.new("L", size, 0)
43
  draw = ImageDraw.Draw(mask)
44
- draw.ellipse([w*0.3, h*0.08, w*0.7, h*0.22], fill=255) # neck/head area avoid
45
- draw.rectangle([w*0.05, h*0.20, w*0.95, h*0.65], fill=255) # torso
46
- draw.rectangle([w*0.0, h*0.20, w*0.15, h*0.60], fill=255) # left arm
47
- draw.rectangle([w*0.85, h*0.20, w*1.0, h*0.60], fill=255) # right arm
48
  return mask.convert("RGB")
49
 
50
  @app.get("/", response_class=HTMLResponse)
@@ -58,24 +57,14 @@ async def status():
58
  @app.post("/tryon")
59
  async def tryon(person: UploadFile = File(...), garment: UploadFile = File(...)):
60
  if not MODEL_LOADED:
61
- return JSONResponse({"status":"loading","message":"Model loading, retry in 30s"}, status_code=503)
62
  try:
63
  SIZE = (512, 768)
64
- person_img = Image.open(io.BytesIO(await person.read())).convert("RGB").resize(SIZE)
65
- garment_img = Image.open(io.BytesIO(await garment.read())).convert("RGB").resize((SIZE[0], SIZE[0]))
66
- mask_img = make_upper_body_mask(SIZE)
67
 
68
- # Build a prompt from garment image colors
69
- g = np.array(garment_img)
70
- dominant = g.mean(axis=(0,1)).astype(int)
71
- color_hint = f"rgb({dominant[0]},{dominant[1]},{dominant[2]})"
72
-
73
- prompt = (
74
- "A person wearing a clean fitted garment, "
75
- "photorealistic fashion photo, high quality, "
76
- "same pose, same background, sharp details"
77
- )
78
- negative = "deformed, blurry, bad anatomy, extra limbs, watermark, text"
79
 
80
  result = pipe(
81
  prompt=prompt,
@@ -84,9 +73,9 @@ async def tryon(person: UploadFile = File(...), garment: UploadFile = File(...))
84
  mask_image=mask_img,
85
  height=SIZE[1],
86
  width=SIZE[0],
87
- num_inference_steps=30,
88
  guidance_scale=7.5,
89
- strength=0.99,
90
  ).images[0]
91
 
92
  return JSONResponse({"status":"ok","image": pil_to_b64(result)})
 
14
  def load_model():
15
  global pipe, MODEL_LOADED, LOAD_ERROR
16
  try:
17
+ print("πŸ“₯ Loading model on CPU...")
18
  from diffusers import StableDiffusionInpaintPipeline
19
  pipe = StableDiffusionInpaintPipeline.from_pretrained(
20
  "runwayml/stable-diffusion-inpainting",
21
+ torch_dtype=torch.float32, # CPU needs float32
22
  safety_checker=None,
23
  requires_safety_checker=False,
24
  )
25
+ # CPU ONLY β€” no .to("cuda"), no cpu_offload
26
  pipe.enable_attention_slicing()
 
27
  MODEL_LOADED = True
28
+ print("βœ… Model ready on CPU!")
29
  except Exception as e:
30
  LOAD_ERROR = str(e)
31
  print(f"❌ {e}")
 
37
  img.save(buf, format="PNG")
38
  return base64.b64encode(buf.getvalue()).decode()
39
 
40
+ def make_mask(size):
41
  w, h = size
42
  mask = Image.new("L", size, 0)
43
  draw = ImageDraw.Draw(mask)
44
+ draw.rectangle([w*0.05, h*0.18, w*0.95, h*0.68], fill=255)
45
+ draw.rectangle([w*0.0, h*0.18, w*0.15, h*0.58], fill=255)
46
+ draw.rectangle([w*0.85, h*0.18, w*1.0, h*0.58], fill=255)
 
47
  return mask.convert("RGB")
48
 
49
  @app.get("/", response_class=HTMLResponse)
 
57
  @app.post("/tryon")
58
  async def tryon(person: UploadFile = File(...), garment: UploadFile = File(...)):
59
  if not MODEL_LOADED:
60
+ return JSONResponse({"status":"loading","message":"Model still loading, please wait and retry."}, status_code=503)
61
  try:
62
  SIZE = (512, 768)
63
+ person_img = Image.open(io.BytesIO(await person.read())).convert("RGB").resize(SIZE)
64
+ mask_img = make_mask(SIZE)
 
65
 
66
+ prompt = "Person wearing a clean stylish garment, photorealistic, high quality fashion photo, same pose, same background"
67
+ negative = "nude, deformed, blurry, bad anatomy, extra limbs, watermark, logo, text, disfigured"
 
 
 
 
 
 
 
 
 
68
 
69
  result = pipe(
70
  prompt=prompt,
 
73
  mask_image=mask_img,
74
  height=SIZE[1],
75
  width=SIZE[0],
76
+ num_inference_steps=25,
77
  guidance_scale=7.5,
78
+ strength=0.95,
79
  ).images[0]
80
 
81
  return JSONResponse({"status":"ok","image": pil_to_b64(result)})