From b734cdc18bec039c6dea77f3c46e67ff907e93bf Mon Sep 17 00:00:00 2001 From: DreamFly Date: Mon, 2 Jun 2025 14:42:59 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E5=A4=9A=E9=9A=8F=E6=9C=BA?= =?UTF-8?q?=E7=94=9F=E6=88=90=E6=8F=90=E7=A4=BA=E8=AF=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- __init__.py | 5 + nodes/prompt_randoms.py | 217 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 222 insertions(+) create mode 100644 nodes/prompt_randoms.py diff --git a/__init__.py b/__init__.py index e635ffa..fcbcee3 100644 --- a/__init__.py +++ b/__init__.py @@ -15,6 +15,7 @@ from .nodes.prompt_custom import CustomPrompt from .nodes.prompt_save import SavePrompt from .nodes.prompt_text import InputPrompt, RandomLinePrompt from .nodes.prompt_preview import PreviewPrompt +from .nodes.prompt_randoms import RandomsPrompt, RandomsWeightPrompt @@ -38,6 +39,8 @@ NODE_CLASS_MAPPINGS = { "SavePrompt": SavePrompt, "InputPrompt": InputPrompt, "RandomLinePrompt": RandomLinePrompt, + "RandomsPrompt": RandomsPrompt, + "RandomsWeightPrompt": RandomsWeightPrompt, "PreviewPrompt": PreviewPrompt, } @@ -61,6 +64,8 @@ NODE_DISPLAY_NAME_MAPPINGS = { "SavePrompt": "Save Prompt", "InputPrompt": "Input Prompt", "RandomLinePrompt": "Random Line Prompt", + "RandomsPrompt": "Randoms Prompt", + "RandomsWeightPrompt": "Randoms Weight Prompt", "PreviewPrompt": "Preview Prompt", } diff --git a/nodes/prompt_randoms.py b/nodes/prompt_randoms.py new file mode 100644 index 0000000..d6b0c03 --- /dev/null +++ b/nodes/prompt_randoms.py @@ -0,0 +1,217 @@ +import os +from ..modules.build_prompt import BuildPrompt +from ..modules.build_prompt import buildOptionList, buildClassifyDatas, buildPromptWeight +from ..modules.build_prompt import EMPTY_OPTION, RANDOM_OPTION, DEFAULT_OPTION +from ..modules.config import config + + + +# Randoms Prompt +class RandomsPrompt: + + @classmethod + def INPUT_TYPES(s): + return { + "required": { + # 选择语言 + "language": (["Chinese", "English"], { + "default": "Chinese" + }), + "classify1": (buildOptionList(config.classifies, True, False), { + "default": DEFAULT_OPTION + }), + "classify2": (buildOptionList(config.classifies, True, False), { + "default": DEFAULT_OPTION + }), + "classify3": (buildOptionList(config.classifies, True, False), { + "default": DEFAULT_OPTION + }), + "classify4": (buildOptionList(config.classifies, True, False), { + "default": DEFAULT_OPTION + }), + "classify5": (buildOptionList(config.classifies, True, False), { + "default": DEFAULT_OPTION + }), + "classify6": (buildOptionList(config.classifies, True, False), { + "default": DEFAULT_OPTION + }), + "seed": ("INT", { "default": 0, "min": 0, "max": 0xFFFFFFFFFFFFFFFF }), + # 启用 + "enable": ("BOOLEAN", {"default": True}) + }, + "optional": { + "input_prompt": ("STRING", {"forceInput": True}), + } + } + + RETURN_TYPES = ("STRING",) + RETURN_NAMES = ("text",) + FUNCTION = "randoms_prompt" + CATEGORY = "PromptWrapper" + + def randoms_prompt(self, language, classify1, classify2, classify3, classify4, classify5, classify6, + seed=0, enable=True, input_prompt=""): + + prompt_words = [] + + if input_prompt != "": + prompt_words.append(input_prompt) + + if enable == True: + # 获取Script的目录路径 + script_dir = os.path.dirname(os.path.abspath(__file__)) # Script directory + language_dir = "zh" if language == "Chinese" else "en" + classifys = [classify1, classify2, classify3, classify4, classify5, classify6] + + for classify in classifys: + if classify != EMPTY_OPTION: + classify_datas = buildClassifyDatas(classify) + file_name = f"{classify_datas}.jsonl" + data_file = os.path.join(script_dir, "..", "assets", language_dir, file_name) + + build = BuildPrompt(data_file) + prompt = build.generate_prompt(seed) + + if prompt != "": + prompt_words.append(prompt) + + + output_prompt = "" + if len(prompt_words) > 0: + output_prompt = ", ".join(prompt_words) + + return (output_prompt, ) + + + + + +# Randoms Weight Prompt +class RandomsWeightPrompt: + + @classmethod + def INPUT_TYPES(s): + max_float_value = 2 + return { + "required": { + # 选择语言 + "language": (["Chinese", "English"], { + "default": "Chinese" + }), + "classify1": (buildOptionList(config.classifies, True, False), { + "default": DEFAULT_OPTION + }), + "classify1_weight": ("FLOAT", { + "default": 1, + "min": 0, + "max": max_float_value, + "round": 0.01, + "step": 0.05, + "display": "slider", + }), + "classify2": (buildOptionList(config.classifies, True, False), { + "default": DEFAULT_OPTION + }), + "classify2_weight": ("FLOAT", { + "default": 1, + "min": 0, + "max": max_float_value, + "round": 0.01, + "step": 0.05, + "display": "slider", + }), + "classify3": (buildOptionList(config.classifies, True, False), { + "default": DEFAULT_OPTION + }), + "classify3_weight": ("FLOAT", { + "default": 1, + "min": 0, + "max": max_float_value, + "round": 0.01, + "step": 0.05, + "display": "slider", + }), + "classify4": (buildOptionList(config.classifies, True, False), { + "default": DEFAULT_OPTION + }), + "classify4_weight": ("FLOAT", { + "default": 1, + "min": 0, + "max": max_float_value, + "round": 0.01, + "step": 0.05, + "display": "slider", + }), + "classify5": (buildOptionList(config.classifies, True, False), { + "default": DEFAULT_OPTION + }), + "classify5_weight": ("FLOAT", { + "default": 1, + "min": 0, + "max": max_float_value, + "round": 0.01, + "step": 0.05, + "display": "slider", + }), + "classify6": (buildOptionList(config.classifies, True, False), { + "default": DEFAULT_OPTION + }), + "classify6_weight": ("FLOAT", { + "default": 1, + "min": 0, + "max": max_float_value, + "round": 0.01, + "step": 0.05, + "display": "slider", + }), + "seed": ("INT", { "default": 0, "min": 0, "max": 0xFFFFFFFFFFFFFFFF }), + # 启用 + "enable": ("BOOLEAN", {"default": True}) + }, + "optional": { + "input_prompt": ("STRING", {"forceInput": True}), + } + } + + RETURN_TYPES = ("STRING",) + RETURN_NAMES = ("text",) + FUNCTION = "randoms_weight_prompt" + CATEGORY = "PromptWrapper" + + def randoms_weight_prompt(self, language, classify1, classify1_weight, classify2, classify2_weight, + classify3, classify3_weight, classify4, classify4_weight, classify5, classify5_weight, classify6, classify6_weight, + seed=0, enable=True, input_prompt=""): + + prompt_words = [] + + if input_prompt != "": + prompt_words.append(input_prompt) + + if enable == True: + # 获取Script的目录路径 + script_dir = os.path.dirname(os.path.abspath(__file__)) # Script directory + language_dir = "zh" if language == "Chinese" else "en" + classify_weights = [(classify1, classify1_weight), (classify2, classify2_weight), (classify3, classify3_weight), + (classify4, classify4_weight), (classify5, classify5_weight), (classify6, classify6_weight)] + + for classify_weight in classify_weights: + classify = classify_weight[0] + weight = classify_weight[1] + + if classify != EMPTY_OPTION: + classify_datas = buildClassifyDatas(classify) + file_name = f"{classify_datas}.jsonl" + data_file = os.path.join(script_dir, "..", "assets", language_dir, file_name) + + build = BuildPrompt(data_file) + prompt = buildPromptWeight(build.generate_prompt(seed), weight) + + if prompt != "": + prompt_words.append(prompt) + + + output_prompt = "" + if len(prompt_words) > 0: + output_prompt = ", ".join(prompt_words) + + return (output_prompt, )