百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 热门文章 > 正文

将ComfyUI工作流部署为API服务

bigegpt 2025-01-12 11:36 7 浏览

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

相关推荐

10w qps缓存数据库——Redis(redis缓存调优)

一、Redis数据库介绍:Redis:非关系型缓存数据库nosql:非关系型数据库没有表,没有表与表之间的关系,更不存在外键存储数据的形式为key:values的形式c语言写的服务(监听端口),用来存...

Redis系列专题4--Redis配置参数详解

本文基于windowsX64,3.2.100版本讲解,不同版本默认配置参数不同在Redis中,Redis的根目录中有一个配置文件(redis.conf,windows下为redis.windows....

开源一夏 | 23 张图,4500 字从入门到精通解释 Redis

redis是目前出场率最高的NoSQL数据库,同时也是一个开源的数据结构存储系统,在缓存、数据库、消息处理等场景使用的非常多,本文瑞哥就带着大家用一篇文章入门这个强大的开源数据库——Redis。...

redis的简单与集群搭建(redis建立集群)

Redis是什么?是开源免费用c语言编写的单线程高性能的(key-value形式)内存数据库,基于内存运行并支持持久化的nosql数据库作用主要用来做缓存,单不仅仅是做缓存,比如:redis的计数器生...

推荐几个好用Redis图形化客户端工具

RedisPlushttps://gitee.com/MaxBill/RedisPlusRedisPlus是为Redis可视化管理开发的一款开源免费的桌面客户端软件,支持Windows、Linux...

关于Redis在windows上运行及fork函数问题

Redis在将数据库进行持久化操作时,需要fork一个进程,但是windows并不支持fork,导致在持久化操作期间,Redis必须阻塞所有的客户端直至持久化操作完成。微软的一些工程师花费时间在解决在...

你必须懂的Redis十大应用场景(redis常见应用场景)

Redis作为一款高性能的键值存储数据库,在互联网业务中有着广泛的应用。今天,我们就来详细盘点一下Redis的十大常用业务场景,并附上Golang的示例代码和简图,帮助大家更好地理解和应用Redis。...

极简Redis配置(redis的配置)

一、概述Redis的配置文件位于Redis安装目录下,文件名为redis.conf(Windows名为redis.windows.conf,linux下的是redis.conf)你可以通过C...

什么是redis,怎么启动及如何压测

从今天起咱们一起来学习一下关于“redis监控与调优”的内容。一、Redis介绍Redis是一种高级key-value数据库。它跟memcached类似,不过数据可以持久化,而且支持的数据类型很丰富。...

一款全新Redis UI可视化管理工具,支持WebUI和桌面——P3X Redis UI

介绍P3XRedisUI这是一个非常实用的RedisGUI,提供响应式WebUI访问或作为桌面应用程序使用,桌面端是跨平台的,而且完美支持中文界面。Githubhttps://github....

windows系统的服务器快速部署java项目环境地址

1、mysql:https://dev.mysql.com/downloads/mysql/(msi安装包)2、redis:https://github.com/tporadowski/redis/r...

window11 下 redis 下载与安装(windows安装redis客户端)

#热爱编程是一种怎样的体验#window11下redis下载与安装1)各个版本redis下载(windows)https://github.com/MicrosoftArchive/r...

一款轻量级的Redis客户端工具,贼好用!

使用命令行来操作Redis是一件非常麻烦的事情,我们一般会选用客户端工具来操作Redis。今天给大家分享一款好用的Redis客户端工具TinyRDM,它的界面清新又优雅,希望对大家有所帮助!简介Ti...

一个.NET开发且功能强大的Windows远程控制系统

我们致力于探索、分享和推荐最新的实用技术栈、开源项目、框架和实用工具。每天都有新鲜的开源资讯等待你的发现!项目介绍SiMayRemoteMonitorOS是一个基于Windows的远程控制系统,完...

Redis客户端工具详解(4款主流工具)

大家好,我是mikechen。Redis是大型架构的基石,也是大厂最爱考察内容,今天就给大家重点详解4款Redis工具@mikechen本篇已收于mikechen原创超30万字《阿里架构师进阶专题合集...