Upload app.py with huggingface_hub
Browse files
app.py
CHANGED
|
@@ -13,7 +13,6 @@ IDM_SPACE = "https://yisol-idm-vton.hf.space"
|
|
| 13 |
SVD_SPACE = "https://multimodalart-stable-video-diffusion.hf.space"
|
| 14 |
DASHSCOPE_API_KEY = os.environ.get("DASHSCOPE_API_KEY", "")
|
| 15 |
MODAL_TID = os.environ.get("MODAL_TOKEN_ID", "")
|
| 16 |
-
MODAL_TSEC = os.environ.get("MODAL_TOKEN_SECRET", "")
|
| 17 |
|
| 18 |
# ββ History βββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 19 |
def save_h(**kw):
|
|
@@ -24,32 +23,6 @@ def save_h(**kw):
|
|
| 24 |
def load_h():
|
| 25 |
return json.loads(HIST.read_text()) if HIST.exists() else []
|
| 26 |
|
| 27 |
-
# ββ CatVTON on Modal A10G βββββββββββββββββββββββββββββββββββββββββββ
|
| 28 |
-
def _catvton_tryon(person_path, garment_path, cloth_type="overall"):
|
| 29 |
-
"""CatVTON on Modal A10G. Free, unlimited, professional quality."""
|
| 30 |
-
import modal
|
| 31 |
-
modal.config.override({"token_id": MODAL_TID, "token_secret": MODAL_TSEC})
|
| 32 |
-
f = modal.Function.from_name("catvton", "run")
|
| 33 |
-
|
| 34 |
-
# Upload images to temporary public URLs
|
| 35 |
-
space_url = "https://adeel020-ivm.hf.space"
|
| 36 |
-
pub_dir = Path("/tmp/public"); pub_dir.mkdir(exist_ok=True)
|
| 37 |
-
pid = uuid.uuid4().hex[:8]
|
| 38 |
-
p_pub = pub_dir / f"person_{pid}.jpg"
|
| 39 |
-
g_pub = pub_dir / f"garment_{pid}.jpg"
|
| 40 |
-
|
| 41 |
-
Image.open(person_path).convert("RGB").save(p_pub, "JPEG", quality=95)
|
| 42 |
-
Image.open(garment_path).convert("RGB").save(g_pub, "JPEG", quality=95)
|
| 43 |
-
|
| 44 |
-
p_url = f"{space_url}/file={p_pub}"
|
| 45 |
-
g_url = f"{space_url}/file={g_pub}"
|
| 46 |
-
|
| 47 |
-
result = f.remote(p_url, g_url, cloth_type, 30, 2.5, 42, 768, 1024)
|
| 48 |
-
out = str(OUT / f"ivm_{uuid.uuid4().hex[:8]}.png")
|
| 49 |
-
with open(out, "wb") as fout:
|
| 50 |
-
fout.write(result)
|
| 51 |
-
return out
|
| 52 |
-
|
| 53 |
# ββ IDM-VTON via HF Space API ββββββββββββββββββββββββββββββββββββββ
|
| 54 |
def _idm_tryon(person_path, garment_path, desc, steps=30, seed=42):
|
| 55 |
def _upload(fp):
|
|
@@ -112,18 +85,7 @@ def generate(dress, model, category, photo_type):
|
|
| 112 |
|
| 113 |
img = None; method = "PIL"
|
| 114 |
|
| 115 |
-
# Try 1:
|
| 116 |
-
if model and MODAL_TID:
|
| 117 |
-
try:
|
| 118 |
-
t0 = time.time()
|
| 119 |
-
raw = _catvton_tryon(model, dress, category)
|
| 120 |
-
img = Image.open(raw).convert("RGB")
|
| 121 |
-
method = "CatVTON (Modal A10G)"
|
| 122 |
-
print(f"[IVM] CatVTON: {time.time()-t0:.1f}s {img.size}")
|
| 123 |
-
except Exception as e:
|
| 124 |
-
print(f"[IVM] CatVTON failed: {e}")
|
| 125 |
-
|
| 126 |
-
# Try 2: IDM-VTON (via HF Space)
|
| 127 |
if img is None and model:
|
| 128 |
try:
|
| 129 |
t0 = time.time()
|
|
@@ -245,7 +207,7 @@ label{color:#aaa!important;font-size:13px!important;font-weight:500!important}
|
|
| 245 |
.upload-btn{background:#22222e!important;border:1px solid rgba(255,255,255,0.08)!important;color:#aaa!important}
|
| 246 |
"""
|
| 247 |
|
| 248 |
-
with gr.Blocks(title="IVM Studio"
|
| 249 |
gr.HTML("""
|
| 250 |
<div class='app-header'>
|
| 251 |
<h1>IVM Studio</h1>
|
|
@@ -256,12 +218,12 @@ with gr.Blocks(title="IVM Studio", css=CSS, theme=gr.themes.Soft()) as demo:
|
|
| 256 |
with gr.Row(equal_height=False):
|
| 257 |
# Left column β Inputs
|
| 258 |
with gr.Column(scale=1, min_width=340):
|
| 259 |
-
with gr.
|
| 260 |
gr.Markdown("### Upload Photos")
|
| 261 |
dress = gr.Image(label="Dress / Garment", type="filepath", sources=["upload"], height=220)
|
| 262 |
model = gr.Image(label="Model Photo", type="filepath", sources=["upload"], height=220)
|
| 263 |
|
| 264 |
-
with gr.
|
| 265 |
gr.Markdown("### Options")
|
| 266 |
with gr.Row():
|
| 267 |
category = gr.Dropdown(choices=["tops", "bottoms", "one-pieces"], value="one-pieces", label="Garment Type")
|
|
@@ -280,7 +242,7 @@ with gr.Blocks(title="IVM Studio", css=CSS, theme=gr.themes.Soft()) as demo:
|
|
| 280 |
result_vid = gr.Video(label="Animation", height=250)
|
| 281 |
|
| 282 |
# History
|
| 283 |
-
with gr.
|
| 284 |
gr.Markdown("### Previously Generated")
|
| 285 |
hist = gr.HTML(value=history_html())
|
| 286 |
gr.Button("Refresh History", size="sm", elem_classes="gr-button-sm").click(fn=history_html, outputs=hist)
|
|
@@ -291,4 +253,4 @@ with gr.Blocks(title="IVM Studio", css=CSS, theme=gr.themes.Soft()) as demo:
|
|
| 291 |
vid_btn.click(fn=generate_video, inputs=[result_img], outputs=[result_vid], api_name="vid")
|
| 292 |
|
| 293 |
if __name__ == "__main__":
|
| 294 |
-
demo.launch(server_name="0.0.0.0", server_port=7860, share=False, show_error=True)
|
|
|
|
| 13 |
SVD_SPACE = "https://multimodalart-stable-video-diffusion.hf.space"
|
| 14 |
DASHSCOPE_API_KEY = os.environ.get("DASHSCOPE_API_KEY", "")
|
| 15 |
MODAL_TID = os.environ.get("MODAL_TOKEN_ID", "")
|
|
|
|
| 16 |
|
| 17 |
# ββ History βββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 18 |
def save_h(**kw):
|
|
|
|
| 23 |
def load_h():
|
| 24 |
return json.loads(HIST.read_text()) if HIST.exists() else []
|
| 25 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
# ββ IDM-VTON via HF Space API ββββββββββββββββββββββββββββββββββββββ
|
| 27 |
def _idm_tryon(person_path, garment_path, desc, steps=30, seed=42):
|
| 28 |
def _upload(fp):
|
|
|
|
| 85 |
|
| 86 |
img = None; method = "PIL"
|
| 87 |
|
| 88 |
+
# Try 1: IDM-VTON (best quality open-source, via HF Space)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
if img is None and model:
|
| 90 |
try:
|
| 91 |
t0 = time.time()
|
|
|
|
| 207 |
.upload-btn{background:#22222e!important;border:1px solid rgba(255,255,255,0.08)!important;color:#aaa!important}
|
| 208 |
"""
|
| 209 |
|
| 210 |
+
with gr.Blocks(title="IVM Studio") as demo:
|
| 211 |
gr.HTML("""
|
| 212 |
<div class='app-header'>
|
| 213 |
<h1>IVM Studio</h1>
|
|
|
|
| 218 |
with gr.Row(equal_height=False):
|
| 219 |
# Left column β Inputs
|
| 220 |
with gr.Column(scale=1, min_width=340):
|
| 221 |
+
with gr.Group():
|
| 222 |
gr.Markdown("### Upload Photos")
|
| 223 |
dress = gr.Image(label="Dress / Garment", type="filepath", sources=["upload"], height=220)
|
| 224 |
model = gr.Image(label="Model Photo", type="filepath", sources=["upload"], height=220)
|
| 225 |
|
| 226 |
+
with gr.Group():
|
| 227 |
gr.Markdown("### Options")
|
| 228 |
with gr.Row():
|
| 229 |
category = gr.Dropdown(choices=["tops", "bottoms", "one-pieces"], value="one-pieces", label="Garment Type")
|
|
|
|
| 242 |
result_vid = gr.Video(label="Animation", height=250)
|
| 243 |
|
| 244 |
# History
|
| 245 |
+
with gr.Group():
|
| 246 |
gr.Markdown("### Previously Generated")
|
| 247 |
hist = gr.HTML(value=history_html())
|
| 248 |
gr.Button("Refresh History", size="sm", elem_classes="gr-button-sm").click(fn=history_html, outputs=hist)
|
|
|
|
| 253 |
vid_btn.click(fn=generate_video, inputs=[result_img], outputs=[result_vid], api_name="vid")
|
| 254 |
|
| 255 |
if __name__ == "__main__":
|
| 256 |
+
demo.launch(server_name="0.0.0.0", server_port=7860, share=False, show_error=True, css=CSS)
|