百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 技术教程 > 正文

将ComfyUI工作流部署为API服务

csdh11 2024-12-14 10:39 32 浏览

ComfyUI 是一种流行的 GUI,用于支持 Stable Diffusion 工作流程。ComfyUI 为你提供了一个简单的用户界面来运行 Stable Diffusion,而无需了解diffusers的代码。

对于大多数使用 ComfyUI 的工作流程而言,运行自定义节点的能力已变得至关重要。这就是我们构建新 build_commands 功能的原因:你现在可以轻松打包 ComfyUI 工作流程以使用 Baseten 上的任何自定义节点或模型检查点!

让我们一起来看一个例子。

1、自定义 ComfyUI 工作流

风格转换如今风靡一时,因此让我们在 Baseten 上部署一个风格转换工作流,将宠物图片转换为动漫风格。我们将使用的工作流可在此处找到。

首先,让我们从 Truss 示例 Github 存储库中获取 ComfyUI Truss:

git clone https://github.com/basetenlabs/truss-examples.git
cd truss-examples/comfyui-truss

此存储库已包含部署 ComfyUI 工作流所需的所有文件。我们只需要修改两个文件: config.yaml 和 data/comfy_ui_workflow.json。让我们从 config.yaml 开始。

2、在 config.yaml 中添加 build_commands

在 config.yaml 文件中,我们可以指定一个名为 build_commands 的键,它们是将在我们的容器构建过程中运行的 shell 命令。以下是一个例子:

build_commands:
- git clone https://github.com/comfyanonymous/ComfyUI.git
- cd ComfyUI && git checkout b1fd26fe9e55163f780bf9e5f56bf9bf5f035c93 && pip install -r requirements.txt
- cd ComfyUI/custom_nodes && git clone https://github.com/LykosAI/ComfyUI-Inference-Core-Nodes --recursive && cd ComfyUI-Inference-Core-Nodes && pip install -e .[cuda12]
- cd ComfyUI/custom_nodes && git clone https://github.com/ZHO-ZHO-ZHO/ComfyUI-Gemini --recursive && cd ComfyUI-Gemini && pip install -r requirements.txt
- cd ComfyUI/custom_nodes && git clone https://github.com/kijai/ComfyUI-Marigold --recursive && cd ComfyUI-Marigold && pip install -r requirements.txt
- cd ComfyUI/custom_nodes && git clone https://github.com/omar92/ComfyUI-QualityOfLifeSuit_Omar92 --recursive
- cd ComfyUI/custom_nodes && git clone https://github.com/Fannovel16/comfyui_controlnet_aux --recursive && cd comfyui_controlnet_aux && pip install -r requirements.txt
- cd ComfyUI/models/controlnet && wget -O control-lora-canny-rank256.safetensors https://huggingface.co/stabilityai/control-lora/resolve/main/control-LoRAs-rank256/control-lora-canny-rank256.safetensors
- cd ComfyUI/models/controlnet && wget -O control-lora-depth-rank256.safetensors https://huggingface.co/stabilityai/control-lora/resolve/main/control-LoRAs-rank256/control-lora-depth-rank256.safetensors
- cd ComfyUI/models/checkpoints && wget -O dreamshaperXL_v21TurboDPMSDE.safetensors https://civitai.com/api/download/models/351306
- cd ComfyUI/models/loras && wget -O StudioGhibli.Redmond-StdGBRRedmAF-StudioGhibli.safetensors https://huggingface.co/artificialguybr/StudioGhibli.Redmond-V2/resolve/main/StudioGhibli.Redmond-StdGBRRedmAF-StudioGhibli.safetensors

ComfyUI 管理器允许你轻松安装自定义节点。在后台,管理器只需克隆存储库并安装 Python 依赖项。我们可以使用 git clone ... && pip install -r requirements 之类的命令来模拟该行为。

在 build_commands 下,你可以运行任意 shell 命令,例如 git clone、 cd 或 wget。这样,你可以安装任何检查点、LoRA 和 ControlNet,并将它们放在 ComfyUI 内的相应文件夹中。你甚至可以创建新目录,例如 ipadapter。

build_commands 下的每一行都执行 Docker RUN 命令;由于这些命令是在映像构建步骤期间运行的,因此它们会缓存到 Docker 映像中。这样,当你的容器启动时,所有自定义节点和模型都已下载,这大大减少了冷启动的时间。

这是我们将用于示例的完整 config.yaml 文件:

