adbrasi commited on
Commit
42da4ab
·
verified ·
1 Parent(s): 1b4ee18

Update wanForProduction.sh

Browse files
Files changed (1) hide show
  1. wanForProduction.sh +283 -171
wanForProduction.sh CHANGED
@@ -1,13 +1,10 @@
1
  #!/usr/bin/env bash
2
- # setup_comfyui_simple.sh (base do CODE1, ajustado)
3
- # - troca de modelos/nodes pelos do CODE2
4
- # - adiciona SageAttention
5
- # - remove tratamento 5090/Blackwell
6
- # - melhora download_hf para paths com subpastas e /resolve/
7
 
8
  set -euo pipefail
9
 
10
- # Cores
11
  RED='\033[0;31m'
12
  GREEN='\033[0;32m'
13
  YELLOW='\033[1;33m'
@@ -20,44 +17,40 @@ log_warn() { echo -e "${YELLOW}[!]${NC} $1"; }
20
  log_error() { echo -e "${RED}[✗]${NC} $1"; }
21
 
22
  # Configuração
23
- COMFY_DIR="/root/comfy/ComfyUI"
24
  MODELS_DIR="$COMFY_DIR/models"
25
- COMFY_HOST="0.0.0.0"
26
- COMFY_PORT="8818"
27
- CIVITAI_TOKEN="4fcb2834969399006a736ee402b061e5"
 
28
 
29
  # Performance
30
- export MAX_JOBS=32
31
  export HF_HUB_ENABLE_HF_TRANSFER=1
 
 
 
 
32
 
