55import base64
66import io
77import os
8- # Disable HF transfer for better error handling
9- os .environ ["HF_HUB_ENABLE_HF_TRANSFER" ] = "0"
108
119import logging
1210from pathlib import Path
@@ -44,38 +42,41 @@ def __init__(self, base_cache_dir: str = "./cache", cleanup_on_init: bool = True
4442 self ._check_disk_space (required_gb = 40 )
4543
4644 def _download_miner_files (self , repo_id : str , uid : str ) -> Path :
47- """
48- Downloads required files from HuggingFace.
49-
50- Args:
51- repo_id: HuggingFace repository ID
52- uid: Unique identifier for the download
53-
54- Returns:
55- Path to downloaded files
56- """
45+ """Downloads required files from HuggingFace."""
5746 try :
58-
47+ # Check disk space with higher requirement for model download
48+ self ._check_disk_space (required_gb = 40 )
5949
6050 repo_name = repo_id .replace ("/" , "_" )
6151 miner_dir = self .base_cache_dir / repo_name
52+
53+ # Clean up any existing directory
54+ if miner_dir .exists ():
55+ bt .logging .info (f"Cleaning up existing directory: { miner_dir } " )
56+ shutil .rmtree (miner_dir )
57+
6258 miner_dir .mkdir (parents = True , exist_ok = True )
6359
6460 bt .logging .info (f"Downloading files from { repo_id } to { miner_dir } " )
6561
6662 try :
63+ # Configure HuggingFace download
6764 downloaded_path = huggingface_hub .snapshot_download (
6865 repo_id = repo_id ,
6966 local_dir = miner_dir ,
70- local_dir_use_symlinks = False
67+ local_dir_use_symlinks = False ,
7168 )
72- bt .logging .info (f"Downloaded files to { downloaded_path } " )
69+
70+ bt .logging .info (f"Successfully downloaded files to { downloaded_path } " )
71+ return miner_dir
72+
7373 except Exception as e :
7474 bt .logging .error (f"Failed to download repo: { str (e )} " )
75+ # Clean up partial download
76+ if miner_dir .exists ():
77+ shutil .rmtree (miner_dir )
7578 raise
7679
77- return miner_dir
78-
7980 except Exception as e :
8081 bt .logging .error (f"Failed to download miner files: { str (e )} " )
8182 raise
@@ -87,64 +88,52 @@ def get_memory_limit(self):
8788
8889 def _check_disk_space (self , required_gb : int = 20 ) -> None :
8990 """Check available disk space and clean if necessary."""
90- self .cleanup_docker_resources ()
91- self ._clean_old_repos ()
92-
93-
94- total , used , free = shutil .disk_usage (str (self .base_cache_dir ))
95- free_gb = free / (1024 * 1024 * 1024 )
96-
97- bt .logging .info (f"Available disk space: { free_gb :.2f} GB" )
98-
99- if free_gb < required_gb :
100- bt .logging .warning (f"Low disk space ({ free_gb :.2f} GB free), cleaning cache directory" )
101-
102- # Clean Docker resources first
91+ try :
92+ # First clean up Docker resources
10393 self .cleanup_docker_resources (aggressive = True )
10494
105- # Clean Hugging Face cache specifically
106- self ._clean_huggingface_cache ()
95+ # Then clean old repos
96+ self ._clean_old_repos ( keep_latest = 1 ) # Keep only the latest repo
10797
108-
98+ # Check disk space
99+ total , used , free = shutil .disk_usage (str (self .base_cache_dir ))
100+ free_gb = free / (1024 * 1024 * 1024 )
109101
110- # Clear HuggingFace cache if exists
111- hf_cache = os .path .expanduser ("~/.cache/huggingface/hub" )
112- if os .path .exists (hf_cache ):
113- bt .logging .info (f"Cleaning HuggingFace cache: { hf_cache } " )
114- try :
115- shutil .rmtree (hf_cache )
116- except Exception as e :
117- bt .logging .error (f"Failed to clean HuggingFace cache: { str (e )} " )
102+ bt .logging .info (f"Available disk space: { free_gb :.2f} GB" )
118103
119- # Clear our cache directory
120- for item in os .listdir (str (self .base_cache_dir )):
121- item_path = os .path .join (str (self .base_cache_dir ), item )
122- if os .path .isdir (item_path ):
123- try :
124- shutil .rmtree (item_path )
125- bt .logging .info (f"Removed directory: { item_path } " )
126- except Exception as e :
127- bt .logging .error (f"Failed to remove directory { item_path } : { str (e )} " )
128- else :
104+ if free_gb < required_gb :
105+ bt .logging .warning (f"Low disk space ({ free_gb :.2f} GB free), performing aggressive cleanup" )
106+
107+ # Clean HuggingFace cache
108+ self ._clean_huggingface_cache ()
109+
110+ # Clear our cache directory
111+ for item in os .listdir (str (self .base_cache_dir )):
112+ item_path = os .path .join (str (self .base_cache_dir ), item )
129113 try :
130- os .remove (item_path )
131- bt .logging .info (f"Removed file: { item_path } " )
114+ if os .path .isdir (item_path ):
115+ shutil .rmtree (item_path )
116+ else :
117+ os .remove (item_path )
118+ bt .logging .info (f"Removed: { item_path } " )
132119 except Exception as e :
133- bt .logging .error (f"Failed to remove file { item_path } : { str (e )} " )
134-
135- # Recreate necessary directories
136- self .base_cache_dir .mkdir (parents = True , exist_ok = True )
137- for dir_name in ['models' , 'hub' , 'downloads' ]:
138- (self .base_cache_dir / dir_name ).mkdir (parents = True , exist_ok = True )
139-
140- # Check if we have enough space now
141- _ , _ , free = shutil .disk_usage (str (self .base_cache_dir ))
142- free_gb = free / (1024 * 1024 * 1024 )
143- bt .logging .info (f"Available disk space after cleanup: { free_gb :.2f} GB" )
144-
145- if free_gb < required_gb :
146- bt .logging .error (f"Still insufficient disk space ({ free_gb :.2f} GB) after cleanup" )
147- raise RuntimeError (f"Insufficient disk space: { free_gb :.2f} GB available, { required_gb } GB required" )
120+ bt .logging .error (f"Failed to remove { item_path } : { str (e )} " )
121+
122+ # Recreate necessary directories
123+ self .base_cache_dir .mkdir (parents = True , exist_ok = True )
124+ for dir_name in ['models' , 'hub' , 'downloads' ]:
125+ (self .base_cache_dir / dir_name ).mkdir (parents = True , exist_ok = True )
126+
127+ # Check space again
128+ _ , _ , free = shutil .disk_usage (str (self .base_cache_dir ))
129+ free_gb = free / (1024 * 1024 * 1024 )
130+
131+ if free_gb < required_gb :
132+ raise RuntimeError (f"Insufficient disk space: { free_gb :.2f} GB available, { required_gb } GB required" )
133+
134+ except Exception as e :
135+ bt .logging .error (f"Error during disk space check: { str (e )} " )
136+ raise
148137
149138 def _clean_huggingface_cache (self ):
150139 """Clean Hugging Face cache specifically."""
0 commit comments