Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,3 +1,83 @@
|
|
| 1 |
-
import
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import spaces
|
| 2 |
+
from diffusers import AutoPipelineForInpainting, AutoencoderKL
|
| 3 |
+
import gradio as gr
|
| 4 |
+
from diffusers.utils import load_image
|
| 5 |
+
import torch
|
| 6 |
+
from PIL import Image
|
| 7 |
+
from SegBody import segment_body
|
| 8 |
+
from SegCloth import segment_clothing
|
| 9 |
|
| 10 |
+
vae = AutoencoderKL.from_pretrained("madebyollin/sdxl-vae-fp16-fix", torch_dtype=torch.float16)
|
| 11 |
+
pipeline = AutoPipelineForInpainting.from_pretrained(os.environ.get('MODEL'), vae=vae, torch_dtype=torch.float16, variant="fp16", use_safetensors=True).to("cuda")
|
| 12 |
+
pipeline.load_ip_adapter(os.environ.get('IP_ADAPTER'), subfolder="sdxl_models", weight_name="ip-adapter_sdxl.bin")
|
| 13 |
+
|
| 14 |
+
@spaces.GPU(enable_queue=True)
|
| 15 |
+
def squarify_image(img):
|
| 16 |
+
if(img.height > img.width): bg_size = img.height
|
| 17 |
+
else: bg_size = img.width
|
| 18 |
+
bg = Image.new(mode="RGB", size=(bg_size,bg_size), color="white")
|
| 19 |
+
bg.paste(img, ( int((bg.width - bg.width)/2), 0) )
|
| 20 |
+
|
| 21 |
+
return bg
|
| 22 |
+
|
| 23 |
+
@spaces.GPU(enable_queue=True)
|
| 24 |
+
def divisible_by_8(image):
|
| 25 |
+
width, height = image.size
|
| 26 |
+
|
| 27 |
+
# Calculate the new width and height that are divisible by 8
|
| 28 |
+
new_width = (width // 8) * 8
|
| 29 |
+
new_height = (height // 8) * 8
|
| 30 |
+
|
| 31 |
+
# Resize the image
|
| 32 |
+
resized_image = image.resize((new_width, new_height))
|
| 33 |
+
|
| 34 |
+
return resized_image
|
| 35 |
+
|
| 36 |
+
@spaces.GPU(enable_queue=True)
|
| 37 |
+
def generate(person, clothing):
|
| 38 |
+
person.thumbnail((1024,1024))
|
| 39 |
+
person = divisible_by_8(person)
|
| 40 |
+
|
| 41 |
+
clothing.thumbnail((1024,1024))
|
| 42 |
+
clothing = divisible_by_8(clothing)
|
| 43 |
+
|
| 44 |
+
image = squarify_image(person)
|
| 45 |
+
|
| 46 |
+
seg_image, mask_image = segment_body(image, face=False)
|
| 47 |
+
|
| 48 |
+
seg_cloth = segment_clothing(clothing, clothes= ["Upper-clothes", "Skirt", "Pants", "Dress", "Belt"])
|
| 49 |
+
#seg_cloth = clothing
|
| 50 |
+
|
| 51 |
+
pipeline.to("cuda")
|
| 52 |
+
pipeline.set_ip_adapter_scale(1.0)
|
| 53 |
+
images = pipeline(
|
| 54 |
+
prompt="photorealistic, perfect body, beautiful skin, realistic skin, natural skin",
|
| 55 |
+
negative_prompt="ugly, bad quality, bad anatomy, deformed body, deformed hands, deformed feet, deformed face, deformed clothing, deformed skin, bad skin, leggings, tights, stockings",
|
| 56 |
+
image=image,
|
| 57 |
+
mask_image=mask_image,
|
| 58 |
+
ip_adapter_image=seg_cloth,
|
| 59 |
+
width=image.width,
|
| 60 |
+
height=image.height,
|
| 61 |
+
strength=0.99,
|
| 62 |
+
guidance_scale=7.5,
|
| 63 |
+
num_inference_steps=100,
|
| 64 |
+
).images
|
| 65 |
+
|
| 66 |
+
final = images[0].crop((0, 0, person.width, person.height))
|
| 67 |
+
|
| 68 |
+
return final
|
| 69 |
+
|
| 70 |
+
iface = gr.Interface(fn=generate,
|
| 71 |
+
inputs=[gr.Image(label='Person', type='pil'), gr.Image(label='Clothing', type='pil')],
|
| 72 |
+
outputs=[gr.Image(label='Result')],
|
| 73 |
+
title='Fashion Try-On',
|
| 74 |
+
description="""
|
| 75 |
+
by [Tony Assi](https://www.tonyassi.com/)
|
| 76 |
+
|
| 77 |
+
Check out [Virtual Try-On Pro](https://huggingface.co/spaces/tonyassi/Virtual-Try-On-Pro) !
|
| 78 |
+
|
| 79 |
+
Please ❤️ this Space. I build custom AI apps for companies. <a href="mailto: tony.assi.media@gmail.com">Email me</a> for business inquiries.
|
| 80 |
+
""",
|
| 81 |
+
theme = gr.themes.Base(primary_hue="teal",secondary_hue="teal",neutral_hue="slate"),
|
| 82 |
+
examples=[["images/person1.jpg", "images/clothing1.jpg"], ["images/person1.jpg", "images/clothing2.jpg"]],)
|
| 83 |
+
iface.launch()
|