-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
152 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import os | ||
import json | ||
import torch | ||
from PIL import Image | ||
import asyncio | ||
import numpy as np | ||
|
||
from .interface import Pipeline | ||
from comfystream.client import ComfyStreamClient | ||
|
||
COMFY_UI_WORKSPACE_ENV = "COMFY_UI_WORKSPACE" | ||
DEFAULT_WORKFLOW_JSON = ''' | ||
{ | ||
"1": { | ||
"inputs": { | ||
"images": ["2", 0] | ||
}, | ||
"class_type": "SaveTensor", | ||
"_meta": { | ||
"title": "SaveTensor" | ||
} | ||
}, | ||
"2": { | ||
"inputs": { | ||
"engine": "depth_anything_vitl14-fp16.engine", | ||
"images": ["3", 0] | ||
}, | ||
"class_type": "DepthAnythingTensorrt", | ||
"_meta": { | ||
"title": "Depth Anything Tensorrt" | ||
} | ||
}, | ||
"3": { | ||
"inputs": {}, | ||
"class_type": "LoadTensor", | ||
"_meta": { | ||
"title": "LoadTensor" | ||
} | ||
} | ||
} | ||
''' | ||
|
||
|
||
class ComfyUI(Pipeline): | ||
def __init__(self, **params): | ||
super().__init__(**params) | ||
|
||
comfy_ui_workspace = os.getenv(COMFY_UI_WORKSPACE_ENV) | ||
self.client = ComfyStreamClient(cwd=comfy_ui_workspace) | ||
|
||
params = {'prompt': json.loads(DEFAULT_WORKFLOW_JSON)} | ||
self.update_params(**params) | ||
|
||
# Comfy will cache nodes that only need to be run once (i.e. a node that loads model weights) | ||
# We can run the prompt once before actual inputs come in to "warmup" | ||
warmup_input = torch.randn(1, 512, 512, 3) | ||
asyncio.get_event_loop().run_until_complete(self.client.queue_prompt(warmup_input)) | ||
|
||
def process_frame(self, image: Image.Image) -> Image.Image: | ||
# Normalize by dividing by 255 to ensure the tensor values are between 0 and 1 | ||
image_np = np.array(image.convert("RGB")).astype(np.float32) / 255.0 | ||
# Convert from numpy to torch.Tensor | ||
# Initially, the torch.Tensor will have shape HWC but we want BHWC | ||
# unsqueeze(0) will add a batch dimension at the beginning of 1 which means we just have 1 image | ||
image_tensor = torch.tensor(image_np).unsqueeze(0) | ||
|
||
# Process using ComfyUI pipeline | ||
result_tensor = asyncio.get_event_loop().run_until_complete(self.client.queue_prompt(image_tensor)) | ||
|
||
# Convert back from Tensor to PIL.Image | ||
result_tensor = result_tensor.squeeze(0) | ||
result_image_np = (result_tensor * 255).byte() | ||
result_image = Image.fromarray(result_image_np.cpu().numpy()) | ||
return result_image | ||
|
||
def update_params(self, **params): | ||
# params['prompt'] is the JSON string with the ComfyUI workflow | ||
self.client.set_prompt(params['prompt']) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
ARG BASE_IMAGE=livepeer/ai-runner:live-base | ||
FROM ${BASE_IMAGE} | ||
|
||
# Create directory for ComfyUI custom nodes and models | ||
RUN mkdir -p /comfyui/custom_nodes | ||
|
||
# Install required Python version | ||
ARG PYTHON_VERSION=3.10 | ||
RUN pyenv install $PYTHON_VERSION && \ | ||
pyenv global $PYTHON_VERSION && \ | ||
pyenv rehash | ||
|
||
# Upgrade pip and install required packages | ||
ARG PIP_VERSION=23.3.2 | ||
ENV PIP_PREFER_BINARY=1 | ||
RUN pip install --no-cache-dir --upgrade pip==${PIP_VERSION} setuptools==69.5.1 wheel==0.43.0 | ||
|
||
# Install ComfyUI-Depth-Anything-Tensorrt Node (https://github.com/yuvraj108c/ComfyUI-Depth-Anything-Tensorrt) | ||
RUN cd /comfyui/custom_nodes && \ | ||
git clone https://github.com/yuvraj108c/ComfyUI-Depth-Anything-Tensorrt.git && \ | ||
cd ComfyUI-Depth-Anything-Tensorrt && \ | ||
pip install -r requirements.txt | ||
|
||
# Upgrade TensorRT to 10.6.0 | ||
RUN pip uninstall -y tensorrt && \ | ||
pip install tensorrt==10.6.0 | ||
|
||
RUN pip install torch==2.5.1 torchvision torchaudio tqdm | ||
|
||
# Install comfystream (which includes ComfyUI) | ||
RUN pip install git+https://github.com/yondonfu/comfystream.git | ||
RUN git clone https://github.com/yondonfu/comfystream.git && \ | ||
cd comfystream && \ | ||
pip install -r requirements.txt && \ | ||
cp -r nodes/tensor_utils /comfyui/custom_nodes/ && \ | ||
cd .. | ||
|
||
# Set up ComfyUI workspace | ||
ENV COMFY_UI_WORKSPACE="/comfyui" | ||
RUN ln -s /comfyui/models /models |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters