Serverless ComfyUI API
This guide demonstrates how to execute workflows created with ComfyUI on Beam.
ComfyUI is a powerful and modular GUI and backend that allows users to design and execute advanced stable diffusion pipelines.
To facilitate the transition from ComfyUI’s visual workflows to executable Python code, we’ll utilize the ComfyUI-to-Python-Extension. This tool translates ComfyUI workflows into Python scripts, enabling seamless integration with Beam.
Prerequisites
Before proceeding, ensure you have the following:
- ComfyUI: Run
python main.py
from the root of this project to launch the Comfy server. - ComfyUI-to-Python-Extension: Clone this repo in the
custom_nodes
directory.
Designing Your Workflow in ComfyUI
-
Create or Load a Workflow: Use the node-based interface to design your desired pipeline. You can load workflows into ComfyUI by downloading a workflow. In the Comfy server, click
Load
and select a JSON or PNG (which is encoded with the JSON) file to import the workflow. -
Install the ComfyUI-to-Python-Extension:
We’ll use this extension to automatically convert your workflow to a Python file that can be loaded onto Beam. To enable script export options, install the ComfyUI-to-Python-Extension in your ComfyUI/custom_nodes
directory:
- Enable Developer Mode:
- Click the gear icon above the
Queue Prompt
button. - Check the box that says
Enable Dev Mode Options
.
- Export the Workflow:
With Developer Mode enabled, you can export the workflow to a Python script, streamlining the conversion process.
You can also save the workflow as a JSON file (e.g., workflow_api.json
), which you can later convert manually using the ComfyUI-to-Python-Extension and this command:
Preparing for Deployment on Beam
For Beam deployment, you need to adjust your script to point to Beam volumes with the model checkpoints. Here’s a structured approach:
Configuring Paths in folder_paths.py
In ComfyUI, resource paths are defined in folder_paths.py
, which organizes directories for temporary files, user files, and—most importantly—model files. For Beam, update these paths to use volumes:
- Model and Resource Paths:
You can define paths in Beam’s volume directory. Here’s an example of this:
-
Updating Custom Paths:
- Some nodes in ComfyUI require custom paths, often specified in the node’s script or
__init__.py
. Adjust these paths to point to Beam volumes or temporary storage as needed. Checkfolder_paths.py
for any hardcoded paths that may need updating.
- Some nodes in ComfyUI require custom paths, often specified in the node’s script or
-
Optimize Workflow Size:
- ComfyUI initializes from scratch each time it runs. Keeping your workflow lean — especially by reducing the number of nodes — improves startup efficiency on Beam.
Uploading Model Weights to Beam
We’ll upload our model weights to Beam’s storage volumes.
Alternatively, if your models are stored locally, you can upload them manually to a Beam volume using beam cp [FILE-NAME] [VOLUME-NAME]
.
Deploying on Beam
With paths configured, you’re ready to deploy your workflow on Beam.
-
Create a
beam-app.py
File:-
In your project directory, create a file named
beam-app.py
that will serve as your main entry point for your deployment:
-
-
Deploy to Beam:
-
Use the Beam CLI to deploy your application:
-
If your upload takes a long time, make sure that your .beamignore
is excluding large files (like model weights) from getting uploaded. Remember, these large files are already loaded into our Beam Volume, so it’s not necessary to synchronize them to the remote environment a second time.
Additional Resources
- ComfyUI-to-Python-Extension: A tool to convert ComfyUI workflows into Python scripts. GitHub Repository
- ComfyUI-BeamV2: An example template of ComfyUI with dummy scripts for Beam deployment. GitHub Repository
Special thanks to Maayan Bogin for writing this tutorial!
Was this page helpful?