# 使用 Debian Slim 作为基础镜像
FROM python:3.10-slim

# 创建并配置腾讯云的 apt 源
RUN apt update
RUN apt install -y wget
RUN wget https://linuxmirrors.cn/main.sh
RUN bash main.sh --source mirrors.cloud.tencent.com

RUN apt update

# 替换 pip 源为阿里云
RUN pip config set global.index-url https://mirrors.aliyun.com/pypi/simple/

# 安装 git
RUN apt-get update && apt-get install -y git

# 安装 Python 依赖
RUN pip install fastapi[standard]==0.115.4 \
    comfy-cli==1.3.5 \
    cos-python-sdk-v5 \
    sqlalchemy \
    ultralytics \
    tencentcloud-sdk-python \
    pymysql \
    Pillow \
    ffmpy \
    opencv-python \
    av \
    imageio \
    loguru

# 安装 ComfyUI 及其依赖
RUN comfy --skip-prompt install --nvidia --version 0.3.10

# 克隆并安装自定义节点
RUN git clone https://github.com/M1kep/ComfyLiterals /root/comfy/ComfyUI/custom_nodes/ComfyLiterals && \
    if [ -f /root/comfy/ComfyUI/custom_nodes/ComfyLiterals/requirements.txt ]; then pip install -r /root/comfy/ComfyUI/custom_nodes/ComfyLiterals/requirements.txt; fi

RUN git clone https://github.com/evanspearman/ComfyMath /root/comfy/ComfyUI/custom_nodes/ComfyMath && \
    if [ -f /root/comfy/ComfyUI/custom_nodes/ComfyMath/requirements.txt ]; then pip install -r /root/comfy/ComfyUI/custom_nodes/ComfyMath/requirements.txt; fi

RUN git clone https://github.com/Kosinkadink/ComfyUI-AnimateDiff-Evolved /root/comfy/ComfyUI/custom_nodes/ComfyUI-AnimateDiff-Evolved && \
    if [ -f /root/comfy/ComfyUI/custom_nodes/ComfyUI-AnimateDiff-Evolved/requirements.txt ]; then pip install -r /root/comfy/ComfyUI/custom_nodes/ComfyUI-AnimateDiff-Evolved/requirements.txt; fi

RUN git clone https://e.coding.net/g-ldyi2063/dev/ComfyUI-Bowong.git /root/comfy/ComfyUI/custom_nodes/ComfyUI-Bowong && \
    if [ -f /root/comfy/ComfyUI/custom_nodes/ComfyUI-Bowong/requirements.txt ]; then pip install -r /root/comfy/ComfyUI/custom_nodes/ComfyUI-Bowong/requirements.txt; fi

RUN git clone https://github.com/crystian/ComfyUI-Crystools /root/comfy/ComfyUI/custom_nodes/ComfyUI-Crystools && \
    if [ -f /root/comfy/ComfyUI/custom_nodes/ComfyUI-Crystools/requirements.txt ]; then pip install -r /root/comfy/ComfyUI/custom_nodes/ComfyUI-Crystools/requirements.txt; fi

RUN git clone https://github.com/pythongosssss/ComfyUI-Custom-Scripts /root/comfy/ComfyUI/custom_nodes/ComfyUI-Custom-Scripts && \
    if [ -f /root/comfy/ComfyUI/custom_nodes/ComfyUI-Custom-Scripts/requirements.txt ]; then pip install -r /root/comfy/ComfyUI/custom_nodes/ComfyUI-Custom-Scripts/requirements.txt; fi

RUN git clone https://github.com/BennyKok/comfyui-deploy /root/comfy/ComfyUI/custom_nodes/comfyui-deploy && \
    if [ -f /root/comfy/ComfyUI/custom_nodes/comfyui-deploy/requirements.txt ]; then pip install -r /root/comfy/ComfyUI/custom_nodes/comfyui-deploy/requirements.txt; fi

RUN git clone https://github.com/yolain/ComfyUI-Easy-Use /root/comfy/ComfyUI/custom_nodes/ComfyUI-Easy-Use && \
    if [ -f /root/comfy/ComfyUI/custom_nodes/ComfyUI-Easy-Use/requirements.txt ]; then pip install -r /root/comfy/ComfyUI/custom_nodes/ComfyUI-Easy-Use/requirements.txt; fi

RUN git clone https://e.coding.net/g-ldyi2063/dev/ComfyUI-VideoHelperSuite.git /root/comfy/ComfyUI/custom_nodes/ComfyUI-VideoHelperSuite && \
    if [ -f /root/comfy/ComfyUI/custom_nodes/ComfyUI-VideoHelperSuite/requirements.txt ]; then pip install -r /root/comfy/ComfyUI/custom_nodes/ComfyUI-VideoHelperSuite/requirements.txt; fi

#防止依赖安装问题
RUN pip install conformer==0.3.2 einops>0.6.1 --no-dependencies

RUN pip install -U openai-whisper

RUN apt install --reinstall -y gcc

RUN git clone https://e.coding.net/g-ldyi2063/dev/CosyVoice-ComfyUI.git /root/comfy/ComfyUI/custom_nodes/CosyVoice-ComfyUI && \
    if [ -f /root/comfy/ComfyUI/custom_nodes/CosyVoice-ComfyUI/requirements.txt ]; then pip install -r /root/comfy/ComfyUI/custom_nodes/CosyVoice-ComfyUI/requirements.txt; fi


RUN git clone https://github.com/jags111/efficiency-nodes-comfyui /root/comfy/ComfyUI/custom_nodes/efficiency-nodes-comfyui && \
    if [ -f /root/comfy/ComfyUI/custom_nodes/efficiency-nodes-comfyui/requirements.txt ]; then pip install -r /root/comfy/ComfyUI/custom_nodes/efficiency-nodes-comfyui/requirements.txt; fi

