ComfyUI 快速文生图模型生成

ComfyUI 快速文生图(基于 z_image.json)

目标 :最快速度部署 ComfyUI + Lumina2 Turbo 模型,支持通过 z_image.json 工作流一键生成图片。


1. 环境要求

  • Python 3.12+
  • CUDA 12.4+(驱动版本 ≥ 535)
  • 已安装 Conda

2. 安装 ComfyUI

bash 复制代码
cd /opt/ml/conf
git clone https://github.com/comfyanonymous/ComfyUI.git
cd ComfyUI

conda create -n comfyui python=3.12 -y
conda activate comfyui

pip install --upgrade pip
pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu121

# 修复 NVIDIA 驱动兼容性(驱动版本 12080 需指定 cuBLAS 版本)
pip install --no-deps nvidia-cublas-cu12==12.4.5.8

pip install -r requirements.txt
pip install sqlalchemy alembic blake3 filelock --upgrade

3. 下载模型(关键)

将以下模型放入对应目录:

模型文件 存放路径 来源
z_image_turbo_bf16.safetensors models/unet/ 自定义模型
qwen_3_4b.safetensors models/clip/ Qwen CLIP
ae.safetensors models/vae/acevsok/ae___safetensors/ VAE

目录结构示例

复制代码
/opt/ml/conf/ComfyUI/models/
├── unet/
│   └── z_image_turbo_bf16.safetensors
├── clip/
│   └── qwen_3_4b.safetensors
└── vae/
    └── acevsok/
        └── ae___safetensors/
            └── ae.safetensors

4. 安装必要 Custom Nodes

bash 复制代码
cd /opt/ml/conf/ComfyUI/custom_nodes

git clone https://github.com/ltdrdata/ComfyUI-Manager.git

cd /opt/ml/conf/ComfyUI
conda activate comfyui
pip install -r custom_nodes/ComfyUI-Manager/requirements.txt

5. 启动 ComfyUI

bash 复制代码
cd /opt/ml/conf/ComfyUI
conda activate comfyui

python main.py --listen 0.0.0.0 --port 8188 --disable-auto-launch

