File size: 19,567 Bytes
590c961
42da4ab
 
590c961
 
 
42da4ab
590c961
 
 
 
 
 
 
 
 
 
 
 
42da4ab
590c961
42da4ab
 
 
 
1b4ee18
7329201
 
 
1b4ee18
42da4ab
590c961
42da4ab
 
 
 
590c961
42da4ab
1b4ee18
ce2d1d1
 
971e2ea
 
ce2d1d1
 
 
 
8ded0be
ce2d1d1
 
 
 
 
 
 
 
 
 
 
590c961
 
1b4ee18
 
590c961
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9001d85
590c961
 
 
 
 
 
75c9a38
590c961
 
82e01b7
42da4ab
1b4ee18
 
8ded0be
82e01b7
42da4ab
8ded0be
82e01b7
 
42da4ab
 
 
 
 
 
 
 
 
82e01b7
546a048
 
8ded0be
42da4ab
 
82e01b7
 
8ded0be
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42da4ab
 
 
 
82e01b7
8e7014d
 
42da4ab
8ded0be
 
 
 
 
 
 
 
 
42da4ab
 
 
82e01b7
42da4ab
8ded0be
 
8e7014d
42da4ab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
82e01b7
42da4ab
 
f8f4e07
 
 
 
42da4ab
 
 
546a048
 
 
 
 
 
42da4ab
 
82e01b7
42da4ab
 
 
 
 
 
 
82e01b7
 
8e7014d
1b4ee18
82e01b7
 
1b4ee18
82e01b7
42da4ab
 
 
 
 
 
1b4ee18
42da4ab
82e01b7
42da4ab
 
 
82e01b7
 
42da4ab
 
 
 
 
 
 
 
1b4ee18
42da4ab
 
 
8e7014d
42da4ab
 
 
 
82e01b7
42da4ab
 
 
 
 
 
 
 
 
 
 
546a048
 
 
 
 
 
42da4ab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
82e01b7
42da4ab
 
 
8e7014d
 
 
228d01e
8e7014d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42da4ab
 
 
 
 
590c961
 
8e7014d
 
590c961
42da4ab
590c961
42da4ab
590c961
 
82e01b7
42da4ab
 
 
 
 
 
 
 
 
8ded0be
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1b4ee18
42da4ab
 
 
590c961
 
42da4ab
 
 
 
 
590c961
 
1b4ee18
590c961
 
82e01b7
3eb3a02
 
 
42da4ab
3eb3a02
 
 
 
 
590c961
3eb3a02
 
 
 
 
590c961
82e01b7
42da4ab
3eb3a02
 
 
 
 
590c961
 
 
1b4ee18
590c961
42da4ab
590c961
 
1b4ee18
42da4ab
 
 
 
 
 
 
 
 
 
1b4ee18
42da4ab
1b4ee18
42da4ab
 
590c961
42da4ab
590c961
42da4ab
590c961
42da4ab
 
 
 
590c961
42da4ab
1b4ee18
42da4ab
1b4ee18
 
 
 
42da4ab
 
 
 
 
 
 
 
 
1b4ee18
42da4ab
1b4ee18
82e01b7
42da4ab
82e01b7
590c961
42da4ab
1b4ee18
42da4ab
 
 
 
 
 
1b4ee18
 
 
590c961
 
42da4ab
 
7b9dad8
590c961
42da4ab
 
 
590c961
7b9dad8
42da4ab
590c961
1b4ee18
590c961
42da4ab
 
 
 
82e01b7
 
590c961
42da4ab
 
 
 
 
8ded0be
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
#!/usr/bin/env bash
# setup_comfyui_wan22.sh
# Script baseado no CODE1 com modelos/nodes do CODE2 e melhorias de robustez

set -euo pipefail

# Cores para output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'

log_info() { echo -e "${BLUE}[INFO]${NC} $1"; }
log_success() { echo -e "${GREEN}[✓]${NC} $1"; }
log_warn() { echo -e "${YELLOW}[!]${NC} $1"; }
log_error() { echo -e "${RED}[✗]${NC} $1"; }

# Configuração
COMFY_DIR="${COMFY_DIR:-/root/comfy/ComfyUI}"
MODELS_DIR="$COMFY_DIR/models"
COMFY_HOST="${COMFY_HOST:-0.0.0.0}"
COMFY_PORT="${COMFY_PORT:-8818}"
CIVITAI_TOKEN="${CIVITAI_TOKEN:-}"
HF_TOKEN="${HF_TOKEN:-}"

