文章目录
Toggle前言
由于在Macmini上部署ComfyUI踩了不少坑,所以本章内容将部署过程和注意事项进行了整理记录,以便方便未来的复用。
目标
目标1:在Macmini上通过ComfyUI+Flux模型,实现文生图片的功能。
目标2:将服务整合进Dify平台,实现对模型能力的统一管理。
1. 本地部署步骤
为了实现上述目标1,需要完成以下步骤:
- 下载并安装ComfyUI
- 下载并安装ComfyUI-Manager
- 下载并安装ComfyUI-KJNode
- 下载并配置Flux模型
- 配置文生图片工作流
具体操作步骤如下方法如下
1.1 创建虚拟环境
conda create -n comfyui python=3.10
conda activate comfyui
1.2 下载并安装comfyUI
-
下载ComfyUI至本地
git clone https://github.com/comfyanonymous/ComfyUI.git
-
安装依赖包
cd ComfyUI
pip install -r requirements.txt
1.3 下载并安装ComfyUI-Manager
- 下载ComfyUI-Manager
# 切换目录至ComfyUI\custom_nodes
cd ComfyUI/custom_nodes
# 下载ComfyUI-Manager
git clone https://github.com/ltdrdata/ComfyUI-Manager.git
- 安装依赖包
cd ComfyUI/custom_nodes/ComfyUI-Manager
pip install -r requirements.txt
1.4 下载并安装ComfyUI-KJNode
由于通过ComfyUI-Manager下载ComfyUI-KJNode之后,经常性会报节点丢失的问题,所以此处我们手动安装ComfyUI-KJNode。
- 下载ComfyUI-KJNode
# 切换目录至ComfyUI\custom_nodes
cd ComfyUI/custom_nodes
# 下载ComfyUI-KJNode
git clone https://github.com/kijai/ComfyUI-KJNodes.git
- 安装依赖包
cd ComfyUI/custom_nodes/ComfyUI-KJNodes
pip install -r requirements.txt
1.5 下载ComfyUI-GGUF
由于Macmini4的显存有限,所以我们运行flux得使用量化版本,因此需要下载ComfyUI-GGUF进行GGUF量化模型的运行支持。
- 下载ComfyUI-GGUF
# 切换目录至ComfyUI\custom_nodes
cd ComfyUI/custom_nodes
# 下载ComfyUI-GGUF
git clone https://github.com/city96/ComfyUI-GGUF.git
- 安装依赖包
cd ComfyUI/custom_nodes/ComfyUI-GGUF
pip install -r requirements.txt
1.6 下载flux模型
要正常运行flux模型,总共需要下载三个文件:
- FLUX.1-dev-gguf
- t5-v1_1-xxl-encoder-gguf
- ae.safetensors
1.6.1 下载FLUX.1-dev-gguf
由于huggingface.co被墙,所以我们可以使用镜像站hf-mirror下载。
- 访问hf-mirror
- 搜索
city96/FLUX.1-dev-gguf
- 在
Files and versions
中选择要下载的模型,例如:flux1-dev-Q4_1.gguf
- 下载之后,保存至
ComfyUI/models/unet
文件夹中
1.6.2 下载t5-v1_1-xxl-encoder-gguf
与上面类似的方法,
- 搜索
city96/t5-v1_1-xxl-encoder-gguf
- 下载
t5-v1_1-xxl-encoder-Q5_K_M.gguf
- 保存至
ComfyUI/models/
文件夹中ComfyUI/models/clip
1.6.3 下载flux_vae
- 搜索
black-forest-labs/FLUX.1-dev
- 下载
ae.safetensors
- 保存至
ComfyUI/models/vae
文件夹中
1.7 启动ComfyUI
完成上述工作后,切换至ComfyUI目录,执行以下命令启动ComfyUI:
python main.py --listen 0.0.0.0
备注:
–listen 0.0.0.0 代表所有IP都可以访问。
使用浏览器访问http://localhost:8188
1.8 配置FLUX GGUF工作流
- 访问https://promptingpixels.com/flux-gguf/
- 按照页面提示,下载
ComfyUI Workflow
- 将下载好的
FLUX-GGUF.json
拖动至浏览器中的ComfyUI界面 - 工作流中各个节点的模型,如下图所示:
1.9 试用效果
- 输入提示词
在第一个(即靠上)的Clip中输入正向提示词:
Vibrant colorful illustration of solar system planets orbiting the Sun, cartoonish style with accurate planetary features (Saturn’s rings, Jupiter’s red spot), cosmic dust and asteroids in the background, glowing orbital paths showing elliptical trajectories, soft stardust effects, Earth prominently positioned, Mercury/Mars/Venus in correct sequence, icy Neptune/Uranus with atmospheric details, educational labels with friendly fonts, warm sunlight casting soft gradients, outer space depth with twinkling stars, 3D floating planet alignment, children-friendly cosmic theme, whimsical but scientifically accurate proportions, dynamic cosmic perspective with comet trails
备注:
- 通过实际测试,中文提示词的效果不好,需要英文提示词。
2. 集成Dify
为了实现目标2,将ComfyUI
集成至Dify
,我们可以借助Dify平台
中内置的ComfyUI插件
。
2.1 搭建Dify平台
- 拉取Dify平台代码
git clone https://github.com/langgenius/dify.git
- 通过Docker方式安装Dify平台
cd dify
cd docker
cp .env.example .env
docker compose up -d
2.2 授权ComfyUI插件
-
访问Dify平台https://localhost/
备注:首次登录Dify平台需要注册账号,因为较为简单,本章不再赘述。
-
登录Dify平台,选择工具->搜索’ComfyUI’->点击授权
-
输入ComfyUI的URL地址,即:http://localhost:8188 后确认即可
2.3 使用ComfyUI插件
prompt_template = """
请根据用户输入内容生成图像生成的正面提示词和负面提示词,按以下JSON格式响应:
{
"positive_prompt": "详细描述期望元素的提示词,包含风格/构图/细节等,请用英文输出",
"negative_prompt": "需要排除元素的提示词,如低质量/水印等,请用英文输出"
}
用户输入:{{input}}
"""
-
代码执行:该节点实现将LLM输出的内容进行提取,将positive_prompt和negative_prompt进行输出,以便提供给ComfyUI进行使用。
def main(model_output: str) -> dict: try: data = json.loads(model_output) return { "positive_prompt": data.get("positive_prompt", "default_positive"), "negative_prompt": data.get("negative_prompt", "default_negative") } except Exception as e: return { "positive_prompt": "default_positive", "negative_prompt": "default_negative" }
5.2 通过菜单->导出(API),将工作流导出.json文件
5.3 在Dify平台的ComfyUI节点上,将.json内容复制粘贴到Workflow文本框中
说明:
由于Macmini4的性能有限,目前生成一张图的耗时在4-5分钟之间,未来如果想提升生成图片的效率和效果,可以考虑换用GPU服务器或者使用第三方的API。