Spaces:
Runtime error
Runtime error
Create deforum_data.py
Browse files- deforum_data.py +34 -0
deforum_data.py
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
import numexpr
|
| 3 |
+
import re
|
| 4 |
+
import json
|
| 5 |
+
|
| 6 |
+
def parse_weight_schedule(string, max_frames):
|
| 7 |
+
string = str(string).replace(' ', '')
|
| 8 |
+
keyframes = {}
|
| 9 |
+
pattern = r'((?P<frame>[0-9]+):[\s]*[\(](?P<param>[\S\s]*?)[\)])'
|
| 10 |
+
for match in re.finditer(pattern, string):
|
| 11 |
+
keyframes[int(match.group("frame"))] = match.group("param")
|
| 12 |
+
if 0 not in keyframes: keyframes[0] = "0"
|
| 13 |
+
|
| 14 |
+
series = np.zeros(max_frames)
|
| 15 |
+
sorted_keys = sorted(keyframes.keys())
|
| 16 |
+
for i in range(len(sorted_keys)):
|
| 17 |
+
f_start = sorted_keys[i]
|
| 18 |
+
f_end = sorted_keys[i+1] if i < len(sorted_keys)-1 else max_frames
|
| 19 |
+
formula = keyframes[f_start]
|
| 20 |
+
for f in range(f_start, f_end):
|
| 21 |
+
try:
|
| 22 |
+
val = numexpr.evaluate(formula, local_dict={'t':f, 'pi':np.pi, 'sin':np.sin, 'cos':np.cos, 'tan':np.tan})
|
| 23 |
+
series[f] = float(val)
|
| 24 |
+
except:
|
| 25 |
+
try: series[f] = float(formula)
|
| 26 |
+
except: series[f] = series[f-1] if f > 0 else 0.0
|
| 27 |
+
return series
|
| 28 |
+
|
| 29 |
+
def parse_prompts(prompt_json):
|
| 30 |
+
try:
|
| 31 |
+
data = json.loads(prompt_json.replace("'", '"'))
|
| 32 |
+
return {int(k): v for k, v in data.items()}
|
| 33 |
+
except:
|
| 34 |
+
return {0: "error parsing prompts"}
|