33
- # ===============================
34
- # Lista de downloads (do CODE2)
35
- # ===============================
36
- # Formato: "URL|TIPO|NOME_OPCIONAL"
37
  DOWNLOAD_FILES=(
38
- # Wan 2.2 (Kijai) - use URLs SEM /resolve/ para usar o CLI da HF
39
- "https://huggingface.co/Kijai/WanVideo_comfy/Wan22-Lightning/Wan2.2-Lightning_T2V-v1.1-A14B-4steps-lora_HIGH_fp16.safetensors|loras|Wan2.2-Lightning_T2V-v1.1-A14B-4steps-lora_HIGH_fp16.safetensors"
40
- "https://huggingface.co/Kijai/WanVideo_comfy/Wan22-Lightning/Wan2.2-Lightning_T2V-v1.1-A14B-4steps-lora_LOW_fp16.safetensors|loras|Wan2.2-Lightning_T2V-v1.1-A14B-4steps-lora_LOW_fp16.safetensors"
41
- "https://huggingface.co/Kijai/WanVideo_comfy_fp8_scaled/I2V/Wan2_2-I2V-A14B-HIGH_fp8_e5m2_scaled_KJ.safetensors|diffusion_models|Wan2_2-I2V-A14B-HIGH_fp8_e5m2_scaled_KJ.safetensors"
42
- "https://huggingface.co/Kijai/WanVideo_comfy_fp8_scaled/I2V/Wan2_2-I2V-A14B-LOW_fp8_e5m2_scaled_KJ.safetensors|diffusion_models|Wan2_2-I2V-A14B-LOW_fp8_e5m2_scaled_KJ.safetensors"
43
- "https://huggingface.co/Kijai/WanVideo_comfy/Wan22-Lightning/Wan2.2-Lightning_I2V-A14B-4steps-lora_HIGH_fp16.safetensors|loras|Wan2.2-Lightning_I2V-A14B-4steps-lora_HIGH_fp16.safetensors"
44
- "https://huggingface.co/Kijai/WanVideo_comfy/Wan22-Lightning/Wan2.2-Lightning_I2V-A14B-4steps-lora_LOW_fp16.safetensors|loras|Wan2.2-Lightning_I2V-A14B-4steps-lora_LOW_fp16.safetensors"
45
- "https://huggingface.co/Kijai/WanVideo_comfy/Wan2_1_VAE_fp32.safetensors|vae|Wan2_1_VAE_fp32.safetensors"
46
- "https://huggingface.co/Kijai/WanVideo_comfy/umt5-xxl-enc-bf16.safetensors|text_encoders|umt5-xxl-enc-bf16.safetensors"
47
-
48
- # Wan 2.1
49
- "https://huggingface.co/Kijai/WanVideo_comfy/Lightx2v/lightx2v_I2V_14B_480p_cfg_step_distill_rank256_bf16.safetensors|loras|lightx2v_I2V_14B_480p_cfg_step_distill_rank256_bf16.safetensors"
50
- "https://huggingface.co/Comfy-Org/Wan_2.2_ComfyUI_Repackaged/split_files/vae/wan_2.1_vae.safetensors|vae|wan_2.1_vae.safetensors"
51
- "https://huggingface.co/Comfy-Org/Wan_2.2_ComfyUI_Repackaged/split_files/text_encoders/umt5_xxl_fp16.safetensors|text_encoders|umt5_xxl_fp16.safetensors"
52
-
53
- # Wan 2.1 clip vision
54
- "https://huggingface.co/Kijai/WanVideo_comfy/open-clip-xlm-roberta-large-vit-huge-14_visual_fp32.safetensors|clip_vision|open-clip-xlm-roberta-large-vit-huge-14_visual_fp32.safetensors"
55
- "https://huggingface.co/Comfy-Org/Wan_2.1_ComfyUI_repackaged/split_files/clip_vision/clip_vision_h.safetensors|clip_vision|clip_vision_h.safetensors"
56
-
57
- # Upscaler extra (com /resolve/ para baixar via HTTP direto)
58
- "https://huggingface.co/ABDALLALSWAITI/Upscalers/resolve/main/anime/2x-AnimeSharpV2_MoSR_Soft.pth|upscale_models|2x-AnimeSharpV2_MoSR_Soft.pth"
59
-
60
- # LoRAs via Mega.nz (do CODE2) — prefixo mega:// para usar função existente
61
  "mega://https://mega.nz/file/QFxREJpZ#0dge8TyRgb52lMFKdsk-Ac-rwcoG49hvBHvaqNsZkuk|loras|"
62
  "mega://https://mega.nz/file/lIhQETjT#nKlQ01Euw6fUxo8P5bfwlQrxAkWEedXQQOqHv5u4yrM|loras|"
63
  "mega://https://mega.nz/file/UIAESACY#Qlei1Pj5Nwno3Sz-wsTekvD-YgCkN1A5QbFKoRHXG9E|loras|"
@@ -69,15 +62,11 @@ DOWNLOAD_FILES=(
69
  "mega://https://mega.nz/file/wcAFhRJZ#ifZcckts0wPq1FG693tYLkpFrvjXvab1ZQeL6DuL1T8|loras|"
70
  "mega://https://mega.nz/file/wNIgmRwb#vn6rKM3QuTDpWVTLEBDsKjIkNkaX2wLKeauKNeVkcFs|loras|"
71
  "mega://https://mega.nz/file/hZhhhCQS#4je0Im2g1kEvTCuPotjVWkA-g6XICkqMb-mKxMt6R3Y|loras|"
72
-
73
- # Upscale via Mega.nz (do CODE2)
74
  "mega://https://mega.nz/file/qdQwAJSC#rPY2Hg4x-_RbHiHUBFW-YU8xZUqPpq5Gpl1uPFiJvW4|upscale_models|"
75
  "mega://https://mega.nz/file/KIh2marA#CehVNXxQhGfp1bqUL0K1YPWNqrCUoLPCZGq2W6XqmWo|upscale_models|"
76
  )
77
 
78
- # ===============================
79
  # Custom nodes (do CODE2)
80
- # ===============================
81
  CUSTOM_NODES=(
82
  "https://github.com/kijai/ComfyUI-Florence2"
83
  "https://github.com/adbrasi/groqrouter"
@@ -105,63 +94,101 @@ CUSTOM_NODES=(
105
  "https://github.com/grmchn/ComfyUI-ProportionChanger"
106
  )
107
 
108
- # ===============================
109
- # Funções de download (mesmas do CODE1, porém download_hf melhorado)
110
- # ===============================
111
-
112
  download_hf() {
113
- local url="$1"
114
  local target_dir="$2"
115
  local filename="$3"
116
 
117
- if [ -n "$filename" ] && [ -f "$target_dir/$filename" ]; then
118
- log_success "Já existe: $filename"
119
- return 0
120
  fi
121
 
122
- log_info "Baixando HF: ${filename:-$(basename "$url")}"
 
 
 
 
 
 
 
 
123
 
124
- # Se a URL vier com /resolve/, usa HTTP direto (evita problemas com 'hf download')
125
- if [[ "$url" == *"/resolve/"* ]]; then
126
- mkdir -p "$target_dir"
127
- wget -q --show-progress -c -O "$target_dir/$filename" "https://huggingface.co/$url"
128
- return $?
129
  fi
130
 
131
- # Parse correto: repo_id = 2 primeiros segmentos; file_path = resto
132
- local repo_id file_path
133
- repo_id="$(echo "$url" | cut -d'/' -f1-2)"
134
- file_path="$(echo "$url" | cut -d'/' -f3-)"
 
135
 
136
- if command -v hf >/dev/null 2>&1; then
137
- mkdir -p "$target_dir"
138
- (cd "$target_dir" && HF_HUB_ENABLE_HF_TRANSFER=1 hf download "$repo_id" "$file_path" --local-dir . >/dev/null 2>&1) || {
139
- log_warn "hf download falhou; tentando HTTP"
140
- wget -q --show-progress -c -O "$target_dir/$filename" "https://huggingface.co/$repo_id/resolve/main/$file_path" || \
141
- log_error "Download falhou: $filename"
142
- }
143
- else
144
- mkdir -p "$target_dir"
145
- wget -q --show-progress -c -O "$target_dir/$filename" "https://huggingface.co/$repo_id/resolve/main/$file_path" || \
146
- log_error "Download falhou: $filename"
147
- fi
148
- }
149
 
150
- download_mega() {
151
- local url="$1"
152
- local target_dir="$2"
 
 
153
 
154
- url="${url#mega://}" # remove prefixo
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
155
 
156
- log_info "Baixando do Mega..."
 
 
 
 
 
 
 
 
 
157
 
158
- cd "$target_dir"
159
- if megadl "$url" 2>/dev/null; then
160
- log_success "Mega download OK"
161
- else
162
- log_warn "Mega download falhou"
163
- fi
164
- cd - >/dev/null
165
  }
166
 
167
  download_file() {
@@ -169,156 +196,241 @@ download_file() {
169
  local target_dir="$2"
170
  local filename="$3"
171
 
172
- # Adicionar token Civitai se necessário
173
- if [[ "$url" == *"civitai.com"* ]] && [[ "$url" != *"token="* ]]; then
174
- url="${url}&token=${CIVITAI_TOKEN}"
 
 
 
175
  fi
 
176
 
177
- if [ -n "$filename" ] && [ -f "$target_dir/$filename" ]; then
178
- log_success "Já existe: $filename"
179
- return 0
180
  fi
181
 
182
- log_info "Baixando: ${filename:-arquivo}"
 
 
 
 
 
 
 
183
 
184
- if command -v aria2c >/dev/null 2>&1; then
185
- if [ -n "$filename" ]; then
186
- aria2c -c -x 4 -s 4 --console-log-level=warn --dir="$target_dir" --out="$filename" "$url" || \
187
- wget -q --show-progress -c -O "$target_dir/$filename" "$url" || \
188
- log_error "Download falhou: $filename"
189
- else
190
- aria2c -c -x 4 -s 4 --console-log-level=warn --dir="$target_dir" "$url" || \
191
- (cd "$target_dir" && wget -q --show-progress -c "$url") || \
192
- log_error "Download falhou: $url"
193
  fi
194
- elif [ -n "$filename" ]; then
195
- wget -q --show-progress -c -O "$target_dir/$filename" "$url" || \
196
- log_error "Download falhou: $filename"
197
- else
198
- (cd "$target_dir" && wget -q --show-progress -c "$url") || \
199
- log_error "Download falhou: $url"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
200
  fi
 
 
 
 
 
 
 
 
 
 
 
 
 
201
  }
202
 
203
  process_downloads() {
 
204
  for entry in "${DOWNLOAD_FILES[@]}"; do
205
- IFS='|' read -r url type filename <<< "$entry"
206
-
207
  local target_dir="$MODELS_DIR/$type"
208
  mkdir -p "$target_dir"
209
 
210
- if [[ "$url" == mega://* ]]; then
211
- download_mega "$url" "$target_dir"
212
- elif [[ "$url" == *.huggingface.co/* ]]; then
213
- local hf_path="${url#*huggingface.co/}"
214
- download_hf "$hf_path" "$target_dir" "$filename"
 
 
 
 
215
  else
216
- download_file "$url" "$target_dir" "$filename"
 
 
217
  fi
218
  done
 
 
 
 
 
219
  }
220
 
221
  clone_repo() {
222
  local url="$1"
223
  local dest="$2"
224
 
225
- if [ -d "$dest/.git" ]; then
226
- git -C "$dest" pull --ff-only 2>/dev/null || true
227
  else
228
- git clone --depth 1 "$url" "$dest" 2>/dev/null || true
229
  fi
230
 
231
- if [ -f "$dest/requirements.txt" ]; then
232
- pip install -q -r "$dest/requirements.txt" 2>/dev/null || true
233
  fi
234
  }
235
 
236
  # ========== INSTALAÇÃO ==========
237
-
238
  log_info "========================================="
239
- log_info " ComfyUI Setup"
240
  log_info "========================================="
241
 
242
  # 1. Dependências
243
- log_info "[1/6] Instalando dependências..."
244
-
245
- apt-get update -qq
246
- apt-get install -y -qq aria2 megatools git wget curl 2>/dev/null
247
-
248
- pip install --upgrade pip wheel setuptools -q
249
- pip install --upgrade "huggingface_hub[cli,hf_transfer]" comfy-cli -q
250
-
251
- log_success "Dependências instaladas"
 
252
 
253
- # 2. ComfyUI
254
  log_info "[2/6] Instalando ComfyUI..."
255
-
256
- if [ -f "$COMFY_DIR/main.py" ]; then
257
- log_warn "ComfyUI já existe"
258
  else
259
- comfy --skip-prompt install --fast-deps --nvidia
260
  fi
 
261
 
262
- log_success "ComfyUI instalado"
 
 
 
263
 
264
- # 3. PyTorch CUDA 12.8 (substitui o bloco de Blackwell/5090 do CODE1)
265
- log_info "[3/6] Instalando PyTorch (CUDA 12.8)..."
266
- pip install --upgrade torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu128 -q
267
- log_success "PyTorch com CUDA 12.8 instalado"
268
-
269
- # 4. SageAttention (do CODE2)
270
  log_info "[4/6] Instalando SageAttention..."
271
- if python3 - <<'PY' 2>/dev/null; then
272
  import torch, sys
273
  sys.exit(0 if torch.cuda.is_available() else 1)
274
  PY
275
  then
276
- pip install -q "https://huggingface.co/adbrasi/comfywheel/resolve/main/sageattention-2.2.0-cp311-cp311-linux_x86_64.whl" || log_warn "Falha ao instalar SageAttention"
277
- if python3 -c 'import sageattention' >/dev/null 2>&1; then
278
- log_success "SageAttention instalado"
 
 
 
 
 
 
279
  else
280
- log_warn "SageAttention não pôde ser importado (seguindo sem)"
281
  fi
282
  else
283
- log_warn "CUDA não detectada, pulando SageAttention"
284
  fi
285
 
286
- # 5. Modelos
287
  log_info "[5/6] Baixando modelos..."
288
- process_downloads
289
- log_success "Downloads processados"
 
 
 
 
290
 
291
  # 6. Custom nodes
292
  log_info "[6/6] Instalando custom nodes..."
293
-
294
  CN_DIR="$COMFY_DIR/custom_nodes"
295
  mkdir -p "$CN_DIR"
296
-
297
- total_nodes=${#CUSTOM_NODES[@]}
298
- current_node=0
299
-
300
  for repo in "${CUSTOM_NODES[@]}"; do
301
- ((current_node++))
302
- node_name=$(basename "$repo")
303
- log_info "[$current_node/$total_nodes] Instalando: $node_name"
304
- clone_repo "$repo" "$CN_DIR/$node_name"
305
  done
 
306
 
307
- log_success "Custom nodes instalados"
308
-
309
- # Iniciar
310
- log_info "========================================="
311
- log_success "Instalação concluída!"
312
- log_info "Iniciando ComfyUI..."
313
- log_info "URL: http://localhost:$COMFY_PORT"
314
  log_info "========================================="
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
315
 
316
- cd "$COMFY_DIR"
 
 
 
 
317
 
318
- # Habilita SageAttention se presente
319
  SAGE_FLAG=""
320
- if python3 -c 'import sageattention' >/dev/null 2>&1; then
 
 
 
321
  SAGE_FLAG="--use-sage-attention"
322
  fi
323
 
 
 
 
 
 
324
  exec comfy launch -- ${SAGE_FLAG:+$SAGE_FLAG} --listen "$COMFY_HOST" --port "$COMFY_PORT"
 
1
  #!/usr/bin/env bash
2
+ # setup_comfyui_wan22.sh
3
+ # Script baseado no CODE1 com modelos/nodes do CODE2 e melhorias de robustez
 
 
 
4
 
5
  set -euo pipefail
6
 
7
+ # Cores para output
8
  RED='\033[0;31m'
9
  GREEN='\033[0;32m'
10
  YELLOW='\033[1;33m'
 
17
  log_error() { echo -e "${RED}[✗]${NC} $1"; }
18
 
19
  # Configuração
20
+ COMFY_DIR="${COMFY_DIR:-/root/comfy/ComfyUI}"
21
  MODELS_DIR="$COMFY_DIR/models"
22
+ COMFY_HOST="${COMFY_HOST:-0.0.0.0}"
23
+ COMFY_PORT="${COMFY_PORT:-8818}"
24
+ CIVITAI_TOKEN="${CIVITAI_TOKEN:-}"
25
+ HF_TOKEN="${HF_TOKEN:-}"
26
 
27
  # Performance
28
+ export MAX_JOBS="${MAX_JOBS:-32}"
29
  export HF_HUB_ENABLE_HF_TRANSFER=1
30
+ export HF_TRANSFER_CONCURRENCY="${HF_TRANSFER_CONCURRENCY:-16}"
31
+ export NVCC_APPEND_FLAGS="${NVCC_APPEND_FLAGS:---threads 8}"
32
+ export UV_SYSTEM_PYTHON=1
33
+ export PYTORCH_CUDA_ALLOC_CONF="expandable_segments:True"
34
 
35
+ # Lista de downloads (formato: URL|TIPO|NOME_OPCIONAL)
 
 
 
36
  DOWNLOAD_FILES=(
37
+ # Wan 2.2 principais (HF)
38
+ "hf://Kijai/WanVideo_comfy/Wan22-Lightning/Wan2.2-Lightning_T2V-v1.1-A14B-4steps-lora_HIGH_fp16.safetensors|loras|Wan2.2-Lightning_T2V-v1.1-A14B-4steps-lora_HIGH_fp16.safetensors"
39
+ "hf://Kijai/WanVideo_comfy/Wan22-Lightning/Wan2.2-Lightning_T2V-v1.1-A14B-4steps-lora_LOW_fp16.safetensors|loras|Wan2.2-Lightning_T2V-v1.1-A14B-4steps-lora_LOW_fp16.safetensors"
40
+ "hf://Kijai/WanVideo_comfy_fp8_scaled/I2V/Wan2_2-I2V-A14B-HIGH_fp8_e5m2_scaled_KJ.safetensors|diffusion_models|Wan2_2-I2V-A14B-HIGH_fp8_e5m2_scaled_KJ.safetensors"
41
+ "hf://Kijai/WanVideo_comfy_fp8_scaled/I2V/Wan2_2-I2V-A14B-LOW_fp8_e5m2_scaled_KJ.safetensors|diffusion_models|Wan2_2-I2V-A14B-LOW_fp8_e5m2_scaled_KJ.safetensors"
42
+ "hf://Kijai/WanVideo_comfy/Wan22-Lightning/Wan2.2-Lightning_I2V-A14B-4steps-lora_HIGH_fp16.safetensors|loras|Wan2.2-Lightning_I2V-A14B-4steps-lora_HIGH_fp16.safetensors"
43
+ "hf://Kijai/WanVideo_comfy/Wan22-Lightning/Wan2.2-Lightning_I2V-A14B-4steps-lora_LOW_fp16.safetensors|loras|Wan2.2-Lightning_I2V-A14B-4steps-lora_LOW_fp16.safetensors"
44
+ "hf://Kijai/WanVideo_comfy/Wan2_1_VAE_fp32.safetensors|vae|Wan2_1_VAE_fp32.safetensors"
45
+ "hf://Kijai/WanVideo_comfy/umt5-xxl-enc-bf16.safetensors|text_encoders|umt5-xxl-enc-bf16.safetensors"
46
+ "hf://Kijai/WanVideo_comfy/Lightx2v/lightx2v_I2V_14B_480p_cfg_step_distill_rank256_bf16.safetensors|loras|lightx2v_I2V_14B_480p_cfg_step_distill_rank256_bf16.safetensors"
47
+ "hf://Comfy-Org/Wan_2.2_ComfyUI_Repackaged/split_files/vae/wan_2.1_vae.safetensors|vae|wan_2.1_vae.safetensors"
48
+ "hf://Comfy-Org/Wan_2.2_ComfyUI_Repackaged/split_files/text_encoders/umt5_xxl_fp16.safetensors|text_encoders|umt5_xxl_fp16.safetensors"
49
+ "hf://Kijai/WanVideo_comfy/open-clip-xlm-roberta-large-vit-huge-14_visual_fp32.safetensors|clip_vision|open-clip-xlm-roberta-large-vit-huge-14_visual_fp32.safetensors"
50
+ "hf://Comfy-Org/Wan_2.1_ComfyUI_repackaged/split_files/clip_vision/clip_vision_h.safetensors|clip_vision|clip_vision_h.safetensors"
51
+ "hf://ABDALLALSWAITI/Upscalers/anime/2x-AnimeSharpV2_MoSR_Soft.pth|upscale_models|2x-AnimeSharpV2_MoSR_Soft.pth"
52
+
53
+ # Wan 2.2 extras (Mega.nz)
 
 
 
 
 
 
54
  "mega://https://mega.nz/file/QFxREJpZ#0dge8TyRgb52lMFKdsk-Ac-rwcoG49hvBHvaqNsZkuk|loras|"
55
  "mega://https://mega.nz/file/lIhQETjT#nKlQ01Euw6fUxo8P5bfwlQrxAkWEedXQQOqHv5u4yrM|loras|"
56
  "mega://https://mega.nz/file/UIAESACY#Qlei1Pj5Nwno3Sz-wsTekvD-YgCkN1A5QbFKoRHXG9E|loras|"
 
62
  "mega://https://mega.nz/file/wcAFhRJZ#ifZcckts0wPq1FG693tYLkpFrvjXvab1ZQeL6DuL1T8|loras|"
63
  "mega://https://mega.nz/file/wNIgmRwb#vn6rKM3QuTDpWVTLEBDsKjIkNkaX2wLKeauKNeVkcFs|loras|"
64
  "mega://https://mega.nz/file/hZhhhCQS#4je0Im2g1kEvTCuPotjVWkA-g6XICkqMb-mKxMt6R3Y|loras|"
 
 
65
  "mega://https://mega.nz/file/qdQwAJSC#rPY2Hg4x-_RbHiHUBFW-YU8xZUqPpq5Gpl1uPFiJvW4|upscale_models|"
66
  "mega://https://mega.nz/file/KIh2marA#CehVNXxQhGfp1bqUL0K1YPWNqrCUoLPCZGq2W6XqmWo|upscale_models|"
67
  )
68
 
 
69
  # Custom nodes (do CODE2)
 
70
  CUSTOM_NODES=(
71
  "https://github.com/kijai/ComfyUI-Florence2"
72
  "https://github.com/adbrasi/groqrouter"
 
94
  "https://github.com/grmchn/ComfyUI-ProportionChanger"
95
  )
96
 
 
 
 
 
97
  download_hf() {
98
+ local hf_path="$1"
99
  local target_dir="$2"
100
  local filename="$3"
101
 
102
+ if [[ -z "$filename" ]]; then
103
+ filename="$(basename "$hf_path")"
 
104
  fi
105
 
106
+ mkdir -p "$target_dir"
107
+ if [[ -f "$target_dir/$filename" ]]; then
108
+ local existing_size
109
+ existing_size=$(stat -c%s "$target_dir/$filename" 2>/dev/null || echo 0)
110
+ if [[ "$existing_size" -gt 1048576 ]]; then
111
+ log_success "Já existe: $filename"
112
+ return 0
113
+ fi
114
+ fi
115
 
116
+ IFS='/' read -r owner repo rest <<< "$hf_path"
117
+ if [[ -z "$owner" || -z "$repo" || -z "$rest" ]]; then
118
+ log_error "Caminho HuggingFace inválido: $hf_path"
119
+ return 1
 
120
  fi
121
 
122
+ local repo_id="$owner/$repo"
123
+ local attempt success temp_dir
124
+ for attempt in 1 2 3; do
125
+ temp_dir="$(mktemp -d)"
126
+ success=false
127
 
128
+ if command -v hf >/dev/null 2>&1; then
129
+ if HF_HUB_ENABLE_HF_TRANSFER=1 hf download "$repo_id" "$rest" --local-dir "$temp_dir" --local-dir-use-symlinks False; then
130
+ success=true
131
+ fi
132
+ fi
 
 
 
 
 
 
 
 
133
 
134
+ if [[ "$success" == false ]] && command -v huggingface-cli >/dev/null 2>&1; then
135
+ if HF_HUB_ENABLE_HF_TRANSFER=1 huggingface-cli download "$repo_id" "$rest" --local-dir "$temp_dir" --local-dir-use-symlinks False; then
136
+ success=true
137
+ fi
138
+ fi
139
 
140
+ if [[ "$success" == false ]]; then
141
+ local direct_url="https://huggingface.co/${repo_id}/resolve/main/${rest}"
142
+ if command -v aria2c >/dev/null 2>&1; then
143
+ local aria_cmd=(aria2c -c -s 16 -x 16 -k 1M --console-log-level=warn --summary-interval=10 --dir "$temp_dir" --out "$filename")
144
+ if [[ -n "$HF_TOKEN" ]]; then
145
+ aria_cmd+=(--header="Authorization: Bearer $HF_TOKEN")
146
+ fi
147
+ aria_cmd+=("$direct_url")
148
+ if "${aria_cmd[@]}"; then
149
+ success=true
150
+ fi
151
+ fi
152
+ if [[ "$success" == false ]] && command -v curl >/dev/null 2>&1; then
153
+ local curl_cmd=(curl -L -# -o "$temp_dir/$filename")
154
+ if [[ -n "$HF_TOKEN" ]]; then
155
+ curl_cmd+=(-H "Authorization: Bearer $HF_TOKEN")
156
+ fi
157
+ curl_cmd+=("$direct_url")
158
+ if "${curl_cmd[@]}"; then
159
+ success=true
160
+ fi
161
+ fi
162
+ if [[ "$success" == false ]] && command -v wget >/dev/null 2>&1; then
163
+ local wget_cmd=(wget -q --show-progress -c -O "$temp_dir/$filename")
164
+ if [[ -n "$HF_TOKEN" ]]; then
165
+ wget_cmd+=(--header="Authorization: Bearer $HF_TOKEN")
166
+ fi
167
+ wget_cmd+=("$direct_url")
168
+ if "${wget_cmd[@]}"; then
169
+ success=true
170
+ fi
171
+ fi
172
+ fi
173
 
174
+ if [[ "$success" == true ]]; then
175
+ local downloaded
176
+ downloaded="$(find "$temp_dir" -maxdepth 1 -type f | head -n 1)"
177
+ if [[ -n "$downloaded" ]]; then
178
+ mv "$downloaded" "$target_dir/$filename"
179
+ rm -rf "$temp_dir"
180
+ log_success "Download concluído: $filename"
181
+ return 0
182
+ fi
183
+ fi
184
 
185
+ rm -rf "$temp_dir"
186
+ log_warn "Tentativa $attempt falhou para $filename"
187
+ sleep $((2 * attempt))
188
+ done
189
+
190
+ log_error "Falha ao baixar: $filename"
191
+ return 1
192
  }
193
 
194
  download_file() {
 
196
  local target_dir="$2"
197
  local filename="$3"
198
 
199
+ if [[ "$url" == *"civitai.com/api/download"* ]] && [[ "$url" != *"token="* ]] && [[ -n "$CIVITAI_TOKEN" ]]; then
200
+ if [[ "$url" == *"?"* ]]; then
201
+ url="${url}&token=${CIVITAI_TOKEN}"
202
+ else
203
+ url="${url}?token=${CIVITAI_TOKEN}"
204
+ fi
205
  fi
206
+ mkdir -p "$target_dir"
207
 
208
+ local final_name="$filename"
209
+ if [[ -z "$final_name" ]]; then
210
+ final_name="$(basename "${url%%\?*}")"
211
  fi
212
 
213
+ if [[ -f "$target_dir/$final_name" ]]; then
214
+ local existing_size
215
+ existing_size=$(stat -c%s "$target_dir/$final_name" 2>/dev/null || echo 0)
216
+ if [[ "$existing_size" -gt 524288 ]]; then
217
+ log_success "Já existe: $final_name"
218
+ return 0
219
+ fi
220
+ fi
221
 
222
+ local attempt success
223
+ for attempt in 1 2 3; do
224
+ success=false
225
+ if command -v aria2c >/dev/null 2>&1; then
226
+ if aria2c -c -s 16 -x 16 -k 1M --console-log-level=warn --summary-interval=10 --dir "$target_dir" --out "$final_name" "$url"; then
227
+ success=true
228
+ fi
 
 
229
  fi
230
+ if [[ "$success" == false ]] && command -v wget >/dev/null 2>&1; then
231
+ if wget -q --show-progress -c -O "$target_dir/$final_name" "$url"; then
232
+ success=true
233
+ fi
234
+ fi
235
+ if [[ "$success" == false ]] && command -v curl >/dev/null 2>&1; then
236
+ if curl -L -# -C - -o "$target_dir/$final_name" "$url"; then
237
+ success=true
238
+ fi
239
+ fi
240
+ if [[ "$success" == true ]]; then
241
+ log_success "Download concluído: $final_name"
242
+ return 0
243
+ fi
244
+ log_warn "Tentativa $attempt falhou para $final_name"
245
+ sleep $((2 * attempt))
246
+ done
247
+
248
+ log_error "Falha ao baixar: $final_name"
249
+ return 1
250
+ }
251
+
252
+ download_mega() {
253
+ local url="$1"
254
+ local target_dir="$2"
255
+
256
+ url="${url#mega://}"
257
+ mkdir -p "$target_dir"
258
+
259
+ if ! command -v megadl >/dev/null 2>&1; then
260
+ log_warn "megadl não encontrado; tente instalar megatools manualmente"
261
+ return 1
262
  fi
263
+
264
+ local attempt
265
+ for attempt in 1 2 3; do
266
+ if (cd "$target_dir" && megadl "$url"); then
267
+ log_success "Download Mega.nz concluído"
268
+ return 0
269
+ fi
270
+ log_warn "Tentativa $attempt falhou para Mega.nz"
271
+ sleep $((2 * attempt))
272
+ done
273
+
274
+ log_error "Falha ao baixar de Mega.nz: $url"
275
+ return 1
276
  }
277
 
278
  process_downloads() {
279
+ local failed=0
280
  for entry in "${DOWNLOAD_FILES[@]}"; do
281
+ IFS='|' read -r raw_url type filename <<< "$entry"
 
282
  local target_dir="$MODELS_DIR/$type"
283
  mkdir -p "$target_dir"
284
 
285
+ if [[ "$raw_url" == mega://* ]]; then
286
+ if ! download_mega "$raw_url" "$target_dir"; then
287
+ ((failed++))
288
+ fi
289
+ elif [[ "$raw_url" == hf://* ]]; then
290
+ local hf_path="${raw_url#hf://}"
291
+ if ! download_hf "$hf_path" "$target_dir" "$filename"; then
292
+ ((failed++))
293
+ fi
294
  else
295
+ if ! download_file "$raw_url" "$target_dir" "$filename"; then
296
+ ((failed++))
297
+ fi
298
  fi
299
  done
300
+
301
+ if (( failed > 0 )); then
302
+ return 1
303
+ fi
304
+ return 0
305
  }
306
 
307
  clone_repo() {
308
  local url="$1"
309
  local dest="$2"
310
 
311
+ if [[ -d "$dest/.git" ]]; then
312
+ git -C "$dest" pull --ff-only 2>/dev/null || log_warn "Falha ao atualizar $(basename "$dest")"
313
  else
314
+ git clone --depth 1 "$url" "$dest" 2>/dev/null || log_warn "Falha ao clonar $(basename "$dest")"
315
  fi
316
 
317
+ if [[ -f "$dest/requirements.txt" ]]; then
318
+ pip install -q -r "$dest/requirements.txt" 2>/dev/null || log_warn "Falha ao instalar requirements de $(basename "$dest")"
319
  fi
320
  }
321
 
322
  # ========== INSTALAÇÃO ==========
 
323
  log_info "========================================="
324
+ log_info " ComfyUI + Wan 2.2 Setup"
325
  log_info "========================================="
326
 
327
  # 1. Dependências
328
+ log_info "[1/6] Instalando dependências do sistema..."
329
+ if command -v apt-get >/dev/null 2>&1; then
330
+ apt-get update -qq || log_warn "apt-get update falhou; seguindo assim mesmo"
331
+ apt-get install -y -qq aria2 megatools git wget curl || log_warn "apt-get install retornou erro"
332
+ else
333
+ log_warn "apt-get indisponível; certifique-se de que aria2, megatools, git, wget e curl estão instalados"
334
+ fi
335
+ pip install --upgrade pip wheel setuptools -q || log_warn "Falha na atualização do pip"
336
+ pip install --upgrade "huggingface_hub[cli,hf_transfer]" comfy-cli -q || log_warn "Falha instalando huggingface_hub/comfy-cli"
337
+ log_success "Dependências tratadas"
338
 
339
+ # 2. Instalar ComfyUI
340
  log_info "[2/6] Instalando ComfyUI..."
341
+ if [[ -f "$COMFY_DIR/main.py" ]]; then
342
+ log_warn "ComfyUI presente"
 
343
  else
344
+ comfy --skip-prompt install --fast-deps --nvidia || log_error "Instalação do ComfyUI falhou"
345
  fi
346
+ log_success "ComfyUI pronto"
347
 
348
+ # 3. Instalar PyTorch (CUDA 12.8)
349
+ log_info "[3/6] Instalando PyTorch CUDA 12.8..."
350
+ python3 -m pip install --upgrade torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu128 || log_error "Falha ao instalar PyTorch"
351
+ log_success "PyTorch configurado"
352
 
353
+ # 4. Instalar SageAttention (condicional)
 
 
 
 
 
354
  log_info "[4/6] Instalando SageAttention..."
355
+ if python3 - <<'PY' >/dev/null 2>&1
356
  import torch, sys
357
  sys.exit(0 if torch.cuda.is_available() else 1)
358
  PY
359
  then
360
+ if python3 -m pip install "https://huggingface.co/adbrasi/comfywheel/resolve/main/sageattention-2.2.0-cp311-cp311-linux_x86_64.whl" -q; then
361
+ if python3 - <<'PY' >/dev/null 2>&1
362
+ import sageattention
363
+ PY
364
+ then
365
+ log_success "SageAttention instalado"
366
+ else
367
+ log_warn "SageAttention instalado, mas não pôde ser importado"
368
+ fi
369
  else
370
+ log_warn "Falha ao instalar SageAttention"
371
  fi
372
  else
373
+ log_warn "CUDA indisponível; pulando SageAttention"
374
  fi
375
 
376
+ # 5. Downloads de modelos
377
  log_info "[5/6] Baixando modelos..."
378
+ mkdir -p "$MODELS_DIR"
379
+ if process_downloads; then
380
+ log_success "Downloads concluídos (ou já presentes)"
381
+ else
382
+ log_warn "Alguns downloads falharam; reexecute o script para tentar novamente"
383
+ fi
384
 
385
  # 6. Custom nodes
386
  log_info "[6/6] Instalando custom nodes..."
 
387
  CN_DIR="$COMFY_DIR/custom_nodes"
388
  mkdir -p "$CN_DIR"
389
+ node_total=${#CUSTOM_NODES[@]}
390
+ node_index=0
 
 
391
  for repo in "${CUSTOM_NODES[@]}"; do
392
+ ((node_index++))
393
+ log_info "[$node_index/$node_total] $(basename "$repo")"
394
+ clone_repo "$repo" "$CN_DIR/$(basename "$repo")"
 
395
  done
396
+ log_success "Custom nodes processados"
397
 
398
+ # Resumo final
 
 
 
 
 
 
399
  log_info "========================================="
400
+ log_info "Verificando arquivos críticos..."
401
+ critical_missing=0
402
+ for required in \
403
+ "$COMFY_DIR/main.py" \
404
+ "$MODELS_DIR/vae/wan_2.1_vae.safetensors" \
405
+ "$MODELS_DIR/vae/Wan2_1_VAE_fp32.safetensors" \
406
+ "$MODELS_DIR/text_encoders/umt5_xxl_fp16.safetensors" \
407
+ "$MODELS_DIR/text_encoders/umt5-xxl-enc-bf16.safetensors" \
408
+ "$MODELS_DIR/clip_vision/clip_vision_h.safetensors"; do
409
+ if [[ -f "$required" ]]; then
410
+ log_success "✓ $(basename "$required")"
411
+ else
412
+ log_warn "✗ $(basename "$required") não encontrado"
413
+ ((critical_missing++))
414
+ fi
415
+ done
416
 
417
+ if (( critical_missing == 0 )); then
418
+ log_success "Instalação concluída com sucesso!"
419
+ else
420
+ log_warn "Instalação finalizada com pendências; verifique avisos acima"
421
+ fi
422
 
 
423
  SAGE_FLAG=""
424
+ if python3 - <<'PY' >/dev/null 2>&1
425
+ import sageattention
426
+ PY
427
+ then
428
  SAGE_FLAG="--use-sage-attention"
429
  fi
430
 
431
+ log_info "========================================="
432
+ log_info "Iniciando ComfyUI em http://${COMFY_HOST}:${COMFY_PORT}"
433
+ log_info "========================================="
434
+
435
+ cd "$COMFY_DIR"
436
  exec comfy launch -- ${SAGE_FLAG:+$SAGE_FLAG} --listen "$COMFY_HOST" --port "$COMFY_PORT"