Merge branch 'dev': v1.0.1
This commit is contained in:
+7
-2
@@ -1,7 +1,12 @@
|
||||
# `TODO`
|
||||
- Recursive formatting.
|
||||
## `TODO`
|
||||
|
||||
- Multi-item nodes.
|
||||
|
||||
# v1.0.1
|
||||
|
||||
- ❗ Recursive formatting.
|
||||
- Tiny tweaks to tooltips/descriptions.
|
||||
|
||||
# v1.0.0
|
||||
|
||||
The minimally-complete version.
|
||||
|
||||
@@ -12,6 +12,7 @@
|
||||
- Build your dictionary of available text chunks once. Pass it further as a single line (bus/pipe design).
|
||||
- Easily reuse these sub-strings to build many variations of a prompt as needed.
|
||||
- It's especially handy for regional prompting (aka area composition).
|
||||
- [`✨New in v1.0.1` <img src="img/recursive_toggle.png" height="24" />](#recursive-formatting)
|
||||
- ...
|
||||
- PROFIT!
|
||||
|
||||
@@ -60,3 +61,16 @@ The rules are very simple:
|
||||
> Internally, just a built-in `str.format()` is called with keyword arguments from the passed Format-Dict, which is literally just a dict with string keys... though, all the nodes in the pack ensure they return it as an immutable one.
|
||||
>
|
||||
> So any formatting patterns are available (like `{float_value:.3f}`) + `Add ANY to Format-Dict` node is there for exactly that.
|
||||
|
||||
## Recursive formatting
|
||||
|
||||
It's quite simple in principle yet the most powerful feature of the pack.
|
||||
|
||||
When formatting the string, you can let chunks reference each other, which unlocks immense possibilities - like building entire **HIERARCHIES** of descriptions with various granularity (tailored for different resolutions) or even conditional string formatting for the most advanced users.
|
||||
|
||||

|
||||
|
||||
> [!WARNING]
|
||||
> Remember that with great power comes great responsibility!
|
||||
>
|
||||
> With a sloppy use, you can create chunks that cross-reference each other in an infinite loop. The node will error out after reaching a high level of recursion (about 1k), so you're safe. But still, you've been warned!
|
||||
|
||||
+42
-7
@@ -5,20 +5,55 @@ The actual code (MVC pattern) for `StringConstructorFormatter` node.
|
||||
|
||||
import typing as _t
|
||||
|
||||
from .enums import T as _T
|
||||
import sys as _sys
|
||||
|
||||
from .funcs_common import _show_text_on_node, _verify_input_dict
|
||||
|
||||
|
||||
def formatter(template: str, format_dict: _t.Dict[str, _T] = None, show: bool = True, unique_id: str = None) -> _t.Tuple[str]:
|
||||
_RECURSION_LIMIT = max(int(_sys.getrecursionlimit()), 10) # You can externally monkey-patch it... but if it blows up, your fault 🤷🏻♂️
|
||||
|
||||
|
||||
def _recursive_format(template: str, format_dict: _t.Dict[str, _t.Any], show: bool = True, unique_id: str = None) -> str:
|
||||
"""
|
||||
It's not actually recursive - because, you know, any recursion could be turned into iteration,
|
||||
and good boys do that. 😊
|
||||
"""
|
||||
assert isinstance(_RECURSION_LIMIT, int) and _RECURSION_LIMIT > 0
|
||||
|
||||
prev: str = ''
|
||||
new: str = template
|
||||
for i in range(_RECURSION_LIMIT):
|
||||
if prev == new:
|
||||
break
|
||||
prev = new
|
||||
new = new.format_map(format_dict)
|
||||
if prev == new:
|
||||
return new
|
||||
|
||||
msg = (
|
||||
f"Recursion limit ({_RECURSION_LIMIT}) reached on attempt to format a string: {template!r}\n"
|
||||
f"Last two formatting attempts:\n{prev!r}\n{new!r}"
|
||||
)
|
||||
if show and unique_id:
|
||||
_show_text_on_node(msg, unique_id)
|
||||
raise RecursionError(msg)
|
||||
# noinspection PyUnreachableCode
|
||||
return '' # just to be safe
|
||||
|
||||
|
||||
def formatter(
|
||||
template: str, recursive: bool = False, format_dict: _t.Dict[str, _t.Any] = None,
|
||||
show: bool = True, unique_id: str = None
|
||||
) -> _t.Tuple[str]:
|
||||
format_dict = _verify_input_dict(format_dict)
|
||||
if not isinstance(template, str):
|
||||
raise TypeError(f"Not a string: {template!r}")
|
||||
out_text = template.format_map(format_dict)
|
||||
print(template)
|
||||
print(format_dict)
|
||||
print(out_text)
|
||||
|
||||
out_text = (
|
||||
_recursive_format(template, format_dict, show, unique_id)
|
||||
if recursive
|
||||
else template.format_map(format_dict)
|
||||
)
|
||||
if show and unique_id:
|
||||
_show_text_on_node(out_text, unique_id)
|
||||
|
||||
return (out_text, )
|
||||
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 301 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 3.9 KiB |
@@ -26,9 +26,9 @@ _input_types = _deepfreeze({
|
||||
'strings': (_IO.STRING, {'multiline': True, 'tooltip': (
|
||||
"Sub-string names followed by their text. Different sub-string chunks are separated by empty lines. Example:\n\n"
|
||||
"char1_short\n1boy, blond, short hair\n\n"
|
||||
"char1_long\n1boy, smiling, blue eyes, blond, short hair,\nsitting on a motorcycle, wearing a leather jacket"
|
||||
"char1_long\n1boy, smiling, blue eyes, blond, short hair,\nwearing a leather jacket, sitting on a bike"
|
||||
)}),
|
||||
'show_status': (_IO.BOOLEAN, {'default': False, 'label_on': 'found names', 'label_off': 'no', 'tooltip': (
|
||||
'show_status': (_IO.BOOLEAN, {'default': False, 'label_on': 'detected names', 'label_off': 'no', 'tooltip': (
|
||||
"Show detected string names on the node itself?"
|
||||
)}),
|
||||
},
|
||||
|
||||
+11
-2
@@ -25,6 +25,15 @@ _input_types = _deepfreeze({
|
||||
"score_9, score_8_up, score_7_up, {char1_short}, standing next to {char2_short},\n"
|
||||
"{char1_long}\n{char2_long}"
|
||||
)}),
|
||||
'recursive_format': (_IO.BOOLEAN, {'default': False, 'label_on': '❗ yes', 'label_off': 'no', 'tooltip': (
|
||||
"Do recursive format - i.e., allow the chunks from the dictionary to reference other chunks - which in turn "
|
||||
"lets you do really crazy stuff like building entire HIERARCHIES of sub-prompts, all linked to the same "
|
||||
"wording typed in one place.\n\n"
|
||||
"The exclamation mark reminds you that with great power comes great responsibility!\n"
|
||||
"You can end up with chunks cross-referencing each other in a loop. In such case, the node will just error out "
|
||||
"and won't let you crash the entire ComfyUI - but still, it's your responsibility to prevent "
|
||||
"such looping dictionaries."
|
||||
)}),
|
||||
'show_status': (_IO.BOOLEAN, {'default': True, 'label_on': 'formatted string', 'label_off': 'no', 'tooltip': (
|
||||
"Show the final string constructed from the text-template and format-dictionary?"
|
||||
)}),
|
||||
@@ -61,8 +70,8 @@ class StringConstructorFormatter:
|
||||
return _input_types
|
||||
|
||||
def main(
|
||||
self, template: str, show_status: bool,
|
||||
self, template: str, recursive_format: bool, show_status: bool,
|
||||
dict: _t.Dict[str, _t.Any] = None,
|
||||
unique_id: str = None,
|
||||
):
|
||||
return _formatter(template, dict, show=show_status, unique_id=unique_id)
|
||||
return _formatter(template, recursive_format, dict, show=show_status, unique_id=unique_id)
|
||||
|
||||
+2
-2
@@ -1,7 +1,7 @@
|
||||
[project]
|
||||
name = "string-constructor"
|
||||
description = "Build your dictionary of named strings (text chunks), then easily compile different prompt variations from them - with simple string formatting.\nUnlike string-formatting nodes from other popular packs, this one is designed for specifying your sub-strings once, then dragging the entire \"library\" of them across the entire workflow (like a bus), updating it in-place as necessary. This is especially handy for regional prompting aka area composition."
|
||||
version = "1.0.0"
|
||||
description = "Composing prompt variants from the same text pieces with ease:\n• Build your \"library\" (dictionary) of named text chunks (sub-strings) to use it across the entire workflow.\n• Compile these snippets into different prompts in-place - with just one string formatting node.\n• Freely update the dictionary down the line - get different prompts.\n• Reference text chunks within each other to build dependent hierarchies of less/more detailed descriptions.\n• A real life-saver for regional prompting (aka area composition)."
|
||||
version = "1.0.1"
|
||||
authors = [
|
||||
{name = "Lex Darlog"}
|
||||
]
|
||||
|
||||
Reference in New Issue
Block a user