forked from KoreTeknology/ComfyUI-Universal-Styler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
naistyler_nodes.py
271 lines (233 loc) · 10.2 KB
/
naistyler_nodes.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
import os
import re
import folder_paths
from pathlib import Path
#DEBUG pathlib (to replace folder_path from OS)
print(Path.cwd())
print("############################################")
BASE_DIR = Path.cwd()
DATAPATH = BASE_DIR.joinpath("custom_nodes","ComfyUI-NAI-styler","CSV")
print(DATAPATH)
print("############################################")
my_database = [str(file) for file in DATAPATH.glob("*.csv")]
print(my_database)
print("############################################")
################
# NAI Show text v0.3 ##########################################################################
################
class ShowText:
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"text": ("STRING", {"forceInput": True}),
},
"hidden": {
"unique_id": "UNIQUE_ID",
"extra_pnginfo": "EXTRA_PNGINFO",
},
}
INPUT_IS_LIST = True
RETURN_TYPES = ("STRING",)
FUNCTION = "notify"
OUTPUT_NODE = True
OUTPUT_IS_LIST = (True,)
CATEGORY = "✴️ Universal NAI Nodes"
def notify(self, text, unique_id=None, extra_pnginfo=None):
if unique_id is not None and extra_pnginfo is not None:
if not isinstance(extra_pnginfo, list):
print("Error: extra_pnginfo is not a list")
elif (
not isinstance(extra_pnginfo[0], dict)
or "workflow" not in extra_pnginfo[0]
):
print("Error: extra_pnginfo[0] is not a dict or missing 'workflow' key")
else:
workflow = extra_pnginfo[0]["workflow"]
node = next(
(x for x in workflow["nodes"] if str(x["id"]) == str(unique_id[0])),
None,
)
if node:
node["widgets_values"] = [text]
return {"ui": {"text": text}, "result": (text,)}
################
# NAI STYLER v0.3 ##########################################################################
################
class NaiStylerComplexCSVLoader:
# Part 1
@staticmethod
def load_naistyles_csv(naistyles_path: str):
"""Loads csv file, Ignore the first row (header).
Returns:
list: List of naistyles. Each style is a dict with keys: style_name and value: [positive_prompt, negative_prompt]
"""
naistyles = {"Error loading naistyles.csv, check the console": ["",""]}
if not os.path.exists(naistyles_path):
print(f"""Error. No naistyles.csv found. Put your naistyles.csv in the {naistyles_path} directory of ComfyUI. Then press "Refresh".
Your current root directory is: {folder_paths.base_path}
""")
return naistyles
try:
with open(naistyles_path, "r", encoding="utf-8") as f:
naistyles = [[x.replace('"', '').replace('\n','') for x in re.split(',(?=(?:[^"]*"[^"]*")*[^"]*$)', line)] for line in f.readlines()[1:]]
naistyles = {x[0]: [x[1],x[2]] for x in naistyles}
except Exception as e:
print(f"""Error loading naistyles.csv. Make sure it is in the {naistyles_path} directory of ComfyUI. Then press "Refresh".
Your current root directory is: {folder_paths.base_path}
Error: {e}
""")
return naistyles
# part 2
@staticmethod
def load_naifilters_csv(naifilters_path: str):
"""Loads filtercsv file, Ignore the first row (header).
Returns:
list: List of naistyles. Each style is a dict with keys: style_name and value: [positive_prompt, negative_prompt]
"""
naifilters = {"Error loading naistyles.csv, check the console": ["",""]}
if not os.path.exists(naifilters_path):
print(f"""Error. No naistyles.csv found. Put your naistyles.csv in the {naifilters_path} directory of ComfyUI. Then press "Refresh".
Your current root directory is: {folder_paths.base_path}
""")
return naifilters
try:
with open(naifilters_path, "r", encoding="utf-8") as f:
naifilters = [[x.replace('"', '').replace('\n','') for x in re.split(',(?=(?:[^"]*"[^"]*")*[^"]*$)', line)] for line in f.readlines()[1:]]
naifilters = {x[0]: [x[1],x[2]] for x in naifilters}
except Exception as e:
print(f"""Error loading naistyles.csv. Make sure it is in the {naifilters_path} directory of ComfyUI. Then press "Refresh".
Your current root directory is: {folder_paths.base_path}
Error: {e}
""")
return naifilters
# part 3
@staticmethod
def load_naitypes_csv(naitypes_path: str):
"""Loads filtercsv file, Ignore the first row (header).
Returns:
list: List of naistyles. Each style is a dict with keys: style_name and value: [positive_prompt, negative_prompt]
"""
naitypes = {"Error loading naistyles.csv, check the console": ["",""]}
if not os.path.exists(naitypes_path):
print(f"""Error. No naistyles.csv found. Put your naistyles.csv in the {naitypes_path} directory of ComfyUI. Then press "Refresh".
Your current root directory is: {folder_paths.base_path}
""")
return naitypes
try:
with open(naitypes_path, "r", encoding="utf-8") as f:
naitypes = [[x.replace('"', '').replace('\n','') for x in re.split(',(?=(?:[^"]*"[^"]*")*[^"]*$)', line)] for line in f.readlines()[1:]]
naitypes = {x[0]: [x[1],x[2]] for x in naitypes}
except Exception as e:
print(f"""Error loading naistyles.csv. Make sure it is in the {naitypes_path} directory of ComfyUI. Then press "Refresh".
Your current root directory is: {folder_paths.base_path}
Error: {e}
""")
return naitypes
# Data
@classmethod
def INPUT_TYPES(cls):
cls.naistyles_csv = cls.load_naistyles_csv(os.path.join(folder_paths.base_path, "custom_nodes/ComfyUI-NAI-styler/CSV/naifilters.csv"))
cls.naifilters_csv = cls.load_naifilters_csv(os.path.join(folder_paths.base_path, "custom_nodes/ComfyUI-NAI-styler/CSV/naistyles.csv"))
cls.naitypes_csv = cls.load_naitypes_csv(os.path.join(folder_paths.base_path, "custom_nodes/ComfyUI-NAI-styler/CSV/naitypes.csv"))
return {
"required": {
#"mute": (["On", "Off"],),
"naifilters": (list(cls.naistyles_csv.keys()),),
"naistyles": (list(cls.naifilters_csv.keys()),),
"naitypes": (list(cls.naitypes_csv.keys()),),
#"clip": ("CLIP", ),
},
}
RETURN_TYPES = ("STRING","STRING")
RETURN_NAMES = ("Full prompt","Short prompt")
FUNCTION = "execute"
CATEGORY = "✴️ Universal NAI Nodes"
def execute(self, naistyles, naifilters, naitypes):
return str(self.naistyles_csv[naistyles][0], self.naistyles_csv[naistyles][1],self.naifilters_csv[naifilters][0], self.naifilters_csv[naifilters][1],self.naitypes_csv[naitypes][0], self.naitypes_csv[naitypes][1],)
################
# NAI STYLER v0.1 ##########################################################################
################
class NaiStyler:
"""
A new custom node
"""
def __init__(self):
pass
@classmethod
def INPUT_TYPES(s):
"""
All param and values
"""
return {
"required": {
"clip": ("CLIP", ),
"mute": (["On", "Off"],),
"mix": ("INT", {
"default": 50,
"min": 0,
"max": 100,
"step": 1,
"display": "slider" #"number" or "slider"
}),
"float_field": ("FLOAT", {
"default": 0.5,
"min": 0.0,
"max": 1.0,
"step": 0.01,
"round": 0.001,
"display": "slider"}),
"string_field": ("STRING", {
"multiline": True,
"default": "Define Object"
}),
"string_field2": ("STRING", {
"multiline": True,
"default": "Define Background"
}),
},
}
RETURN_TYPES = ("CONDITIONING","STRING","STRING","INT")
RETURN_NAMES = ("Compiled prompt","Value","Value2","mix")
FUNCTION = "test"
#OUTPUT_NODE = False
CATEGORY = "✴️ Universal NAI Nodes"
def test(self, string_field, string_field2, mix, float_field, mute):
if mute == "On":
print(f"""Your input contains:
string_field aka input text: {string_field}
int_field: {mix}
float_field: {float_field}
""")
################
# NAI concat v0.1 ##########################################################################
################
class ConcatenateFields:
@classmethod
def INPUT_TYPES(cls):
return {"required": {
"text1": ("STRING", {"multiline": False, "default": "Hello"}),
"text2": ("STRING", {"multiline": False, "default": "World"}),
}
}
RETURN_TYPES = ("STRING",)
FUNCTION = "concatenate_text"
CATEGORY = "✴️ Universal NAI Nodes"
def concatenate_text(self, text1, text2):
text_out = text1 + " " + text2
return (text_out,)
################
# NODES MAPPING ##########################################################################
################
NODE_CLASS_MAPPINGS = {
"ShowText|pysssss": ShowText,
"Load Nai Styles Complex CSV": NaiStylerComplexCSVLoader,
"Universal_Styler_Node": NaiStyler,
"concat": ConcatenateFields,
}
NODE_DISPLAY_NAME_MAPPINGS = {
"ShowText|pysssss": "✴️ U-NAI Get Text",
"Load Nai Styles Complex CSV": "✴️ U-NAI Styles Launcher",
"Universal_Styler_Node": "✴️ U-NAI Styler - v0.2.1",
"concat": "✴️ U-NAI Fields Concatenate",
}