Skip to content

Commit 7e4b7d0

Browse files
kklemonTimPietruskyMeptl
authored
feat/restore-snapshot (#30)
* feat: provide option to run the handler locally as API * ci: run the workflow on our extended instance * feat: the local API should run on 0.0.0.0 * feat: make the image smaller * ci: use semantic-version to create releases automatically * chore: we don't want to break anyone with a minor release * docs: added section for local API testing * ci: use custom runner * fix: added .releaserc, otherwise semantic-release will complain about a missing "package.json" * feat: support network volumes, skip default models (#16) * Support network volumes * README tweaks * docs: added comment on what is happening * feat: don't overwrite the default paths, but add "runpod_worker_comfy" to have additional paths * docs: updated "bring your own models" --------- Co-authored-by: Tim Pietrusky <[email protected]> * feat: provide access to ComfyUI via web * fix: use the full path to the output image * feat: added env vars COMFY_POLLING_INTERVAL_MS and COMFY_POLLING_MAX_RETRIES * test: added "subfolder" * Implement optional restoring of ComfyUI snapshots * feat: use comfy-cli to install ComfyUI & restore snapshot * fix: install all dependencies from the snapshot * chore: moved example snapshot to test_resources * feat: allow any kind of snapshot file * feat: allow any file that has "snapshot" in its name * ci: added test for "restore-snapshot" * ci: run restore snapshat test automatically * docs: use "snapshots" to bake custom nodes into the docker image * docs: add link to ComfyUI Manager docs on how to export snapshots * ci: use correct path to example_snapshot.json * ci: fix the path * ci: fix the file path * ci: use a mock instead of the actual file * ci: fix the path * chore: don't ignore snapshot.json as people might use this --------- Co-authored-by: Tim Pietrusky <[email protected]> Co-authored-by: Meptl <[email protected]>
1 parent 06164a3 commit 7e4b7d0

File tree

6 files changed

+402
-58
lines changed

6 files changed

+402
-58
lines changed

.github/workflows/test.yml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,10 @@ jobs:
2323
- name: Install dependencies
2424
run: pip install -r requirements.txt
2525

26-
- name: Run tests
26+
- name: Run Python tests
2727
run: python -m unittest discover
28+
29+
- name: Run snapshot restoration tests
30+
run: |
31+
chmod +x tests/test_restore_snapshot.sh
32+
./tests/test_restore_snapshot.sh

Dockerfile

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,39 +7,49 @@ ENV DEBIAN_FRONTEND=noninteractive
77
ENV PIP_PREFER_BINARY=1
88
# Ensures output from python is printed immediately to the terminal without buffering
99
ENV PYTHONUNBUFFERED=1
10+
# Speed up some cmake builds
11+
ENV CMAKE_BUILD_PARALLEL_LEVEL=8
1012

1113
# Install Python, git and other necessary tools
1214
RUN apt-get update && apt-get install -y \
1315
python3.10 \
1416
python3-pip \
1517
git \
16-
wget
18+
wget \
19+
libgl1 \
20+
&& ln -sf /usr/bin/python3.10 /usr/bin/python \
21+
&& ln -sf /usr/bin/pip3 /usr/bin/pip
1722

1823
# Clean up to reduce image size
1924
RUN apt-get autoremove -y && apt-get clean -y && rm -rf /var/lib/apt/lists/*
2025

21-
# Clone ComfyUI repository
22-
RUN git clone https://github.com/comfyanonymous/ComfyUI.git /comfyui
26+
# Install comfy-cli
27+
RUN pip install comfy-cli
28+
29+
# Install ComfyUI
30+
RUN /usr/bin/yes | comfy --workspace /comfyui install --cuda-version 11.8 --nvidia --version 0.2.7
2331

2432
# Change working directory to ComfyUI
2533
WORKDIR /comfyui
2634

27-
# Install ComfyUI dependencies
28-
RUN pip3 install --upgrade --no-cache-dir torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu121 \
29-
&& pip3 install --upgrade -r requirements.txt
30-
3135
# Install runpod
32-
RUN pip3 install runpod requests
36+
RUN pip install runpod requests
3337

3438
# Support for the network volume
3539
ADD src/extra_model_paths.yaml ./
3640

3741
# Go back to the root
3842
WORKDIR /
3943

40-
# Add the start and the handler
41-
ADD src/start.sh src/rp_handler.py test_input.json ./
42-
RUN chmod +x /start.sh
44+
# Add scripts
45+
ADD src/start.sh src/restore_snapshot.sh src/rp_handler.py test_input.json ./
46+
RUN chmod +x /start.sh /restore_snapshot.sh
47+
48+
# Optionally copy the snapshot file
49+
ADD *snapshot*.json /
50+
51+
# Restore the snapshot to install custom nodes
52+
RUN /restore_snapshot.sh
4353

4454
# Stage 2: Download models
4555
FROM base as downloader
@@ -79,4 +89,3 @@ FROM base as final
7989
COPY --from=downloader /comfyui/models /comfyui/models
8090

8191
# Start the container
82-
CMD /start.sh

README.md

Lines changed: 59 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -10,39 +10,40 @@ Read our article here: https://blib.la/blog/comfyui-on-runpod
1010

1111
[![Discord](https://img.shields.io/discord/1091306623819059300?color=7289da&label=Discord&logo=discord&logoColor=fff&style=for-the-badge)](https://discord.com/invite/m3TBB9XEkb)
1212

13-
→ Please also checkout [Captain: The AI Platform](https://github.com/blib-la/captain)
14-
1513
---
1614

1715
<!-- toc -->
1816

1917
- [Quickstart](#quickstart)
2018
- [Features](#features)
2119
- [Config](#config)
22-
* [Upload image to AWS S3](#upload-image-to-aws-s3)
20+
- [Upload image to AWS S3](#upload-image-to-aws-s3)
2321
- [Use the Docker image on RunPod](#use-the-docker-image-on-runpod)
24-
* [Create your template (optional)](#create-your-template-optional)
25-
* [Create your endpoint](#create-your-endpoint)
26-
* [GPU recommendations](#gpu-recommendations)
22+
- [Create your template (optional)](#create-your-template-optional)
23+
- [Create your endpoint](#create-your-endpoint)
24+
- [GPU recommendations](#gpu-recommendations)
2725
- [API specification](#api-specification)
28-
* [JSON Request Body](#json-request-body)
29-
* [Fields](#fields)
30-
+ ["input.images"](#inputimages)
26+
- [JSON Request Body](#json-request-body)
27+
- [Fields](#fields)
28+
- ["input.images"](#inputimages)
3129
- [Interact with your RunPod API](#interact-with-your-runpod-api)
32-
* [Health status](#health-status)
33-
* [Generate an image](#generate-an-image)
34-
+ [Example request for SDXL with cURL](#example-request-for-sdxl-with-curl)
30+
- [Health status](#health-status)
31+
- [Generate an image](#generate-an-image)
32+
- [Example request for SDXL with cURL](#example-request-for-sdxl-with-curl)
3533
- [How to get the workflow from ComfyUI?](#how-to-get-the-workflow-from-comfyui)
3634
- [Bring Your Own Models and Nodes](#bring-your-own-models-and-nodes)
37-
* [Network Volume](#network-volume)
38-
* [Custom Docker Image](#custom-docker-image)
35+
- [Network Volume](#network-volume)
36+
- [Custom Docker Image](#custom-docker-image)
37+
- [Adding Custom Models](#adding-custom-models)
38+
- [Adding Custom Nodes](#adding-custom-nodes)
39+
- [Building the Image](#building-the-image)
3940
- [Local testing](#local-testing)
40-
* [Setup](#setup)
41-
+ [Setup for Windows](#setup-for-windows)
42-
* [Testing the RunPod handler](#testing-the-runpod-handler)
43-
* [Local API](#local-api)
44-
+ [Access the local Worker API](#access-the-local-worker-api)
45-
+ [Access local ComfyUI](#access-local-comfyui)
41+
- [Setup](#setup)
42+
- [Setup for Windows](#setup-for-windows)
43+
- [Testing the RunPod handler](#testing-the-runpod-handler)
44+
- [Local API](#local-api)
45+
- [Access the local Worker API](#access-the-local-worker-api)
46+
- [Access local ComfyUI](#access-local-comfyui)
4647
- [Automatically deploy to Docker hub with GitHub Actions](#automatically-deploy-to-docker-hub-with-github-actions)
4748
- [Acknowledgments](#acknowledgments)
4849

@@ -293,39 +294,52 @@ Note: The folders in the Network Volume are automatically available to ComfyUI w
293294

294295
### Custom Docker Image
295296

296-
If you prefer to include your models directly in the Docker image, follow these steps:
297+
If you prefer to include your models and custom nodes directly in the Docker image, follow these steps:
297298

298299
1. **Fork the Repository**:
299-
300300
- Fork this repository to your own GitHub account.
301301

302-
2. **Add Your Models in the Dockerfile**:
302+
#### Adding Custom Models
303303

304-
- Edit the `Dockerfile` to include your models:
305-
```Dockerfile
306-
RUN wget -O models/checkpoints/sd_xl_base_1.0.safetensors https://huggingface.co/stabilityai/stable-diffusion-xl-base-1.0/resolve/main/sd_xl_base_1.0.safetensors
307-
```
308-
- You can also add custom nodes:
309-
```Dockerfile
310-
RUN git clone https://github.com/<username>/<custom-node-repo>.git custom_nodes/<custom-node-repo>
311-
```
304+
To include additional models in your Docker image, edit the `Dockerfile` and add the download commands:
312305

313-
3. **Build Your Docker Image**:
314-
- Build the **base** image locally:
315-
```bash
316-
docker build -t <your_dockerhub_username>/runpod-worker-comfy:dev-base --target base --platform linux/amd64 .
317-
```
318-
- Build the **sdxl** image locally:
319-
```bash
320-
docker build --build-arg MODEL_TYPE=sdxl -t <your_dockerhub_username>/runpod-worker-comfy:dev-sdxl --platform linux/amd64 .
321-
```
322-
- Build the **sd3** image locally:
323-
```bash
324-
docker build --build-arg MODEL_TYPE=sd3 --build-arg HUGGINGFACE_ACCESS_TOKEN=<your-huggingface-token> -t <your_dockerhub_username>/runpod-worker-comfy:dev-sd3 --platform linux/amd64 .
325-
```
306+
```Dockerfile
307+
RUN wget -O models/checkpoints/sd_xl_base_1.0.safetensors https://huggingface.co/stabilityai/stable-diffusion-xl-base-1.0/resolve/main/sd_xl_base_1.0.safetensors
308+
```
309+
310+
#### Adding Custom Nodes
311+
312+
To include custom nodes in your Docker image:
313+
314+
1. [Export a snapshot from ComfyUI Manager](https://github.com/ltdrdata/ComfyUI-Manager?tab=readme-ov-file#snapshot-manager) that includes all your desired custom nodes
315+
a. Open "Manager > Snapshot Manager"
316+
b. Create a new snapshot by clicking on "Save snapshot"
317+
c. Get the `*_snapshot.json` from your ComfyUI: `ComfyUI/custom_nodes/ComfyUI-Manager/snapshots`
318+
2. Save the snapshot file in the root directory of the project
319+
3. The snapshot will be automatically restored during the Docker build process, see [Building the Image](#building-the-image)
320+
321+
> [!NOTE]
322+
>
323+
> - Some custom nodes may download additional models during installation, which can significantly increase the image size
324+
> - Having many custom nodes may increase ComfyUI's initialization time
325+
326+
#### Building the Image
327+
328+
Build your customized Docker image locally:
329+
330+
```bash
331+
# Build the base image
332+
docker build -t <your_dockerhub_username>/runpod-worker-comfy:dev-base --target base --platform linux/amd64 .
333+
334+
# Build the SDXL image
335+
docker build --build-arg MODEL_TYPE=sdxl -t <your_dockerhub_username>/runpod-worker-comfy:dev-sdxl --platform linux/amd64 .
336+
337+
# Build the SD3 image
338+
docker build --build-arg MODEL_TYPE=sd3 --build-arg HUGGINGFACE_ACCESS_TOKEN=<your-huggingface-token> -t <your_dockerhub_username>/runpod-worker-comfy:dev-sd3 --platform linux/amd64 .
339+
```
326340
327341
> [!NOTE]
328-
> Ensure to specify `--platform linux/amd64` to avoid errors on RunPod, see [issue #13](https://github.com/blib-la/runpod-worker-comfy/issues/13).
342+
> Ensure to specify `--platform linux/amd64` to avoid errors on RunPod, see [issue #13](https://github.com/blib-la/runpod-worker-comfy/issues/13)
329343
330344
## Local testing
331345

src/restore_snapshot.sh

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#!/usr/bin/env bash
2+
3+
set -e
4+
5+
SNAPSHOT_FILE=$(ls /*snapshot*.json 2>/dev/null | head -n 1)
6+
7+
if [ -z "$SNAPSHOT_FILE" ]; then
8+
echo "runpod-worker-comfy: No snapshot file found. Exiting..."
9+
exit 0
10+
fi
11+
12+
echo "runpod-worker-comfy: restoring snapshot: $SNAPSHOT_FILE"
13+
14+
comfy --workspace /comfyui node restore-snapshot "$SNAPSHOT_FILE" --pip-non-url
15+
16+
echo "runpod-worker-comfy: restored snapshot file: $SNAPSHOT_FILE"

0 commit comments

Comments
 (0)