File size: 1,336 Bytes
4f48282
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import modal
import os
from pathlib import Path

# Create or get existing volume
volume = modal.Volume.from_name("flux-model-vol-2", create_if_missing=True)
MODEL_DIR = Path("/data/models")

# Set up image with dependencies
download_image = (
    modal.Image.debian_slim()
    .pip_install("huggingface_hub[hf_transfer]")
    .env({"HF_HUB_ENABLE_HF_TRANSFER": "1"})  # Enable fast Rust download client
)

# Create Modal app
app = modal.App("flux-model-setup")

@app.function(
    volumes={MODEL_DIR: volume},
    image=download_image,
    secrets=[modal.Secret.from_name("huggingface-token")]  # Correct secrets syntax
)
def download_flux():
    from huggingface_hub import snapshot_download
    
    # Get token from environment variable
    token = os.getenv("HF_TOKEN")
    if not token:
        raise ValueError("HF_TOKEN not found in environment variables. Ensure the secret is correctly set.")
    
    repo_id = "black-forest-labs/FLUX.1-dev"
    local_dir = MODEL_DIR / repo_id.split("/")[1]
    
    # Ensure the directory exists
    local_dir.mkdir(parents=True, exist_ok=True)
    
    # Download the model
    snapshot_download(
        repo_id=repo_id,
        local_dir=local_dir,
        token=token
    )
    print(f"FLUX model downloaded to {local_dir}")

@app.local_entrypoint()
def main():
    download_flux.remote()