#!/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()