From f3e44033174c1fb5d6cc8355dde590a43565a76f Mon Sep 17 00:00:00 2001 From: AlekPet Date: Thu, 31 Oct 2024 21:56:13 +0300 Subject: [PATCH] update code ChatGLM, fix check prompts Add to pyproject.toml new nodes --- ChatGLMNode/README.md | 1 + ChatGLMNode/chatglm_translate_node.py | 88 ++++++++++++++------------- pyproject.toml | 4 +- 3 files changed, 48 insertions(+), 45 deletions(-) diff --git a/ChatGLMNode/README.md b/ChatGLMNode/README.md index f124710..d9c1eb9 100644 --- a/ChatGLMNode/README.md +++ b/ChatGLMNode/README.md @@ -5,6 +5,7 @@ Translation is carried out with the help of artificial intelligence using GLM mo ### Note: To use it, you need to register on the site [bigmodel.cn](https://bigmodel.cn/) and get an API key. Free model is the 'glm-4-flash'! > Includes: + > **ChatGLM4TranslateCLIPTextEncodeNode** - translate text, and return CONDITIONING > > **ChatGLM4TranslateTextNode** - translate text and return text (STRING) diff --git a/ChatGLMNode/chatglm_translate_node.py b/ChatGLMNode/chatglm_translate_node.py index 36fd514..9c27c73 100644 --- a/ChatGLMNode/chatglm_translate_node.py +++ b/ChatGLMNode/chatglm_translate_node.py @@ -25,50 +25,52 @@ def translate(prompt, srcTrans, toTrans, model, max_tokens, temperature, top_p): - if prompt and prompt.strip() != "": - - # Create body request - payload = { - "model": model, - "messages": [ - { - "role": "user", - "content": f"Translate from {srcTrans} to {toTrans}: {prompt}", - }, - ], - "max_tokens": max_tokens, - "temperature": temperature, - "top_p": top_p, - } - - # Header - headers = { - "Authorization": f"Bearer {ZHIPUAI_API_KEY}", - "Content-Type": "application/json", - } - - try: - response = requests.post(ENDPOINT_URL, headers=headers, json=payload) - response.raise_for_status() - - if response.status_code == 200: - json_data = response.json() - translate_text_prompt = json_data.get("choices")[0]["message"][ - "content" - ].strip() - - return ( - translate_text_prompt if translate_text_prompt and not None else "" - ) - - except requests.HTTPError as e: - print( - f"Error translate text ChatGLM: {response.status_code}, {response.text}" + # Check prompt exist + if prompt is None or prompt.strip() == "": + return "" + + # Create body request + payload = { + "model": model, + "messages": [ + { + "role": "user", + "content": f"Translate from {srcTrans} to {toTrans}: {prompt}", + }, + ], + "max_tokens": max_tokens, + "temperature": temperature, + "top_p": top_p, + } + + # Headers + headers = { + "Authorization": f"Bearer {ZHIPUAI_API_KEY}", + "Content-Type": "application/json", + } + + try: + response = requests.post(ENDPOINT_URL, headers=headers, json=payload) + response.raise_for_status() + + if response.status_code == 200: + json_data = response.json() + translate_text_prompt = json_data.get("choices")[0]["message"][ + "content" + ].strip() + + return ( + translate_text_prompt if translate_text_prompt and not None else "" ) - raise e - except Exception as e: - print(f"Error translate text ChatGLM: {e}") - raise e + + except requests.HTTPError as e: + print( + f"Error translate text ChatGLM: {response.status_code}, {response.text}" + ) + raise e + except Exception as e: + print(f"Error translate text ChatGLM: {e}") + raise e class ChatGLM4TranslateCLIPTextEncodeNode: diff --git a/pyproject.toml b/pyproject.toml index 0e0377b..f743550 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, PreviewTextNode, HexToHueNode, ColorsCorrectNode, IDENode." -version = "1.0.31" +description = "Nodes: PoseNode, PainterNode, TranslateTextNode, TranslateCLIPTextEncodeNode, DeepTranslatorTextNode, DeepTranslatorCLIPTextEncodeNode, ArgosTranslateTextNode, ArgosTranslateCLIPTextEncodeNode, ChatGLM4TranslateCLIPTextEncodeNode, ChatGLM4TranslateTextNode, PreviewTextNode, HexToHueNode, ColorsCorrectNode, IDENode." +version = "1.0.32" license = { file = "LICENSE" } [project.urls]