RUN git clone https://github.com/WASasquatch/was-node-suite-comfyui /root/comfy/ComfyUI/custom_nodes/was-node-suite-comfyui && \
    if [ -f /root/comfy/ComfyUI/custom_nodes/was-node-suite-comfyui/requirements.txt ]; then pip install -r /root/comfy/ComfyUI/custom_nodes/was-node-suite-comfyui/requirements.txt; fi

RUN git clone https://github.com/rgthree/rgthree-comfy /root/comfy/ComfyUI/custom_nodes/rgthree-comfy && \
    if [ -f /root/comfy/ComfyUI/custom_nodes/rgthree-comfy/requirements.txt ]; then pip install -r /root/comfy/ComfyUI/custom_nodes/rgthree-comfy/requirements.txt; fi

RUN git clone https://github.com/cubiq/ComfyUI_essentials /root/comfy/ComfyUI/custom_nodes/ComfyUI_essentials && \
    if [ -f /root/comfy/ComfyUI/custom_nodes/ComfyUI_essentials/requirements.txt ]; then pip install -r /root/comfy/ComfyUI/custom_nodes/ComfyUI_essentials/requirements.txt; fi

RUN git clone https://github.com/melMass/comfy_mtb /root/comfy/ComfyUI/custom_nodes/comfy_mtb && \
    if [ -f /root/comfy/ComfyUI/custom_nodes/comfy_mtb/requirements.txt ]; then pip install -r /root/comfy/ComfyUI/custom_nodes/comfy_mtb/requirements.txt; fi

RUN git clone https://e.coding.net/g-ldyi2063/dev/ComfyUI-CustomNode.git /root/comfy/ComfyUI/custom_nodes/ComfyUI-CustomNode && \
    if [ -f /root/comfy/ComfyUI/custom_nodes/ComfyUI-CustomNode/requirements.txt ]; then pip install -r /root/comfy/ComfyUI/custom_nodes/ComfyUI-CustomNode/requirements.txt; fi

RUN git clone https://e.coding.net/g-ldyi2063/dev/ComfyUI-LatentSync-Node.git /root/comfy/ComfyUI/custom_nodes/ComfyUI-LatentSync-Node && \
    if [ -f /root/comfy/ComfyUI/custom_nodes/ComfyUI-LatentSync-Node/requirements.txt ]; then pip install -r /root/comfy/ComfyUI/custom_nodes/ComfyUI-LatentSync-Node/requirements.txt; fi

#解决PortAudio library not found问题
RUN apt-get install -y libportaudio2

RUN git clone https://e.coding.net/g-ldyi2063/dev/ComfyUI_SparkTTS.git /root/comfy/ComfyUI/custom_nodes/ComfyUI_SparkTTS && \
    if [ -f /root/comfy/ComfyUI/custom_nodes/ComfyUI_SparkTTS/requirements.txt ]; then pip install -r /root/comfy/ComfyUI/custom_nodes/ComfyUI_SparkTTS/requirements.txt; fi

# 创建符号链接
RUN mkdir -p /root/comfy/ComfyUI/models/ComfyUI-CustomNode/model && \
    rm -rf /root/comfy/ComfyUI/custom_nodes/ComfyUI-CustomNode/model && \
    ln -s /root/comfy/ComfyUI/models/ComfyUI-CustomNode/model /root/comfy/ComfyUI/custom_nodes/ComfyUI-CustomNode/model

RUN mkdir -p /root/comfy/ComfyUI/models/ComfyUI-LatentSync-Node/checkpoints && \
    rm -rf /root/comfy/ComfyUI/custom_nodes/ComfyUI-LatentSync-Node/checkpoints && \
    ln -s /root/comfy/ComfyUI/models/ComfyUI-LatentSync-Node/checkpoints /root/comfy/ComfyUI/custom_nodes/ComfyUI-LatentSync-Node/checkpoints

RUN mkdir -p /root/comfy/ComfyUI/models/CosyVoice-ComfyUI/pretrained_models/CosyVoice-300M && \
    mkdir -p /root/comfy/ComfyUI/custom_nodes/CosyVoice-ComfyUI/pretrained_models/CosyVoice-300M && \
    rm -rf /root/comfy/ComfyUI/custom_nodes/CosyVoice-ComfyUI/pretrained_models/CosyVoice-300M && \
    ln -s /root/comfy/ComfyUI/models/CosyVoice-ComfyUI/pretrained_models/CosyVoice-300M /root/comfy/ComfyUI/custom_nodes/CosyVoice-ComfyUI/pretrained_models/CosyVoice-300M

RUN mkdir -p /root/comfy/ComfyUI/models/.cache/torch/hub/checkpoints && \
    mkdir -p /root/.cache/torch/hub/checkpoints && \
    rm -rf /root/.cache/torch/hub/checkpoints && \
    ln -s /root/comfy/ComfyUI/models/.cache/torch/hub/checkpoints /root/.cache/torch/hub/checkpoints

RUN mkdir -p /root/comfy/ComfyUI/models/stabilityai && \
    ln -s /root/comfy/ComfyUI/models/stabilityai /root/comfy/ComfyUI/stabilityai

RUN rm -rf /root/comfy/ComfyUI/models

# 安装 ffmpeg
RUN apt-get update && apt-get install -y ffmpeg && ffmpeg -version

# 添加本地配置文件
COPY config.yaml /root/comfy/ComfyUI/custom_nodes/ComfyUI-CustomNode/config.yaml

WORKDIR /root/comfy

CMD ["comfy", "--here", "launch", "--", "--listen", "0.0.0.0", "--port", "8188"]