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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import spaces
|
| 2 |
+
|
| 3 |
+
import gradio as gr
|
| 4 |
+
from tryon_inference import run_inference
|
| 5 |
+
import os
|
| 6 |
+
import numpy as np
|
| 7 |
+
from PIL import Image
|
| 8 |
+
import tempfile
|
| 9 |
+
import torch
|
| 10 |
+
from diffusers import FluxTransformer2DModel, FluxFillPipeline
|
| 11 |
+
import subprocess
|
| 12 |
+
|
| 13 |
+
subprocess.run("rm -rf /data-nvme/zerogpu-offload/*", env={}, shell=True)
|
| 14 |
+
dtype = torch.bfloat16
|
| 15 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 16 |
+
|
| 17 |
+
print('Loading diffusion model ...')
|
| 18 |
+
transformer = FluxTransformer2DModel.from_pretrained(
|
| 19 |
+
"xiaozaa/catvton-flux-alpha",
|
| 20 |
+
torch_dtype=dtype
|
| 21 |
+
)
|
| 22 |
+
pipe = FluxFillPipeline.from_pretrained(
|
| 23 |
+
"black-forest-labs/FLUX.1-dev",
|
| 24 |
+
transformer=transformer,
|
| 25 |
+
torch_dtype=dtype
|
| 26 |
+
).to(device)
|
| 27 |
+
print('Loading Finished!')
|
| 28 |
+
|
| 29 |
+
@spaces.GPU(duration=120)
|
| 30 |
+
def gradio_inference(
|
| 31 |
+
image_data,
|
| 32 |
+
garment,
|
| 33 |
+
num_steps=50,
|
| 34 |
+
guidance_scale=30.0,
|
| 35 |
+
seed=-1,
|
| 36 |
+
width=768,
|
| 37 |
+
height=1024
|
| 38 |
+
):
|
| 39 |
+
"""Wrapper function for Gradio interface"""
|
| 40 |
+
# Check if mask has been drawn
|
| 41 |
+
if image_data is None or "layers" not in image_data or not image_data["layers"]:
|
| 42 |
+
raise gr.Error("Please draw a mask over the clothing area before generating!")
|
| 43 |
+
|
| 44 |
+
# Check if mask is empty (all black)
|
| 45 |
+
mask = image_data["layers"][0]
|
| 46 |
+
mask_array = np.array(mask)
|
| 47 |
+
if np.all(mask_array < 10):
|
| 48 |
+
raise gr.Error("The mask is empty! Please draw over the clothing area you want to replace.")
|
| 49 |
+
|
| 50 |
+
# Use temporary directory
|
| 51 |
+
with tempfile.TemporaryDirectory() as tmp_dir:
|
| 52 |
+
# Save inputs to temp directory
|
| 53 |
+
temp_image = os.path.join(tmp_dir, "image.png")
|
| 54 |
+
temp_mask = os.path.join(tmp_dir, "mask.png")
|
| 55 |
+
temp_garment = os.path.join(tmp_dir, "garment.png")
|
| 56 |
+
|
| 57 |
+
# Extract image and mask from ImageEditor data
|
| 58 |
+
image = image_data["background"]
|
| 59 |
+
mask = image_data["layers"][0] # First layer contains the mask
|
| 60 |
+
|
| 61 |
+
# Convert to numpy array and process mask
|
| 62 |
+
mask_array = np.array(mask)
|
| 63 |
+
is_black = np.all(mask_array < 10, axis=2)
|
| 64 |
+
mask = Image.fromarray(((~is_black) * 255).astype(np.uint8))
|
| 65 |
+
|
| 66 |
+
# Save files to temp directory
|
| 67 |
+
image.save(temp_image)
|
| 68 |
+
mask.save(temp_mask)
|
| 69 |
+
garment.save(temp_garment)
|
| 70 |
+
|
| 71 |
+
try:
|
| 72 |
+
# Run inference
|
| 73 |
+
_, tryon_result = run_inference(
|
| 74 |
+
pipe=pipe,
|
| 75 |
+
image_path=temp_image,
|
| 76 |
+
mask_path=temp_mask,
|
| 77 |
+
garment_path=temp_garment,
|
| 78 |
+
num_steps=num_steps,
|
| 79 |
+
guidance_scale=guidance_scale,
|
| 80 |
+
seed=seed,
|
| 81 |
+
size=(width, height)
|
| 82 |
+
)
|
| 83 |
+
return tryon_result
|
| 84 |
+
except Exception as e:
|
| 85 |
+
raise gr.Error(f"Error during inference: {str(e)}")
|
| 86 |
+
|
| 87 |
+
with gr.Blocks() as demo:
|
| 88 |
+
gr.Markdown("""
|
| 89 |
+
# CATVTON FLUX Virtual Try-On Demo
|
| 90 |
+
Upload a model image, draw a mask, and a garment image to generate virtual try-on results.
|
| 91 |
+
|
| 92 |
+
[](https://huggingface.co/xiaozaa/catvton-flux-alpha)
|
| 93 |
+
[](https://github.com/nftblackmagic/catvton-flux)
|
| 94 |
+
""")
|
| 95 |
+
|
| 96 |
+
# gr.Video("example/github.mp4", label="Demo Video: How to use the tool")
|
| 97 |
+
|
| 98 |
+
with gr.Column():
|
| 99 |
+
gr.Markdown("""
|
| 100 |
+
### ⚠️ Important:
|
| 101 |
+
1. Choose a model image or upload your own
|
| 102 |
+
2. Use the Pen tool to draw a mask over the clothing area you want to replace
|
| 103 |
+
3. Choose a garment image or upload your own
|
| 104 |
+
""")
|
| 105 |
+
|
| 106 |
+
with gr.Row():
|
| 107 |
+
with gr.Column():
|
| 108 |
+
image_input = gr.ImageMask(
|
| 109 |
+
label="Model Image (Click 'Edit' and draw mask over the clothing area)",
|
| 110 |
+
type="pil",
|
| 111 |
+
height=600,
|
| 112 |
+
width=300
|
| 113 |
+
)
|
| 114 |
+
gr.Examples(
|
| 115 |
+
examples=[
|
| 116 |
+
["./example/person/00008_00.jpg"],
|
| 117 |
+
["./example/person/00055_00.jpg"],
|
| 118 |
+
["./example/person/00057_00.jpg"],
|
| 119 |
+
["./example/person/00067_00.jpg"],
|
| 120 |
+
["./example/person/00069_00.jpg"],
|
| 121 |
+
],
|
| 122 |
+
inputs=[image_input],
|
| 123 |
+
label="Person Images",
|
| 124 |
+
)
|
| 125 |
+
with gr.Column():
|
| 126 |
+
garment_input = gr.Image(label="Garment Image", type="pil", height=600, width=300)
|
| 127 |
+
gr.Examples(
|
| 128 |
+
examples=[
|
| 129 |
+
["./example/garment/04564_00.jpg"],
|
| 130 |
+
["./example/garment/00055_00.jpg"],
|
| 131 |
+
["./example/garment/00396_00.jpg"],
|
| 132 |
+
["./example/garment/00067_00.jpg"],
|
| 133 |
+
["./example/garment/00069_00.jpg"],
|
| 134 |
+
],
|
| 135 |
+
inputs=[garment_input],
|
| 136 |
+
label="Garment Images",
|
| 137 |
+
)
|
| 138 |
+
with gr.Column():
|
| 139 |
+
tryon_output = gr.Image(label="Try-On Result", height=600, width=300)
|
| 140 |
+
|
| 141 |
+
with gr.Row():
|
| 142 |
+
num_steps = gr.Slider(
|
| 143 |
+
minimum=1,
|
| 144 |
+
maximum=100,
|
| 145 |
+
value=30,
|
| 146 |
+
step=1,
|
| 147 |
+
label="Number of Steps"
|
| 148 |
+
)
|
| 149 |
+
guidance_scale = gr.Slider(
|
| 150 |
+
minimum=1.0,
|
| 151 |
+
maximum=50.0,
|
| 152 |
+
value=30.0,
|
| 153 |
+
step=0.5,
|
| 154 |
+
label="Guidance Scale"
|
| 155 |
+
)
|
| 156 |
+
seed = gr.Slider(
|
| 157 |
+
minimum=-1,
|
| 158 |
+
maximum=2147483647,
|
| 159 |
+
step=1,
|
| 160 |
+
value=-1,
|
| 161 |
+
label="Seed (-1 for random)"
|
| 162 |
+
)
|
| 163 |
+
width = gr.Slider(
|
| 164 |
+
minimum=256,
|
| 165 |
+
maximum=1024,
|
| 166 |
+
step=64,
|
| 167 |
+
value=768,
|
| 168 |
+
label="Width"
|
| 169 |
+
)
|
| 170 |
+
height = gr.Slider(
|
| 171 |
+
minimum=256,
|
| 172 |
+
maximum=1024,
|
| 173 |
+
step=64,
|
| 174 |
+
value=1024,
|
| 175 |
+
label="Height"
|
| 176 |
+
)
|
| 177 |
+
|
| 178 |
+
|
| 179 |
+
submit_btn = gr.Button("Generate Try-On", variant="primary")
|
| 180 |
+
|
| 181 |
+
|
| 182 |
+
with gr.Row():
|
| 183 |
+
gr.Markdown("""
|
| 184 |
+
### Notes:
|
| 185 |
+
- The model is trained on VITON-HD dataset. It focuses on the woman upper body try-on generation.
|
| 186 |
+
- The mask should indicate the region where the garment will be placed.
|
| 187 |
+
- The garment image should be on a clean background.
|
| 188 |
+
- The model is not perfect. It may generate some artifacts.
|
| 189 |
+
- The model is slow. Please be patient.
|
| 190 |
+
- The model is just for research purpose.
|
| 191 |
+
""")
|
| 192 |
+
|
| 193 |
+
submit_btn.click(
|
| 194 |
+
fn=gradio_inference,
|
| 195 |
+
inputs=[
|
| 196 |
+
image_input,
|
| 197 |
+
garment_input,
|
| 198 |
+
num_steps,
|
| 199 |
+
guidance_scale,
|
| 200 |
+
seed,
|
| 201 |
+
width,
|
| 202 |
+
height
|
| 203 |
+
],
|
| 204 |
+
outputs=[tryon_output],
|
| 205 |
+
api_name="try-on"
|
| 206 |
+
)
|
| 207 |
+
|
| 208 |
+
|
| 209 |
+
demo.launch()
|