Skip to main content

Introduction

ComfyUI is a popular no-code interface for building complex stable diffusion workflows. Its modular setup and intuitive flowchart interface have produced an extensive collection of community workflows. Several websites offer shared workflows: Production-scale deployment guidance for ComfyUI is limited. This tutorial covers deploying ComfyUI pipelines on Cerebrium as autoscaling API endpoints with pay-as-you-go compute. Find the full example code here.

Creating your Comfy UI workflow locally

Create the workflow locally or using a rented GPU from Lambda Labs. Ensure ComfyUI is installed in the local environment. This tutorial uses Stable Diffusion XL and ControlNet to create custom QR codes. Skip to “Export ComfyUI Workflow” if a workflow already exists.
  1. Create the Cerebrium project: cerebrium init 1-comfyui
  2. Clone the ComfyUI GitHub project inside the project directory: git clone https://github.com/comfyanonymous/ComfyUI
  3. Download the following models and install them in the appropriate folders within the ComfyUI folder:
    • SDXL base in models/checkpoints.
    • ControlNet in models/ControlNet.
  4. Run ComfyUI locally from inside the cloned ComfyUI folder: python main.py —force-fp16 on MacOS.
  5. A server should be loaded locally at http://127.0.0.1:8188/‍
The default ComfyUI workflow interface appears in this view. Use this locally running instance to build the image generation pipeline.

Export ComfyUI Workflow

The example GitHub repository contains a workflow.json file. Click the “Load” button on the right to load the workflow. It should appear populated ComfyUI Workflow Don’t worry about the pre-filled values and prompts — they get overridden at inference time. To export the workflow in API format, click the gear icon (settings) in the top-right hovering panel. Ensure that “Enable dev mode” is selected, then close the popup. Save ComfyUI API format A button appears in the right hover panel labeled “Save (API format)”. Use it to save the workflow as “workflow_api.json”

ComfyUI Application

The main code in main.py uses the exported ComfyUI API workflow to create an endpoint. The code:
  1. Initializes the ComfyUI server
  2. Loads the workflow API template
  3. Processes inputs (prompts, images) through the run function
  4. Generates base64-encoded image outputs
Note: Cerebrium runs code outside the run function only during initialization, while the run function handles all subsequent requests. Alter the workflow_api.json file to include placeholders for user values at inference time:
  • Replace line 4, the seed input, with: “{{seed}}”
  • Replace line 45, the input text of node 6 with: “{{positive_prompt}}”
  • Replace line 58, the input text of node 7 with: “{{negative_prompt}}”
  • Replace line 108, the image of node 11 with: “{{controlnet_image}}“

FastAPI app

In addition to the main.py file below (which runs the FastAPI server and initializes ComfyUI on application start), a separate helpers.py file contains utility functions for working with ComfyUI. Find the helper code here. Create a file named helpers.py and copy the code into it.

Deploy ComfyUI Application

Before deploying, define the deployment configuration in cerebrium.toml. This specifies dependencies, hardware, scaling, and any pre-run scripts:
Running cerebrium deploy uploads both the ComfyUI directory and ~10GB of model weights. For slower connections, use the helper file to download model weights directly to the appropriate folders. Create a file called model.json with the following contents:
This configuration tells the helper function where to download models from and where to save them. Models download only on first deployment, with subsequent deploys checking for existing files. This is what your file folder structure should look like: ComfyUI folder structure Deploy the application: cerebrium deploy After successful deployment, make a request to the endpoint with the following JSON payload:
The output contains two responses:
  • A base64-encoded image of the ControlNet input outline, used as input in the flow.
  • A base64-encoded image of the final result.
ComfyUI generated QR code

Conclusion

Cerebrium enables production-ready ComfyUI workflows that automatically scale with demand, with costs aligned to actual compute usage. Share creations by tagging @cerebriumai.