Run Flux on ComfyUI as an API
In this example, we show you how to turn a ComfyUI workflow into a scalable API endpoint.
Quickstart
To run this simple text-to-image Flux Schnell workflow as an API:
- Deploy ComfyUI behind a web endpoint:
- In another terminal, run inference:
The first inference will take ~1m since the container needs to launch the ComfyUI server and load Flux into memory. Successive calls on a warm container should take a few seconds.
Installing ComfyUI
We use comfy-cli to install ComfyUI and its dependencies.
Downloading custom nodes
We’ll also use comfy-cli to download custom nodes, in this case the popular WAS Node Suite.
Use the ComfyUI Registry to find the specific custom node name to use with this command.
See this post for more examples on how to install popular custom nodes like ComfyUI Impact Pack and ComfyUI IPAdapter Plus.
Downloading models
comfy-cli also supports downloading models, but we’ve found it’s faster to use hf_hub_download directly by:
- Enabling faster downloads
- Mounting the cache directory to a Volume
By persisting the cache to a Volume, you avoid re-downloading the models every time you rebuild your image. For more on storing model weights on Modal, see this guide.
Lastly, copy the ComfyUI workflow JSON to the container.
Running ComfyUI interactively
Spin up an interactive ComfyUI server by wrapping the comfy launch command in a Modal Function
and serving it as a web server.
At this point you can run modal serve 06_gpu_and_ml/comfyui/comfyapp.py and open the UI in your browser for the classic ComfyUI experience.
Remember to close your UI tab when you are done developing. This will close the connection with the container serving ComfyUI and you will stop being charged.
Running ComfyUI as an API
To run a workflow as an API:
Stand up a “headless” ComfyUI server in the background when the app starts.
Define an
infermethod that takes in a workflow path and runs the workflow on the ComfyUI server.Create a web handler
apias a web endpoint, so that we can run our workflow as a service and accept inputs from clients.
We group all these steps into a single Modal cls object, which we’ll call ComfyUI.
This serves the workflow_api.json in this repo. When deploying your own workflows, make sure you select the “Export (API)” option in the ComfyUI menu:
More resources
Use memory snapshots to speed up cold starts (check out the
memory_snapshotdirectory on Github)Run a ComfyUI workflow as a Python script
When to use A1111 vs ComfyUI
Understand tradeoffs of parallel processing strategies when scaling ComfyUI