> ## Content Index
> Fetch the complete content index at: https://www.hubwiz.com/blog/llms.txt
> Use this file to discover other available public pages before exploring further.

# 将ComfyUI工作流部署为API服务
- URL: https://www.hubwiz.com/blog/deploy-comfyui-workflow-as-api-service/
- Published: 2024-12-04T08:09:45.000Z
- Updated: 2024-12-04T08:09:45.000Z
- Description: 本文介绍如何使用Baseten将ComfyUI工作流部署为API服务。
- Author: admin
- Tags: TOOL, COMFYUI, BASETEN, WORKFLOW TO API, #Import 2026-10-14 14:41

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

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

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

## 1、自定义 ComfyUI 工作流

风格转换如今风靡一时，因此让我们在 Baseten 上部署一个风格转换工作流，将宠物图片转换为动漫风格。我们将使用的工作流可在[此处](https://openart.ai/workflows/willling/animal-2-anime/NoE7zH8tJ4KlrXbFr6AG?ref=hubwiz.com)找到。

![](https://www.hubwiz.com/blog/content/images/2024/12/image-289.png)

首先，让我们从 [Truss 示例 Github 存储库](https://github.com/basetenlabs/truss-examples/tree/main/comfyui-truss?ref=hubwiz.com)中获取 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 模型](https://www.baseten.co/blog/how-to-serve-your-comfyui-model-behind-an-api-endpoint/?ref=hubwiz.com)。

对于本教程，可以从[此处](https://github.com/basetenlabs/truss-examples/blob/main/comfyui-truss/examples/anime-style-transfer/workflow.json?ref=hubwiz.com)复制工作流文件。此工作流有两个输入：提示和图像。我们可以使用 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 密钥](https://docs.baseten.co/quickstart?%5Fgl=1%2A1synmvs%2A%5Fgcl%5Fau%2AMTY2MDEzNDM0MC4xNzMzMjk4ODMx&ref=hubwiz.com#get-your-api-key)。此命令将打包你的 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 对象并将其保存为图像。

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

![](https://www.hubwiz.com/blog/content/images/2024/12/image-290.png)

![](https://www.hubwiz.com/blog/content/images/2024/12/image-291.png)

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

---

原文链接：[Deploying custom ComfyUI workflows as APIs](https://www.baseten.co/blog/deploying-custom-comfyui-workflows-as-apis/?ref=hubwiz.com)

汇智网翻译整理，转载请标明出处