77import os
88import sys
99import logging
10- import traceback
10+ import subprocess
1111
1212logging .basicConfig (stream = sys .stdout , level = logging .DEBUG , force = True )
1313logger = logging .getLogger ()
1414logger .setLevel (logging .DEBUG )
1515
16+ def log_huggingface_whoami ():
17+ logger .info ("🔑 Checking Hugging Face authentication with 'huggingface-cli whoami'..." )
18+ try :
19+ result = subprocess .run (
20+ ["huggingface-cli" , "whoami" ],
21+ capture_output = True ,
22+ text = True ,
23+ check = True
24+ )
25+ logger .info (f"Hugging Face user: { result .stdout .strip ()} " )
26+ except Exception as e :
27+ logger .error (f"❌ Unable to verify Hugging Face authentication: { e } " )
28+
1629def download_weights (repo_id : str , filenames : list [str ]):
30+ log_huggingface_whoami ()
1731 # Create destination path
1832 dest_dir = os .path .join ("models" , repo_id .replace ("/" , "_" ))
1933 os .makedirs (dest_dir , exist_ok = True )
@@ -22,28 +36,37 @@ def download_weights(repo_id: str, filenames: list[str]):
2236 logger .info (f"📥 Downloading { filename } from { repo_id } on Hugging Face Hub..." )
2337 try :
2438 path = hf_hub_download (repo_id = repo_id , filename = filename )
25-
2639 # Move and rename
2740 dest_path = os .path .join (dest_dir , filename )
2841 shutil .copy (path , dest_path )
42+ logger .info (f"✅ Saved { filename } to { dest_path } " )
2943 except (RepositoryNotFoundError , HfHubHTTPError ) as e :
3044 logger .error (f"❌ Failed to fetch '{ filename } ' from '{ repo_id } ': { e } " )
3145 continue
3246
33- logger .info (f"✅ All saved to: { dest_dir } " )
47+ # Verification step
48+ missing = []
49+ for filename in filenames :
50+ dest_path = os .path .join (dest_dir , filename )
51+ if not os .path .isfile (dest_path ):
52+ missing .append (filename )
53+ if missing :
54+ logger .error (f"❌ The following files are missing after download: { missing } " )
55+ else :
56+ logger .info (f"✅ All expected files present in: { dest_dir } " )
3457
3558if __name__ == "__main__" :
36- parser = argparse .ArgumentParser (description = "Download Hugging Face model weights" )
37- parser .add_argument (
38- "--repo-id" ,
39- required = True ,
40- help = "Hugging Face repository ID (e.g., openai/clip-vit-base-patch32)"
41- )
42- parser .add_argument (
43- "--filenames" ,
44- nargs = "+" ,
45- default = ["pytorch_model.bin" , "config.json" ],
46- help = "List of filenames to download (space-separated)"
47- )
48- args = parser .parse_args ()
49- download_weights (args .repo_id , args .filenames )
59+ parser = argparse .ArgumentParser (description = "Download Hugging Face model weights" )
60+ parser .add_argument (
61+ "--repo-id" ,
62+ required = True ,
63+ help = "Hugging Face repository ID (e.g., openai/clip-vit-base-patch32)"
64+ )
65+ parser .add_argument (
66+ "--filenames" ,
67+ nargs = "+" ,
68+ default = ["pytorch_model.bin" , "config.json" ],
69+ help = "List of filenames to download (space-separated)"
70+ )
71+ args = parser .parse_args ()
72+ download_weights (args .repo_id , args .filenames )
0 commit comments