【模型部署】在Dify中接入ComfyUI+Flux實現文生圖 原創
前言
由于在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
1. 下載ComfyUI至本地
git clone https://github.com/comfyanonymous/ComfyUI.git
2. 安裝依賴包
cd ComfyUI
pip install -r requirements.txt
1.3 下載并安裝ComfyUI-Manager
1. 下載ComfyUI-Manager
# 切換目錄至ComfyUI\custom_nodes
cd ComfyUI/custom_nodes
# 下載ComfyUI-Manager
git clone https://github.com/ltdrdata/ComfyUI-Manager.git
2. 安裝依賴包
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
2. 安裝依賴包
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
2. 安裝依賴包
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
備注:
- 通過實際測試,中文提示詞的效果不好,需要英文提示詞。
- 點擊?
?執行?
?,等待5分鐘左右
2. 獲取到生成的圖片
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插件
- 在Dify平臺上創建一個ChatFlow
- 如圖配置相應的節點:
- LLM節點:主要實現對于用戶輸入內容進行提示詞優化,具體內容:
prompt_template = """
請根據用戶輸入內容生成圖像生成的正面提示詞和負面提示詞,按以下JSON格式響應:
{
"positive_prompt": "詳細描述期望元素的提示詞,包含風格/構圖/細節等,請用英文輸出",
"negative_prompt": "需要排除元素的提示詞,如低質量/水印等,請用英文輸出"
}
用戶輸入:{{input}}
"""
4. 代碼執行:該節點實現將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"
}
- ComfyUI:該節點主要用來配置ComfyUI的工作流,具體方法
a.在ComfyUI頁面,修改工作流如下圖:
b. 通過菜單->導出(API),將工作流導出.json文件
c. 在Dify平臺的ComfyUI節點上,將.json內容復制粘貼到Workflow文本框中
- 最后調試并發布,即可實現與Agent對話生成對應的圖片。
說明:由于Macmini4的性能有限,目前生成一張圖的耗時在4-5分鐘之間,未來如果想提升生成圖片的效率和效果,可以考慮換用GPU服務器或者使用第三方的API。
本文轉載自公眾號一起AI技術 作者:熱情的Dongming