From 277b2878725d442385be501c17b18def871d5a89 Mon Sep 17 00:00:00 2001 From: Level Pixel Dev Date: Mon, 9 Dec 2024 05:04:06 +0600 Subject: [PATCH] Added autotranslator Added autotranslator based on Google Translate --- nodes/text/text_utils_LP.py | 50 +++++++++++++++++++++++++++++++++++-- pyproject.toml | 4 +-- requirements.txt | 6 ++++- 3 files changed, 55 insertions(+), 5 deletions(-) diff --git a/nodes/text/text_utils_LP.py b/nodes/text/text_utils_LP.py index 7c2e21f..c7fabfa 100644 --- a/nodes/text/text_utils_LP.py +++ b/nodes/text/text_utils_LP.py @@ -1,6 +1,8 @@ import re import random import time +from deep_translator import GoogleTranslator +from langdetect import detect class TextChoiceParser: @classmethod @@ -51,11 +53,55 @@ class TextChoiceParser: def IS_CHANGED(s, text, variables="", seed=None): return (text, variables, seed) +class CLIPTextEncodeTranslate: + @classmethod + def INPUT_TYPES(s): + return {"required": {"text": ("STRING", {"multiline": True, "dynamicPrompts": True}), "clip": ("CLIP", )}} + RETURN_TYPES = ("CONDITIONING",) + FUNCTION = "clip_text_encode_translate" + CATEGORY = "LevelPixel/Text" + + def clip_text_encode_translate(self, clip, text): + if text.strip(): + detected_lang = detect(text) + if detected_lang != 'en': + try: + translator = GoogleTranslator(source='auto', target='en') + text = translator.translate(text) + except Exception as e: + print(f"Translation error: {e}") + tokens = clip.tokenize(text) + cond, pooled = clip.encode_from_tokens(tokens, return_pooled=True) + return ([[cond, {"pooled_output": pooled}]], ) + +class TextTranslate: + @classmethod + def INPUT_TYPES(s): + return {"required": {"text": ("STRING", {"default": "text", "multiline": True})}} + RETURN_TYPES = ("STRING",) + RETURN_NAMES = ("text",) + FUNCTION = "text_translate" + CATEGORY = "LevelPixel/Text" + + def text_translate(self, prompt): + if prompt.strip(): + detected_lang = detect(prompt) + if detected_lang != 'en': + try: + translator = GoogleTranslator(source='auto', target='en') + prompt = translator.translate(prompt) + except Exception as e: + print(f"Translation error: {e}") + return (prompt,) + NODE_CLASS_MAPPINGS = { "TextChoiceParser|LP": TextChoiceParser, + "CLIPTextEncodeTranslate|LP": CLIPTextEncodeTranslate, + "TextTranslate|LP": TextTranslate, } NODE_DISPLAY_NAME_MAPPINGS = { "TextChoiceParser|LP": "Text Choice Parser [LP]", -} - + "CLIPTextEncodeTranslate|LP": "CLIP Text Encode Translate [LP]", + "TextTranslate|LP": "Text Translate [LP]", +} \ No newline at end of file diff --git a/pyproject.toml b/pyproject.toml index a39515e..eceda66 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,9 +1,9 @@ [project] name = "comfyui_level_pixel" description = "Various nodes of the Level Pixel company. Includes convenient advanced nodes for working with images from folders; counting files in a folder; cleaning memory; tag filters. Model Unloader, LLM Unloader (GGUF unloaders), Free memory, Tag Filters, Tag Category Filters, Tag Choice Parser, File counter, Image Loader From Path (with counters), Image Remove Background based on RemBG, Autotagger." -version = "1.0.9" +version = "1.1.0" license = { file = "LICENSE" } -dependencies = ["torch>=2.0.1", "torchvision>=0.15.2", "numpy", "matplotlib", "scikit-build-core>=0.10.7", "rembg>=2.0.59", "onnxruntime-gpu>=1.18.0", "onnxruntime>=1.20.0"] +dependencies = ["torch>=2.0.1", "torchvision>=0.15.2", "pillow>=9.4.0", "numpy", "matplotlib", "scikit-build-core>=0.10.7", "rembg>=2.0.59", "onnxruntime-gpu>=1.20.0", "onnxruntime>=1.20.0", "googletrans", "langdetect", "httpcore", "deep_translator"] [project.urls] Repository = "https://github.com/LevelPixel/ComfyUI-LevelPixel" diff --git a/requirements.txt b/requirements.txt index 9eb1cd9..bf80732 100644 --- a/requirements.txt +++ b/requirements.txt @@ -6,4 +6,8 @@ matplotlib scikit-build-core>=0.10.7 rembg>=2.0.59 onnxruntime-gpu>=1.20.0 -onnxruntime>=1.20.0 \ No newline at end of file +onnxruntime>=1.20.0 +googletrans +langdetect +httpcore +deep_translator \ No newline at end of file