-
Notifications
You must be signed in to change notification settings - Fork 3
/
ZenkaiWildcardV2.py
104 lines (88 loc) · 4 KB
/
ZenkaiWildcardV2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import os
import random
import re
import logging
logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger(__name__)
class ZenkaiWildcardV2:
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"prompt": ("STRING", {"multiline": True}),
"seed": ("INT", {
"default": 0,
"min": 0,
"max": 0xFFFFFFFF
}),
"wildcard_symbol": ("STRING", {"default": "$$"}),
"recursive_depth": ("INT", {
"default": 5,
"min": 1,
"max": 10,
"step": 1
})
}
}
RETURN_TYPES = ("STRING",)
FUNCTION = "process_wildcards"
CATEGORY = "DJZ-Nodes"
def process_wildcards(self, prompt, seed, wildcard_symbol, recursive_depth):
random.seed(seed)
wildcards_folder = os.path.join(os.path.dirname(__file__), 'wildcards')
logger.debug(f"Wildcards folder: {wildcards_folder}")
logger.debug(f"Current working directory: {os.getcwd()}")
logger.debug(f"Directory contents of wildcards folder: {os.listdir(wildcards_folder)}")
def replace_wildcard(match, depth=0):
if depth >= recursive_depth:
logger.debug(f"Max depth reached: {depth}")
return match.group(0)
wildcard = match.group(1)
file_path = os.path.join(wildcards_folder, f"{wildcard}.txt")
logger.debug(f"Looking for file: {file_path} (depth: {depth})")
logger.debug(f"File exists: {os.path.exists(file_path)}")
if os.path.exists(file_path):
try:
with open(file_path, 'r', encoding='utf-8') as f:
lines = [line.strip() for line in f if line.strip()]
if lines:
choice = random.choice(lines)
logger.debug(f"Replaced {wildcard} with: {choice} (depth: {depth})")
if wildcard_symbol in choice:
logger.debug(f"Found nested wildcard in: {choice}")
processed_choice = re.sub(pattern, lambda m: replace_wildcard(m, depth + 1), choice)
logger.debug(f"After recursive processing: {processed_choice} (depth: {depth})")
return processed_choice
else:
return choice
else:
logger.warning(f"File {file_path} is empty")
return match.group(0)
except Exception as e:
logger.error(f"Error reading file {file_path}: {str(e)}")
return match.group(0)
else:
logger.warning(f"File not found: {file_path}")
return match.group(0)
escaped_symbol = re.escape(wildcard_symbol)
pattern = f"{escaped_symbol}([a-zA-Z0-9_]+)"
logger.debug(f"Original prompt: {prompt}")
logger.debug(f"Using pattern: {pattern}")
processed_prompt = prompt
for i in range(recursive_depth):
new_prompt = re.sub(pattern, lambda m: replace_wildcard(m, 0), processed_prompt)
if new_prompt == processed_prompt:
break
processed_prompt = new_prompt
logger.debug(f"Iteration {i+1} result: {processed_prompt}")
logger.debug(f"Final processed prompt: {processed_prompt}")
return (processed_prompt,)
@classmethod
def IS_CHANGED(cls, prompt, seed, wildcard_symbol, recursive_depth):
return float(seed)
NODE_CLASS_MAPPINGS = {
"ZenkaiWildcardV2": ZenkaiWildcardV2
}
NODE_DISPLAY_NAME_MAPPINGS = {
"ZenkaiWildcardV2": "Zenkai-Wildcard V2"
}