Update app.py
Browse files
app.py
CHANGED
|
@@ -271,6 +271,88 @@ def _reset_hydra():
|
|
| 271 |
except Exception:
|
| 272 |
pass
|
| 273 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 274 |
# =============================================================================
|
| 275 |
# CHAPTER 4: MEMORY MANAGER
|
| 276 |
# =============================================================================
|
|
|
|
| 271 |
except Exception:
|
| 272 |
pass
|
| 273 |
|
| 274 |
+
# =============================================================================
|
| 275 |
+
# CHAPTER 3A: STARTUP CLEANUP (safe, configurable)
|
| 276 |
+
# =============================================================================
|
| 277 |
+
# Controls:
|
| 278 |
+
# CLEAN_ON_START = off | light | deep (default: light)
|
| 279 |
+
# off -> do nothing
|
| 280 |
+
# light -> clear ./temp, ./outputs (incl. ./outputs/backgrounds)
|
| 281 |
+
# deep -> light + /tmp/gradio + HF caches (NOT model hub unless CLEAR_MODELS=1)
|
| 282 |
+
# CLEAR_MODELS = 0|1 (deep-only) also clears ~/.cache/huggingface/hub and ./checkpoints
|
| 283 |
+
# CLEAR_PLATFORM_TMP = 0|1 (additionally clear /tmp/gradio even in light)
|
| 284 |
+
CLEAN_ON_START = os.environ.get("CLEAN_ON_START", "light").lower().strip()
|
| 285 |
+
CLEAR_MODELS = os.environ.get("CLEAR_MODELS", "0") == "1"
|
| 286 |
+
CLEAR_PLATFORM_TMP = os.environ.get("CLEAR_PLATFORM_TMP", "0") == "1"
|
| 287 |
+
|
| 288 |
+
def _safe_rmtree(path: Path) -> bool:
|
| 289 |
+
"""Delete a directory only if it is our project dir or an allowed /tmp path."""
|
| 290 |
+
try:
|
| 291 |
+
path = path.resolve()
|
| 292 |
+
if not path.exists():
|
| 293 |
+
return False
|
| 294 |
+
# allow: anything under project BASE_DIR
|
| 295 |
+
if path.is_dir() and path.is_relative_to(BASE_DIR):
|
| 296 |
+
shutil.rmtree(path, ignore_errors=True)
|
| 297 |
+
return True
|
| 298 |
+
# allow: /tmp/gradio* when platform uses temp uploads
|
| 299 |
+
if str(path).startswith("/tmp/gradio"):
|
| 300 |
+
shutil.rmtree(path, ignore_errors=True)
|
| 301 |
+
return True
|
| 302 |
+
except Exception:
|
| 303 |
+
pass
|
| 304 |
+
return False
|
| 305 |
+
|
| 306 |
+
def startup_cleanup():
|
| 307 |
+
"""Clear caches/files so each run starts fresh."""
|
| 308 |
+
if CLEAN_ON_START not in ("light", "deep"):
|
| 309 |
+
logger.info(f"Startup cleanup skipped (CLEAN_ON_START={CLEAN_ON_START})")
|
| 310 |
+
return
|
| 311 |
+
|
| 312 |
+
to_clear: List[Path] = []
|
| 313 |
+
|
| 314 |
+
# Always clear our temp/output trees
|
| 315 |
+
to_clear += [TEMP_DIR, OUT_DIR, BACKGROUND_DIR]
|
| 316 |
+
|
| 317 |
+
# /tmp/gradio holds uploads & intermediates on many hosts
|
| 318 |
+
if CLEAN_ON_START == "deep" or CLEAR_PLATFORM_TMP:
|
| 319 |
+
gradio_tmp = Path(os.environ.get("GRADIO_TEMP_DIR", "/tmp/gradio"))
|
| 320 |
+
to_clear += [gradio_tmp]
|
| 321 |
+
|
| 322 |
+
if CLEAN_ON_START == "deep":
|
| 323 |
+
# Clear non-model HF caches
|
| 324 |
+
hf_base = Path(os.environ.get("HF_HOME", Path.home() / ".cache" / "huggingface"))
|
| 325 |
+
to_clear += [hf_base / "diffusers", hf_base / "datasets"]
|
| 326 |
+
# Optionally clear model hub + checkpoints (will redownload!)
|
| 327 |
+
if CLEAR_MODELS:
|
| 328 |
+
to_clear += [hf_base / "hub", CHECKPOINTS]
|
| 329 |
+
|
| 330 |
+
# Perform deletions
|
| 331 |
+
cleared = []
|
| 332 |
+
for d in to_clear:
|
| 333 |
+
if isinstance(d, Path) and d.exists():
|
| 334 |
+
if _safe_rmtree(d):
|
| 335 |
+
cleared.append(str(d))
|
| 336 |
+
|
| 337 |
+
# Re-create our working directories
|
| 338 |
+
for p in (CHECKPOINTS, TEMP_DIR, OUT_DIR, BACKGROUND_DIR):
|
| 339 |
+
p.mkdir(parents=True, exist_ok=True)
|
| 340 |
+
|
| 341 |
+
# Free CUDA cache too
|
| 342 |
+
if TORCH_AVAILABLE and CUDA_AVAILABLE:
|
| 343 |
+
try:
|
| 344 |
+
torch.cuda.empty_cache()
|
| 345 |
+
except Exception:
|
| 346 |
+
pass
|
| 347 |
+
|
| 348 |
+
logger.info(
|
| 349 |
+
f"Startup cleanup done (mode={CLEAN_ON_START}, clear_models={int(CLEAR_MODELS)}). "
|
| 350 |
+
f"Cleared: {', '.join(cleared) if cleared else 'nothing'}"
|
| 351 |
+
)
|
| 352 |
+
|
| 353 |
+
|
| 354 |
+
|
| 355 |
+
|
| 356 |
# =============================================================================
|
| 357 |
# CHAPTER 4: MEMORY MANAGER
|
| 358 |
# =============================================================================
|