6. 导入并使用 z_image.json 工作流

  1. 浏览器打开 ComfyUI(http://服务器IP:8188 或平台子路径)。
  2. 点击右上角 「Load」 按钮。
  3. 选择并上传 z_image.json 文件。
  4. 工作流会自动加载以下节点:
    • UNETLoader → 自动选择 z_image_turbo_bf16.safetensors
    • CLIPLoader → 自动选择 qwen_3_4b.safetensors
    • VAELoader → 自动选择 ae.safetensors
  5. CLIP Text Encode 节点修改提示词(默认:"诸葛来来")。
  6. 点击 「Queue Prompt」 即可生成图片(仅需 3 步)。

7. 后台快速启动脚本

/opt/ml/conf/wuhui 目录下创建 start_comfyui.sh

bash 复制代码
cat > /opt/ml/conf/wuhui/start_comfyui.sh << 'EOF'
#!/bin/bash
set -e
COMFYUI_DIR="/opt/ml/conf/ComfyUI"
CONDA_ENV="comfyui"
LOG_FILE="/opt/ml/conf/wuhui/comfyui.log"
PID_FILE="/opt/ml/conf/wuhui/comfyui.pid"

cd "$COMFYUI_DIR"
source ~/miniconda3/etc/profile.d/conda.sh
conda activate "$CONDA_ENV"

echo "[$(date '+%Y-%m-%d %H:%M:%S')] 启动 ComfyUI ..."
nohup python main.py --listen 0.0.0.0 --port 8188 --disable-auto-launch > "$LOG_FILE" 2>&1 &
echo $! > "$PID_FILE"
echo "ComfyUI 已启动,PID: $(cat $PID_FILE)"
EOF

chmod +x /opt/ml/conf/wuhui/start_comfyui.sh

启动命令:

bash 复制代码
bash /opt/ml/conf/wuhui/start_comfyui.sh

8. 一键生成图片(推荐流程)

  1. 启动 ComfyUI 后导入 z_image.json
  2. 修改提示词 → 点击 Queue Prompt
  3. 生成的图片默认保存在 output/z-image/ 目录下

性能参考(3 步出图):

  • 单张生成时间:约 0.5~1 秒(A100/H20)
  • 显存占用:约 8~12GB

9. API 调用与多提示词批量生成

ComfyUI 原生支持通过 HTTP API 调用工作流。以下脚本可实现多提示词批量生成 ,无需修改 z_image.json

9.1 安装依赖

bash 复制代码
conda activate comfyui
pip install requests

9.2 批量生成脚本(batch_generate.py)

/opt/ml/conf/wuhui 目录下创建脚本:

python 复制代码
#!/usr/bin/env python3
"""
ComfyUI 批量文生图脚本(基于 z_image.json)
支持多提示词自动生成,图片保存到 output/z-image/
"""

import json
import time
import requests
import os
from typing import List

# ================== 配置 ==================
COMFYUI_URL = "http://127.0.0.1:8188"
WORKFLOW_PATH = "/opt/ml/conf/ComfyUI/z_image.json"
OUTPUT_DIR = "/opt/ml/conf/ComfyUI/output/z-image"
PROMPTS = [
    "诸葛来来",
    "a beautiful landscape",
    "cyberpunk city at night",
    "cute cat wearing sunglasses",
]

os.makedirs(OUTPUT_DIR, exist_ok=True)

# ================== 核心函数 ==================
def load_workflow() -> dict:
    with open(WORKFLOW_PATH, "r", encoding="utf-8") as f:
        return json.load(f)

def submit_prompt(workflow: dict, prompt_text: str) -> str:
    """提交工作流到 ComfyUI,返回 prompt_id"""
    wf = json.loads(json.dumps(workflow))  # 深拷贝
    # z_image.json 中 node 45 是提示词节点
    wf["45"]["inputs"]["text"] = prompt_text

    resp = requests.post(f"{COMFYUI_URL}/prompt", json={"prompt": wf})
    if resp.status_code != 200:
        raise Exception(f"提交失败: {resp.text}")
    return resp.json()["prompt_id"]

def wait_for_result(prompt_id: str, timeout: int = 120) -> dict:
    """轮询任务结果"""
    start = time.time()
    while time.time() - start < timeout:
        resp = requests.get(f"{COMFYUI_URL}/history/{prompt_id}")
        if resp.status_code == 200:
            data = resp.json()
            if prompt_id in data and data[prompt_id].get("outputs"):
                return data[prompt_id]
        time.sleep(0.5)
    raise TimeoutError(f"任务 {prompt_id} 超时")

def download_images(result: dict, prompt_text: str, idx: int):
    """下载生成的图片"""
    if "9" not in result.get("outputs", {}):
        print(f"[{idx}] 未找到输出节点 9")
        return

    for i, img_info in enumerate(result["outputs"]["9"].get("images", [])):
        filename = img_info["filename"]
        url = f"{COMFYUI_URL}/view?filename={filename}&type=output"
        save_name = f"{idx:03d}_{prompt_text[:20].replace(' ', '_')}.png"
        save_path = os.path.join(OUTPUT_DIR, save_name)

        r = requests.get(url, stream=True)
        with open(save_path, "wb") as f:
            for chunk in r.iter_content(chunk_size=8192):
                f.write(chunk)
        print(f"[{idx}] 已保存: {save_path}")

# ================== 主流程 ==================
def main():
    print("加载工作流...")
    workflow = load_workflow()

    for idx, prompt in enumerate(PROMPTS):
        print(f"\n[{idx}] 生成提示词: {prompt}")
        try:
            prompt_id = submit_prompt(workflow, prompt)
            result = wait_for_result(prompt_id)
            download_images(result, prompt, idx)
        except Exception as e:
            print(f"[{idx}] 失败: {e}")

    print("\n批量生成完成!")

if __name__ == "__main__":
    main()

9.3 使用方法

bash 复制代码
# 1. 确保 ComfyUI 已启动
bash /opt/ml/conf/wuhui/start_comfyui.sh

# 2. 运行批量生成脚本
cd /opt/ml/conf/wuhui
python batch_generate.py

输出示例

复制代码
[0] 生成提示词: 诸葛来来
[0] 已保存: /opt/ml/conf/ComfyUI/output/z-image/000_诸葛来来.png

[1] 生成提示词: a beautiful landscape
[1] 已保存: /opt/ml/conf/ComfyUI/output/z-image/001_a_beautiful_landscape.png
...
批量生成完成!

10. 注意事项

  • 该工作流使用 Lumina2 Turbo 架构,cfg 必须保持为 1steps 建议为 3
  • 首次加载模型较慢,后续生成速度极快。
  • 如提示词需要中文支持,请确保 qwen_3_4b.safetensors 已正确加载。
  • 批量脚本默认使用 node 45 作为提示词节点,如工作流结构变化请修改对应节点 ID。

11、z-image.json工作流

json 复制代码
{
  "9": {
    "inputs": {
      "filename_prefix": "z-image",
      "images": [
        "43",
        0
      ]
    },
    "class_type": "SaveImage",
    "_meta": {
      "title": "保存图像"
    }
  },
  "39": {
    "inputs": {
      "clip_name": "qwen_3_4b.safetensors",
      "type": "lumina2",
      "device": "default"
    },
    "class_type": "CLIPLoader",
    "_meta": {
      "title": "加载CLIP"
    }
  },
  "40": {
    "inputs": {
      "vae_name": "acevsok/ae___safetensors/ae.safetensors"
    },
    "class_type": "VAELoader",
    "_meta": {
      "title": "加载VAE"
    }
  },
  "41": {
    "inputs": {
      "width": 512,
      "height": 512,
      "batch_size": 1
    },
    "class_type": "EmptySD3LatentImage",
    "_meta": {
      "title": "空Latent图像(SD3)"
    }
  },
  "42": {
    "inputs": {
      "conditioning": [
        "45",
        0
      ]
    },
    "class_type": "ConditioningZeroOut",
    "_meta": {
      "title": "条件零化"
    }
  },
  "43": {
    "inputs": {
      "samples": [
        "44",
        0
      ],
      "vae": [
        "40",
        0
      ]
    },
    "class_type": "VAEDecode",
    "_meta": {
      "title": "VAE解码"
    }
  },
  "44": {
    "inputs": {
      "seed": 291475514551654,
      "steps": 3,
      "cfg": 1,
      "sampler_name": "res_multistep",
      "scheduler": "simple",
      "denoise": 1,
      "model": [
        "47",
        0
      ],
      "positive": [
        "45",
        0
      ],
      "negative": [
        "42",
        0
      ],
      "latent_image": [
        "41",
        0
      ]
    },
    "class_type": "KSampler",
    "_meta": {
      "title": "K采样器"
    }
  },
  "45": {
    "inputs": {
      "text": "诸葛来来",
      "clip": [
        "39",
        0
      ]
    },
    "class_type": "CLIPTextEncode",
    "_meta": {
      "title": "CLIP文本编码"
    }
  },
  "46": {
    "inputs": {
      "unet_name": "z_image_turbo_bf16.safetensors",
      "weight_dtype": "default"
    },
    "class_type": "UNETLoader",
    "_meta": {
      "title": "UNet加载器"
    }
  },
  "47": {
    "inputs": {
      "shift": 3,
      "model": [
        "46",
        0
      ]
    },
    "class_type": "ModelSamplingAuraFlow",
    "_meta": {
      "title": "采样算法(AuraFlow)"
    }
  }
}
相关推荐
HyperAI超神经3 小时前
深度估计准确率冲上0.9,Meta提出VLM³,论证视觉模型天生会学3D,以Qwen3-VL-4B为基础实现多任务的统一建模
人工智能·3d·大模型·多模态·空间推理·3d感知·3d理解
xixixi777773 小时前
空天地通信、高速光模块、AI 智能体攻击、同态加密芯片四大事件解读:AI 算力底座攻防与全域通信同步升级
大数据·人工智能·深度学习·ai·大模型·光模块·智能体
DogDaoDao5 小时前
【GitHub】Hermes Agent 深度技术分析
程序员·大模型·github·ai编程·ai agent·智能体·hermers agent
汤姆yu5 小时前
AI全生命周期七大安全模块落地指南
人工智能·信息安全·大模型
战族狼魂6 小时前
AI 全栈开发实战训练路线(企业级)
人工智能·python·chatgpt·大模型
AI原来如此7 小时前
Claude与ChatGPT激战正酣,国内AI中转站却突破2000家
人工智能·ai·chatgpt·大模型·编程
镜舟科技8 小时前
从 Prompt 到 Context Engineering:如何用 StarRocks 构建 AI Agent 的实时上下文引擎?
starrocks·大模型·prompt·ai agent·数据基础设施·上下文工程
张彦峰ZYF8 小时前
LangGraph Tool Calling 入门:从 @tool 到完整调用链
人工智能·大模型·agent·langgraph·tool calling
像风一样自由20208 小时前
量化压缩实战:INT8 / INT4 / AWQ / GPTQ 全面对比
android·人工智能·语言模型·大模型
嘛也学不会9 小时前
Compact时,大模型干了什么?
人工智能·大模型·agent·压缩上下文·compact