Spaces:
Sleeping
Sleeping
| #!/usr/bin/env python3 | |
| """ | |
| Cache clearing script for Hugging Face Spaces | |
| Removes stuck lock files and clears cache to fix PermissionError | |
| """ | |
| import os | |
| import shutil | |
| import subprocess | |
| from pathlib import Path | |
| def clear_hf_cache(): | |
| """Clear Hugging Face cache and remove lock files""" | |
| # Common cache locations | |
| cache_paths = [ | |
| "/.cache/huggingface", | |
| "/tmp/huggingface", | |
| os.path.expanduser("~/.cache/huggingface"), | |
| os.path.expanduser("~/.huggingface") | |
| ] | |
| print("π§Ή Clearing Hugging Face cache...") | |
| for cache_path in cache_paths: | |
| if os.path.exists(cache_path): | |
| print(f"π Found cache at: {cache_path}") | |
| # Remove lock files | |
| lock_files = list(Path(cache_path).rglob("*.lock")) | |
| for lock_file in lock_files: | |
| try: | |
| os.remove(lock_file) | |
| print(f"π Removed lock file: {lock_file}") | |
| except Exception as e: | |
| print(f"β οΈ Could not remove {lock_file}: {e}") | |
| # Clear the cache directory | |
| try: | |
| shutil.rmtree(cache_path) | |
| print(f"ποΈ Cleared cache: {cache_path}") | |
| except Exception as e: | |
| print(f"β οΈ Could not clear {cache_path}: {e}") | |
| else: | |
| print(f"β Cache not found at: {cache_path}") | |
| print("β Cache clearing complete!") | |
| if __name__ == "__main__": | |
| clear_hf_cache() | |