Retry embedding startup for DeepFashion demo
Browse files
demo.py
CHANGED
|
@@ -5,6 +5,7 @@ from __future__ import annotations
|
|
| 5 |
|
| 6 |
import os
|
| 7 |
import re
|
|
|
|
| 8 |
from collections import Counter
|
| 9 |
from pathlib import Path
|
| 10 |
from typing import Any
|
|
@@ -37,6 +38,8 @@ ALLOW_CANDIDATE_FALLBACK = os.environ.get("HYPERVIEW_ALLOW_CANDIDATE_FALLBACK",
|
|
| 37 |
"true",
|
| 38 |
"yes",
|
| 39 |
}
|
|
|
|
|
|
|
| 40 |
RUNTIME_WARNINGS: list[str] = []
|
| 41 |
|
| 42 |
MODEL_SPECS = [
|
|
@@ -252,18 +255,40 @@ def add_deepfashion_samples(dataset: hv.Dataset) -> None:
|
|
| 252 |
print(f"Prepared DeepFashion samples ({added} added, {updated} updated).", flush=True)
|
| 253 |
|
| 254 |
|
| 255 |
-
def
|
| 256 |
-
|
| 257 |
-
for spec in MODEL_SPECS:
|
| 258 |
-
print(f"Ensuring {spec['display_name']} embeddings...", flush=True)
|
| 259 |
try:
|
| 260 |
-
|
| 261 |
model=spec["model"],
|
| 262 |
provider=spec["provider"],
|
| 263 |
batch_size=32,
|
| 264 |
show_progress=True,
|
| 265 |
)
|
| 266 |
-
except
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 267 |
if spec["key"] == "candidate" and ALLOW_CANDIDATE_FALLBACK and "clip" in layouts:
|
| 268 |
warning = (
|
| 269 |
f"Hyper3-CLIP embeddings are unavailable ({type(exc).__name__}: {exc}). "
|
|
|
|
| 5 |
|
| 6 |
import os
|
| 7 |
import re
|
| 8 |
+
import time
|
| 9 |
from collections import Counter
|
| 10 |
from pathlib import Path
|
| 11 |
from typing import Any
|
|
|
|
| 38 |
"true",
|
| 39 |
"yes",
|
| 40 |
}
|
| 41 |
+
EMBEDDING_MAX_ATTEMPTS = max(1, int(os.environ.get("HYPERVIEW_EMBEDDING_MAX_ATTEMPTS", "4")))
|
| 42 |
+
EMBEDDING_RETRY_DELAY_SECONDS = float(os.environ.get("HYPERVIEW_EMBEDDING_RETRY_DELAY_SECONDS", "15"))
|
| 43 |
RUNTIME_WARNINGS: list[str] = []
|
| 44 |
|
| 45 |
MODEL_SPECS = [
|
|
|
|
| 255 |
print(f"Prepared DeepFashion samples ({added} added, {updated} updated).", flush=True)
|
| 256 |
|
| 257 |
|
| 258 |
+
def compute_embeddings_with_retry(dataset: hv.Dataset, spec: dict[str, Any]) -> str:
|
| 259 |
+
for attempt in range(1, EMBEDDING_MAX_ATTEMPTS + 1):
|
|
|
|
|
|
|
| 260 |
try:
|
| 261 |
+
return dataset.compute_embeddings(
|
| 262 |
model=spec["model"],
|
| 263 |
provider=spec["provider"],
|
| 264 |
batch_size=32,
|
| 265 |
show_progress=True,
|
| 266 |
)
|
| 267 |
+
except BaseException as exc:
|
| 268 |
+
if isinstance(exc, (KeyboardInterrupt, SystemExit)):
|
| 269 |
+
raise
|
| 270 |
+
if attempt >= EMBEDDING_MAX_ATTEMPTS:
|
| 271 |
+
raise
|
| 272 |
+
delay = EMBEDDING_RETRY_DELAY_SECONDS * attempt
|
| 273 |
+
print(
|
| 274 |
+
f"Embedding load failed for {spec['display_name']} "
|
| 275 |
+
f"({type(exc).__name__}: {exc}). Retrying in {delay:.0f}s "
|
| 276 |
+
f"({attempt + 1}/{EMBEDDING_MAX_ATTEMPTS})...",
|
| 277 |
+
flush=True,
|
| 278 |
+
)
|
| 279 |
+
time.sleep(delay)
|
| 280 |
+
raise RuntimeError(f"Failed to compute embeddings for {spec['display_name']}")
|
| 281 |
+
|
| 282 |
+
|
| 283 |
+
def ensure_layouts(dataset: hv.Dataset) -> dict[str, str]:
|
| 284 |
+
layouts: dict[str, str] = {}
|
| 285 |
+
for spec in MODEL_SPECS:
|
| 286 |
+
print(f"Ensuring {spec['display_name']} embeddings...", flush=True)
|
| 287 |
+
try:
|
| 288 |
+
space_key = compute_embeddings_with_retry(dataset, spec)
|
| 289 |
+
except BaseException as exc:
|
| 290 |
+
if isinstance(exc, (KeyboardInterrupt, SystemExit)):
|
| 291 |
+
raise
|
| 292 |
if spec["key"] == "candidate" and ALLOW_CANDIDATE_FALLBACK and "clip" in layouts:
|
| 293 |
warning = (
|
| 294 |
f"Hyper3-CLIP embeddings are unavailable ({type(exc).__name__}: {exc}). "
|