@@ -660,20 +660,32 @@ def main(argv):
660660 dtype = jnp .bfloat16 if config .weights_dtype == "bfloat16" else jnp .float32 ,
661661 )
662662
663- # 6. Locate cached PyTorch weights
663+ # 6. Locate cached PyTorch weights (with automatic download if missing)
664664 hf_home = os .environ .get ("HF_HOME" )
665665 if not hf_home :
666666 if os .path .exists ("/mnt/data/hf_cache" ):
667667 hf_home = "/mnt/data/hf_cache"
668+ # Set it in the environment so downstream HF loaders use it
669+ os .environ ["HF_HOME" ] = hf_home
668670 else :
669671 hf_home = os .path .expanduser ("~/.cache/huggingface" )
670672
671- cache_dir = os .path .join (hf_home , "hub" , "models--black-forest-labs--FLUX.2-klein-4B" , "snapshots" )
673+ repo_id = "black-forest-labs/FLUX.2-klein-4B"
674+ cache_dir = os .path .join (hf_home , "hub" , f"models--{ repo_id .replace ('/' , '--' )} " , "snapshots" )
675+
676+ if not os .path .exists (cache_dir ) or not os .listdir (cache_dir ):
677+ print (f"\n 📢 Model cache not found at { cache_dir } ." )
678+ print (f"🚀 Downloading '{ repo_id } ' from Hugging Face Hub (this may take a few minutes)..." )
679+ from huggingface_hub import snapshot_download
680+ # This will automatically use the resolved HF_HOME env var
681+ snapshot_download (repo_id = repo_id , local_files_only = False )
682+
672683 if not os .path .exists (cache_dir ):
673- raise FileNotFoundError (f"Hugging Face cache directory not found: { cache_dir } . Please ensure HF_HOME is set correctly." )
684+ raise FileNotFoundError (f"Hugging Face cache directory still not found after download: { cache_dir } " )
685+
674686 snapshots = os .listdir (cache_dir )
675687 if not snapshots :
676- raise FileNotFoundError ("No snapshots found in Hugging Face cache directory. " )
688+ raise FileNotFoundError (f "No snapshots found in Hugging Face cache directory: { cache_dir } " )
677689 snapshot_dir = os .path .join (cache_dir , snapshots [0 ])
678690 safetensors_path = os .path .join (snapshot_dir , "transformer" , "diffusion_pytorch_model.safetensors" )
679691 vae_safetensors_path = os .path .join (snapshot_dir , "vae" , "diffusion_pytorch_model.safetensors" )
0 commit comments