Spaces:
Sleeping
Sleeping
Upload clear_cache.py
Browse files- src/clear_cache.py +52 -0
src/clear_cache.py
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""
|
| 3 |
+
Cache clearing script for Hugging Face Spaces
|
| 4 |
+
Removes stuck lock files and clears cache to fix PermissionError
|
| 5 |
+
"""
|
| 6 |
+
|
| 7 |
+
import os
|
| 8 |
+
import shutil
|
| 9 |
+
import subprocess
|
| 10 |
+
from pathlib import Path
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
def clear_hf_cache():
|
| 14 |
+
"""Clear Hugging Face cache and remove lock files"""
|
| 15 |
+
|
| 16 |
+
# Common cache locations
|
| 17 |
+
cache_paths = [
|
| 18 |
+
"/.cache/huggingface",
|
| 19 |
+
"/tmp/huggingface",
|
| 20 |
+
os.path.expanduser("~/.cache/huggingface"),
|
| 21 |
+
os.path.expanduser("~/.huggingface")
|
| 22 |
+
]
|
| 23 |
+
|
| 24 |
+
print("🧹 Clearing Hugging Face cache...")
|
| 25 |
+
|
| 26 |
+
for cache_path in cache_paths:
|
| 27 |
+
if os.path.exists(cache_path):
|
| 28 |
+
print(f"📁 Found cache at: {cache_path}")
|
| 29 |
+
|
| 30 |
+
# Remove lock files
|
| 31 |
+
lock_files = list(Path(cache_path).rglob("*.lock"))
|
| 32 |
+
for lock_file in lock_files:
|
| 33 |
+
try:
|
| 34 |
+
os.remove(lock_file)
|
| 35 |
+
print(f"🔓 Removed lock file: {lock_file}")
|
| 36 |
+
except Exception as e:
|
| 37 |
+
print(f"⚠️ Could not remove {lock_file}: {e}")
|
| 38 |
+
|
| 39 |
+
# Clear the cache directory
|
| 40 |
+
try:
|
| 41 |
+
shutil.rmtree(cache_path)
|
| 42 |
+
print(f"🗑️ Cleared cache: {cache_path}")
|
| 43 |
+
except Exception as e:
|
| 44 |
+
print(f"⚠️ Could not clear {cache_path}: {e}")
|
| 45 |
+
else:
|
| 46 |
+
print(f"❌ Cache not found at: {cache_path}")
|
| 47 |
+
|
| 48 |
+
print("✅ Cache clearing complete!")
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
if __name__ == "__main__":
|
| 52 |
+
clear_hf_cache()
|