Fix cache cleanup: wrap removals in try/except, also clear .locks placeholder
Browse files- 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
|
| 45 |
-
#
|
|
|
|
| 46 |
_model_cache = os.path.join(_cache_dir, "models--black-forest-labs--FLUX.2-klein-4B")
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
os.
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
| 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")
|