# Suprimir avisos do pip quando executado como root
export PIP_ROOT_USER_ACTION=ignore

# Performance
export MAX_JOBS="${MAX_JOBS:-32}"
export HF_HUB_ENABLE_HF_TRANSFER=1
export HF_TRANSFER_CONCURRENCY="${HF_TRANSFER_CONCURRENCY:-16}"
export NVCC_APPEND_FLAGS="${NVCC_APPEND_FLAGS:---threads 8}"
export UV_SYSTEM_PYTHON=1
export PYTORCH_CUDA_ALLOC_CONF="expandable_segments:True"

# Lista de downloads (formato: URL|TIPO|NOME_OPCIONAL)
DOWNLOAD_FILES=(
    "https://huggingface.co/Kijai/WanVideo_comfy_fp8_scaled/resolve/main/I2V/Wan2_2-I2V-A14B-LOW_fp8_e4m3fn_scaled_KJ.safetensors|diffusion_models|"
    "https://huggingface.co/Kijai/WanVideo_comfy_fp8_scaled/resolve/main/I2V/Wan2_2-I2V-A14B-HIGH_fp8_e4m3fn_scaled_KJ.safetensors|diffusion_models|"
    "https://huggingface.co/jrewingwannabe/Wan2.2-Lightning_I2V-A14B-4steps-lora/resolve/main/Wan2.2-Lightning_I2V-A14B-4steps-lora_HIGH_fp16.safetensors|loras|"
    "https://huggingface.co/jrewingwannabe/Wan2.2-Lightning_I2V-A14B-4steps-lora/resolve/main/Wan2.2-Lightning_I2V-A14B-4steps-lora_LOW_fp16.safetensors|loras|"
    "https://huggingface.co/Kijai/WanVideo_comfy/resolve/main/LoRAs/AniSora/Wan2_2_I2V_AniSora_3_2_HIGH_rank_64_fp16.safetensors|loras|"
    "https://huggingface.co/Kijai/WanVideo_comfy/resolve/main/Wan2_1_VAE_bf16.safetensors|vae|"
    "https://huggingface.co/Kijai/WanVideo_comfy/resolve/main/open-clip-xlm-roberta-large-vit-huge-14_visual_fp16.safetensors|clip_vision|"
    "https://huggingface.co/Kijai/WanVideo_comfy/resolve/main/umt5-xxl-enc-bf16.safetensors|text_encoders|"
    "https://huggingface.co/ABDALLALSWAITI/Upscalers/resolve/main/general/Swin2SR_ClassicalSR_X2_64.pth|upscale_models|"
    "mega://https://mega.nz/file/QFxREJpZ#0dge8TyRgb52lMFKdsk-Ac-rwcoG49hvBHvaqNsZkuk|loras|"
    "mega://https://mega.nz/file/lIhQETjT#nKlQ01Euw6fUxo8P5bfwlQrxAkWEedXQQOqHv5u4yrM|loras|"
    "mega://https://mega.nz/file/UIAESACY#Qlei1Pj5Nwno3Sz-wsTekvD-YgCkN1A5QbFKoRHXG9E|loras|"
    "mega://https://mega.nz/file/cdAg1Q7D#tQVrer2sB-M7XdYiKAqr7kRRfJw4pXCdliOg2aPJUGE|loras|"
    "mega://https://mega.nz/file/5RQFiAAJ#QCWrHkuJjXlFufR18g5R9YXJGqJ1pQp7xbqNRgSYUjc|loras|"
    "mega://https://mega.nz/file/tABgxLTS#NNlPV8NPc7R52MIjABMFUb41EOFDxFoszBxOrPMN13Q|loras|"
    "mega://https://mega.nz/file/4MxBgJ5Y#R8OSby0u-t-QFBnpnOtdjN3xuiVRvHa4_OEzTwWoGKQ|loras|"
    "mega://https://mega.nz/file/dJJxiSRR#o3XsYfSvb04xg7gNVdfHb3XTVHgvx0ZyXe9N8VQ9Lfw|loras|"
    "mega://https://mega.nz/file/wcAFhRJZ#ifZcckts0wPq1FG693tYLkpFrvjXvab1ZQeL6DuL1T8|loras|"
    "mega://https://mega.nz/file/wNIgmRwb#vn6rKM3QuTDpWVTLEBDsKjIkNkaX2wLKeauKNeVkcFs|loras|"
    "mega://https://mega.nz/file/hZhhhCQS#4je0Im2g1kEvTCuPotjVWkA-g6XICkqMb-mKxMt6R3Y|loras|"
)

