download.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. # ## Downloading Llama 3.2 3B Instruct Model
  2. # This script uses a Modal Function to download the model into a cloud Volume.
  3. #
  4. # Run it with:
  5. # modal run download
  6. import modal
  7. MODELS_DIR = "/llamas"
  8. DEFAULT_NAME = "meta-llama/Llama-3.2-3B-Instruct"
  9. MINUTES = 60
  10. HOURS = 60 * MINUTES
  11. # Create a modal Volume to store the model
  12. volume = modal.Volume.from_name("llamas", create_if_missing=True)
  13. # This defines the image to use for the modal function
  14. image = (
  15. modal.Image.debian_slim(python_version="3.10")
  16. .pip_install(
  17. [
  18. "huggingface_hub", # download models from the Hugging Face Hub
  19. "hf-transfer", # download models faster with Rust
  20. ]
  21. )
  22. .env({"HF_HUB_ENABLE_HF_TRANSFER": "1"})
  23. )
  24. # We run the function from a modal App, which will have our HF_SECRET env var set.
  25. # Add your HuggingFace secret access token here: https://modal.com/secrets
  26. # secret name: huggingface
  27. # env var name: HF_TOKEN
  28. app = modal.App(image=image, secrets=[modal.Secret.from_name("huggingface")])
  29. # This function will be ran in the cloud, with the volume mounted.
  30. @app.function(volumes={MODELS_DIR: volume}, timeout=4 * HOURS)
  31. def download_model(model_name, force_download=False):
  32. from huggingface_hub import snapshot_download
  33. volume.reload()
  34. snapshot_download(
  35. model_name,
  36. local_dir=MODELS_DIR + "/" + model_name,
  37. ignore_patterns=[
  38. "*.pt",
  39. "*.bin",
  40. "*.pth",
  41. "original/*",
  42. ], # Ensure safetensors
  43. force_download=force_download,
  44. )
  45. volume.commit()
  46. print("Model successfully downloaded")
  47. @app.local_entrypoint()
  48. def main(
  49. model_name: str = DEFAULT_NAME,
  50. force_download: bool = False,
  51. ):
  52. download_model.remote(model_name, force_download)