Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
TimPietrusky committed Oct 9, 2023
0 parents commit 0d485ff
Show file tree
Hide file tree
Showing 10 changed files with 895 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.DS_Store
50 changes: 50 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# Use Nvidia CUDA base image
FROM nvidia/cuda:11.8.0-cudnn8-runtime-ubuntu22.04 as base

# Prevents prompts from packages asking for user input during installation
ENV DEBIAN_FRONTEND=noninteractive
# Prefer binary wheels over source distributions for faster pip installations
ENV PIP_PREFER_BINARY=1
# Ensures output from python is printed immediately to the terminal without buffering
ENV PYTHONUNBUFFERED=1

# Install Python, git and other necessary tools
RUN apt-get update && apt-get install -y \
python3.10 \
python3-pip \
git \
wget

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

# Clone ComfyUI repository
RUN git clone https://github.com/comfyanonymous/ComfyUI.git /ComfyUI

# Change working directory to ComfyUI
WORKDIR /ComfyUI

# Install ComfyUI dependencies
RUN pip3 install --no-cache-dir torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu121 \
&& pip3 install --no-cache-dir xformers==0.0.21 \
&& pip3 install -r requirements.txt

# Install runpod
RUN pip3 install runpod

# Download Stable Diffusion XL
# 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

# Add the models
ADD models/checkpoints/sd_xl_base_1.0.safetensors models/checkpoints/
ADD models/checkpoints/sdxl_vae.safetensors models/checkpoints/

# Go back to the root
WORKDIR /

# Add the start and the handler
ADD src/start.sh src/rp_handler.py ./
RUN chmod +x /start.sh

# Start the container
CMD /start.sh
661 changes: 661 additions & 0 deletions LICENSE

Large diffs are not rendered by default.

23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# RunPod Worker Comfy

> ComfyUI via API on [RunPod](https://www.runpod.io/) serverless
<!-- toc -->

- [What](#what)

<!-- tocstop -->

---

## What

* Use ubuntu with cuda driver as base
* make sure to set the correct options as with a1111 from ashley
* clone comfy & install dependencies
* create a start.sh script to start comfy and then the handler
* use the start.sh as the entrypoint in the dockerfile
* create a loop to create the image
* store hte generated image in S3
* return the URL to the image via API
* use the network volume
11 changes: 11 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
version: '3.8'

services:
comfyui:
image: timpietruskyblibla/runpod-worker-comfy:1.0.0
container_name: comfyui-worker
environment:
- NVIDIA_VISIBLE_DEVICES=all
runtime: nvidia
volumes:
- ./data:/data
14 changes: 14 additions & 0 deletions src/rp_handler.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import runpod


def hello_world(job):
job_input = job["input"]
greeting = job_input["greeting"]

if not isinstance(greeting, str):
return {"error": "Please provide a String"}

return f"Hello {greeting}"


runpod.serverless.start({"handler": hello_world})
12 changes: 12 additions & 0 deletions src/start.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/usr/bin/env bash

echo "runpod-worker-comfy: Starting ComfyUI"

# Use libtcmalloc for better memory management
TCMALLOC="$(ldconfig -p | grep -Po "libtcmalloc.so.\d" | head -n 1)"
export LD_PRELOAD="${TCMALLOC}"

python /ComfyUI/main.py --disable-auto-launch --disable-metadata > /workspace/logs/comfy.log 2>&1 &

echo "runpod-worker-comfy: Starting RunPod Handler"
python3 -u /rp_handler.py
118 changes: 118 additions & 0 deletions test/prompt.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
import json
from urllib import request, parse

# This is the ComfyUI api prompt format.

# If you want it for a specific workflow you can "enable dev mode options"
# in the settings of the UI (gear beside the "Queue Size: ") this will enable
# a button on the UI to save workflows in api format.

# keep in mind ComfyUI is pre alpha software so this format will change a bit.

# this is the one for the default workflow
prompt_text = """
{
"3": {
"class_type": "KSampler",
"inputs": {
"cfg": 8,
"denoise": 1,
"latent_image": [
"5",
0
],
"model": [
"4",
0
],
"negative": [
"7",
0
],
"positive": [
"6",
0
],
"sampler_name": "euler",
"scheduler": "normal",
"seed": 8566257,
"steps": 20
}
},
"4": {
"class_type": "CheckpointLoaderSimple",
"inputs": {
"ckpt_name": "sd_xl_base_1.0.safetensors"
}
},
"5": {
"class_type": "EmptyLatentImage",
"inputs": {
"batch_size": 1,
"height": 512,
"width": 512
}
},
"6": {
"class_type": "CLIPTextEncode",
"inputs": {
"clip": [
"4",
1
],
"text": "masterpiece best quality girl"
}
},
"7": {
"class_type": "CLIPTextEncode",
"inputs": {
"clip": [
"4",
1
],
"text": "bad hands"
}
},
"8": {
"class_type": "VAEDecode",
"inputs": {
"samples": [
"3",
0
],
"vae": [
"4",
2
]
}
},
"9": {
"class_type": "SaveImage",
"inputs": {
"filename_prefix": "ComfyUI",
"images": [
"8",
0
]
}
}
}
"""


def queue_prompt(prompt):
p = {"prompt": prompt}
data = json.dumps(p).encode("utf-8")
req = request.Request("http://127.0.0.1:8188/prompt", data=data)
request.urlopen(req)


prompt = json.loads(prompt_text)
# set the text prompt for our positive CLIPTextEncode
prompt["6"]["inputs"]["text"] = "masterpiece best quality man"

# set the seed for our KSampler node
prompt["3"]["inputs"]["seed"] = 5


queue_prompt(prompt)
Empty file added test/requirements.txt
Empty file.
5 changes: 5 additions & 0 deletions test_input.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"input": {
"greeting": "world"
}
}

0 comments on commit 0d485ff

Please sign in to comment.