Spaces:
Runtime error
Runtime error
Upload 3 files
Browse files
README.md
CHANGED
|
@@ -1,15 +1,29 @@
|
|
| 1 |
---
|
| 2 |
-
title:
|
| 3 |
-
emoji:
|
| 4 |
colorFrom: green
|
| 5 |
-
colorTo:
|
| 6 |
sdk: gradio
|
| 7 |
-
sdk_version: 6.
|
| 8 |
-
python_version: '3.12'
|
| 9 |
app_file: app.py
|
| 10 |
pinned: false
|
| 11 |
-
license: other
|
| 12 |
-
short_description: ComfyUI workflow running on Hugging Face ZeroGPU
|
| 13 |
---
|
| 14 |
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
---
|
| 2 |
+
title: CatVTON Outfit Try-On
|
| 3 |
+
emoji: π
|
| 4 |
colorFrom: green
|
| 5 |
+
colorTo: green
|
| 6 |
sdk: gradio
|
| 7 |
+
sdk_version: 6.0.0
|
|
|
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
|
|
|
|
|
|
| 10 |
---
|
| 11 |
|
| 12 |
+
# CatVTON Outfit Try-On β ZeroGPU
|
| 13 |
+
|
| 14 |
+
This Space wraps CatVTON in a Gradio UI and maps the uploaded workflow as:
|
| 15 |
+
|
| 16 |
+
1. Person image
|
| 17 |
+
2. Garment/reference image
|
| 18 |
+
3. Clothing mask
|
| 19 |
+
4. Mask inversion
|
| 20 |
+
5. CatVTON inference
|
| 21 |
+
6. Result image
|
| 22 |
+
|
| 23 |
+
The app downloads the upstream CatVTON source and model checkpoint at runtime.
|
| 24 |
+
|
| 25 |
+
## Important
|
| 26 |
+
|
| 27 |
+
The upstream CatVTON project is licensed CC BY-NC-SA 4.0 and states that its demo and weights are for non-commercial use. Review the upstream license before using the Space for business/commercial work.
|
| 28 |
+
|
| 29 |
+
The original ComfyUI workflow used `LoadCatVTONPipeline_AMD` and `CatVTON_AMD`. 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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import sys
|
| 3 |
+
import subprocess
|
| 4 |
+
from pathlib import Path
|
| 5 |
+
|
| 6 |
+
import gradio as gr
|
| 7 |
+
import numpy as np
|
| 8 |
+
import torch
|
| 9 |
+
import spaces
|
| 10 |
+
from PIL import Image, ImageOps
|
| 11 |
+
from diffusers.image_processor import VaeImageProcessor
|
| 12 |
+
from huggingface_hub import snapshot_download
|
| 13 |
+
|
| 14 |
+
# ---------------------------------------------------------------------
|
| 15 |
+
# CatVTON ZeroGPU wrapper
|
| 16 |
+
# Based on the user's ComfyUI workflow:
|
| 17 |
+
# LoadCatVTONPipeline_AMD -> LoadImage(person) -> LoadImage(garment)
|
| 18 |
+
# -> LoadImage(mask) -> ImageInvert -> CatVTON_AMD -> PreviewImage
|
| 19 |
+
# ---------------------------------------------------------------------
|
| 20 |
+
|
| 21 |
+
ROOT = Path("/tmp/catvton_src")
|
| 22 |
+
ROOT.mkdir(parents=True, exist_ok=True)
|
| 23 |
+
|
| 24 |
+
def ensure_catvton_source():
|
| 25 |
+
"""Download the upstream CatVTON source code once per Space runtime."""
|
| 26 |
+
marker = ROOT / "model" / "pipeline.py"
|
| 27 |
+
if marker.exists():
|
| 28 |
+
return
|
| 29 |
+
|
| 30 |
+
if (ROOT / ".git").exists():
|
| 31 |
+
subprocess.run(
|
| 32 |
+
["git", "-C", str(ROOT), "pull", "--ff-only"],
|
| 33 |
+
check=False,
|
| 34 |
+
stdout=subprocess.DEVNULL,
|
| 35 |
+
stderr=subprocess.DEVNULL,
|
| 36 |
+
)
|
| 37 |
+
return
|
| 38 |
+
|
| 39 |
+
subprocess.run(
|
| 40 |
+
[
|
| 41 |
+
"git", "clone", "--depth", "1", "--branch", "edited",
|
| 42 |
+
"https://github.com/Zheng-Chong/CatVTON.git",
|
| 43 |
+
str(ROOT),
|
| 44 |
+
],
|
| 45 |
+
check=True,
|
| 46 |
+
)
|
| 47 |
+
|
| 48 |
+
ensure_catvton_source()
|
| 49 |
+
sys.path.insert(0, str(ROOT))
|
| 50 |
+
|
| 51 |
+
from model.pipeline import CatVTONPipeline
|
| 52 |
+
from utils import init_weight_dtype, resize_and_crop, resize_and_padding
|
| 53 |
+
|
| 54 |
+
# Model IDs. The original CatVTON code uses the copied SD-inpainting
|
| 55 |
+
# checkpoint because the old runwayml repository was removed.
|
| 56 |
+
BASE_MODEL = os.getenv(
|
| 57 |
+
"CATVTON_BASE_MODEL",
|
| 58 |
+
"booksforcharlie/stable-diffusion-inpainting",
|
| 59 |
+
)
|
| 60 |
+
CATVTON_MODEL = os.getenv("CATVTON_MODEL", "zhengchong/CatVTON")
|
| 61 |
+
|
| 62 |
+
WIDTH = int(os.getenv("CATVTON_WIDTH", "768"))
|
| 63 |
+
HEIGHT = int(os.getenv("CATVTON_HEIGHT", "1024"))
|
| 64 |
+
|
| 65 |
+
# ZeroGPU expects GPU-dependent functions to use @spaces.GPU.
|
| 66 |
+
# Model construction follows the ZeroGPU recommendation to place the
|
| 67 |
+
# model on CUDA at module level.
|
| 68 |
+
print("Downloading CatVTON checkpoint...")
|
| 69 |
+
catvton_repo = snapshot_download(repo_id=CATVTON_MODEL)
|
| 70 |
+
|
| 71 |
+
print("Loading CatVTON pipeline...")
|
| 72 |
+
dtype = torch.float16
|
| 73 |
+
pipe = CatVTONPipeline(
|
| 74 |
+
base_ckpt=BASE_MODEL,
|
| 75 |
+
attn_ckpt=catvton_repo,
|
| 76 |
+
attn_ckpt_version="mix",
|
| 77 |
+
weight_dtype=dtype,
|
| 78 |
+
use_tf32=True,
|
| 79 |
+
device="cuda",
|
| 80 |
+
skip_safety_check=False,
|
| 81 |
+
)
|
| 82 |
+
|
| 83 |
+
mask_processor = VaeImageProcessor(
|
| 84 |
+
vae_scale_factor=8,
|
| 85 |
+
do_normalize=False,
|
| 86 |
+
do_binarize=True,
|
| 87 |
+
do_convert_grayscale=True,
|
| 88 |
+
)
|
| 89 |
+
|
| 90 |
+
|
| 91 |
+
def load_image(x):
|
| 92 |
+
if x is None:
|
| 93 |
+
return None
|
| 94 |
+
if isinstance(x, Image.Image):
|
| 95 |
+
return x.convert("RGB")
|
| 96 |
+
return Image.open(x).convert("RGB")
|
| 97 |
+
|
| 98 |
+
|
| 99 |
+
def prepare_mask(mask_image):
|
| 100 |
+
"""
|
| 101 |
+
Reproduce the user's ImageInvert node:
|
| 102 |
+
the uploaded mask is inverted before being sent to CatVTON.
|
| 103 |
+
"""
|
| 104 |
+
mask = load_image(mask_image).convert("L")
|
| 105 |
+
# ComfyUI ImageInvert is effectively 255 - pixel.
|
| 106 |
+
mask = ImageOps.invert(mask)
|
| 107 |
+
return mask
|
| 108 |
+
|
| 109 |
+
|
| 110 |
+
@spaces.GPU(duration=120)
|
| 111 |
+
def generate(person_file, garment_file, mask_file, steps, cfg, seed):
|
| 112 |
+
if person_file is None:
|
| 113 |
+
raise gr.Error("Person image upload karo.")
|
| 114 |
+
if garment_file is None:
|
| 115 |
+
raise gr.Error("Garment / clothing image upload karo.")
|
| 116 |
+
if mask_file is None:
|
| 117 |
+
raise gr.Error("Clothing mask upload karo.")
|
| 118 |
+
|
| 119 |
+
person = load_image(person_file)
|
| 120 |
+
garment = load_image(garment_file)
|
| 121 |
+
mask = prepare_mask(mask_file)
|
| 122 |
+
|
| 123 |
+
# Match the original CatVTON preprocessing used by the upstream app.
|
| 124 |
+
person = resize_and_crop(person, (WIDTH, HEIGHT))
|
| 125 |
+
garment = resize_and_padding(garment, (WIDTH, HEIGHT))
|
| 126 |
+
mask = resize_and_crop(mask, (WIDTH, HEIGHT))
|
| 127 |
+
mask = mask_processor.blur(mask, blur_factor=9)
|
| 128 |
+
|
| 129 |
+
seed = int(seed)
|
| 130 |
+
generator = None
|
| 131 |
+
if seed >= 0:
|
| 132 |
+
generator = torch.Generator(device="cuda").manual_seed(seed)
|
| 133 |
+
|
| 134 |
+
steps = int(steps)
|
| 135 |
+
cfg = float(cfg)
|
| 136 |
+
|
| 137 |
+
result = pipe(
|
| 138 |
+
image=person,
|
| 139 |
+
condition_image=garment,
|
| 140 |
+
mask=mask,
|
| 141 |
+
num_inference_steps=steps,
|
| 142 |
+
guidance_scale=cfg,
|
| 143 |
+
height=HEIGHT,
|
| 144 |
+
width=WIDTH,
|
| 145 |
+
generator=generator,
|
| 146 |
+
)[0]
|
| 147 |
+
|
| 148 |
+
return result
|
| 149 |
+
|
| 150 |
+
|
| 151 |
+
CSS = """
|
| 152 |
+
.gradio-container {max-width: 1200px !important;}
|
| 153 |
+
.title {text-align:center; margin-bottom: 8px;}
|
| 154 |
+
.note {text-align:center; opacity:.75;}
|
| 155 |
+
"""
|
| 156 |
+
|
| 157 |
+
with gr.Blocks(
|
| 158 |
+
title="CatVTON Outfit Try-On β ZeroGPU",
|
| 159 |
+
css=CSS,
|
| 160 |
+
theme=gr.themes.Soft(),
|
| 161 |
+
) as demo:
|
| 162 |
+
gr.Markdown(
|
| 163 |
+
"""
|
| 164 |
+
<div class="title">
|
| 165 |
+
<h1>π CatVTON Outfit Try-On</h1>
|
| 166 |
+
</div>
|
| 167 |
+
<div class="note">
|
| 168 |
+
ZeroGPU β’ Based on your ComfyUI CatVTON workflow
|
| 169 |
+
</div>
|
| 170 |
+
"""
|
| 171 |
+
)
|
| 172 |
+
|
| 173 |
+
with gr.Row():
|
| 174 |
+
with gr.Column():
|
| 175 |
+
person = gr.Image(
|
| 176 |
+
type="filepath",
|
| 177 |
+
label="π€ Person Image",
|
| 178 |
+
)
|
| 179 |
+
garment = gr.Image(
|
| 180 |
+
type="filepath",
|
| 181 |
+
label="π Garment / Clothing Image",
|
| 182 |
+
)
|
| 183 |
+
mask = gr.Image(
|
| 184 |
+
type="filepath",
|
| 185 |
+
label="π Clothing Mask",
|
| 186 |
+
)
|
| 187 |
+
|
| 188 |
+
with gr.Column():
|
| 189 |
+
result = gr.Image(
|
| 190 |
+
type="pil",
|
| 191 |
+
label="β¨ Generated Result",
|
| 192 |
+
)
|
| 193 |
+
|
| 194 |
+
with gr.Row():
|
| 195 |
+
steps = gr.Slider(
|
| 196 |
+
minimum=10,
|
| 197 |
+
maximum=50,
|
| 198 |
+
step=1,
|
| 199 |
+
value=30,
|
| 200 |
+
label="Inference Steps",
|
| 201 |
+
)
|
| 202 |
+
cfg = gr.Slider(
|
| 203 |
+
minimum=1.0,
|
| 204 |
+
maximum=5.0,
|
| 205 |
+
step=0.1,
|
| 206 |
+
value=2.5,
|
| 207 |
+
label="CFG",
|
| 208 |
+
)
|
| 209 |
+
seed = gr.Number(
|
| 210 |
+
value=-1,
|
| 211 |
+
precision=0,
|
| 212 |
+
label="Seed (-1 = random)",
|
| 213 |
+
)
|
| 214 |
+
|
| 215 |
+
generate_btn = gr.Button(
|
| 216 |
+
"π GENERATE OUTFIT",
|
| 217 |
+
variant="primary",
|
| 218 |
+
size="lg",
|
| 219 |
+
)
|
| 220 |
+
|
| 221 |
+
gr.Markdown(
|
| 222 |
+
"""
|
| 223 |
+
**Workflow mapping:** Person β target image, Garment β reference image,
|
| 224 |
+
Mask β inverted before CatVTON, then CatVTON β result.
|
| 225 |
+
|
| 226 |
+
β οΈ The CatVTON project/checkpoints are released under CC BY-NC-SA 4.0
|
| 227 |
+
and the upstream project states the demo/weights are for non-commercial use.
|
| 228 |
+
"""
|
| 229 |
+
)
|
| 230 |
+
|
| 231 |
+
generate_btn.click(
|
| 232 |
+
generate,
|
| 233 |
+
inputs=[person, garment, mask, steps, cfg, seed],
|
| 234 |
+
outputs=result,
|
| 235 |
+
)
|
| 236 |
+
|
| 237 |
+
if __name__ == "__main__":
|
| 238 |
+
demo.queue(max_size=8).launch()
|