build_commands:
- git clone https://github.com/comfyanonymous/ComfyUI.git
- cd ComfyUI && git checkout b1fd26fe9e55163f780bf9e5f56bf9bf5f035c93 && pip install -r requirements.txt
- cd ComfyUI/custom_nodes && git clone https://github.com/LykosAI/ComfyUI-Inference-Core-Nodes --recursive && cd ComfyUI-Inference-Core-Nodes && pip install -e .[cuda12]
- cd ComfyUI/custom_nodes && git clone https://github.com/ZHO-ZHO-ZHO/ComfyUI-Gemini --recursive && cd ComfyUI-Gemini && pip install -r requirements.txt
- cd ComfyUI/custom_nodes && git clone https://github.com/kijai/ComfyUI-Marigold --recursive && cd ComfyUI-Marigold && pip install -r requirements.txt
- cd ComfyUI/custom_nodes && git clone https://github.com/omar92/ComfyUI-QualityOfLifeSuit_Omar92 --recursive
- cd ComfyUI/custom_nodes && git clone https://github.com/Fannovel16/comfyui_controlnet_aux --recursive && cd comfyui_controlnet_aux && pip install -r requirements.txt
- cd ComfyUI/models/controlnet && wget -O control-lora-canny-rank256.safetensors https://huggingface.co/stabilityai/control-lora/resolve/main/control-LoRAs-rank256/control-lora-canny-rank256.safetensors
- cd ComfyUI/models/controlnet && wget -O control-lora-depth-rank256.safetensors https://huggingface.co/stabilityai/control-lora/resolve/main/control-LoRAs-rank256/control-lora-depth-rank256.safetensors
- cd ComfyUI/models/checkpoints && wget -O dreamshaperXL_v21TurboDPMSDE.safetensors https://civitai.com/api/download/models/351306
- cd ComfyUI/models/loras && wget -O StudioGhibli.Redmond-StdGBRRedmAF-StudioGhibli.safetensors https://huggingface.co/artificialguybr/StudioGhibli.Redmond-V2/resolve/main/StudioGhibli.Redmond-StdGBRRedmAF-StudioGhibli.safetensors
environment_variables: {}
external_package_dirs: []
model_metadata: {}
model_name: ComfyUI Anime Pet Style Transfer
python_version: py310
requirements:
 - websocket-client==1.6.4
 - accelerate==0.23.0
 - opencv-python
resources:
 accelerator: A100
 use_gpu: true
secrets: {}
system_packages:
 - wget
 - ffmpeg
 - libgl1-mesa-glx

3、将 ComfyUI 工作流修改为与 API 兼容的格式

ComfyUI 工作流可以通过以 API 格式导出在 Baseten 上运行。如果你需要帮助相应地转换工作流,请查看我们的博客,了解如何在 API 端点后面提供 ComfyUI 模型。

对于本教程,可以从此处复制工作流文件。此工作流有两个输入:提示和图像。我们可以使用 handlebars 模板 {{prompt}} 和 {{input_image}} 在我们的工作流 JSON 文件中指定这些变量。

就是这样!我们现在可以将我们的 ComfyUI 工作流部署到 Baseten!

4、将ComfyUI 工作流部署到 ??Baseten

要将我们的工作流部署到 ??Baseten,请确保你已安装 truss Python 包。

pip install truss --upgrade

使用 truss_examples/comfyui_truss 作为根目录,我们可以运行以下命令来部署到 Baseten:

truss push --publish

如果出现提示,请粘贴你的 Baseten API 密钥。此命令将打包你的 Truss 并将其部署到 Baseten 的云上。Docker 容器将被构建,然后作为 API 端点部署。

现在我们将运行我们的第一个推理!

5、在ComfyUI API 端点上运行推理

一旦你的模型已部署并处于活动状态,就可以像这样调用 API 端点:

import requests
import os
import base64
from PIL import Image
from io import BytesIO

# Replace the empty string with your model id below
model_id = ""
baseten_api_key = os.environ["BASETEN_API_KEY"]
BASE64_PREAMBLE = "data:image/png;base64,"

def pil_to_b64(pil_img):
   buffered = BytesIO()
   pil_img.save(buffered, format="PNG")
   img_str = base64.b64encode(buffered.getvalue()).decode("utf-8")
   return img_str

def b64_to_pil(b64_str):
   return Image.open(BytesIO(base64.b64decode(b64_str.replace(BASE64_PREAMBLE, ""))))

values = {
 "prompt": "american Shorthair",
 "input_image": {"type": "image", "data": pil_to_b64(Image.open("/path/to/cat.png"))}
}

resp = requests.post(
   f"https://model-{model_id}.api.baseten.co/production/predict",
   headers={"Authorization": f"Api-Key {baseten_api_key}"},
   json={"workflow_values": values}
)

res = resp.json()
results = res.get("result")

