File size: 7,023 Bytes
b701455
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import glob
import os
import shutil
from pathlib import Path

from huggingface_hub import hf_hub_download


def CheckAndDownloadFlux2():
    """Check and download Flux2 Klein model components if needed.
    
    Downloads:
    - Flux2 Klein diffusion model to ./include/diffusion_model/
    - Qwen3 4B text encoder to ./include/text_encoder/
    - Flux VAE to ./include/vae/
    """
    
    # Check for diffusion model
    diffusion_dir = "./include/diffusion_model/"
    os.makedirs(diffusion_dir, exist_ok=True)
    
    # Look for actual diffusion model files (safetensors, pt, pth) that match flux/klein names, including nested subdirectories
    diffusion_candidates = glob.glob(os.path.join(diffusion_dir, "**", "*flux*"), recursive=True) + glob.glob(os.path.join(diffusion_dir, "**", "*klein*"), recursive=True)
    flux2_models = [p for p in diffusion_candidates if os.path.isfile(p) and os.path.splitext(p)[1].lower() in (".safetensors", ".pt", ".pth")]
    if not flux2_models:
        print("Downloading Flux2 Klein diffusion model...")
        try:
            filename = "split_files/diffusion_models/flux-2-klein-4b.safetensors"
            path = hf_hub_download(
                repo_id="black-forest-labs/FLUX.2-klein-4B",
                filename="flux-2-klein-4b.safetensors",
                local_dir=diffusion_dir,
            )
            # If it downloaded into a subfolder, move it up
            if os.path.dirname(filename):
                target = os.path.join(diffusion_dir, os.path.basename(filename))
                if os.path.exists(path) and not os.path.exists(target):
                    shutil.move(path, target)
                    # Cleanup empty subfolders
                    subfolder = os.path.join(diffusion_dir, filename.split('/')[0])
                    if os.path.exists(subfolder):
                        shutil.rmtree(subfolder)
        except Exception as e:
            print(f"Could not download Flux2 diffusion model: {e}")
    
    # Check for text encoder
    text_encoder_dir = "./include/text_encoder/"
    os.makedirs(text_encoder_dir, exist_ok=True)
    
    # Look for actual text encoder model files (safetensors, pt, pth) matching qwen/klein, including nested subdirs
    qwen_candidates = glob.glob(os.path.join(text_encoder_dir, "**", "*qwen*"), recursive=True) + glob.glob(os.path.join(text_encoder_dir, "**", "*klein*"), recursive=True)
    qwen_models = [p for p in qwen_candidates if os.path.isfile(p) and os.path.splitext(p)[1].lower() in (".safetensors", ".pt", ".pth")]
    if not qwen_models:
        print("Downloading Qwen3 text encoder for Flux2 Klein...")
        try:
            filename = "split_files/text_encoders/qwen_3_4b.safetensors"
            path = hf_hub_download(
                repo_id="Comfy-Org/vae-text-encorder-for-flux-klein-4b",
                filename=filename, 
                local_dir=text_encoder_dir,
            )
            # If it downloaded into a subfolder, move it up
            if os.path.dirname(filename):
                target = os.path.join(text_encoder_dir, os.path.basename(filename))
                if os.path.exists(path) and not os.path.exists(target):
                    shutil.move(path, target)
                    # Cleanup empty subfolders
                    subfolder = os.path.join(text_encoder_dir, filename.split('/')[0])
                    if os.path.exists(subfolder):
                        shutil.rmtree(subfolder)
        except Exception as e:
            print(f"Could not download Qwen text encoder: {e}")
    
    # Check for VAE
    vae_dir = "./include/vae/"
    os.makedirs(vae_dir, exist_ok=True)
    
    vae_models = glob.glob(f"{vae_dir}*.safetensors") + glob.glob(f"{vae_dir}*.pt")
    if not vae_models:
        print("Downloading Flux VAE...")
        try:
            filename = "split_files/vae/flux2-vae.safetensors"
            path = hf_hub_download(
                repo_id="Comfy-Org/flux2-dev",
                filename=filename,
                local_dir=vae_dir,
            )
            # If it downloaded into a subfolder, move it up
            if os.path.dirname(filename):
                target = os.path.join(vae_dir, os.path.basename(filename))
                if os.path.exists(path) and not os.path.exists(target):
                    shutil.move(path, target)
                    # Cleanup empty subfolders
                    subfolder = os.path.join(vae_dir, filename.split('/')[0])
                    if os.path.exists(subfolder):
                        shutil.rmtree(subfolder)
        except Exception as e:
            print(f"Could not download Flux VAE: {e}")


def CheckAndDownload():
    """#### Check and download all the necessary safetensors and checkpoints models"""
    if glob.glob("./include/checkpoints/*.safetensors") == []:
        hf_hub_download(
            repo_id="Lykon/DreamShaper",
            filename="DreamShaper_8_pruned.safetensors",
            local_dir="./include/checkpoints/",
        )
    if glob.glob("./include/yolos/*.pt") == []:

        hf_hub_download(
            repo_id="Bingsu/adetailer",
            filename="hand_yolov9c.pt",
            local_dir="./include/yolos/",
        )
        hf_hub_download(
            repo_id="Bingsu/adetailer",
            filename="face_yolov9c.pt",
            local_dir="./include/yolos/",
        )
        hf_hub_download(
            repo_id="Bingsu/adetailer",
            filename="person_yolov8m-seg.pt",
            local_dir="./include/yolos/",
        )
        hf_hub_download(
            repo_id="segments-arnaud/sam_vit_b",
            filename="sam_vit_b_01ec64.pth",
            local_dir="./include/yolos/",
        )
    if glob.glob("./include/ESRGAN/*.pth") == []:

        hf_hub_download(
            repo_id="lllyasviel/Annotators",
            filename="RealESRGAN_x4plus.pth",
            local_dir="./include/ESRGAN/",
        )
    if glob.glob("./include/loras/*.safetensors") == []:

        hf_hub_download(
            repo_id="EvilEngine/add_detail",
            filename="add_detail.safetensors",
            local_dir="./include/loras/",
        )
    if glob.glob("./include/embeddings/*.pt") == []:

        hf_hub_download(
            repo_id="EvilEngine/badhandv4",
            filename="badhandv4.pt",
            local_dir="./include/embeddings/",
        )
        # hf_hub_download(
        #     repo_id="segments-arnaud/sam_vit_b",
        #     filename="EasyNegative.safetensors",
        #     local_dir="./include/embeddings/",
        # )
    if glob.glob("./include/vae_approx/taesd_decoder.safetensors") == []:

        hf_hub_download(
            repo_id="madebyollin/taesd",
            filename="taesd_decoder.safetensors",
            local_dir="./include/vae_approx/",
        )
    
    # Check and download Flux2 components if user wants to use them
    # This is optional - only downloads if the directories don't exist
    # Users can manually trigger by calling CheckAndDownloadFlux2()