#!/usr/bin/env python3 """ Cleanup script to reduce Hugging Face Space size """ import os import shutil import glob def cleanup_space(): """Clean up unnecessary files to reduce space usage""" # Remove common cache directories cache_dirs = [ '__pycache__', '.pytest_cache', '.mypy_cache', '.coverage', 'htmlcov', 'gradio_cached_examples', 'flagged', '.cache', 'huggingface_hub' ] for cache_dir in cache_dirs: if os.path.exists(cache_dir): print(f"Removing {cache_dir}") shutil.rmtree(cache_dir, ignore_errors=True) # Remove common cache files cache_patterns = [ '*.pyc', '*.pyo', '*.log', '*.tmp', '*.temp', '.DS_Store', 'Thumbs.db' ] for pattern in cache_patterns: for file_path in glob.glob(pattern, recursive=True): try: if os.path.isfile(file_path): os.remove(file_path) print(f"Removed {file_path}") except Exception as e: print(f"Could not remove {file_path}: {e}") # Remove large model files if they exist large_extensions = ['.pth', '.bin', '.model', '.weights', '.h5', '.hdf5', '.pkl', '.pickle'] for ext in large_extensions: for file_path in glob.glob(f'**/*{ext}', recursive=True): try: if os.path.isfile(file_path): size_mb = os.path.getsize(file_path) / (1024 * 1024) if size_mb > 10: # Only remove files larger than 10MB os.remove(file_path) print(f"Removed large file {file_path} ({size_mb:.1f}MB)") except Exception as e: print(f"Could not remove {file_path}: {e}") print("Cleanup completed!") if __name__ == "__main__": cleanup_space()