Skip to content

Commit

Permalink
update code ChatGLM, fix check prompts
Browse files Browse the repository at this point in the history
Add to pyproject.toml new nodes
  • Loading branch information
AlekPet committed Oct 31, 2024
1 parent 1dd4e41 commit f3e4403
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 45 deletions.
1 change: 1 addition & 0 deletions ChatGLMNode/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
88 changes: 45 additions & 43 deletions ChatGLMNode/chatglm_translate_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -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]
Expand Down

0 comments on commit f3e4403

Please sign in to comment.