kazuhasasd
commited on
Commit
•
ad4e05b
1
Parent(s):
6bb6eef
Upload e7ai-animate.py
Browse files- e7ai-animate.py +114 -0
e7ai-animate.py
ADDED
@@ -0,0 +1,114 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# This code is released without a license.
|
2 |
+
|
3 |
+
import yaml
|
4 |
+
|
5 |
+
class TagSwap:
|
6 |
+
|
7 |
+
"""
|
8 |
+
Accepts a comma-delimited prompt and YAML input. Operates in two modes:
|
9 |
+
|
10 |
+
In replace mode, replaces tags if they are present or injects them if they are absent.
|
11 |
+
In select mode, adds a tag if the tag is present. Injects if it is absent.
|
12 |
+
|
13 |
+
The YAML format is:
|
14 |
+
|
15 |
+
select|replace:
|
16 |
+
- tag_name:
|
17 |
+
p: present action
|
18 |
+
a: absent action
|
19 |
+
"""
|
20 |
+
|
21 |
+
@classmethod
|
22 |
+
def INPUT_TYPES(s):
|
23 |
+
return {
|
24 |
+
"required": {
|
25 |
+
"tags": ("STRING", {"default": '', "multiline": True, "forceInput": True}),
|
26 |
+
"rules": ("STRING", {"default": '', "multiline": True })
|
27 |
+
}
|
28 |
+
}
|
29 |
+
|
30 |
+
RETURN_TYPES = ("STRING",)
|
31 |
+
RETURN_NAMES = ("replacements",)
|
32 |
+
FUNCTION = "apply"
|
33 |
+
CATEGORY = "utils"
|
34 |
+
|
35 |
+
def __init__(self):
|
36 |
+
pass
|
37 |
+
|
38 |
+
def replace(self, needles, haystack):
|
39 |
+
for tag in haystack:
|
40 |
+
if tag in needles:
|
41 |
+
if 'p' in needles[tag]:
|
42 |
+
yield needles[tag]['p']
|
43 |
+
else:
|
44 |
+
yield tag
|
45 |
+
|
46 |
+
for needle, actions in needles.items():
|
47 |
+
if 'a' in actions and needle not in haystack:
|
48 |
+
yield actions['a']
|
49 |
+
|
50 |
+
def select(self, needles, haystack):
|
51 |
+
for tag in needles:
|
52 |
+
if tag in haystack:
|
53 |
+
if 'p' in needles[tag]:
|
54 |
+
yield needles[tag]['p']
|
55 |
+
else:
|
56 |
+
if 'a' in needles[tag]:
|
57 |
+
yield needles[tag]['a']
|
58 |
+
|
59 |
+
def apply(self, tags, rules):
|
60 |
+
haystack = [ tag.strip() for tag in tags.split(',') ]
|
61 |
+
input = yaml.safe_load(rules)
|
62 |
+
if 'replace' in input:
|
63 |
+
needles = input['replace']
|
64 |
+
return ( ', '.join(list(self.replace(needles, haystack))), )
|
65 |
+
if 'select' in input:
|
66 |
+
needles = input['select']
|
67 |
+
return ( ', '.join(list(self.select(needles, haystack))), )
|
68 |
+
raise Exception("Must use either 'replace' or 'select'")
|
69 |
+
|
70 |
+
from collections import OrderedDict
|
71 |
+
|
72 |
+
class PromptMerge:
|
73 |
+
|
74 |
+
"""
|
75 |
+
Takes a list of prompts. Merges identical, adjacent prompts. Outputs a
|
76 |
+
format compatible with BatchPromptScheudle.
|
77 |
+
"""
|
78 |
+
|
79 |
+
@classmethod
|
80 |
+
def INPUT_TYPES(s):
|
81 |
+
return {
|
82 |
+
"required": {
|
83 |
+
"prompts": ("STRING",{"default": [], "forceInput": True}),
|
84 |
+
}
|
85 |
+
}
|
86 |
+
|
87 |
+
RETURN_TYPES = ("STRING",)
|
88 |
+
RETURN_NAMES = ("prompt",)
|
89 |
+
INPUT_IS_LIST = True
|
90 |
+
FUNCTION = "apply"
|
91 |
+
CATEGORY = "utils"
|
92 |
+
|
93 |
+
def apply(self, prompts):
|
94 |
+
print("Called with %s"%prompts)
|
95 |
+
merged = OrderedDict()
|
96 |
+
last = None
|
97 |
+
for i, prompt in enumerate(prompts):
|
98 |
+
if prompt != last:
|
99 |
+
merged[i] = prompt
|
100 |
+
last = prompt
|
101 |
+
travel = [ """ "%d": "%s" """ % (index, prompt) for (index, prompt) in merged.items() ]
|
102 |
+
print(travel)
|
103 |
+
return (', '.join(travel),)
|
104 |
+
|
105 |
+
NODE_CLASS_MAPPINGS = {
|
106 |
+
"TagSwap": TagSwap,
|
107 |
+
"PromptMerge": PromptMerge,
|
108 |
+
}
|
109 |
+
|
110 |
+
# A dictionary that contains the friendly/humanly readable titles for the nodes
|
111 |
+
NODE_DISPLAY_NAME_MAPPINGS = {
|
112 |
+
"TagSwap": "Tag Swap",
|
113 |
+
"PromptMerge": "Prompt Merge",
|
114 |
+
}
|