# ComfyUI

面向 Stable Diffusion 的基于节点的界面，在 CLORE.AI GPU 上提供极致灵活性。

{% hint style="success" %}
所有示例都可以在通过以下方式租用的 GPU 服务器上运行： [CLORE.AI 市场](https://clore.ai/marketplace).
{% endhint %}

## 服务器要求

| 参数   | 最低        | 推荐     |
| ---- | --------- | ------ |
| 内存   | 16GB      | 32GB+  |
| 显存   | 8GB（SDXL） | 12GB+  |
| 网络   | 500Mbps   | 1Gbps+ |
| 启动时间 | 5-10 分钟   | -      |

{% hint style="warning" %}
**启动时间：** 首次启动将下载依赖项和模型（取决于网络速度需 5-10 分钟）。此期间出现 HTTP 502 是正常的。
{% endhint %}

{% hint style="danger" %}
**重要：** ComfyUI 与 FLUX 模型需要 16GB+ 显存。对于带 ControlNet 的 SDXL，确保至少有 10GB 显存。
{% endhint %}

## 为什么选择 ComfyUI？

* **基于节点的工作流** - 用于图像生成的可视化编程
* **最大控制** - 微调管道的每个步骤
* **高效** - 比替代方案更低的显存使用
* **可扩展** - 大量自定义节点生态系统
* **工作流共享** - 以 JSON 导入/导出

## 在 CLORE.AI 上快速部署

**Docker 镜像：**

```
yanwk/comfyui-boot:cu126-slim
```

**端口：**

```
22/tcp
8188/http
```

**环境：**

```
CLI_ARGS=--listen 0.0.0.0
```

### 验证是否正常运行

部署后，在以下位置查找您的 `http_pub` URL： **我的订单**:

```bash
# 检查 UI 是否可访问（首次运行可能需 5-10 分钟）
curl https://your-http-pub.clorecloud.net/
```

{% hint style="info" %}
如果出现 HTTP 502 超过 15 分钟，请检查：

1. 服务器是否有 16GB+ RAM
2. 服务器具有用于 SDXL 的 8GB+ 显存，FLUX 需要 16GB+
3. 网络速度足以下载模型
   {% endhint %}

## 访问您的服务

部署在 CLORE.AI 时，通过以下方式访问 ComfyUI： `http_pub` URL：

* **Web 界面：** `https://your-http-pub.clorecloud.net/`
* **API：** `https://your-http-pub.clorecloud.net/prompt`
* **WebSocket：** `wss://your-http-pub.clorecloud.net/ws`

{% hint style="info" %}
全部 `localhost:8188` 下面的示例在通过 SSH 连接时可用。若需外部访问，请替换为你的 `https://your-http-pub.clorecloud.net/` URL。
{% endhint %}

## 安装

### 使用 Docker（推荐）

```bash
docker run -d --gpus all \
  -p 8188:8188 \\
  -v comfyui-data:/root \\
  -e CLI_ARGS="--listen 0.0.0.0" \\
  yanwk/comfyui-boot:cu126-slim
```

### 手动安装

```bash
# 克隆仓库
git clone https://github.com/comfyanonymous/ComfyUI.git
cd ComfyUI

# 创建虚拟环境
python -m venv venv
source venv/bin/activate

# 安装带 CUDA 的 PyTorch
pip install torch torchvision --index-url https://download.pytorch.org/whl/cu124

# 安装依赖
pip install -r requirements.txt

# 运行
python main.py --listen 0.0.0.0
```

## 目录结构

```
ComfyUI/
├── models/
│   ├── checkpoints/     # SD 模型（.safetensors）
│   ├── loras/           # LoRA 模型
│   ├── vae/             # VAE 模型
│   ├── controlnet/      # ControlNet 模型
│   ├── upscale_models/  # 放大模型
│   └── clip/            # CLIP 模型
├── input/               # 输入图像
├── output/              # 生成的图像
└── custom_nodes/        # 扩展
```

## 下载模型

### Stable Diffusion 检查点

```bash
cd ComfyUI/models/checkpoints

# SDXL 基础模型
wget https://huggingface.co/stabilityai/stable-diffusion-xl-base-1.0/resolve/main/sd_xl_base_1.0.safetensors

# SDXL 精炼器
wget https://huggingface.co/stabilityai/stable-diffusion-xl-refiner-1.0/resolve/main/sd_xl_refiner_1.0.safetensors

# SD 1.5（更小、更快）
wget https://huggingface.co/runwayml/stable-diffusion-v1-5/resolve/main/v1-5-pruned.safetensors

# Realistic Vision（照片级写实）
wget https://huggingface.co/SG161222/Realistic_Vision_V6.0_B1_noVAE/resolve/main/Realistic_Vision_V6.0_B1_fp16.safetensors
```

### VAE

```bash
cd ComfyUI/models/vae

# SDXL VAE
wget https://huggingface.co/stabilityai/sdxl-vae/resolve/main/sdxl_vae.safetensors

# SD 1.5 VAE（更好色彩）
wget https://huggingface.co/stabilityai/sd-vae-ft-mse-original/resolve/main/vae-ft-mse-840000-ema-pruned.safetensors
```

### LoRA 模型

```bash
cd ComfyUI/models/loras

# 从 CivitAI 或 HuggingFace 下载
# 将 .safetensors 文件放在此处
```

## 基本工作流

### 文本到图像

1. 添加节点：
   * **加载检查点** → 选择模型
   * **CLIP 文本编码** (x2) → 正负提示词
   * **空的潜在图像** → 设置尺寸
   * **KSampler** → 连接全部
   * **VAE 解码** → 从潜在到图像
   * **保存图像** → 输出
2. 连接：

```
[Checkpoint] → MODEL → [KSampler]
[Checkpoint] → CLIP → [CLIP Text Encode +]
[Checkpoint] → CLIP → [CLIP Text Encode -]
[Checkpoint] → VAE → [VAE Decode]
[Text Encode +] → CONDITIONING → [KSampler]
[Text Encode -] → CONDITIONING → [KSampler]
[Empty Latent] → LATENT → [KSampler]
[KSampler] → LATENT → [VAE Decode]
[VAE Decode] → IMAGE → [Save Image]
```

### 图像到图像

替换 **空的潜在图像** 为：

1. **加载图像** → 你的源图像
2. **VAE 编码** → 转换为潜在表示
3. 调整 **去噪** 在 KSampler（0.5-0.8）中

## ComfyUI 管理器

ComfyUI 管理器是一个 **必要的扩展** 它为安装、更新和管理自定义节点添加了 GUI。它是扩展 ComfyUI 的标准方式。

### 安装

```bash
demo.launch(server_name="0.0.0.0", server_port=7860)
git clone https://github.com/ltdrdata/ComfyUI-Manager.git
# 重启 ComfyUI — 工具栏将出现一个 “Manager” 按钮
```

### 使用 ComfyUI 管理器

重启后， **Manager** 按钮会出现在 ComfyUI 界面右上角。

**主要功能：**

| 特性      | 如何访问                                   |
| ------- | -------------------------------------- |
| 安装自定义节点 | Manager → Install Custom Nodes         |
| 更新所有节点  | Manager → Update All                   |
| 禁用/启用节点 | Manager → Custom Nodes Manager         |
| 安装缺失的节点 | Manager → Install Missing Custom Nodes |
| 获取模型信息  | Manager → Model Manager                |
| 恢复快照    | Manager → Snapshot Manager             |

**工作流：安装新的节点包**

1. 点击 **Manager** 按钮
2. 选择 **Install Custom Nodes**
3. 按名称搜索（例如："FLUX"、"AnimateDiff"）
4. 点击 **安装** 在所需包上
5. 点击 **在提示时重启** Restart
6. 新节点会出现在右键添加菜单中

**自动安装缺失节点：** 当你导入使用你没有的节点的工作流 JSON 时，Manager 会检测到它们并提供通过自动安装的选项，使用 **Install Missing Custom Nodes**.

### 保持节点更新

```bash
# 从命令行（作为 GUI 的替代）：
cd ComfyUI/custom_nodes/ComfyUI-Manager
git pull

# 或在 UI 中使用 Manager → Update All
```

***

## ComfyUI 中的 FLUX 工作流

FLUX 使用与标准 SD 模型不同的节点结构。下面是完整的 FLUX.1-dev 工作流。

### 所需文件

在运行工作流之前，下载：

```bash
# FLUX 模型（dev 或 schnell）
cd ComfyUI/models/unet
wget https://huggingface.co/black-forest-labs/FLUX.1-dev/resolve/main/flux1-dev.safetensors

# 文本编码器
cd ../clip
wget https://huggingface.co/comfyanonymous/flux_text_encoders/resolve/main/clip_l.safetensors
wget https://huggingface.co/comfyanonymous/flux_text_encoders/resolve/main/t5xxl_fp16.safetensors

# VAE
cd ../vae
wget https://huggingface.co/black-forest-labs/FLUX.1-dev/resolve/main/ae.safetensors
```

### FLUX.1-dev 工作流 JSON

保存为 `flux_dev_workflow.json` 并通过以下方式导入： **加载** ComfyUI 中的按钮：

```json
{
  "last_node_id": 12,
  "last_link_id": 20,
  "nodes": [
    {
      "id": 1,
      "type": "UNETLoader",
      "pos": [100, 100],
      "size": [300, 60],
      "inputs": [],
      "outputs": [{"name": "MODEL", "type": "MODEL", "links": [1]}],
      "properties": {},
      "widgets_values": ["flux1-dev.safetensors", "default"]
    },
    {
      "id": 2,
      "type": "DualCLIPLoader",
      "pos": [100, 200],
      "size": [350, 80],
      "inputs": [],
      "outputs": [{"name": "CLIP", "type": "CLIP", "links": [2, 3]}],
      "properties": {},
      "widgets_values": ["clip_l.safetensors", "t5xxl_fp16.safetensors", "flux"]
    },
    {
      "id": 3,
      "type": "CLIPTextEncode",
      "pos": [500, 150],
      "size": [425, 180],
      "inputs": [{"name": "clip", "type": "CLIP", "link": 2}],
      "outputs": [{"name": "CONDITIONING", "type": "CONDITIONING", "links": [4]}],
      "properties": {},
      "widgets_values": ["A stunning photorealistic landscape, golden hour lighting, 8K"]
    },
    {
      "id": 4,
      "type": "EmptySD3LatentImage",
      "pos": [100, 350],
      "size": [300, 100],
      "inputs": [],
      "outputs": [{"name": "LATENT", "type": "LATENT", "links": [5]}],
      "properties": {},
      "widgets_values": [1024, 1024, 1]
    },
    {
      "id": 5,
      "type": "ModelSamplingFlux",
      "pos": [500, 350],
      "size": [300, 80],
      "inputs": [{"name": "model", "type": "MODEL", "link": 1}],
      "outputs": [{"name": "MODEL", "type": "MODEL", "links": [6]}],
      "properties": {},
      "widgets_values": [1.15, 0.5, 1024, 1024]
    },
    {
      "id": 6,
      "type": "KSampler",
      "pos": [850, 250],
      "size": [350, 240],
      "inputs": [
        {"name": "model", "type": "MODEL", "link": 6},
        {"name": "positive", "type": "CONDITIONING", "link": 4},
        {"name": "negative", "type": "CONDITIONING", "link": 7},
        {"name": "latent_image", "type": "LATENT", "link": 5}
      ],
      "outputs": [{"name": "LATENT", "type": "LATENT", "links": [8]}],
      "properties": {},
      "widgets_values": [42, "fixed", 20, 3.5, "euler", "simple", 1.0]
    },
    {
      "id": 7,
      "type": "CLIPTextEncode",
      "pos": [500, 500],
      "size": [300, 60],
      "inputs": [{"name": "clip", "type": "CLIP", "link": 3}],
      "outputs": [{"name": "CONDITIONING", "type": "CONDITIONING", "links": [7]}],
      "properties": {},
      "widgets_values": [""]
    },
    {
      "id": 8,
      "type": "VAELoader",
      "pos": [100, 500],
      "size": [300, 60],
      "inputs": [],
      "outputs": [{"name": "VAE", "type": "VAE", "links": [9]}],
      "properties": {},
      "widgets_values": ["ae.safetensors"]
    },
    {
      "id": 9,
      "type": "VAEDecode",
      "pos": [1250, 300],
      "size": [210, 46],
      "inputs": [
        {"name": "samples", "type": "LATENT", "link": 8},
        {"name": "vae", "type": "VAE", "link": 9}
      ],
      "outputs": [{"name": "IMAGE", "type": "IMAGE", "links": [10]}],
      "properties": {}
    },
    {
      "id": 10,
      "type": "SaveImage",
      "pos": [1500, 300],
      "size": [300, 270],
      "inputs": [{"name": "images", "type": "IMAGE", "link": 10}],
      "outputs": [],
      "properties": {},
      "widgets_values": ["flux_output"]
    }
  ],
  "links": [
    [1, 1, 0, 5, 0, "MODEL"],
    [2, 2, 0, 3, 0, "CLIP"],
    [3, 2, 0, 7, 0, "CLIP"],
    [4, 3, 0, 6, 1, "CONDITIONING"],
    [5, 4, 0, 6, 3, "LATENT"],
    [6, 5, 0, 6, 0, "MODEL"],
    [7, 7, 0, 6, 2, "CONDITIONING"],
    [8, 6, 0, 9, 0, "LATENT"],
    [9, 8, 0, 9, 1, "VAE"],
    [10, 9, 0, 10, 0, "IMAGE"]
  ],
  "groups": [],
  "config": {},
  "extra": {"ds": {"scale": 0.8, "offset": [0, 0]}},
  "version": 0.4
}
```

### FLUX.1-schnell 工作流（4 步）

对于 schnell，请在上述 JSON 中更改 KSampler 设置：

* `num_inference_steps`: `4`
* `cfg`: `1.0`
* `scheduler`: `"simple"`
* 模型文件： `flux1-schnell.safetensors`

或通过 UI 设置：KSampler → steps： **4**, cfg： **1.0**, sampler： **euler**, scheduler： **simple**

### 关键节点差异：FLUX 与 SD

| 节点    | SD/SDXL        | FLUX                            |
| ----- | -------------- | ------------------------------- |
| 模型加载器 | 加载检查点          | UNETLoader                      |
| 文本编码器 | CLIPTextEncode | DualCLIPLoader + CLIPTextEncode |
| 潜在表示  | 空的潜在图像         | EmptySD3LatentImage             |
| 额外    | —              | ModelSamplingFlux               |
| 负面提示词 | 需要             | 可选（留空）                          |

***

## 重要的自定义节点

### 推荐的节点包

| 节点包                      | GitHub                                  | 模型变体                 |
| ------------------------ | --------------------------------------- | -------------------- |
| **ComfyUI-Manager**      | ltdrdata/ComfyUI-Manager                | 安装并管理所有其他节点          |
| **ComfyUI-FLUX**         | XLabs-AI/x-flux-comfyui                 | FLUX 的 ControlNet 节点 |
| **was-node-suite**       | WASasquatch/was-node-suite-comfyui      | 100+ 实用节点            |
| ComfyUI-Impact-Pack      | ltdrdata/ComfyUI-Impact-Pack            | 人脸检测、SAM、ADetailer   |
| ComfyUI-Inspire-Pack     | ltdrdata/ComfyUI-Inspire-Pack           | 高级采样器、工作流            |
| ComfyUI-AnimateDiff      | Kosinkadink/ComfyUI-AnimateDiff-Evolved | 视频 / 动画生成            |
| ComfyUI-VideoHelperSuite | Kosinkadink/ComfyUI-VideoHelperSuite    | 视频 I/O 处理            |
| ComfyUI-GGUF             | city96/ComfyUI-GGUF                     | 运行量化的 GGUF 模型        |
| ComfyUI-KJNodes          | kijai/ComfyUI-KJNodes                   | 实用与蒙版节点              |
| rgthree-comfy            | rgthree/rgthree-comfy                   | 工作流助手、更好的 UI         |

### ComfyUI-FLUX（XLabs-AI）

在 ComfyUI 中为 FLUX 添加 ControlNet 支持：

```bash
demo.launch(server_name="0.0.0.0", server_port=7860)
git clone https://github.com/XLabs-AI/x-flux-comfyui
cd x-flux-comfyui
pip install -r requirements.txt
```

添加节点： `应用 ControlNet（FLUX）`, `加载 ControlNet 模型（FLUX）`, `XFlux 采样器`

### was-node-suite

用于高级工作流的 100+ 实用节点：

```bash
demo.launch(server_name="0.0.0.0", server_port=7860)
git clone https://github.com/WASasquatch/was-node-suite-comfyui
cd was-node-suite-comfyui
pip install -r requirements.txt
```

关键节点：图像批处理、文本操作、图像分析、缓存节点、总线节点、放大、蒙版操作

### 通过 Manager 安装

1. 点击 **Manager** 按钮
2. **Install Custom Nodes**
3. 搜索并安装
4. 重启 ComfyUI

## 高级工作流

### ControlNet

```bash
# 下载 ControlNet 模型
cd ComfyUI/models/controlnet

# Canny
wget https://huggingface.co/lllyasviel/ControlNet-v1-1/resolve/main/control_v11p_sd15_canny.pth

# Depth（深度）
wget https://huggingface.co/lllyasviel/ControlNet-v1-1/resolve/main/control_v11f1p_sd15_depth.pth

# OpenPose
wget https://huggingface.co/lllyasviel/ControlNet-v1-1/resolve/main/control_v11p_sd15_openpose.pth
```

工作流：

1. 加载图像 → Canny 边缘检测器
2. 应用 ControlNet → KSampler
3. 使用姿态/边缘引导生成

### 放大（Upscaling）

```bash
# 下载放大器
cd ComfyUI/models/upscale_models
wget https://github.com/xinntao/Real-ESRGAN/releases/download/v0.1.0/RealESRGAN_x4plus.pth
```

工作流：

1. 在较低分辨率（768x768）生成图像
2. Upscale Image（模型）节点
3. 可选：用于细节的 img2img 步骤

### SDXL + 精炼器

1. 使用 SDXL 基础模型生成（步骤 1-20）
2. 将潜在传递给 SDXL 精炼器（步骤 21-30）
3. VAE 解码最终结果

## 键盘快捷键

| 键                  | 操作          |
| ------------------ | ----------- |
| `Ctrl+Enter`       | 加入队列提示      |
| `Ctrl+Shift+Enter` | 将提示加入队列（前置） |
| `Ctrl+Z`           | 撤销          |
| `Ctrl+Y`           | 重做          |
| `Ctrl+S`           | 保存工作流       |
| `Ctrl+O`           | 加载工作流       |
| `Ctrl+A`           | 全选          |
| `Delete`           | 删除所选项       |
| `Ctrl+M`           | 静音节点        |
| `Ctrl+B`           | 绕过节点        |

## API 使用

### 队列提示

```python
import json
import urllib.request

# 若需外部访问，请使用你的 http_pub URL：
SERVER = "your-http-pub.clorecloud.net"
# 或通过 SSH：SERVER = "localhost:8188"

def queue_prompt(prompt, server=SERVER):
    data = json.dumps({"prompt": prompt}).encode('utf-8')
    req = urllib.request.Request(f"https://{server}/prompt", data=data)
    urllib.request.urlopen(req)

# 加载工作流 JSON 并加入队列
with open("workflow.json") as f:
    workflow = json.load(f)
queue_prompt(workflow)
```

### 进度的 WebSocket

```python
import websocket
import json

# 若需外部访问，请使用带有你的 http_pub URL 的 wss://
ws = websocket.WebSocket()
ws.connect(f"wss://{SERVER}/ws")

while True:
    msg = json.loads(ws.recv())
    if msg['type'] == 'progress':
        print(f"Step {msg['data']['value']}/{msg['data']['max']}")
    elif msg['type'] == 'executed':
        print("完成！")
        break
```

## 性能优化建议

1. **启用 --lowvram** 用于 <8GB 显存
2. **尽可能使用 fp16** 模型
3. **批量大小 1** 用于显存受限时
4. **分块 VAE** 用于高分辨率图像
5. **禁用预览** 以加快生成速度

## GPU 要求

| A100              | 最低显存 | 推荐显存 | 最小内存 |
| ----------------- | ---- | ---- | ---- |
| SD 1.5            | 4GB  | 8GB  | 16GB |
| SDXL              | 8GB  | 12GB | 16GB |
| SDXL + ControlNet | 10GB | 16GB | 16GB |
| FLUX              | 16GB | 24GB | 32GB |

## GPU 预设

### RTX 3060 12GB（入门）

```bash
# 启动并应用优化
python main.py --lowvram --force-fp16

# 推荐设置：
# - SDXL：768x768，批量 1
# - SD 1.5：512x512，批量 4
# - 使用 VAE 平铺
# - 20-30 步
```

**最佳适用：** SD 1.5、SDXL（有一定限制）

### RTX 3090 24GB（理想）

```bash
# 标准启动
python main.py --force-fp16

# 推荐设置：
# - SDXL：1024x1024，批量 2
# - FLUX schnell：1024x1024，批量 1
# - ControlNet + SDXL 效果良好
# - 30-50 步
```

**最佳适用：** SDXL、ControlNet 工作流，中等 FLUX

### RTX 4090 24GB（性能）

```bash
# 全速启动
python main.py

# 推荐设置：
# - SDXL：1024x1024，批量 4
# - FLUX dev：1024x1024，批量 1-2
# - 多个 ControlNet 的复杂工作流
# - 为获得高质量建议 50+ 步
```

**最佳适用：** FLUX、复杂工作流、批量生成

### A100 40GB/80GB（生产）

```bash
# 最大性能
python main.py --highvram

# 推荐设置：
# - SDXL：1024x1024，批量 8+
# - FLUX：1024x1024，批量 2-4
# - 同时加载多个模型
# - 高分辨率输出（2048x2048）
```

**最佳适用：** 生产级工作负载、FLUX、高分辨率生成

## 下载所有所需的检查点

典型 CLORE.AI 市场价格：

| GPU     | 显存   | 价格/天       | SDXL 速度  |
| ------- | ---- | ---------- | -------- |
| 按小时费率   | 12GB | $0.15–0.30 | \~15 秒/图 |
| 速度      | 24GB | $0.30–1.00 | \~8 秒/图  |
| 512x512 | 24GB | $0.50–2.00 | \~4 秒/图  |
| 2s      | 40GB | $1.50–3.00 | \~3 秒/图  |

*价格以美元/天计。费率因提供商而异——请查看* [*CLORE.AI 市场*](https://clore.ai/marketplace) *A100 40GB*

## # 使用固定种子以获得一致结果

### 长时间出现 HTTP 502

1. **检查内存（RAM）：** 服务器必须有 16GB+ RAM
2. **检查显存（VRAM）：** SDXL 需 8GB+，FLUX 需 16GB+
3. **依赖项下载：** 首次运行需 5-10 分钟
4. **模型下载：** 大型模型需更长时间

### 内存不足

```bash
# 使用低显存模式运行
python main.py --lowvram

# 或强制使用 fp16
python main.py --force-fp16
```

### 黑图（全黑图像）

* 检查 VAE 是否已加载
* 尝试不同的 VAE
* 降低图像大小

### 生成速度慢

* 启用 CUDA
* 使用 fp16 模型
* 减少步数（20-30 步通常足够）

## 工作流示例

在 ComfyUI 中导入这些 JSON 工作流：

* [基础 txt2img](https://comfyworkflows.com)
* [SDXL + 精炼器](https://comfyworkflows.com)
* [ControlNet Canny](https://comfyworkflows.com)
* [AnimateDiff 视频](https://comfyworkflows.com)

## 使用以下方式支付

* [ControlNet 指南](https://docs.clore.ai/guides/guides_v2-zh/tu-xiang-chu-li/controlnet-advanced)
* [Real-ESRGAN 放大](https://docs.clore.ai/guides/guides_v2-zh/tu-xiang-chu-li/real-esrgan-upscaling)
* [Kohya 训练](https://docs.clore.ai/guides/guides_v2-zh/xun-lian/kohya-training) - 训练自定义 LoRA
* [- 将重光图像转换为 3D](https://docs.clore.ai/guides/guides_v2-zh/tu-xiang-sheng-cheng/fooocus-simple-sd) - 更简单的替代方案
