From 7bb8679045c8fde87fe44f146c4370ec2c716c47 Mon Sep 17 00:00:00 2001 From: shenenqing001 Date: Thu, 23 May 2024 18:46:36 +0800 Subject: [PATCH] init --- Dockerfile | 39 +++++++++++++++++++ custom_nodes/image_url_node.py | 57 ++++++++++++++++++++++++++++ custom_nodes/websocket_image_save.py | 46 +++++++++++++++++++++- nodes.py | 15 ++++++++ 4 files changed, 156 insertions(+), 1 deletion(-) create mode 100644 Dockerfile create mode 100644 custom_nodes/image_url_node.py diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 000000000..8db5b70cf --- /dev/null +++ b/Dockerfile @@ -0,0 +1,39 @@ +FROM harbor.intra.ke.com/aistudio/serving/public/arch-inference-serving_dimage:master_202405090948 +LABEL authors="shenenqing001" + +ARG commit=main + +ENV DEBIAN_FRONTEND=noninteractive LANG=en_US.UTF-8 LC_ALL=en_US.UTF-8 + +ARG CONDA_VERSION=py310_23.3.1-0 + + +RUN pip install --upgrade pip -i https://pypi.tuna.tsinghua.edu.cn/simple +RUN pip install opencv-python -i https://pypi.tuna.tsinghua.edu.cn/simple + + +RUN pip install torch -i https://pypi.tuna.tsinghua.edu.cn/simple +RUN pip install torchsde -i https://pypi.tuna.tsinghua.edu.cn/simple +RUN pip install torchvision -i https://pypi.tuna.tsinghua.edu.cn/simple +RUN pip install einops -i https://pypi.tuna.tsinghua.edu.cn/simple +RUN pip install transformers>=4.25.1 -i https://pypi.tuna.tsinghua.edu.cn/simple +RUN pip install safetensors>=0.3.0 -i https://pypi.tuna.tsinghua.edu.cn/simple +RUN pip install aiohttp -i https://pypi.tuna.tsinghua.edu.cn/simple +RUN pip install pyyaml -i https://pypi.tuna.tsinghua.edu.cn/simple +RUN pip install Pillow -i https://pypi.tuna.tsinghua.edu.cn/simple +RUN pip install scipy -i https://pypi.tuna.tsinghua.edu.cn/simple +RUN pip install tqdm -i https://pypi.tuna.tsinghua.edu.cn/simple +RUN pip install psutil -i https://pypi.tuna.tsinghua.edu.cn/simple +RUN pip install kornia>=0.7.1 -i https://pypi.tuna.tsinghua.edu.cn/simple + + +RUN rm -rf /workspace +RUN mkdir -p /workspace +RUN mkdir -p /data0/www/applogs/ +WORKDIR /workspace +COPY . /workspace/ComfyUI + +RUN find /workspace/ComfyUI/custom_nodes -type f -name 'requirements.txt' -exec pip install -r {} \; + +ENV TZ=Asia/Shanghai +ENTRYPOINT python /workspace/ComfyUI/main.py \ No newline at end of file diff --git a/custom_nodes/image_url_node.py b/custom_nodes/image_url_node.py new file mode 100644 index 000000000..dcbf27bf4 --- /dev/null +++ b/custom_nodes/image_url_node.py @@ -0,0 +1,57 @@ + + +import numpy as np +import pyheif +import cv2 +import whatimage +import time +from PIL import Image +import requests +import torch + +def image2format(image: bytes): + + fmt = whatimage.identify_image(image) + if fmt in ["heic", "avif"]: + i = pyheif.read_heif(image) + image = Image.frombytes(mode=i.mode, size=i.size, data=i.data) + image = cv2.cvtColor(np.asarray(image), cv2.COLOR_RGB2BGR) + else: + image = np.asarray(bytearray(image), dtype=np.uint8) + image = cv2.imdecode(image, cv2.IMREAD_COLOR) + + return image + +class image_url_node: + def __init__(self): + pass + + @classmethod + def INPUT_TYPES(s): + return { + "required": { + "image_url": ("STRING", {"default": "null"}), + }, + } + + RETURN_TYPES = ("IMAGE",) + FUNCTION = "download_image" + CATEGORY = "image" + + def download_image(self, image_url): + res = requests.get(image_url, timeout=3).content + image = image2format(res) + + image = image.astype(np.float32) / 255. + image = torch.from_numpy(image) + image = image.unsqueeze(0) + + return (image,) + +NODE_CLASS_MAPPINGS = { + "image_url_node": image_url_node +} + +NODE_DISPLAY_NAME_MAPPINGS = { + "image_url_node": "image_url_node" +} \ No newline at end of file diff --git a/custom_nodes/websocket_image_save.py b/custom_nodes/websocket_image_save.py index 5aa573642..ae7c2910f 100644 --- a/custom_nodes/websocket_image_save.py +++ b/custom_nodes/websocket_image_save.py @@ -1,6 +1,8 @@ +import base64 from PIL import Image, ImageOps from io import BytesIO import numpy as np +import requests import struct import comfy.utils import time @@ -40,6 +42,48 @@ class SaveImageWebsocket: def IS_CHANGED(s, images): return time.time() + +class SaveImageHttp: + @classmethod + def INPUT_TYPES(s): + return {"required": {"images": ("IMAGE",), + "callback_url": ("STRING", {"default": "null"})}, + } + + RETURN_TYPES = () + FUNCTION = "save_images" + + OUTPUT_NODE = True + + CATEGORY = "api/image" + + def save_images(self, images, callback_url): + pbar = comfy.utils.ProgressBar(images.shape[0]) + step = 0 + for image in images: + i = 255. * image.cpu().numpy() + img = Image.fromarray(np.clip(i, 0, 255).astype(np.uint8)) + + buffer = BytesIO() + img.save(buffer, format="PNG") + img_str = base64.b64encode(buffer.getvalue()).decode("utf-8") + + pbar.update_absolute(step, images.shape[0], ("PNG", img, None)) + step += 1 + + req = { + "images": img_str, + } + + requests.post(url=callback_url, json=req) + + return {} + + def IS_CHANGED(s, images): + return time.time() + + NODE_CLASS_MAPPINGS = { "SaveImageWebsocket": SaveImageWebsocket, -} + "SaveImageHttp": SaveImageHttp, +} \ No newline at end of file diff --git a/nodes.py b/nodes.py index 34821ca3f..c5367d507 100644 --- a/nodes.py +++ b/nodes.py @@ -44,6 +44,8 @@ def interrupt_processing(value=True): MAX_RESOLUTION=16384 +model_cache = {} + class CLIPTextEncode: @classmethod def INPUT_TYPES(s): @@ -682,12 +684,16 @@ class VAELoader: #TODO: scale factor? def load_vae(self, vae_name): + if vae_name in model_cache: + return (model_cache[vae_name],) + if vae_name in ["taesd", "taesdxl"]: sd = self.load_taesd(vae_name) else: vae_path = folder_paths.get_full_path("vae", vae_name) sd = comfy.utils.load_torch_file(vae_path) vae = comfy.sd.VAE(sd=sd) + model_cache[vae_name] = vae return (vae,) class ControlNetLoader: @@ -702,7 +708,10 @@ class ControlNetLoader: def load_controlnet(self, control_net_name): controlnet_path = folder_paths.get_full_path("controlnet", control_net_name) + if controlnet_path in model_cache: + return (model_cache[controlnet_path],) controlnet = comfy.controlnet.load_controlnet(controlnet_path) + model_cache[controlnet_path] = controlnet return (controlnet,) class DiffControlNetLoader: @@ -811,7 +820,10 @@ class UNETLoader: def load_unet(self, unet_name): unet_path = folder_paths.get_full_path("unet", unet_name) + if unet_path in model_cache: + return (model_cache[unet_path],) model = comfy.sd.load_unet(unet_path) + return (model,) class CLIPLoader: @@ -831,7 +843,10 @@ class CLIPLoader: clip_type = comfy.sd.CLIPType.STABLE_CASCADE clip_path = folder_paths.get_full_path("clip", clip_name) + if clip_path in model_cache: + return (model_cache[clip_path],) clip = comfy.sd.load_clip(ckpt_paths=[clip_path], embedding_directory=folder_paths.get_folder_paths("embeddings"), clip_type=clip_type) + model_cache[clip_path] = clip return (clip,) class DualCLIPLoader: