-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPromtTemplate.py
More file actions
152 lines (130 loc) · 5.29 KB
/
PromtTemplate.py
File metadata and controls
152 lines (130 loc) · 5.29 KB
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
import random
import comfy
import nodes
import json
import re
class PromptTemplateJson:
@classmethod
def INPUT_TYPES(s):
return {
"optional":{
"clip": ("CLIP",),
"template_positive": ("STRING", {
"default": "A *obj on a *place",
"multiline": True,
"dynamicPrompts": True,
"placeholder": "Template positive prompt"
}),
"template_negative": ("STRING", {
"default": '',
"multiline": True,
"dynamicPrompts": True,
"placeholder": "Template negative prompt"
}),
"variables_json": ("STRING", {"default": '{"obj": "cat", "place": "table"}', "multiline": True}),
}
}
RETURN_TYPES = ("CONDITIONING","CONDITIONING","STRING","STRING","STRING",)
RETURN_NAMES = ("positive", "negative", "positive", "negative", "json",)
FUNCTION = "execute"
CATEGORY = "MIM_Prompts"
def execute(self, clip=None, template_positive="", template_negative="", variables_json="{}"):
try:
variables = json.loads(variables_json)
except json.JSONDecodeError:
variables = {}
def replace_vars(text):
if not text:
return text
result = text
for var_name, value in variables.items():
placeholder = f"*{var_name}"
result = result.replace(placeholder, str(value))
return result
pos_prompt = replace_vars(template_positive)
neg_prompt = replace_vars(template_negative)
if(clip):
pos_cond, pos_pooled = clip.encode_from_tokens(clip.tokenize(pos_prompt), return_pooled=True)
neg_cond, neg_pooled = clip.encode_from_tokens(clip.tokenize(neg_prompt), return_pooled=True)
else:
pos_cond, pos_pooled = None, None
neg_cond, neg_pooled = None, None
return ([[pos_cond, {"pooled_output": pos_pooled}]], [[neg_cond, {"pooled_output": neg_pooled}]], pos_prompt, neg_prompt, variables_json)
class PromptTemplateKeyValue:
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"clip": ("CLIP",),
},
"optional": {
"template_positive": ("STRING", {"default": "A *obj on a *place", "multiline": True}),
"template_negative": ("STRING", {"default": '', "multiline": True}),
"variables": ("STRING", {
"default": "obj=cat\nplace=wooden table\nstyle=photorealistic",
"multiline": True,
"placeholder": "key=value\nkey2=value2"
}),
}
}
RETURN_TYPES = ("CONDITIONING","CONDITIONING","STRING","STRING",)
RETURN_NAMES = ("positive", "negative", "positive", "negative",)
FUNCTION = "execute"
CATEGORY = "MIM_Prompts"
def execute(self, clip, template_positive, template_negative="", variables=""):
var_dict = {}
if variables.strip():
for line in variables.strip().splitlines():
line = line.strip()
if not line or line.startswith("#"):
continue
if "=" in line:
key, value = line.split("=", 1)
var_dict[key.strip()] = value.strip()
def replace_vars(text):
if not text:
return text
result = text
for var_name, value in var_dict.items():
placeholder = f"*{var_name}"
result = result.replace(placeholder, str(value))
return result
pos_prompt = replace_vars(template_positive)
neg_prompt = replace_vars(template_negative)
pos_tokens = clip.tokenize(pos_prompt)
neg_tokens = clip.tokenize(neg_prompt)
pos_cond, pos_pooled = clip.encode_from_tokens(pos_tokens, return_pooled=True)
neg_cond, neg_pooled = clip.encode_from_tokens(neg_tokens, return_pooled=True)
return (
[[pos_cond, {"pooled_output": pos_pooled}]],
[[neg_cond, {"pooled_output": neg_pooled}]],
pos_prompt,
neg_prompt
)
class PromptTemplateGetManyRandomTemplates:
@classmethod
def INPUT_TYPES(s):
return {
"optional": {
"TemplatesParts": ("STRING", {
"default": "",
"multiline": True,
"placeholder": "value\nvalue2\nvalue3"
}),
"Count": ("INT", {"default": 1, "min": 1, "max": 1000}),
}
}
RETURN_TYPES = ("STRING",)
RETURN_NAMES = ("template",)
FUNCTION = "execute"
CATEGORY = "MIM_Prompts"
def execute(self, TemplatesParts="", Count=1, **kwargs):
if not TemplatesParts:
return ("",)
lines = [line.strip() for line in TemplatesParts.splitlines() if line.strip()]
unique_templates = list(dict.fromkeys(lines))
if not unique_templates:
return ("",)
actual_count = min(Count, len(unique_templates))
selected = random.sample(unique_templates, actual_count)
return (", ".join(selected),)