Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -11,7 +11,6 @@ from huggingface_hub import login
|
|
| 11 |
SPACE = "yisol/IDM-VTON"
|
| 12 |
API_NAME = "/tryon"
|
| 13 |
|
| 14 |
-
# ✅ IMPORTANT: do NOT hardcode tokens in a Space repo
|
| 15 |
HF_TOKEN = os.getenv("HF_TOKEN", "")
|
| 16 |
|
| 17 |
print("HF_TOKEN set:", bool(HF_TOKEN), "len:", len(HF_TOKEN) if HF_TOKEN else 0)
|
|
@@ -34,10 +33,6 @@ def reset_client():
|
|
| 34 |
|
| 35 |
|
| 36 |
def get_client():
|
| 37 |
-
"""
|
| 38 |
-
Compatible with older gradio_client (no hf_token=...).
|
| 39 |
-
We rely on huggingface_hub login() above.
|
| 40 |
-
"""
|
| 41 |
global _client
|
| 42 |
if _client is None:
|
| 43 |
_client = Client(SPACE)
|
|
@@ -52,29 +47,11 @@ def clamp_int(x, lo, hi):
|
|
| 52 |
return max(lo, min(hi, x))
|
| 53 |
|
| 54 |
|
| 55 |
-
def
|
| 56 |
-
pil_img: Image.Image,
|
| 57 |
-
suffix: str = ".jpg",
|
| 58 |
-
max_side: int = 1600,
|
| 59 |
-
quality: int = 92
|
| 60 |
-
) -> str:
|
| 61 |
-
"""
|
| 62 |
-
Make uploads more stable (reduce write timeouts):
|
| 63 |
-
- convert to RGB
|
| 64 |
-
- downscale to max_side
|
| 65 |
-
- save optimized JPEG
|
| 66 |
-
"""
|
| 67 |
-
img = pil_img.convert("RGB")
|
| 68 |
-
w, h = img.size
|
| 69 |
-
m = max(w, h)
|
| 70 |
-
if m > max_side:
|
| 71 |
-
scale = max_side / m
|
| 72 |
-
img = img.resize((int(w * scale), int(h * scale)), Image.LANCZOS)
|
| 73 |
-
|
| 74 |
f = tempfile.NamedTemporaryFile(delete=False, suffix=suffix)
|
| 75 |
path = f.name
|
| 76 |
f.close()
|
| 77 |
-
|
| 78 |
return path
|
| 79 |
|
| 80 |
|
|
@@ -87,14 +64,12 @@ def tryon_remote(person_pil, garment_pil, garment_desc, auto_mask, crop_center,
|
|
| 87 |
denoise_steps = clamp_int(denoise_steps, 10, 40)
|
| 88 |
seed = clamp_int(seed, 0, 999999)
|
| 89 |
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
g_path = pil_save_temp(garment_pil, ".jpg", max_side=1600, quality=92)
|
| 93 |
|
| 94 |
try:
|
| 95 |
last_err = None
|
| 96 |
|
| 97 |
-
# retries to survive ZeroGPU overload/queue/timeouts
|
| 98 |
for attempt in range(1, 7):
|
| 99 |
try:
|
| 100 |
client = get_client()
|
|
@@ -102,7 +77,7 @@ def tryon_remote(person_pil, garment_pil, garment_desc, auto_mask, crop_center,
|
|
| 102 |
result = client.predict(
|
| 103 |
dict={"background": handle_file(p_path), "layers": [], "composite": None},
|
| 104 |
garm_img=handle_file(g_path),
|
| 105 |
-
garment_des=
|
| 106 |
is_checked=bool(auto_mask),
|
| 107 |
is_checked_crop=bool(crop_center),
|
| 108 |
denoise_steps=int(denoise_steps),
|
|
@@ -120,12 +95,24 @@ def tryon_remote(person_pil, garment_pil, garment_desc, auto_mask, crop_center,
|
|
| 120 |
last_err = e
|
| 121 |
msg = str(e).lower()
|
| 122 |
|
| 123 |
-
is_timeout = (
|
| 124 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 125 |
|
| 126 |
if is_timeout or is_busy:
|
| 127 |
reset_client()
|
| 128 |
-
time.sleep(6.0 * attempt)
|
| 129 |
continue
|
| 130 |
|
| 131 |
time.sleep(1.2 * attempt)
|
|
@@ -161,7 +148,7 @@ with gr.Blocks(title="Virtual Try-On Rendez-vous") as demo:
|
|
| 161 |
|
| 162 |
garment_desc = gr.Textbox(
|
| 163 |
label="Описание одежды",
|
| 164 |
-
value="
|
| 165 |
)
|
| 166 |
|
| 167 |
with gr.Accordion("Настройки", open=False):
|
|
|
|
| 11 |
SPACE = "yisol/IDM-VTON"
|
| 12 |
API_NAME = "/tryon"
|
| 13 |
|
|
|
|
| 14 |
HF_TOKEN = os.getenv("HF_TOKEN", "")
|
| 15 |
|
| 16 |
print("HF_TOKEN set:", bool(HF_TOKEN), "len:", len(HF_TOKEN) if HF_TOKEN else 0)
|
|
|
|
| 33 |
|
| 34 |
|
| 35 |
def get_client():
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
global _client
|
| 37 |
if _client is None:
|
| 38 |
_client = Client(SPACE)
|
|
|
|
| 47 |
return max(lo, min(hi, x))
|
| 48 |
|
| 49 |
|
| 50 |
+
def save_pil_temp(pil_img: Image.Image, suffix: str = ".png") -> str:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
f = tempfile.NamedTemporaryFile(delete=False, suffix=suffix)
|
| 52 |
path = f.name
|
| 53 |
f.close()
|
| 54 |
+
pil_img.save(path, format="PNG") # без ресайза и компрессии
|
| 55 |
return path
|
| 56 |
|
| 57 |
|
|
|
|
| 64 |
denoise_steps = clamp_int(denoise_steps, 10, 40)
|
| 65 |
seed = clamp_int(seed, 0, 999999)
|
| 66 |
|
| 67 |
+
p_path = save_pil_temp(person_pil)
|
| 68 |
+
g_path = save_pil_temp(garment_pil)
|
|
|
|
| 69 |
|
| 70 |
try:
|
| 71 |
last_err = None
|
| 72 |
|
|
|
|
| 73 |
for attempt in range(1, 7):
|
| 74 |
try:
|
| 75 |
client = get_client()
|
|
|
|
| 77 |
result = client.predict(
|
| 78 |
dict={"background": handle_file(p_path), "layers": [], "composite": None},
|
| 79 |
garm_img=handle_file(g_path),
|
| 80 |
+
garment_des=garment_desc, # ← без дефолта
|
| 81 |
is_checked=bool(auto_mask),
|
| 82 |
is_checked_crop=bool(crop_center),
|
| 83 |
denoise_steps=int(denoise_steps),
|
|
|
|
| 95 |
last_err = e
|
| 96 |
msg = str(e).lower()
|
| 97 |
|
| 98 |
+
is_timeout = (
|
| 99 |
+
"write operation timed out" in msg
|
| 100 |
+
or "read operation timed out" in msg
|
| 101 |
+
or "timed out" in msg
|
| 102 |
+
)
|
| 103 |
+
|
| 104 |
+
is_busy = (
|
| 105 |
+
"too many requests" in msg
|
| 106 |
+
or "queue" in msg
|
| 107 |
+
or "too busy" in msg
|
| 108 |
+
or "overloaded" in msg
|
| 109 |
+
or "capacity" in msg
|
| 110 |
+
or "zerogpu" in msg
|
| 111 |
+
)
|
| 112 |
|
| 113 |
if is_timeout or is_busy:
|
| 114 |
reset_client()
|
| 115 |
+
time.sleep(6.0 * attempt)
|
| 116 |
continue
|
| 117 |
|
| 118 |
time.sleep(1.2 * attempt)
|
|
|
|
| 148 |
|
| 149 |
garment_desc = gr.Textbox(
|
| 150 |
label="Описание одежды",
|
| 151 |
+
value="" # ← пусто, без дефолта
|
| 152 |
)
|
| 153 |
|
| 154 |
with gr.Accordion("Настройки", open=False):
|