【模型部署】在Dify中接入ComfyUI+Flux實(shí)現(xiàn)文生圖 原創(chuàng)
前言
由于在Macmini上部署ComfyUI踩了不少坑,所以本章內(nèi)容將部署過(guò)程和注意事項(xiàng)進(jìn)行了整理記錄,以便方便未來(lái)的復(fù)用。
目標(biāo)
目標(biāo)1:在Macmini上通過(guò)ComfyUI+Flux模型,實(shí)現(xiàn)文生圖片的功能。目標(biāo)2:將服務(wù)整合進(jìn)Dify平臺(tái),實(shí)現(xiàn)對(duì)模型能力的統(tǒng)一管理。
1. 本地部署步驟
為了實(shí)現(xiàn)上述目標(biāo)1,需要完成以下步驟:
- 下載并安裝ComfyUI
- 下載并安裝ComfyUI-Manager
- 下載并安裝ComfyUI-KJNode
- 下載并配置Flux模型
- 配置文生圖片工作流
具體操作步驟如下方法如下
1.1 創(chuàng)建虛擬環(huán)境
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
由于通過(guò)ComfyUI-Manager下載ComfyUI-KJNode之后,經(jīng)常性會(huì)報(bào)節(jié)點(diǎn)丟失的問(wèn)題,所以此處我們手動(dòng)安裝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的顯存有限,所以我們運(yùn)行flux得使用量化版本,因此需要下載ComfyUI-GGUF進(jìn)行GGUF量化模型的運(yùn)行支持。
- 下載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模型
要正常運(yùn)行flux模型,總共需要下載三個(gè)文件:
- FLUX.1-dev-gguf
- t5-v1_1-xxl-encoder-gguf
- ae.safetensors
1.6.1 下載FLUX.1-dev-gguf
由于huggingface.co被墻,所以我們可以使用鏡像站hf-mirror下載。
- 訪問(wèn)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 啟動(dòng)ComfyUI
完成上述工作后,切換至ComfyUI目錄,執(zhí)行以下命令啟動(dòng)ComfyUI:
python main.py --listen 0.0.0.0
備注:--listen 0.0.0.0 代表所有IP都可以訪問(wèn)。
使用瀏覽器訪問(wèn)http://localhost:8188
1.8 配置FLUX GGUF工作流
- 訪問(wèn)https://promptingpixels.com/flux-gguf/
- 按照頁(yè)面提示,下載?
?ComfyUI Workflow?
? - 將下載好的?
?FLUX-GGUF.json?
?拖動(dòng)至瀏覽器中的ComfyUI界面 - 工作流中各個(gè)節(jié)點(diǎn)的模型,如下圖所示:
1.9 試用效果
輸入提示詞在第一個(gè)(即靠上)的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
備注:
- 通過(guò)實(shí)際測(cè)試,中文提示詞的效果不好,需要英文提示詞。
- 點(diǎn)擊?
?執(zhí)行?
?,等待5分鐘左右
2. 獲取到生成的圖片
2. 集成Dify
為了實(shí)現(xiàn)目標(biāo)2,將??ComfyUI?
??集成至??Dify?
??,我們可以借助??Dify平臺(tái)?
??中內(nèi)置的??ComfyUI插件?
?。
2.1 搭建Dify平臺(tái)
- 拉取Dify平臺(tái)代碼
git clone https://github.com/langgenius/dify.git
- 通過(guò)Docker方式安裝Dify平臺(tái)
cd dify
cd docker
cp .env.example .env
docker compose up -d
2.2 授權(quán)ComfyUI插件
訪問(wèn)Dify平臺(tái)https://localhost/
備注:首次登錄Dify平臺(tái)需要注冊(cè)賬號(hào),因?yàn)檩^為簡(jiǎn)單,本章不再贅述。
- 登錄Dify平臺(tái),選擇工具->搜索'ComfyUI'->點(diǎn)擊授權(quán)
- 輸入ComfyUI的URL地址,即:http://localhost:8188 后確認(rèn)即可
2.3 使用ComfyUI插件
- 在Dify平臺(tái)上創(chuàng)建一個(gè)ChatFlow
- 如圖配置相應(yīng)的節(jié)點(diǎn):
- LLM節(jié)點(diǎn):主要實(shí)現(xiàn)對(duì)于用戶輸入內(nèi)容進(jìn)行提示詞優(yōu)化,具體內(nèi)容:
prompt_template = """
請(qǐng)根據(jù)用戶輸入內(nèi)容生成圖像生成的正面提示詞和負(fù)面提示詞,按以下JSON格式響應(yīng):
{
"positive_prompt": "詳細(xì)描述期望元素的提示詞,包含風(fēng)格/構(gòu)圖/細(xì)節(jié)等,請(qǐng)用英文輸出",
"negative_prompt": "需要排除元素的提示詞,如低質(zhì)量/水印等,請(qǐng)用英文輸出"
}
用戶輸入:{{input}}
"""
4. 代碼執(zhí)行:該節(jié)點(diǎn)實(shí)現(xiàn)將LLM輸出的內(nèi)容進(jìn)行提取,將positive_prompt和negative_prompt進(jìn)行輸出,以便提供給ComfyUI進(jìn)行使用。
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:該節(jié)點(diǎn)主要用來(lái)配置ComfyUI的工作流,具體方法
a.在ComfyUI頁(yè)面,修改工作流如下圖:
b. 通過(guò)菜單->導(dǎo)出(API),將工作流導(dǎo)出.json文件
c. 在Dify平臺(tái)的ComfyUI節(jié)點(diǎn)上,將.json內(nèi)容復(fù)制粘貼到Workflow文本框中
- 最后調(diào)試并發(fā)布,即可實(shí)現(xiàn)與Agent對(duì)話生成對(duì)應(yīng)的圖片。
說(shuō)明:由于Macmini4的性能有限,目前生成一張圖的耗時(shí)在4-5分鐘之間,未來(lái)如果想提升生成圖片的效率和效果,可以考慮換用GPU服務(wù)器或者使用第三方的API。
本文轉(zhuǎn)載自公眾號(hào)一起AI技術(shù) 作者:熱情的Dongming
