This repository has been archived on 2026-10-21. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
imagelab-runpod/startup.sh
T
donnyandClaude Opus 4.7 c8b93a1300 Initial: ImageLab v3 RunPod image
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-17 21:42:52 -07:00

210 lines
8.5 KiB
Bash
Executable File

#!/bin/bash
set -e
# ============================================================================
# ImageLab RunPod startup
# ----------------------------------------------------------------------------
# All model-downloading is owned by imagelab_v3_extension (it reads
# IMAGELAB_AUTO_DOWNLOAD + CIVITAI_TOKEN at server start). This script just
# brings up ComfyUI + the custom nodes the web app's graph references, then
# launches the server with sensible flags for the detected GPU.
# ============================================================================
COMFYUI_DIR="/workspace/ComfyUI"
CUSTOM_NODES_DIR="$COMFYUI_DIR/custom_nodes"
# Colors
RED='\033[0;31m'; GREEN='\033[0;32m'; YELLOW='\033[1;33m'; BLUE='\033[0;34m'; NC='\033[0m'
log_info() { echo -e "${GREEN}[INFO]${NC} $1"; }
log_warn() { echo -e "${YELLOW}[WARN]${NC} $1"; }
log_error() { echo -e "${RED}[ERROR]${NC} $1"; }
log_step() { echo -e "${BLUE}[STEP]${NC} $1"; }
# ----------------------------------------------------------------------------
# Custom nodes
# ----------------------------------------------------------------------------
# Everything the web app's graph references plus a small set of QoL nodes.
# Keep this list tight — every entry slows boot.
#
# * imagelab_v3_extension → /imagelab/* API: hashing, downloads, CivitAI
# * ComfyUI-Inpaint-CropAndStitch → InpaintCropImproved / InpaintStitchImproved
# * ComfyUI-BRIA_AI-RMBG → BRIA_RMBG_ModelLoader_Zho + BRIA_RMBG_Zho
# * ComfyUI-WD14-Tagger → kept available (old AutoTag path; harmless if unused)
# * ComfyUI-Manager → in-UI node manager (very useful on a fresh pod)
# * rgthree-comfy / custom-scripts → QoL UI nodes the user is likely to want
# ----------------------------------------------------------------------------
CUSTOM_NODES=(
"https://gitea.dev.blueoceanswim.com/donny/imagelab_v3_extension.git"
"https://github.com/lquesada/ComfyUI-Inpaint-CropAndStitch.git"
"https://github.com/ZHO-ZHO-ZHO/ComfyUI-BRIA_AI-RMBG.git"
"https://github.com/pythongosssss/ComfyUI-WD14-Tagger.git"
"https://github.com/ltdrdata/ComfyUI-Manager.git"
"https://github.com/pythongosssss/ComfyUI-Custom-Scripts.git"
"https://github.com/rgthree/rgthree-comfy.git"
"https://github.com/cubiq/ComfyUI_essentials.git"
)
# ----------------------------------------------------------------------------
# ComfyUI
# ----------------------------------------------------------------------------
setup_comfyui() {
log_step "Setting up ComfyUI"
if [ -d "$COMFYUI_DIR/.git" ]; then
log_info "ComfyUI exists — pulling latest"
git -C "$COMFYUI_DIR" pull --ff-only || log_warn "Failed to pull ComfyUI updates"
else
log_info "Cloning ComfyUI"
git clone --depth 1 https://github.com/comfyanonymous/ComfyUI.git "$COMFYUI_DIR"
fi
log_info "Installing ComfyUI requirements (skipping pre-installed torch)"
cd "$COMFYUI_DIR"
grep -v -E "^torch==|^torch\[|^torch$|^torchvision|^torchaudio" requirements.txt > /tmp/comfy_req.txt
pip install -r /tmp/comfy_req.txt 2>&1 | tail -10
rm -f /tmp/comfy_req.txt
}
# ----------------------------------------------------------------------------
# Custom nodes
# ----------------------------------------------------------------------------
install_custom_nodes() {
log_step "Installing custom nodes"
mkdir -p "$CUSTOM_NODES_DIR"
for repo in "${CUSTOM_NODES[@]}"; do
name=$(basename "$repo" .git)
path="$CUSTOM_NODES_DIR/$name"
if [ -d "$path/.git" ]; then
log_info "Updating $name"
git -C "$path" pull --ff-only 2>/dev/null || log_warn "Failed to update $name"
else
log_info "Cloning $name"
git -C "$CUSTOM_NODES_DIR" clone --depth 1 "$repo" 2>/dev/null || log_warn "Failed to clone $name"
fi
done
log_step "Installing node requirements"
for path in "$CUSTOM_NODES_DIR"/*/; do
name=$(basename "$path")
if [ -f "$path/requirements.txt" ]; then
log_info " $name"
pip install -q -r "$path/requirements.txt" 2>&1 | grep -v "already satisfied" || true
fi
done
# Some nodes have an install.py — run it if present.
for path in "$CUSTOM_NODES_DIR"/*/; do
if [ -f "$path/install.py" ]; then
log_info " install.py for $(basename "$path")"
( cd "$path" && python install.py 2>&1 | tail -3 ) || true
fi
done
}
# ----------------------------------------------------------------------------
# BRIA RMBG model (the BRIA_RMBG_* nodes look for it under their own dir)
# ----------------------------------------------------------------------------
download_bria_model() {
BRIA_DIR="$CUSTOM_NODES_DIR/ComfyUI-BRIA_AI-RMBG/RMBG-1.4"
if [ -f "$BRIA_DIR/model.pth" ]; then
log_info "BRIA RMBG-1.4 already present"
return
fi
log_step "Downloading BRIA RMBG-1.4"
python3 - <<PY || log_warn "BRIA RMBG download failed"
import os
from huggingface_hub import snapshot_download
snapshot_download(
repo_id="briaai/RMBG-1.4",
local_dir="$BRIA_DIR",
token=os.environ.get("HF_TOKEN") or None,
)
print("BRIA RMBG-1.4 downloaded")
PY
}
# ----------------------------------------------------------------------------
# SageAttention (background) — ~3x sampler speedup on supported GPUs.
# ----------------------------------------------------------------------------
install_sage_attention() {
local setting="${SAGE_ATTENTION:-true}"
if [ "$setting" = "false" ]; then
log_info "SageAttention disabled (SAGE_ATTENTION=false)"
return
fi
log_step "Installing SageAttention in the background"
(
pip install -q sageattention 2>&1 | tail -3 \
&& touch /tmp/.sageattention_installed \
&& log_info "SageAttention installed" \
|| log_warn "SageAttention install failed"
) &
}
# ----------------------------------------------------------------------------
# ComfyUI launch — GPU-aware default args
# ----------------------------------------------------------------------------
start_comfyui() {
log_step "Starting ComfyUI"
cd "$COMFYUI_DIR"
if [ -z "$COMFYUI_ARGS" ]; then
VRAM_MB=$(nvidia-smi --query-gpu=memory.total --format=csv,noheader,nounits 2>/dev/null | head -1)
VRAM_GB=$(( ${VRAM_MB:-0} / 1024 ))
GPU_NAME=$(nvidia-smi --query-gpu=name --format=csv,noheader 2>/dev/null | head -1)
log_info "GPU: ${GPU_NAME:-unknown} (${VRAM_GB} GB)"
# Common args: bind all ifaces, TAESD live previews (what the web app
# reads as binary WS frames), CORS open so the browser can talk to us.
BASE_ARGS="--listen 0.0.0.0 --port 8188 --preview-method taesd --preview-size 768 --enable-cors-header"
if [ "$VRAM_GB" -ge 180 ]; then
log_info "Blackwell-class GPU — bf16"
COMFYUI_ARGS="$BASE_ARGS --bf16-vae --bf16-unet"
elif [ "$VRAM_GB" -ge 80 ]; then
log_info "Hopper-class GPU — bf16"
COMFYUI_ARGS="$BASE_ARGS --bf16-vae --bf16-unet"
elif [ "$VRAM_GB" -ge 40 ]; then
log_info "Ampere-class GPU — fp16"
COMFYUI_ARGS="$BASE_ARGS --fp16-vae --fp16-unet"
elif [ "$VRAM_GB" -ge 20 ]; then
log_info "Consumer GPU — fp16"
COMFYUI_ARGS="$BASE_ARGS --fp16-vae --fp16-unet"
else
log_info "Low-VRAM GPU — --lowvram"
COMFYUI_ARGS="$BASE_ARGS --lowvram"
fi
if [ -f /tmp/.sageattention_installed ] && [ "${SAGE_ATTENTION:-true}" != "false" ]; then
log_info "Enabling --use-sage-attention"
COMFYUI_ARGS="$COMFYUI_ARGS --use-sage-attention"
fi
fi
log_info "Args: $COMFYUI_ARGS"
log_info "ComfyUI on http://0.0.0.0:8188"
exec python main.py $COMFYUI_ARGS
}
# ----------------------------------------------------------------------------
# main
# ----------------------------------------------------------------------------
log_info "=========================================="
log_info "ImageLab RunPod"
log_info "=========================================="
log_info "CIVITAI_TOKEN=$( [ -n "$CIVITAI_TOKEN" ] && echo set || echo unset )"
log_info "IMAGELAB_AUTO_DOWNLOAD=$( [ -n "$IMAGELAB_AUTO_DOWNLOAD" ] && echo set || echo unset )"
log_info "SAGE_ATTENTION=${SAGE_ATTENTION:-true}"
log_info "=========================================="
setup_comfyui
install_custom_nodes
install_sage_attention # background
download_bria_model &
BRIA_PID=$!
wait $BRIA_PID || true
start_comfyui