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

将ComfyUI工作流部署为API服务

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

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服务 - 汇智网

相关推荐

docker基础知识/尚硅谷docker学习笔记

最近看了好多docker的资料,找了一些尚硅谷docker的教学视频,大概总结了一下前前后后的学习笔记。分享给大家。...

收藏收藏!linux服务器常用服务软件的安装配置

1.安装nginx##安装依赖yum-yinstallgcczlibzlib-develpcre-developensslopenssl-devel##下载wgethttp...

梦幻诛仙12职业1亿元宝版本架设教程(包含资源下载)

架设教程:1;上传脚本zx到bin文件夹,给权限chmod-R777/bin2;输入zx安装宝塔面板输入对应序号,中途输入Y继续...

从0开始,让你的Spring Boot项目跑在Linux服务器

1搭建Linux服务器1.1购买阿里云服务器或安装虚拟机这里建议是CentOS7.X或CentOS8.X,当然其他的Linux如deepin、Ubuntu也可以,只是软件环境的安装包和安装方式...

ShardingSphere-Proxy分库分表以及多租户安装使用

需求:你提供SAAS服务,你有你有2个租户(商户),各自的数据进各自的库,而你不希望你的微服务java里默认配置多个租户数据源,数据连接池太多,而且后面动态增加也不方便,诸如此类很多问题。...

jenkins2.107+tomcat8+jdk1.8的安装和发布代码3种方式

jenkins2.107+tomcat8+jdk1.8的安装和发布代码3种方式如果对运维课程感兴趣,可以在b站上或csdn上搜索我的账号:运维实战课程,可以关注我,学习更多免费的运维实战技术视频...

Ubuntu16.04.1安装Java8

上篇文章讲解了怎么在Windows下安装Java8《Windows10安装Java8》,这里讲解下怎么在Linux下安装Java。由于之前已经安装了Ubuntu16.04.1《...

spark+python环境搭建

最近项目需要用到spark大数据相关技术,周末有空spark环境搭起来...目标...

Ubuntu下常用软件的安装三——java开发环境的搭建

首先是java开发环境的安装,不管是window下还是linux下,网上都有了好多教程,也非常详细,为什么我还要写呢?难道我写的有什么特别的地方,嘿嘿,其实也没有,只是当作自己的作为初学者的笔记而已...

Docker核心:深入理解Dockerfile

一、概述Dockerfile就是用来构建docke镜像的构建文件,是一段命令脚本,通过脚本可以生成镜像,镜像是一层一层的,脚本是一行一行的命令,每个命令都是一层!!...

WeADMIN ITOSS在CentOS8.2中的安装方法

WeADMINITOSS在CentOS8.2Linux中的安装方法完全免费的WeADMIN(微运维)IT运维软件,面向云和移动时代一体化IT运营支撑系统。该软件由、、ITSM(IT服务管理)、四大...

阿里云核心组件rocketmq,消息队列集群搭建,轻松搞定

消息队列作为高并发系统的核心组件之一,能够帮助业务系统解构提升开发效率和系统稳定性。...

大数据实战丨如何快速搭建一个自己的Spark分布式架构

从零开始搭建我们的Spark平台1、准备centeros环境为了搭建一个真正的的集群环境,并且要做到高可用的架构,我们至少准备三个虚拟机来作为集群节点。因此我购买了三台阿里云的服务器,来作为我们的集群...

linux安装jdk

JDK是啥就不用多介绍了哈,外行的人也不会进来看我的博文。依然记得读大学那会,第一次实验课就是在机房安装jdk,编写HelloWorld程序。时光飞逝啊,一下过了十多年了,挣了不少钱,买了跑车,娶了富...

Center OS8安装JDK8小记

事情是这样的朋友那边是搞.NET的,要调用java那边的一个springboot的resful接口,代码写好了想调试一下,所以想把java程序部署到本地CenterOS8的虚拟机里面,折腾了半个...