# Custom nodes (do CODE2)
CUSTOM_NODES=(
    "https://github.com/kijai/ComfyUI-Florence2"
    "https://github.com/adbrasi/groqrouter"
    "https://github.com/kijai/ComfyUI-WanVideoWrapper"
    "https://github.com/Fannovel16/ComfyUI-Frame-Interpolation"
    "https://github.com/kijai/ComfyUI-GIMM-VFI"
    "https://github.com/Kosinkadink/ComfyUI-VideoHelperSuite"
    "https://github.com/kijai/ComfyUI-KJNodes"
    "https://github.com/Fannovel16/comfyui_controlnet_aux"
    "https://github.com/Artificial-Sweetener/comfyui-WhiteRabbit"
    "https://github.com/shiimizu/ComfyUI_smZNodes"
    "https://github.com/CoreyCorza/ComfyUI-CRZnodes"
    "https://github.com/pythongosssss/ComfyUI-Custom-Scripts"
    "https://github.com/pythongosssss/ComfyUI-WD14-Tagger"
    "https://github.com/WASasquatch/was-node-suite-comfyui"
    "https://github.com/justUmen/Bjornulf_custom_nodes"
    "https://github.com/mingsky-ai/ComfyUI-MingNodes"
    "https://github.com/numz/ComfyUI-SeedVR2_VideoUpscaler"
    "https://github.com/1038lab/ComfyUI-RMBG"
    "https://github.com/adbrasi/upavidito"
    "https://github.com/wallish77/wlsh_nodes"
    "https://github.com/yuvraj108c/ComfyUI-Upscaler-Tensorrt"
    "https://github.com/raindrop313/ComfyUI-WanVideoStartEndFrames"
    "https://github.com/MushroomFleet/DJZ-Nodes"
    "https://github.com/Suzie1/ComfyUI_Comfyroll_CustomNodes"
    "https://github.com/grmchn/ComfyUI-ProportionChanger"
    "https://github.com/MoonGoblinDev/Civicomfy"
)

