diff --git a/comfy/dynamic_prompt.py b/comfy/dynamic_prompt.py new file mode 100644 index 000000000..af1d647c7 --- /dev/null +++ b/comfy/dynamic_prompt.py @@ -0,0 +1,124 @@ +import re +import random + +# This function is used to strip comments from a string. +# It supports both single-line and multi-line comments of the following formats: +# // Single-line comment +# /* Multi-line comment */ +# +# @param text The string to strip comments from. +# @return The string with comments stripped. +def strip_comments(text): + return re.sub(r'/\*.*?\*/|//.*?$', '', text, flags=re.MULTILINE|re.DOTALL) + +# This function is used to generate a dynamic prompt from a string. +# It allows you to include random text in your prompt, contained within option blocks of the following format: +# {option1|option2|option3} +# +# For example, the following prompt: +# > "This is a {good|great|spectacular} prompt." +# +# Could generate any of the following prompts: +# > "This is a good prompt." +# > "This is a great prompt." +# > "This is a spectacular prompt." +# +# If you need to include a '{' or '}' character in your prompt, you can escape it by doubling it, like so: +# > "This is a {{ and }} character." +# +# Which would generate the following prompt: +# > "This is a { and } character." +# +# It also strips comments from the prompt, so you can include comments in your prompt like so: +# +# @param text The string to generate a dynamic prompt from. +# @return The generated dynamic prompt. +def process_dynamic_prompt(text): + prompt = strip_comments(text) + + new_prompt = "" + options = [] + inside_option = False + + i = 0 + while i < len(prompt): + char = prompt[i] + if char == "{": + if inside_option: + if i < len(prompt) - 1 and prompt[i + 1] == "{": + options[-1] += char + i += 2 + continue + else: + raise ValueError(f"Option block opened at position {i} inside another option block. " + "Each option block should be closed before starting a new one. " + "If you want to include a '{' inside an option block, use '{{'.") + else: + if i < len(prompt) - 1 and prompt[i + 1] == "{": + new_prompt += char + i += 2 + continue + else: + inside_option = True + options.append("") + elif char == "}": + if not inside_option or (i < len(prompt) - 1 and prompt[i + 1] == "}"): + if inside_option: + random_option = random.choice(options[-1].split("|")) + new_prompt += random_option + else: + new_prompt += char + i += 2 + continue + if not inside_option: + raise ValueError(f"Option block closed at position {i} without being opened. " + "Make sure each '{' has a matching '}'. " + "If you want to include a '}' without closing an option block, use '}}'.") + inside_option = False + random_option = random.choice(options[-1].split("|")) + new_prompt += random_option + else: + if inside_option: + options[-1] += char + else: + new_prompt += char + i += 1 + + if inside_option: + raise ValueError("Option block opened but not closed. " + "Make sure each '{' has a matching '}'. " + "If you want to include a '}' without closing an option block, use '}}'.") + + return new_prompt + +# Checks if the given string is a valid dynamic prompt. +# @param text The string to check. +# @return An error message if the string is not a valid dynamic prompt, True otherwise. +def validate_dynamic_prompt(text): + try: + process_dynamic_prompt(text) + except ValueError as e: + print("validate_dynamic_prompt: %s" % str(e)) + return str(e) + + print("validate_dynamic_prompt: True") + return True + +# Checks if the given string contains any dynamic prompts. +# @param text The string to check. +# @return True if the string contains any dynamic prompts, False otherwise. +def contains_dynamic_prompt(text): + # This should check if there are any { or } characters that aren't escaped + return re.search(r'(? len(tokens["g"]): tokens["g"] += empty["g"] cond, pooled = clip.encode_from_tokens(tokens, return_pooled=True) - return ([[cond, {"pooled_output": pooled, "width": width, "height": height, "crop_w": crop_w, "crop_h": crop_h, "target_width": target_width, "target_height": target_height}]], ) + return ([[ + cond, + { + "pooled_output": pooled, + "width": width, "height": height, + "crop_w": crop_w, "crop_h": crop_h, + "target_width": target_width, "target_height": target_height + } + ]], ) + + @classmethod + def IS_CHANGED(cls, clip, width, height, crop_w, crop_h, target_width, target_height, text_g, text_l): + text_g_changed = is_dynamic_prompt_changed(text_g) + text_l_changed = is_dynamic_prompt_changed(text_l) + # If either is NaN, return NaN, otherwise return the sum + if math.isnan(text_g_changed) or math.isnan(text_l_changed): + # Log to console + print("CLIPTextEncodeSDXL.IS_CHANGED: NaN") + return float("nan") + else: + # Log to console + print("CLIPTextEncodeSDXL.IS_CHANGED: %s" % (text_g_changed + text_l_changed)) + return text_g_changed + text_l_changed + + @classmethod + def VALIDATE_INPUTS(cls, clip, width, height, crop_w, crop_h, target_width, target_height, text_g, text_l): + # These return `True`, or an error message if the input is invalid + text_g_valid = validate_dynamic_prompt(text_g) + text_l_valid = validate_dynamic_prompt(text_l) + # If either is not `True`, return the error message, otherwise return `True` + if text_g_valid is not True: + return text_g_valid + elif text_l_valid is not True: + return text_l_valid NODE_CLASS_MAPPINGS = { "CLIPTextEncodeSDXLRefiner": CLIPTextEncodeSDXLRefiner, diff --git a/nodes.py b/nodes.py index 45655cf79..7db8f2002 100644 --- a/nodes.py +++ b/nodes.py @@ -28,6 +28,8 @@ import comfy.clip_vision import comfy.model_management from comfy.cli_args import args +from comfy.dynamic_prompt import process_dynamic_prompt, is_dynamic_prompt_changed, validate_dynamic_prompt + import importlib import folder_paths @@ -44,17 +46,31 @@ MAX_RESOLUTION=8192 class CLIPTextEncode: @classmethod def INPUT_TYPES(s): - return {"required": {"text": ("STRING", {"multiline": True, "dynamicPrompts": True}), "clip": ("CLIP", )}} + return {"required": { + "text": ("STRING", {"multiline": True}), + "clip": ("CLIP", ) + }} RETURN_TYPES = ("CONDITIONING",) FUNCTION = "encode" CATEGORY = "conditioning" def encode(self, clip, text): + text = process_dynamic_prompt(text) tokens = clip.tokenize(text) cond, pooled = clip.encode_from_tokens(tokens, return_pooled=True) return ([[cond, {"pooled_output": pooled}]], ) + @classmethod + def IS_CHANGED(s, clip, text): + # Assume that if the text contains a dynamic prompt, it is always changed. + return is_dynamic_prompt_changed(text) + + @classmethod + def VALIDATE_INPUTS(s, clip, text): + return validate_dynamic_prompt(text) + + class ConditioningCombine: @classmethod def INPUT_TYPES(s): @@ -820,7 +836,7 @@ class GLIGENTextBoxApply: return {"required": {"conditioning_to": ("CONDITIONING", ), "clip": ("CLIP", ), "gligen_textbox_model": ("GLIGEN", ), - "text": ("STRING", {"multiline": True, "dynamicPrompts": True}), + "text": ("STRING", {"multiline": True}), "width": ("INT", {"default": 64, "min": 8, "max": MAX_RESOLUTION, "step": 8}), "height": ("INT", {"default": 64, "min": 8, "max": MAX_RESOLUTION, "step": 8}), "x": ("INT", {"default": 0, "min": 0, "max": MAX_RESOLUTION, "step": 8}), @@ -833,6 +849,7 @@ class GLIGENTextBoxApply: def append(self, conditioning_to, clip, gligen_textbox_model, text, width, height, x, y): c = [] + text = process_dynamic_prompt(text) cond, cond_pooled = clip.encode_from_tokens(clip.tokenize(text), return_pooled=True) for t in conditioning_to: n = [t[0], t[1].copy()] @@ -845,6 +862,14 @@ class GLIGENTextBoxApply: c.append(n) return (c, ) + @classmethod + def IS_CHANGED(s, conditioning_to, clip, gligen_textbox_model, text, width, height, x, y): + return is_dynamic_prompt_changed(text) + + @classmethod + def VALIDATE_INPUTS(s, conditioning_to, clip, gligen_textbox_model, text, width, height, x, y): + return validate_dynamic_prompt(text) + class EmptyLatentImage: def __init__(self, device="cpu"): self.device = device diff --git a/web/extensions/core/dynamicPrompts.js b/web/extensions/core/dynamicPrompts.js deleted file mode 100644 index 8806e8a5f..000000000 --- a/web/extensions/core/dynamicPrompts.js +++ /dev/null @@ -1,48 +0,0 @@ -import { app } from "../../scripts/app.js"; - -// Allows for simple dynamic prompt replacement -// Inputs in the format {a|b} will have a random value of a or b chosen when the prompt is queued. - -/* - * Strips C-style line and block comments from a string - */ -function stripComments(str) { - return str.replace(/\/\*[\s\S]*?\*\/|\/\/.*/g,''); -} - -app.registerExtension({ - name: "Comfy.DynamicPrompts", - nodeCreated(node) { - if (node.widgets) { - // Locate dynamic prompt text widgets - // Include any widgets with dynamicPrompts set to `true` - const widgets = node.widgets.filter( - (n) => n.dynamicPrompts - ); - for (const widget of widgets) { - // Override the serialization of the value to resolve dynamic prompts for all widgets supporting it in this node - widget.serializeValue = (workflowNode, widgetIndex) => { - let prompt = stripComments(widget.value); - while (prompt.replace("\\{", "").includes("{") && prompt.replace("\\}", "").includes("}")) { - const startIndex = prompt.replace("\\{", "00").indexOf("{"); - const endIndex = prompt.replace("\\}", "00").indexOf("}"); - - const optionsString = prompt.substring(startIndex + 1, endIndex); - const options = optionsString.split("|"); - - const randomIndex = Math.floor(Math.random() * options.length); - const randomOption = options[randomIndex]; - - prompt = prompt.substring(0, startIndex) + randomOption + prompt.substring(endIndex + 1); - } - - // Overwrite the value in the serialized workflow pnginfo - if (workflowNode?.widgets_values) - workflowNode.widgets_values[widgetIndex] = prompt; - - return prompt; - }; - } - } - }, -}); diff --git a/web/scripts/widgets.js b/web/scripts/widgets.js index a92da416c..a8afc29b0 100644 --- a/web/scripts/widgets.js +++ b/web/scripts/widgets.js @@ -190,7 +190,6 @@ function addMultilineWidget(node, name, opts, app) { widget.inputEl.blur(); } }); - widget.dynamicPrompts = opts.dynamicPrompts; widget.parent = node; document.body.appendChild(widget.inputEl);