goumsss Claude Sonnet 4.6 commited on
Commit
fb5e5c4
·
1 Parent(s): 291f11c

Fix cache cleanup: wrap removals in try/except, also clear .locks placeholder

Browse files
Files changed (1) hide show
  1. image_generator.py +11 -7
image_generator.py CHANGED
@@ -41,14 +41,18 @@ if IS_HF_SPACE and os.path.isdir("/data"):
41
  os.environ.setdefault("HF_HOME", _cache_dir)
42
  print(f"Persistent cache active → {_cache_dir}")
43
 
44
- # Clean up any stale placeholder files left by a previous failed download
45
- # (huggingface_hub creates placeholder files that block re-download)
 
46
  _model_cache = os.path.join(_cache_dir, "models--black-forest-labs--FLUX.2-klein-4B")
47
- for _placeholder in [_model_cache, f"{_model_cache}/blobs", f"{_model_cache}/refs",
48
- f"{_model_cache}/snapshots"]:
49
- if os.path.isfile(_placeholder):
50
- os.remove(_placeholder)
51
- print(f"Removed stale placeholder: {_placeholder}")
 
 
 
52
 
53
  if os.path.isdir(_model_cache):
54
  print(f"✅ FLUX.2-klein-4B already cached — skipping download")
 
41
  os.environ.setdefault("HF_HOME", _cache_dir)
42
  print(f"Persistent cache active → {_cache_dir}")
43
 
44
+ # Clean up stale placeholder files left by previous failed downloads.
45
+ # huggingface_hub creates files where it expects directories — remove them.
46
+ import shutil
47
  _model_cache = os.path.join(_cache_dir, "models--black-forest-labs--FLUX.2-klein-4B")
48
+ _locks_path = os.path.join(_cache_dir, ".locks", "models--black-forest-labs--FLUX.2-klein-4B")
49
+ for _stale in [_model_cache, _locks_path]:
50
+ try:
51
+ if os.path.isfile(_stale):
52
+ os.remove(_stale)
53
+ print(f"Removed stale placeholder file: {_stale}")
54
+ except Exception as _e:
55
+ print(f"⚠️ Could not remove {_stale}: {_e}")
56
 
57
  if os.path.isdir(_model_cache):
58
  print(f"✅ FLUX.2-klein-4B already cached — skipping download")