diff --git a/py/zhipu_glm4.py b/py/zhipu_glm4.py new file mode 100644 index 0000000..af1958f --- /dev/null +++ b/py/zhipu_glm4.py @@ -0,0 +1,126 @@ +# layerstyle advance +import os +import torch +import base64 +import requests +from io import BytesIO + +import folder_paths +from PIL import Image +from .imagefunc import log, tensor2pil, get_api_key + + +# apikey申请地址:https://bigmodel.cn/usercenter/proj-mgmt/apikeys +class LS_ZhipuImage: + + def __init__(self): + self.NODE_NAME = 'ZhipuGLM4V' + + @classmethod + def INPUT_TYPES(cls): + glm_model_list = ["glm-4v-flash", "glm-4v", "glm-4v-plus"] + return {"required":{ + "image": ("IMAGE",), + "model": (glm_model_list,), + "user_prompt": ("STRING", {"default": "describe this image", "multiline": True}), + }, + "optional": { + } + } + + RETURN_TYPES = ("STRING",) + RETURN_NAMES = ("text",) + FUNCTION = "zhipu_glm4v" + CATEGORY = '😺dzNodes/LayerUtility' + + def zhipu_glm4v(self, image, model, user_prompt,): + from zhipuai import ZhipuAI + client = ZhipuAI(api_key=get_api_key('zhipu_api_key')) # APIKey + + + img = tensor2pil(image).convert('RGB') + + img_data = BytesIO() + img.save(img_data, format="JPEG") + img_url = base64.b64encode(img_data.getvalue()).decode("utf-8") + + messages = [ + {"role": "user", + "content": [ + {"type": "text", + "text": user_prompt + }, + {"type": "image_url", + "image_url": {"url": img_url} + } + ] + } + + ] + + response = client.chat.completions.create( + model=model, # 填写需要调用的模型名称 + messages=messages + ) + ret_message = response.choices[0].message.content + log(f"{self.NODE_NAME} response is: {ret_message}") + + return (ret_message,) + +class LS_ZhipuText: + + def __init__(self): + self.NODE_NAME = 'ZhipuGLM4' + + @classmethod + def INPUT_TYPES(cls): + glm_model_list = ["GLM-4-Flash", "GLM-4-FlashX", "GLM-4-Plus", "GLM-4-Long","GLM-4-Air", "GLM-4-AirX"] + return {"required":{ + "model": (glm_model_list,), + "user_prompt": ("STRING", {"default": "where is the capital of France?", "multiline": True}), + "history_length": ("INT", {"default": 8, "min": 1, "max": 64, "step": 1}), + }, + "optional": { + "history": ("GLM4_HISTORY",), + } + } + + RETURN_TYPES = ("STRING", "GLM4_HISTORY",) + RETURN_NAMES = ("text", "history",) + FUNCTION = "zhipu_glm4" + CATEGORY = '😺dzNodes/LayerUtility' + + def zhipu_glm4(self, model, user_prompt, history_length, history=None): + + from zhipuai import ZhipuAI + client = ZhipuAI(api_key=get_api_key('zhipu_api_key')) # APIKey + + if history is not None: + messages = history["messages"] + messages = messages[-history_length *2:] + else: + messages = [] + task = {"role": "user", "content": user_prompt} + messages.append(task) + + response = client.chat.completions.create( + model=model, # 填写需要调用的模型名称 + messages=messages + ) + ret_message = response.choices[0].message.content + messages.append({"role": "assistant", "content": ret_message}) + log(f"{self.NODE_NAME} response is: {ret_message}") + + return (ret_message, {"messages":messages},) + + +NODE_CLASS_MAPPINGS = { + "LayerUtility: ZhipuGLM4V": LS_ZhipuImage, + "LayerUtility: ZhipuGLM4": LS_ZhipuText, +} + +NODE_DISPLAY_NAME_MAPPINGS = { + "LayerUtility: ZhipuGLM4V": "LayerUtility: ZhipuGLM4V(Advance)", + "LayerUtility: ZhipuGLM4": "LayerUtility: ZhipuGLM4(Advance)", +} + diff --git a/pyproject.toml b/pyproject.toml index a7b1442..a831c10 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,15 +1,15 @@ [project] -name = "comfyui_layerstyle" -description = "A set of nodes for ComfyUI it generate image like Adobe Photoshop's Layer Style. the Drop Shadow is first completed node, and follow-up work is in progress." -version = "2.0.13" +name = "comfyui_layerstyle_advance" +description = "The nodes detached from ComfyUI Layer Style are mainly those with complex requirements for dependency packages." +version = "2.0.10" license = "MIT" -dependencies = ["numpy", "pillow", "torch", "matplotlib", "Scipy", "scikit_image", "scikit_learn", "opencv-contrib-python", "pymatting", "timm", "colour-science", "transformers", "blend_modes", "huggingface_hub", "loguru"] +dependencies = ["numpy", "matplotlib", "scikit_image", "scikit_learn", "opencv-contrib-python", "pymatting", "timm", "blend_modes", "transformers", "diffusers", "loguru", "colour-science", "huggingface_hub", "segment_anything", "addict", "omegaconf", "yapf", "wget", "iopath", "mediapipe", "typer_config", "fastapi", "rich", "google-generativeai", "ultralytics", "transparent-background", "accelerate", "onnxruntime", "bitsandbytes", "peft", "protobuf", "hydra-core", "blind-watermark", "qrcode", "pyzbar", "psd-tools", "wandb"] [project.urls] -Repository = "https://github.com/chflame163/ComfyUI_LayerStyle" +Repository = "https://github.com/chflame163/ComfyUI_LayerStyle_Advance" # Used by Comfy Registry https://comfyregistry.org [tool.comfy] PublisherId = "chflame163" -DisplayName = "ComfyUI_LayerStyle" +DisplayName = "ComfyUI_LayerStyle_Advance" Icon = "" diff --git a/requirements.txt b/requirements.txt index 834671b..5b9633a 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,15 +1,37 @@ numpy<2.0 -pillow -torch matplotlib -Scipy scikit_image scikit_learn opencv-contrib-python pymatting timm -colour-science -transformers>=4.43.2 blend_modes -huggingface_hub>=0.23.4 +transformers>=4.45.0 +diffusers loguru +colour-science +huggingface_hub>=0.23.4 +segment_anything +addict +omegaconf +yapf +wget +iopath +mediapipe +typer_config +fastapi +rich +google-generativeai +ultralytics>=8.2.0 +transparent-background +accelerate>=0.26.0 +onnxruntime +bitsandbytes>=0.41.1 +peft>=0.12.0 +protobuf +hydra-core +blind-watermark +qrcode +pyzbar +psd-tools +zhipuai \ No newline at end of file