download_hf() {
    local hf_path="$1"
    local target_dir="$2"
    local filename="$3"
    local revision="${4:-}"

    if [[ -z "$filename" ]]; then
        filename="$(basename "${hf_path%%\?*}")"
    fi

    mkdir -p "$target_dir"
    if [[ -f "$target_dir/$filename" ]]; then
        local existing_size
        existing_size=$(stat -c%s "$target_dir/$filename" 2>/dev/null || echo 0)
        if [[ "$existing_size" -gt 1048576 ]]; then
            log_success "Já existe: $filename"
            return 0
        fi
    fi

    local owner="${hf_path%%/*}"
    local remainder="${hf_path#*/}"
    if [[ "$owner" == "$hf_path" ]]; then
        log_error "Caminho HuggingFace inválido: $hf_path"
        return 1
    fi

    local repo_segment="${remainder%%/*}"
    if [[ "$repo_segment" == "$remainder" ]]; then
        log_error "Arquivo não especificado no caminho: $hf_path"
        return 1
    fi

    remainder="${remainder#*/}"
    if [[ -z "$remainder" ]]; then
        log_error "Arquivo não especificado no caminho: $hf_path"
        return 1
    fi

    if [[ "$repo_segment" == *@* ]]; then
        if [[ -z "$revision" ]]; then
            revision="${repo_segment#*@}"
        fi
        repo_segment="${repo_segment%@*}"
    fi

    local repo_id="$owner/$repo_segment"
    local file_path="$remainder"
    local attempt success temp_dir
    for attempt in 1 2 3; do
        temp_dir="$(mktemp -d)"
        success=false

        log_info "[HF] Tentativa $attempt/3: ${repo_id}/${file_path}${revision:+ @}${revision:+$revision}"

        if command -v hf >/dev/null 2>&1; then
            local hf_cmd=(hf download "$repo_id" "$file_path")
            if [[ -n "$revision" ]]; then
                hf_cmd+=(--revision "$revision")
            fi
            hf_cmd+=(--local-dir "$temp_dir" --quiet)
            if [[ -n "$HF_TOKEN" ]]; then
                hf_cmd+=(--token "$HF_TOKEN")
            fi
            if HF_HUB_ENABLE_HF_TRANSFER=1 "${hf_cmd[@]}"; then
                success=true
            fi
        fi

        if [[ "$success" == false ]]; then
            local fallback_revision="${revision:-main}"
            local direct_url="https://huggingface.co/${repo_id}/resolve/${fallback_revision}/${file_path}"
            log_warn "[HF] Tentativa $attempt usando fallback HTTP"
            if command -v aria2c >/dev/null 2>&1; then
                local aria_cmd=(aria2c -c -s 16 -x 16 -k 1M --console-log-level=warn --summary-interval=10 --dir "$temp_dir" --out "$filename")
                if [[ -n "$HF_TOKEN" ]]; then
                    aria_cmd+=(--header="Authorization: Bearer $HF_TOKEN")
                fi
                aria_cmd+=("$direct_url")
                if "${aria_cmd[@]}"; then
                    success=true
                fi
            fi
            if [[ "$success" == false ]] && command -v curl >/dev/null 2>&1; then
                local curl_cmd=(curl -L -# -o "$temp_dir/$filename")
                if [[ -n "$HF_TOKEN" ]]; then
                    curl_cmd+=(-H "Authorization: Bearer $HF_TOKEN")
                fi
                curl_cmd+=("$direct_url")
                if "${curl_cmd[@]}"; then
                    success=true
                fi
            fi
            if [[ "$success" == false ]] && command -v wget >/dev/null 2>&1; then
                local wget_cmd=(wget -q --show-progress -c -O "$temp_dir/$filename")
                if [[ -n "$HF_TOKEN" ]]; then
                    wget_cmd+=(--header="Authorization: Bearer $HF_TOKEN")
                fi
                wget_cmd+=("$direct_url")
                if "${wget_cmd[@]}"; then
                    success=true
                fi
            fi
        fi

        if [[ "$success" == true ]]; then
            local downloaded
            downloaded="$(find "$temp_dir" -type f -name "$filename" -print -quit)"
            if [[ -z "$downloaded" ]]; then
                downloaded="$(find "$temp_dir" -type f -print -quit)"
            fi
            if [[ -n "$downloaded" ]]; then
                mv "$downloaded" "$target_dir/$filename"
                rm -rf "$temp_dir"
                if [[ -s "$target_dir/$filename" ]]; then
                    log_success "Download concluído: $filename"
                    return 0
                fi
                log_warn "Arquivo vazio após download: $filename"
                rm -f "$target_dir/$filename"
            fi
        fi

        rm -rf "$temp_dir"
        log_warn "Tentativa $attempt falhou para $filename"
        sleep $((2 * attempt))
    done

    log_error "Falha ao baixar: $filename"
    return 1
}


download_file() {
    local url="$1"
    local target_dir="$2"
    local filename="$3"

    if [[ "$url" == *"civitai.com/api/download"* ]] && [[ "$url" != *"token="* ]] && [[ -n "$CIVITAI_TOKEN" ]]; then
        if [[ "$url" == *"?"* ]]; then
            url="${url}&token=${CIVITAI_TOKEN}"
        else
            url="${url}?token=${CIVITAI_TOKEN}"
        fi
    fi
    mkdir -p "$target_dir"

    local final_name="$filename"
    if [[ -z "$final_name" ]]; then
        final_name="$(basename "${url%%\?*}")"
    fi

    if [[ -f "$target_dir/$final_name" ]]; then
        local existing_size
        existing_size=$(stat -c%s "$target_dir/$final_name" 2>/dev/null || echo 0)
        if [[ "$existing_size" -gt 524288 ]]; then
            log_success "Já existe: $final_name"
            return 0
        fi
    fi

    local attempt success
    for attempt in 1 2 3; do
        success=false
        log_info "[DL] Tentativa $attempt/3: ${final_name}"
        if command -v aria2c >/dev/null 2>&1; then
            if aria2c -c -s 16 -x 16 -k 1M --console-log-level=warn --summary-interval=10 --dir "$target_dir" --out "$final_name" "$url"; then
                success=true
            fi
        fi
        if [[ "$success" == false ]] && command -v wget >/dev/null 2>&1; then
            if wget -q --show-progress -c -O "$target_dir/$final_name" "$url"; then
                success=true
            fi
        fi
        if [[ "$success" == false ]] && command -v curl >/dev/null 2>&1; then
            if curl -L -# -C - -o "$target_dir/$final_name" "$url"; then
                success=true
            fi
        fi
        if [[ "$success" == true ]]; then
            if [[ -s "$target_dir/$final_name" ]]; then
                log_success "Download concluído: $final_name"
                return 0
            fi
            log_warn "Arquivo vazio após download: $final_name"
            rm -f "$target_dir/$final_name"
        fi
        log_warn "Tentativa $attempt falhou para $final_name"
        sleep $((2 * attempt))
    done

    log_error "Falha ao baixar: $final_name"
    return 1
}

download_mega() {
    local url="$1"
    local target_dir="$2"

    url="${url#mega://}"
    mkdir -p "$target_dir"

    if ! command -v megadl >/dev/null 2>&1; then
        log_warn "megadl não encontrado; tente instalar megatools manualmente"
        return 1
    fi

    local attempt
    for attempt in 1 2 3; do
        log_info "[MEGA] Tentativa $attempt/3"
        local temp_dir
        temp_dir="$(mktemp -d)"
        if megadl --limit-speed 0 --path "$temp_dir" "$url"; then
            local moved=0
            while IFS= read -r -d '' file; do
                if [[ ! -f "$file" ]]; then
                    continue
                fi
                local base
                base="$(basename "$file")"
                if [[ -f "$target_dir/$base" ]]; then
                    log_success "Já existe: $base"
                else
                    if mv "$file" "$target_dir/$base"; then
                        log_success "Download Mega.nz concluído: $base"
                    else
                        log_warn "Falha ao mover $base para destino final"
                        continue
                    fi
                fi
                moved=1
            done < <(find "$temp_dir" -type f -print0)

            rm -rf "$temp_dir"
            if (( moved )); then
                return 0
            fi
            log_warn "[MEGA] Nenhum arquivo detectado após download"
            sleep $((2 * attempt))
        else
            log_warn "Tentativa $attempt falhou para Mega.nz"
            rm -rf "$temp_dir"
            sleep $((2 * attempt))
        fi
    done

    log_error "Falha ao baixar de Mega.nz: $url"
    return 1
}



process_downloads() {
    local failed=0
    for entry in "${DOWNLOAD_FILES[@]}"; do
        IFS='|' read -r raw_url type filename <<< "$entry"
        local target_dir="$MODELS_DIR/$type"
        mkdir -p "$target_dir"

        if [[ "$raw_url" == mega://* ]]; then
            if ! download_mega "$raw_url" "$target_dir"; then
                ((failed++))
            fi
        elif [[ "$raw_url" == hf://* ]]; then
            local hf_path="${raw_url#hf://}"
            if ! download_hf "$hf_path" "$target_dir" "$filename"; then
                ((failed++))
            fi
        elif [[ "$raw_url" == https://huggingface.co/* ]]; then
            local sanitized_url="${raw_url%%\?*}"
            local hf_relative="${sanitized_url#https://huggingface.co/}"
            IFS='/' read -ra hf_parts <<< "$hf_relative"
            if (( ${#hf_parts[@]} >= 4 )) && [[ "${hf_parts[2]}" == "resolve" || "${hf_parts[2]}" == "blob" || "${hf_parts[2]}" == "raw" ]]; then
                local hf_owner="${hf_parts[0]}"
                local hf_repo="${hf_parts[1]}"
                local hf_revision="${hf_parts[3]}"
                local start_index=4
                local hf_file_path=""
                for ((i=start_index; i<${#hf_parts[@]}; i++)); do
                    if [[ -n "$hf_file_path" ]]; then
                        hf_file_path+='/'
                    fi
                    hf_file_path+="${hf_parts[i]}"
                done
                if [[ -n "$hf_file_path" ]]; then
                    local hf_path_combined="${hf_owner}/${hf_repo}/${hf_file_path}"
                    if ! download_hf "$hf_path_combined" "$target_dir" "$filename" "$hf_revision"; then
                        ((failed++))
                    fi
                else
                    if ! download_file "$raw_url" "$target_dir" "$filename"; then
                        ((failed++))
                    fi
                fi
            else
                if ! download_file "$raw_url" "$target_dir" "$filename"; then
                    ((failed++))
                fi
            fi
        else
            if ! download_file "$raw_url" "$target_dir" "$filename"; then
                ((failed++))
            fi
        fi
    done

    if (( failed > 0 )); then
        return 1
    fi
    return 0
}

clone_repo() {
    local url="$1"
    local dest="$2"

    local name
    name="$(basename "$dest")"

    if [[ -d "$dest/.git" ]]; then
        if ! git -C "$dest" pull --ff-only --no-rebase >/dev/null 2>&1; then
            log_warn "Falha ao atualizar $name"
        else
            log_success "Atualizado: $name"
        fi
    else
        if ! git clone --depth 1 "$url" "$dest" >/dev/null 2>&1; then
            log_warn "Falha ao clonar $name"
            return
        fi
        log_success "Clonado: $name"
    fi

    if [[ -f "$dest/requirements.txt" ]]; then
        if ! pip install -q -r "$dest/requirements.txt" >/dev/null 2>&1; then
            log_warn "Falha ao instalar requirements de $name"
        else
            log_info "Dependências instaladas para $name"
        fi
    fi
}

# ========== INSTALAÇÃO ==========
log_info "========================================="
log_info " ComfyUI + Wan 2.2 Setup"
log_info "========================================="

# 1. Dependências
log_info "[1/6] Instalando dependências do sistema..."
if command -v apt-get >/dev/null 2>&1; then
    apt-get update -qq || log_warn "apt-get update falhou; seguindo assim mesmo"
    apt-get install -y -qq aria2 megatools git wget curl || log_warn "apt-get install retornou erro"
else
    log_warn "apt-get indisponível; certifique-se de que aria2, megatools, git, wget e curl estão instalados"
fi
pip install --upgrade pip wheel setuptools -q || log_warn "Falha na atualização do pip"
pip install --upgrade "huggingface_hub[cli,hf_transfer]" comfy-cli -q || log_warn "Falha instalando huggingface_hub/comfy-cli"
log_success "Dependências tratadas"

# 2. Instalar ComfyUI
log_info "[2/6] Instalando ComfyUI..."
if [[ -f "$COMFY_DIR/main.py" ]]; then
    log_warn "ComfyUI já presente"
else
    comfy --skip-prompt install --fast-deps --nvidia || log_error "Instalação do ComfyUI falhou"
fi
log_success "ComfyUI pronto"

# 3. Instalar PyTorch (CUDA 12.8)
log_info "[3/6] Instalando PyTorch CUDA 12.8..."
python3 -m pip install --upgrade torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu128 || log_error "Falha ao instalar PyTorch"
log_success "PyTorch configurado"

# 4. Instalar SageAttention (condicional)
log_info "[4/6] Instalando SageAttention..."
if python3 - <<'PY' >/dev/null 2>&1
import torch, sys
sys.exit(0 if torch.cuda.is_available() else 1)
PY
then
    if python3 -m pip install "https://huggingface.co/adbrasi/comfywheel/resolve/main/sageattention-2.2.0-cp311-cp311-linux_x86_64.whl" -q; then
        if python3 - <<'PY' >/dev/null 2>&1
import sageattention
PY
        then
            log_success "SageAttention instalado"
        else
            log_warn "SageAttention instalado, mas não pôde ser importado"
        fi
    else
        log_warn "Falha ao instalar SageAttention"
    fi
else
    log_warn "CUDA indisponível; pulando SageAttention"
fi

# 5. Downloads de modelos
log_info "[5/6] Baixando modelos..."
mkdir -p "$MODELS_DIR"
if process_downloads; then
    log_success "Downloads concluídos (ou já presentes)"
else
    log_warn "Alguns downloads falharam; reexecute o script para tentar novamente"
fi

# 6. Custom nodes
log_info "[6/6] Instalando custom nodes..."
CN_DIR="$COMFY_DIR/custom_nodes"
mkdir -p "$CN_DIR"
node_total=${#CUSTOM_NODES[@]}
node_index=0
set +e
for repo in "${CUSTOM_NODES[@]}"; do
    ((node_index++))
    log_info "[$node_index/$node_total] $(basename "$repo")"
    clone_repo "$repo" "$CN_DIR/$(basename "$repo")"
done
set -e
log_success "Custom nodes processados"


SAGE_FLAG=""
if python3 - <<'PY' >/dev/null 2>&1
import sageattention
PY
then
    SAGE_FLAG="--use-sage-attention"
fi

log_info "========================================="
log_info "Iniciando ComfyUI em http://${COMFY_HOST}:${COMFY_PORT}"
log_info "========================================="

cd "$COMFY_DIR"
exec comfy launch -- ${SAGE_FLAG:+$SAGE_FLAG} --listen "$COMFY_HOST" --port "$COMFY_PORT"