mirror of
https://git.datalinker.icu/comfyanonymous/ComfyUI
synced 2026-11-22 12:27:05 +08:00
Moved dynamic prompts from Javascript/UI to Python
- Removed `dynamicPrompts.js` - Added `comfy/dynamic_prompts.py` - Added calls to parse/check dynamic prompts from relevant Nodes.
This commit is contained in:
parent
6aadcc2b41
commit
107b1d1b0d
124
comfy/dynamic_prompt.py
Normal file
124
comfy/dynamic_prompt.py
Normal file
@ -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'(?<!\{)\{(?!\{)|(?<!\})\}(?!\})', text) is not None
|
||||
|
||||
|
||||
# Checks a prompt and returns a hash of it if there are no dynamic elements, or NaN if there are, thus forcing it to always change.
|
||||
# @param prompt The prompt to check.
|
||||
# @return A hash of the prompt if there are no dynamic elements, or NaN if there are.
|
||||
def is_dynamic_prompt_changed(prompt):
|
||||
if contains_dynamic_prompt(prompt):
|
||||
print("is_dynamic_prompt_changed: NaN")
|
||||
return float("nan")
|
||||
else:
|
||||
print("is_dynamic_prompt_changed: %s" % hash(prompt))
|
||||
return prompt
|
||||
@ -1,14 +1,16 @@
|
||||
import torch
|
||||
import math
|
||||
from nodes import MAX_RESOLUTION
|
||||
from comfy.dynamic_prompt import process_dynamic_prompt, is_dynamic_prompt_changed, validate_dynamic_prompt
|
||||
|
||||
class CLIPTextEncodeSDXLRefiner:
|
||||
@classmethod
|
||||
def INPUT_TYPES(s):
|
||||
return {"required": {
|
||||
"clip": ("CLIP", ),
|
||||
"ascore": ("FLOAT", {"default": 6.0, "min": 0.0, "max": 1000.0, "step": 0.01}),
|
||||
"width": ("INT", {"default": 1024.0, "min": 0, "max": MAX_RESOLUTION}),
|
||||
"height": ("INT", {"default": 1024.0, "min": 0, "max": MAX_RESOLUTION}),
|
||||
"text": ("STRING", {"multiline": True, "dynamicPrompts": True}), "clip": ("CLIP", ),
|
||||
"text": ("STRING", {"multiline": True}),
|
||||
}}
|
||||
RETURN_TYPES = ("CONDITIONING",)
|
||||
FUNCTION = "encode"
|
||||
@ -16,22 +18,32 @@ class CLIPTextEncodeSDXLRefiner:
|
||||
CATEGORY = "advanced/conditioning"
|
||||
|
||||
def encode(self, clip, ascore, width, height, 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, "aesthetic_score": ascore, "width": width,"height": height}]], )
|
||||
|
||||
@classmethod
|
||||
def IS_CHANGED(s, clip, ascore, width, height, text):
|
||||
return is_dynamic_prompt_changed(text)
|
||||
|
||||
@classmethod
|
||||
def VALIDATE_INPUTS(s, clip, ascore, width, height, text):
|
||||
return validate_dynamic_prompt(text)
|
||||
|
||||
class CLIPTextEncodeSDXL:
|
||||
@classmethod
|
||||
def INPUT_TYPES(s):
|
||||
return {"required": {
|
||||
"clip": ("CLIP", ),
|
||||
"width": ("INT", {"default": 1024.0, "min": 0, "max": MAX_RESOLUTION}),
|
||||
"height": ("INT", {"default": 1024.0, "min": 0, "max": MAX_RESOLUTION}),
|
||||
"crop_w": ("INT", {"default": 0, "min": 0, "max": MAX_RESOLUTION}),
|
||||
"crop_h": ("INT", {"default": 0, "min": 0, "max": MAX_RESOLUTION}),
|
||||
"target_width": ("INT", {"default": 1024.0, "min": 0, "max": MAX_RESOLUTION}),
|
||||
"target_height": ("INT", {"default": 1024.0, "min": 0, "max": MAX_RESOLUTION}),
|
||||
"text_g": ("STRING", {"multiline": True, "default": "CLIP_G", "dynamicPrompts": True}), "clip": ("CLIP", ),
|
||||
"text_l": ("STRING", {"multiline": True, "default": "CLIP_L", "dynamicPrompts": True}), "clip": ("CLIP", ),
|
||||
"text_g": ("STRING", {"multiline": True, "default": "CLIP_G"}),
|
||||
"text_l": ("STRING", {"multiline": True, "default": "CLIP_L"}),
|
||||
}}
|
||||
RETURN_TYPES = ("CONDITIONING",)
|
||||
FUNCTION = "encode"
|
||||
@ -39,6 +51,8 @@ class CLIPTextEncodeSDXL:
|
||||
CATEGORY = "advanced/conditioning"
|
||||
|
||||
def encode(self, clip, width, height, crop_w, crop_h, target_width, target_height, text_g, text_l):
|
||||
text_g = process_dynamic_prompt(text_g)
|
||||
text_l = process_dynamic_prompt(text_l)
|
||||
tokens = clip.tokenize(text_g)
|
||||
tokens["l"] = clip.tokenize(text_l)["l"]
|
||||
if len(tokens["l"]) != len(tokens["g"]):
|
||||
@ -48,7 +62,40 @@ class CLIPTextEncodeSDXL:
|
||||
while len(tokens["l"]) > 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,
|
||||
|
||||
29
nodes.py
29
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
|
||||
|
||||
@ -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;
|
||||
};
|
||||
}
|
||||
}
|
||||
},
|
||||
});
|
||||
@ -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);
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user