From c81bc1e882bbbc35d40c99b7232ece5090ef7daf Mon Sep 17 00:00:00 2001 From: AlekPet Date: Sat, 17 May 2025 16:03:11 +0300 Subject: [PATCH] update ChatGLMNode --- ChatGLMNode/.gitignore | 1 + ChatGLMNode/README.md | 152 +++++++++++++++++++++++++++++++- ChatGLMNode/chatglm_node.py | 62 ++++++++----- ChatGLMNode/config.json | 4 - ChatGLMNode/config.json.example | 6 ++ pyproject.toml | 2 +- 6 files changed, 200 insertions(+), 27 deletions(-) create mode 100644 ChatGLMNode/.gitignore delete mode 100644 ChatGLMNode/config.json create mode 100644 ChatGLMNode/config.json.example diff --git a/ChatGLMNode/.gitignore b/ChatGLMNode/.gitignore new file mode 100644 index 0000000..0cffcb3 --- /dev/null +++ b/ChatGLMNode/.gitignore @@ -0,0 +1 @@ +config.json \ No newline at end of file diff --git a/ChatGLMNode/README.md b/ChatGLMNode/README.md index c2eb89d..89261b5 100644 --- a/ChatGLMNode/README.md +++ b/ChatGLMNode/README.md @@ -2,13 +2,163 @@ Translation is carried out with the help of artificial intelligence using GLM models. +
+Language Codes List + +- af – Afrikaans +- sq – Albanian +- am – Amharic +- ar – Arabic +- hy – Armenian +- as – Assamese +- ay – Aymara +- az – Azerbaijani +- bm – Bambara +- eu – Basque +- be – Belarusian +- bn – Bengali +- bho – Bhojpuri +- bs – Bosnian +- bg – Bulgarian +- ca – Catalan +- ceb – Cebuano +- ny – Chichewa +- zh-CN – Chinese (Simplified) +- zh-TW – Chinese (Traditional) +- co – Corsican +- hr – Croatian +- cs – Czech +- da – Danish +- dv – Divehi +- doi – Dogri +- nl – Dutch +- en – English +- eo – Esperanto +- et – Estonian +- ee – Ewe +- tl – Filipino +- fi – Finnish +- fr – French +- fy – Frisian +- gl – Galician +- ka – Georgian +- de – German +- el – Greek +- gn – Guarani +- gu – Gujarati +- ht – Haitian Creole +- ha – Hausa +- haw – Hawaiian +- iw – Hebrew +- hi – Hindi +- hmn – Hmong +- hu – Hungarian +- is – Icelandic +- ig – Igbo +- ilo – Ilocano +- id – Indonesian +- ga – Irish +- it – Italian +- ja – Japanese +- jw – Javanese +- kn – Kannada +- kk – Kazakh +- km – Khmer +- rw – Kinyarwanda +- gom – Konkani +- ko – Korean +- kri – Krio +- ku – Kurdish (Kurmanji) +- ckb – Kurdish (Sorani) +- ky – Kyrgyz +- lo – Lao +- la – Latin +- lv – Latvian +- ln – Lingala +- lt – Lithuanian +- lg – Luganda +- lb – Luxembourgish +- mk – Macedonian +- mai – Maithili +- mg – Malagasy +- ms – Malay +- ml – Malayalam +- mt – Maltese +- mi – Maori +- mr – Marathi +- mni-Mtei – Meiteilon (Manipuri) +- lus – Mizo +- mn – Mongolian +- my – Myanmar (Burmese) +- ne – Nepali +- no – Norwegian +- or – Odia (Oriya) +- om – Oromo +- ps – Pashto +- fa – Persian +- pl – Polish +- pt – Portuguese +- pa – Punjabi +- qu – Quechua +- ro – Romanian +- ru – Russian +- sm – Samoan +- sa – Sanskrit +- gd – Scots Gaelic +- nso – Sepedi +- sr – Serbian +- st – Sesotho +- sn – Shona +- sd – Sindhi +- si – Sinhala +- sk – Slovak +- sl – Slovenian +- so – Somali +- es – Spanish +- su – Sundanese +- sw – Swahili +- sv – Swedish +- tg – Tajik +- ta – Tamil +- tt – Tatar +- te – Telugu +- th – Thai +- ti – Tigrinya +- ts – Tsonga +- tr – Turkish +- tk – Turkmen +- ak – Twi +- uk – Ukrainian +- ur – Urdu +- ug – Uyghur +- uz – Uzbek +- vi – Vietnamese +- cy – Welsh +- xh – Xhosa +- yi – Yiddish +- yo – Yoruba +- zu – Zulu + +
+ ### Install and use: 1. Install my custom nodes in ComfyUI, used [HERE](https://github.com/AlekPet/ComfyUI_Custom_Nodes_AlekPet#installing) 2. To use ChatGMLNode, you need to register on the site [bigmodel.cn](https://bigmodel.cn/) and get an API key. Free model is the `'glm-4-flash'`! -3. Inside folder **ChatGMLNode** find file `config.json` and add API key geting in point 2, and change `"ZHIPUAI_API_KEY": "your_api_key"` on your API Key. +3. Inside folder **ChatGMLNode** find file `config.json.example` rename to config.json and add API key geting in point 2 in propertu `"ZHIPUAI_API_KEY": "your_api_key"` on your API Key. 4. Run comfyui and add node `ChatGLM4TranslateCLIPTextEncodeNode` or `ChatGLM4TranslateTextNode`. +Optional: You can also set initial language values ​​when creating nodes in the file `config.json`. + +```json +{ + "__comment": "Register on the site https://bigmodel.cn and get a key and add it to the field ZHIPUAI_API_KEY. Change default translate languages ​​'from' and 'to' you use", + "from_translate": "ru", + "to_translate": "en", + "ZHIPUAI_API_KEY": "your_api_key" +} +``` + > Includes: > **ChatGLM4TranslateCLIPTextEncodeNode** - translate text, and return CONDITIONING diff --git a/ChatGLMNode/chatglm_node.py b/ChatGLMNode/chatglm_node.py index bfe8dcc..0f62cfb 100644 --- a/ChatGLMNode/chatglm_node.py +++ b/ChatGLMNode/chatglm_node.py @@ -13,34 +13,52 @@ ALL_CODES_LANGS = ['af', 'sq', 'am', 'ar', 'hy', 'as', 'ay', 'az', 'bm', 'eu', ' ENDPOINT_URL = "https://open.bigmodel.cn/api/paas/v4/chat/completions" -ZHIPUAI_API_KEY = None +def getConfigData(): + # Directory node and config file + dir_node = os.path.dirname(__file__) + config_path = os.path.join(os.path.abspath(dir_node), "config.json") + config = { + "__comment": "Register on the site https://bigmodel.cn and get a key and add it to the field ZHIPUAI_API_KEY. Change default translate languages ​​'from' and 'to' you use", + "from_translate": "ru", + "to_translate": "en", + "ZHIPUAI_API_KEY": "your_api_key" + } -# Directory node and config file -dir_node = os.path.dirname(__file__) -config_path = os.path.join(os.path.abspath(dir_node), "config.json") + # Load config.js file + if not os.path.exists(config_path): + print("[ChatGLMNode] File config.js file not found! Create default config.json...") + with open(config_path, "w", encoding="utf-8") as f: + json.dump(config, f, ensure_ascii=False, indent=4) + return config + else: + with open(config_path, "r") as f: + config = json.load(f) + return config + # ===== -# Load config.js file -if not os.path.exists(config_path): - print("File config.js file not found! Reinstall extensions!") -else: - with open(config_path, "r") as f: - CONFIG = json.load(f) +def checkPropValue(obj, key, not_include = []): + checkVal = lambda v: v is None or v.strip() == "" or v in not_include - # GET ZHIPUAI_API_KEY from json - ZHIPUAI_API_KEY = CONFIG.get("ZHIPUAI_API_KEY") - # ===== + prop_val = obj.get(key) + if checkVal(prop_val): + obj.update(getConfigData()) + return True if checkVal(obj.get(key)) else False + + else: + return False + + +CONFIG = getConfigData() def createRequest(payload): - global ZHIPUAI_API_KEY + global CONFIG - if ( - ZHIPUAI_API_KEY is None - or ZHIPUAI_API_KEY.strip() == "" - or ZHIPUAI_API_KEY == "your_api_key" - ): + if checkPropValue(CONFIG, "ZHIPUAI_API_KEY", ["your_api_key"]): raise ValueError("ZHIPUAI_API_KEY value is empty or missing") + ZHIPUAI_API_KEY = CONFIG.get("ZHIPUAI_API_KEY") + # Headers headers = { "Authorization": f"Bearer {ZHIPUAI_API_KEY}", @@ -92,15 +110,17 @@ def translate(prompt, srcTrans, toTrans, model, max_tokens, temperature, top_p): class ChatGLM4TranslateCLIPTextEncodeNode: @classmethod def INPUT_TYPES(self): + from_lng = CONFIG.get("from_translate") if CONFIG.get("from_translate") in ALL_CODES_LANGS else "ru" + to_lng = CONFIG.get("to_translate") if CONFIG.get("to_translate") in ALL_CODES_LANGS else "en" return { "required": { "from_translate": ( ALL_CODES_LANGS, - {"default": "ru", "tooltip": "Translation from"}, + {"default": from_lng, "tooltip": "Translation from"}, ), "to_translate": ( ALL_CODES_LANGS, - {"default": "en", "tooltip": "Translation to"}, + {"default": to_lng, "tooltip": "Translation to"}, ), "model": ( [ diff --git a/ChatGLMNode/config.json b/ChatGLMNode/config.json deleted file mode 100644 index 95b4391..0000000 --- a/ChatGLMNode/config.json +++ /dev/null @@ -1,4 +0,0 @@ -{ - "__comment": "Register on the site https://bigmodel.cn and get a key and add it to the field ZHIPUAI_API_KEY", - "ZHIPUAI_API_KEY": "your_api_key" -} diff --git a/ChatGLMNode/config.json.example b/ChatGLMNode/config.json.example new file mode 100644 index 0000000..87958b1 --- /dev/null +++ b/ChatGLMNode/config.json.example @@ -0,0 +1,6 @@ +{ + "__comment": "Register on the site https://bigmodel.cn and get a key and add it to the field ZHIPUAI_API_KEY. Change default translate languages ​​'from' and 'to' you use", + "from_translate": "ru", + "to_translate": "en", + "ZHIPUAI_API_KEY": "your_api_key" +} diff --git a/pyproject.toml b/pyproject.toml index 7412fb3..e82122d 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,7 +1,7 @@ [project] name = "comfyui_custom_nodes_alekpet" description = "Nodes: PoseNode, PainterNode, TranslateTextNode, TranslateCLIPTextEncodeNode, DeepTranslatorTextNode, DeepTranslatorCLIPTextEncodeNode, ArgosTranslateTextNode, ArgosTranslateCLIPTextEncodeNode, ChatGLM4TranslateCLIPTextEncodeNode, ChatGLM4TranslateTextNode, ChatGLM4InstructNode, ChatGLM4InstructMediaNode, DeepLXTranslateTextNode, DeepLXTranslateCLIPTextEncodeNode, PreviewTextNode, HexToHueNode, ColorsCorrectNode, IDENode." -version = "1.0.66" +version = "1.0.67" license = { file = "LICENSE" } [project.urls]