for item in results:
   if item.get("format") == "png":
       data = item.get("data")
       img = b64_to_pil(data)
       img.save(f"pet-style-transfer-1.png")

API 调用返回 result,即模型返回的图像列表。 result中每一项的 data是输出图像的 base64 字符串表示。我们将此 base64 字符串转换为 PIL 对象并将其保存为图像。

以下是一些示例输入及其对应的动漫风格化输出:

这就是在 Baseten 上将 ComfyUI 工作流作为 API 端点运行所需的全部内容。现在,借助新的 build_commands 功能,添加你自己的自定义节点和模型检查点变得更加容易。


原文链接:将ComfyUI工作流部署为API服务 - 汇智网

相关推荐

IDEA界面太丑??尝试一下这几个插件

前言IntelliJIDEA主要用于支持Java、Scala、Groovy等语言的开发工具,同时具备支持目前主流的技术和框架,擅长于企业应用、移动应用和Web应用的开发。IntelliJi...

小巧 Vue 页面滚动进度条组件ScrollProgress

今天给大家分享一个轻量级Vue.js全屏滚动进度条组件VueScrollProgress。vue-scroll-progress一款基于vue.js构建的页面滚动进度条组件,...

基于vue实现可视化拖拽编辑器,页面生成工具,提升前端开发效率

项目介绍基于vue实现的可视化拖拽编辑器,实现页面生成工具,提升前端开发效率。可以基层到移动端项目作为自定义json直接生成UI页面。项目特点功能特点...

优秀 vue+heyui 后端管理系统HeyUI-Admin

今天再给小伙伴们推荐一款成熟的企业中后台管理系统HEYUI-Admin。heyui-admin基于vue.js和heyui组件库构建的后台管理系统。包含基础表单/表格功能,拓展组件(图表、富文本编辑...

响应式 Vue.js 前端组件化框架Xvue-UI

今天给小伙伴们推荐一款超不错的Vue轻量级组件框架XVueUI。xvue-ui基于vue2.x构建的响应式前端组件化框架。轻量级、易于上手,提供...

《基于SpringBoot+Vue的在线视频系统设计与实现》开题报告

【计算机毕业设计案例】基于SpringBoot+Vue的在线视频系统设计与实现_哔哩哔哩_bilibili...

超好用 Vue.js 图片裁切组件Vue-ImgCutter

今天给小伙伴们分享一个超棒的Vue图片任意裁剪插件VueImgCutter。vue-img-cutter基于vue2.x构建的轻量级剪切图片组件。支持移动图像、放大缩小图片、任意移动图片、固定比...

Vue 3 进阶用法:异步组件(vue 异步组件原理)

一、代码分割一个大型前端应用,如果所有代码都放在单一文件,体积会特别大,下载时间长,白屏时间久,用户体验差。...

源码补丁神器—patch-package(源码助手怎么用)

作者:张浩一、背景vue项目中使用vue-pdf第三方插件预览pdf,书写业务代码完美运行,pdf文件内容正常预览无问题。后期需求有变,业务需求增加电子签章功能。这个时候pdf文件的内容可以显示出...

经验分享:Vue2 项目升级 Vue3 + Element Plus,借助Deepseek手动升级

Vue3出来好久了,我开发的项目还在使用Vue2框架,一般情况下不考虑升级,但是最近需要接入工作流程引擎之类的,看了下Vue2生态下操作空间不是很好,那索性尝试升级Vue3吧。一番操作下来,升级成功,...

34K Star!史上最全JavaScript资源库 awesome-javascript

34KStar!史上最全JavaScript资源宝库大揭秘引言在GitHub上,有一个备受瞩目的JavaScript资源仓库,以其全面的内容和精心的分类,成为了众多开发者的必备参考。这个拥有超过...

基于 Vue.js 磁片栅格布局组件VueGridLayout

#头条创作挑战赛#今天给大家分享一个超优秀的vue.js拖拽栅格布局插件VueGridLayout。...

6款高颜值 Vue3 PC端UI组件库(vue3开发组件库)

马上到国庆了,还没学习或者想学习vue3的小伙伴们有安排上没?这次推荐几个比较流行的VUE3UI组件库,合理利用,又或者学习借鉴都是不错的选择。1、element-pluselement-plus...

高性能 vue.js+ztree 树形组件Vue-GiantTree

今天给大家分享一款超棒的Vue海量数据渲染树形组件VueGiantTree。vue-giant-tree基于ztree封装的Vue树形组件。轻松实现大数据高性能渲染,适合海量数据渲染场景。zTr...

【推荐】2024年推荐的6款开源免费 Vue 后台管理系统模板,建议收藏

前言在现今的软件开发领域,...