-
Notifications
You must be signed in to change notification settings - Fork 75
/
sdxl_prompt_styler.py
356 lines (284 loc) · 14.9 KB
/
sdxl_prompt_styler.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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
import json
import os
def read_json_file(file_path):
"""
Reads a JSON file's content and returns it.
Ensures content matches the expected format.
"""
if not os.access(file_path, os.R_OK):
print(f"Warning: No read permissions for file {file_path}")
return None
try:
with open(file_path, 'r', encoding='utf-8') as file:
content = json.load(file)
# Check if the content matches the expected format.
if not all(['name' in item and 'prompt' in item and 'negative_prompt' in item for item in content]):
print(f"Warning: Invalid content in file {file_path}")
return None
return content
except Exception as e:
print(f"An error occurred while reading {file_path}: {str(e)}")
return None
def read_sdxl_styles(json_data):
"""
Returns style names from the provided JSON data.
"""
if not isinstance(json_data, list):
print("Error: input data must be a list")
return []
return [item['name'] for item in json_data if isinstance(item, dict) and 'name' in item]
def get_all_json_files(directory):
"""
Returns all JSON files from the specified directory.
"""
return [os.path.join(directory, file) for file in os.listdir(directory) if file.endswith('.json') and os.path.isfile(os.path.join(directory, file))]
def load_styles_from_directory(directory):
"""
Loads styles from all JSON files in the directory.
Renames duplicate style names by appending a suffix.
"""
json_files = get_all_json_files(directory)
combined_data = []
seen = set()
for json_file in json_files:
json_data = read_json_file(json_file)
if json_data:
for item in json_data:
original_style = item['name']
style = original_style
suffix = 1
while style in seen:
style = f"{original_style}_{suffix}"
suffix += 1
item['name'] = style
seen.add(style)
combined_data.append(item)
unique_style_names = [item['name'] for item in combined_data if isinstance(item, dict) and 'name' in item]
return combined_data, unique_style_names
def validate_json_data(json_data):
"""
Validates the structure of the JSON data.
"""
if not isinstance(json_data, list):
return False
for template in json_data:
if 'name' not in template or 'prompt' not in template:
return False
return True
def find_template_by_name(json_data, template_name):
"""
Returns a template from the JSON data by name or None if not found.
"""
for template in json_data:
if template['name'] == template_name:
return template
return None
def split_template_advanced(template: str) -> tuple:
"""
Splits a template into two parts based on a specific pattern.
"""
if " . " in template:
template_prompt_g, template_prompt_l = template.split(" . ", 1)
template_prompt_g = template_prompt_g.strip()
template_prompt_l = template_prompt_l.strip()
else:
template_prompt_g = template
template_prompt_l = ""
return template_prompt_g, template_prompt_l
def replace_prompts_in_template(template, positive_prompt, negative_prompt):
"""
Replace the placeholders in a given template with the provided prompts.
Args:
- template (dict): The template containing prompt placeholders.
- positive_prompt (str): The positive prompt to replace '{prompt}' in the template.
- negative_prompt (str): The negative prompt to be combined with any existing negative prompt in the template.
Returns:
- tuple: A tuple containing the replaced positive and negative prompts.
"""
positive_result = template['prompt'].replace('{prompt}', positive_prompt)
json_negative_prompt = template.get('negative_prompt', "")
negative_result = f"{json_negative_prompt}, {negative_prompt}" if json_negative_prompt and negative_prompt else json_negative_prompt or negative_prompt
return positive_result, negative_result
def deduplicate_merge(prompt_1, prompt_2):
"""
Merge two prompts, deduplicating the tokens.
Args:
- prompt1 (str): The first prompt.
- prompt2 (str): The second prompt.
Returns:
- str: The merged and deduplicated prompt.
"""
if not prompt_2:
return prompt_1
elif not prompt_1:
return prompt_2
token_prompt_1 = list(map(lambda x: x.strip(), prompt_1.split(",")))
token_prompt_2 = list(map(lambda x: x.strip(), prompt_2.split(",")))
# deduplicate common prompt parts
for token in token_prompt_1:
if token in token_prompt_2:
token_prompt_2.remove(token)
token_prompt_1.extend(token_prompt_2)
prompt_out = ", ".join(token_prompt_1)
return prompt_out
def replace_prompts_in_template_advanced(template, positive_prompt_g, positive_prompt_l, negative_prompt, negative_prompt_to, copy_to_l):
"""
Replace the placeholders in a given template with the provided prompts and split them accordingly.
Args:
- template (dict): The template containing prompt placeholders.
- positive_prompt_g (str): The main positive prompt to replace '{prompt}' in the template.
- positive_prompt_l (str): The auxiliary positive prompt to be combined in a specific manner.
- negative_prompt (str): The negative prompt to be combined with any existing negative prompt in the template.
- negative_prompt_to (str): The negative prompt destination {Both, G only, L only}.
- copy_to_l (bool): Copy the G positive prompt to L.
Returns:
- tuple: A tuple containing the replaced main positive, auxiliary positive, combined positive, main negative, auxiliary negative, and negative prompts.
"""
template_prompt_g, template_prompt_l_template = split_template_advanced(template['prompt'])
text_g_positive = template_prompt_g.replace("{prompt}", positive_prompt_g)
text_l_positive = f"{template_prompt_l_template.replace('{prompt}', positive_prompt_g)}, {positive_prompt_l}" if template_prompt_l_template and positive_prompt_l else template_prompt_l_template.replace('{prompt}', positive_prompt_g) or positive_prompt_l
if copy_to_l and positive_prompt_g and "{prompt}" not in template_prompt_l_template:
text_l_positive = deduplicate_merge(text_g_positive, text_l_positive)
text_positive = deduplicate_merge(text_g_positive, text_l_positive) if text_l_positive else text_g_positive
json_negative_prompt = template.get('negative_prompt', "")
text_negative = f"{json_negative_prompt}, {negative_prompt}" if json_negative_prompt and negative_prompt else json_negative_prompt or negative_prompt
text_g_negative = ""
if negative_prompt_to in ("Both", "G only"):
text_g_negative = text_negative
text_l_negative = ""
if negative_prompt_to in ("Both", "L only"):
text_l_negative = text_negative
return text_g_positive, text_l_positive, text_positive, text_g_negative, text_l_negative, text_negative
def read_sdxl_templates_replace_and_combine(json_data, template_name, positive_prompt, negative_prompt):
"""
Find a specific template by its name, then replace and combine its placeholders with the provided prompts.
Args:
- json_data (list): The list of templates.
- template_name (str): The name of the desired template.
- positive_prompt (str): The positive prompt to replace placeholders.
- negative_prompt (str): The negative prompt to be combined.
Returns:
- tuple: A tuple containing the replaced and combined positive and negative prompts.
"""
if not validate_json_data(json_data):
return positive_prompt, negative_prompt
template = find_template_by_name(json_data, template_name)
if template:
return replace_prompts_in_template(template, positive_prompt, negative_prompt)
else:
return positive_prompt, negative_prompt
def read_sdxl_templates_replace_and_combine_advanced(json_data, template_name, positive_prompt_g, positive_prompt_l, negative_prompt, negative_prompt_to, copy_to_l):
"""
Find a specific template by its name, then replace and combine its placeholders with the provided prompts in an advanced manner.
Args:
- json_data (list): The list of templates.
- template_name (str): The name of the desired template.
- positive_prompt_g (str): The main positive prompt.
- positive_prompt_l (str): The auxiliary positive prompt.
- negative_prompt (str): The negative prompt to be combined.
- negative_prompt_to (str): The negative prompt destination {Both, G only, L only}.
- copy_to_l (bool): Copy the G positive prompt to L.
Returns:
- tuple: A tuple containing the replaced and combined main positive, auxiliary positive, combined positive, main negative, auxiliary negative, and negative prompts.
"""
if not validate_json_data(json_data):
return positive_prompt_g, positive_prompt_l, f"{positive_prompt_g} . {positive_prompt_l}", negative_prompt, negative_prompt, negative_prompt
template = find_template_by_name(json_data, template_name)
if template:
return replace_prompts_in_template_advanced(template, positive_prompt_g, positive_prompt_l, negative_prompt, negative_prompt_to, copy_to_l)
else:
return positive_prompt_g, positive_prompt_l, f"{positive_prompt_g} . {positive_prompt_l}", negative_prompt, negative_prompt, negative_prompt
class SDXLPromptStyler:
def __init__(self):
pass
@classmethod
def INPUT_TYPES(self):
current_directory = os.path.dirname(os.path.realpath(__file__))
self.json_data, styles = load_styles_from_directory(current_directory)
return {
"required": {
"text_positive": ("STRING", {"default": "", "multiline": True}),
"text_negative": ("STRING", {"default": "", "multiline": True}),
"style": ((styles), ),
"log_prompt": ("BOOLEAN", {"default": True, "label_on": "yes", "label_off": "no"}),
"style_positive": ("BOOLEAN", {"default": True, "label_on": "yes", "label_off": "no"}),
"style_negative": ("BOOLEAN", {"default": True, "label_on": "yes", "label_off": "no"}),
},
}
RETURN_TYPES = ('STRING','STRING',)
RETURN_NAMES = ('text_positive','text_negative',)
FUNCTION = 'prompt_styler'
CATEGORY = 'utils'
def prompt_styler(self, text_positive, text_negative, style, log_prompt, style_positive, style_negative):
# Process and combine prompts in templates
# The function replaces the positive prompt placeholder in the template,
# and combines the negative prompt with the template's negative prompt, if they exist.
text_positive_styled, text_negative_styled = read_sdxl_templates_replace_and_combine(self.json_data, style, text_positive, text_negative)
# If style_negative is disabled, set text_negative_styled to text_negative
if not style_positive:
text_positive_styled = text_positive
if log_prompt:
print(f"style_positive: disabled")
# If style_negative is disabled, set text_negative_styled to text_negative
if not style_negative:
text_negative_styled = text_negative
if log_prompt:
print(f"style_negative: disabled")
# If logging is enabled (log_prompt is set to "Yes"),
# print the style, positive and negative text, and positive and negative prompts to the console
if log_prompt:
print(f"style: {style}")
print(f"text_positive: {text_positive}")
print(f"text_negative: {text_negative}")
print(f"text_positive_styled: {text_positive_styled}")
print(f"text_negative_styled: {text_negative_styled}")
return text_positive_styled, text_negative_styled
class SDXLPromptStylerAdvanced:
def __init__(self):
pass
@classmethod
def INPUT_TYPES(self):
current_directory = os.path.dirname(os.path.realpath(__file__))
self.json_data, styles = load_styles_from_directory(current_directory)
return {
"required": {
"text_positive_g": ("STRING", {"default": "", "multiline": True}),
"text_positive_l": ("STRING", {"default": "", "multiline": True}),
"text_negative": ("STRING", {"default": "", "multiline": True}),
"style": ((styles), ),
"negative_prompt_to": (["Both", "G only", "L only"], {"default":"Both"}),
"copy_to_l": ("BOOLEAN", {"default": False, "label_on": "yes", "label_off": "no"}),
"log_prompt": ("BOOLEAN", {"default": False, "label_on": "yes", "label_off": "no"}),
},
}
RETURN_TYPES = ('STRING','STRING','STRING','STRING','STRING','STRING',)
RETURN_NAMES = ('text_positive_g','text_positive_l','text_positive','text_negative_g','text_negative_l','text_negative',)
FUNCTION = 'prompt_styler_advanced'
CATEGORY = 'utils'
def prompt_styler_advanced(self, text_positive_g, text_positive_l, text_negative, style, negative_prompt_to, copy_to_l, log_prompt):
# Process and combine prompts in templates
# The function replaces the positive prompt placeholder in the template,
# and combines the negative prompt with the template's negative prompt, if they exist.
text_positive_g_styled, text_positive_l_styled, text_positive_styled, text_negative_g_styled, text_negative_l_styled, text_negative_styled = read_sdxl_templates_replace_and_combine_advanced(self.json_data, style, text_positive_g, text_positive_l, text_negative, negative_prompt_to, copy_to_l)
# If logging is enabled (log_prompt is set to "Yes"),
# print the style, positive and negative text, and positive and negative prompts to the console
if log_prompt:
print(f"style: {style}")
print(f"text_positive_g: {text_positive_g}")
print(f"text_positive_l: {text_positive_l}")
print(f"text_negative: {text_negative}")
print(f"text_positive_g_styled: {text_positive_g_styled}")
print(f"text_positive_l_styled: {text_positive_l_styled}")
print(f"text_positive_styled: {text_positive_styled}")
print(f"text_negative_g_styled: {text_negative_g_styled}")
print(f"text_negative_l_styled: {text_negative_l_styled}")
print(f"text_negative_styled: {text_negative_styled}")
return text_positive_g_styled, text_positive_l_styled, text_positive_styled, text_negative_g_styled, text_negative_l_styled, text_negative_styled
NODE_CLASS_MAPPINGS = {
"SDXLPromptStyler": SDXLPromptStyler,
"SDXLPromptStylerAdvanced": SDXLPromptStylerAdvanced,
}
NODE_DISPLAY_NAME_MAPPINGS = {
"SDXLPromptStyler": "SDXL Prompt Styler",
"SDXLPromptStylerAdvanced": "SDXL Prompt Styler Advanced",
}