diff --git a/__init__.py b/__init__.py index 3df6614..db74b80 100644 --- a/__init__.py +++ b/__init__.py @@ -1,5 +1,3 @@ -# __init__.py - import os # 导入所有节点类 @@ -10,6 +8,7 @@ from .nodes.baidu_translater import BaiduTranslater from .nodes.display_any import DisplayAny # 添加新的 DisplayAny 节点 from .nodes.abc_math import AbcMath # 确保路径正确 from .nodes.image_size_extractor import ImageSizeExtractor # 添加 ImageSizeExtractor 节点 +from .nodes.tencent_translater import TencentTranslater # 导入 TencentTranslater 节点 # 节点类映射 NODE_CLASS_MAPPINGS = { @@ -20,6 +19,7 @@ NODE_CLASS_MAPPINGS = { "Display_Any": DisplayAny, "Abc_Math": AbcMath, "Image_Size_Extractor": ImageSizeExtractor, # 添加 ImageSizeExtractor 映射 + "Tencent_Translater": TencentTranslater, # 添加 TencentTranslater 映射 } # 节点显示名称映射 @@ -31,6 +31,7 @@ NODE_DISPLAY_NAME_MAPPINGS = { "Display_Any": "Display Any", "Abc_Math": "abc Math", # 显示名称映射 "Image_Size_Extractor": "Image Size Extractor", # 添加 Image Size Extractor 显示名称映射 + "Tencent_Translater": "Tencent Translater", # 添加 Tencent Translater 显示名称映射 } # 定义 Web 目录路径 diff --git a/nodes/tencent_translater.py b/nodes/tencent_translater.py new file mode 100644 index 0000000..e450878 --- /dev/null +++ b/nodes/tencent_translater.py @@ -0,0 +1,83 @@ +import json +import requests + +class TencentTranslater: + + def __init__(self): + pass + + @classmethod + def INPUT_TYPES(s): + input_types = { + "required": { + "Text_A": ("STRING", {"multiline": True}), + "Text_B": ("STRING", {"multiline": True}), + "Translate": ("BOOLEAN", {"default": True}), + "From": (['Auto', 'zh', 'en'], {"default": "Auto"}), + "To": (['en', 'zh'], {"default": "en"}), + } + } + return input_types + + CATEGORY = "KayTool" + + RETURN_TYPES = ("STRING", "STRING") + RETURN_NAMES = ("A", "B") + FUNCTION = "translate_texts" + + DESCRIPTION = "The TencentTranslater node provides a convenient way to translate text using the Tencent Translate API. It supports multiple languages and allows users to specify source and target languages." + + def initData(self, source_lang, target_lang, translate_text): + return { + "header": { + "fn": "auto_translation", + "client_key": "browser-chrome-110.0.0-Mac OS-df4bd4c5-a65d-44b2-a40f-42f34f3535f2-1677486696487" # Hard-coded client key + }, + "type": "plain", + "model_category": "normal", + "source": { + "lang": source_lang, + "text_list": [translate_text] + }, + "target": { + "lang": target_lang + } + } + + def translate_texts(self, Text_A, Text_B, Translate, From, To): + if not Translate: + # If translation is disabled, return the original texts. + return (Text_A, Text_B) + + from_lang_map = {'Auto': None, 'zh': 'zh', 'en': 'en'} + to_lang_map = {'en': 'en', 'zh': 'zh'} + + source_lang = from_lang_map[From] + target_lang = to_lang_map[To] + + def translate_text(text): + if not text: + return "" + url = 'https://transmart.qq.com/api/imt' + post_data = self.initData(source_lang, target_lang, text) + headers = { + 'Content-Type': 'application/json', + 'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36', + 'referer': 'https://transmart.qq.com/zh-CN/index' + } + try: + response = requests.post(url, headers=headers, data=json.dumps(post_data)) + result = response.json() + if response.status_code != 200 or 'auto_translation' not in result or not result['auto_translation']: + error_msg = f"Translation failed with status code {response.status_code}: {result.get('error_msg', 'Unknown error')}" + print(error_msg) + raise RuntimeError(error_msg) + return '\n'.join(result['auto_translation']) + except Exception as e: + print(f"Error during translation: {e}") + raise RuntimeError("Error during translation") + + translated_a = translate_text(Text_A) + translated_b = translate_text(Text_B) + + return (translated_a, translated_b) \ No newline at end of file diff --git a/pyproject.toml b/pyproject.toml index a71fc9e..c63ad0f 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,7 +1,7 @@ [project] name = "kaytool" description ="This is a versatile and ever-expanding toolkit for ComfyUI, offering powerful node functionalities such as “Custom Save Image,” “Professional Color Adjustment,” and “Prompt Enhancer.” Its features include precise image saving with support for ICC color profiles (sRGB/Adobe RGB), metadata embedding, advanced image adjustments (exposure, contrast, color temperature, hue, saturation), professional filter previews, dynamic prompt editing, and high-quality Baidu AI translation." -version = "0.7.5" +version = "0.8.0" license = {file = "LICENSE"} [project.urls] diff --git a/web/appearance.js b/web/appearance.js index a0ec716..b4f1035 100644 --- a/web/appearance.js +++ b/web/appearance.js @@ -18,6 +18,7 @@ const COLOR_THEMES = { const NODE_COLORS = { "Strong_Prompt": "blue", "Baidu_Translater": "green", + "Tencent_Translater": "blue", "Display_Any": "pale_blue", "Color_Adjustment": "pale_blue", "Custom_Save_Image": "black",