Deploy a ComfyUI project
Deploy your ComfyUI workflow as an API endpoint
View example on GitHub
In this example, we’ll deploy an anime style transfer ComfyUI workflow using truss. This example won’t require any Python code, but there are a few pre-requisites in order to get started.
Pre-Requisites:
- Convert your ComfyUI workflow to an API compatible JSON format. The regular JSON format that is used to export Comfy workflows will not work here.
- Have a list of the models your workflow requires along with URLs to where each model can be downloaded
Setup
Clone the truss-examples repository and navigate to the comfyui-truss
directory
This repository already contains all the files we need to deploy our ComfyUI workflow. There are just two files we need to modify:
config.yaml
data/comfy_ui_workflow.json
Setting up the config.yaml
The main part that needs to get filled out is under build_commands
. Build commands are shell commands that get run during the build stage of the docker image.
In this example, the first two lines clone the ComfyUI repository and install the python requirements. The latter commands install various custom nodes and models and place them in their respective directory within the ComfyUI repository.
Modifying data/comfy_ui_workflow.json
The comfy_ui_workflow.json
contains the entire ComfyUI workflow in an API compatible format. This is the workflow that will get executed by the ComfyUI server.
Here is the workflow we will be using for this example.
Important:
If you look at the JSON file above, you’ll notice we have templatized a few items using the {{handlebars}}
templating style.
If there are any inputs in your ComfyUI workflow that should be variables such as input prompts, images, etc, you should templatize them using the handlebars format.
In this example workflow, there are two inputs: {{input_image}}
and {{prompt}}
When making an API call to this workflow, we will be able to pass in any variable for these two inputs.
Deploying the Workflow to Baseten
Once you have both your config.yaml
and data/comfy_ui_workflow.json
filled out we can deploy this workflow just like any other model on Baseten.
pip install truss --upgrade
truss push --publish
Running Inference
When you deploy the truss, it will spin up a new deployment in your Baseten account. Each deployment will expose a REST API endpoint which we can use to call this workflow.
If you recall, we templatized two variables in our workflow: prompt
and input_image
. In our API call we can specify the values for these two variables like so:
If your workflow contains more variables, simply add them to the dictionary above.
The API call returns an image in the form of a base64 string, which we convert to a PNG image.