forked from banodoco/Dough
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon_utils.py
364 lines (299 loc) · 11.5 KB
/
common_utils.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
357
358
359
360
361
362
363
364
from datetime import datetime
from pathlib import Path
import os
import csv
import subprocess
import time
from django.db import connection
import portalocker
import psutil
import socket
import requests
import streamlit as st
import json
import platform
from contextlib import contextmanager
import toml
from shared.constants import SERVER, CreativeProcessPage, ServerType
from ui_components.models import InternalUserObject
from utils.cache.cache import CacheKey, StCache
from utils.data_repo.data_repo import DataRepo
from ui_components.constants import DefaultProjectSettingParams
def set_default_values(shot_uuid):
data_repo = DataRepo()
timing_list = data_repo.get_timing_list_from_shot(shot_uuid)
if "selected_page_idx" not in st.session_state:
st.session_state["selected_page_idx"] = 0
if "page" not in st.session_state:
st.session_state["page"] = CreativeProcessPage.value_list()[st.session_state["selected_page_idx"]]
if "strength" not in st.session_state:
st.session_state["strength"] = DefaultProjectSettingParams.batch_strength
st.session_state["prompt_value"] = DefaultProjectSettingParams.batch_prompt
st.session_state["model"] = None
st.session_state["negative_prompt_value"] = DefaultProjectSettingParams.batch_negative_prompt
st.session_state["guidance_scale"] = DefaultProjectSettingParams.batch_guidance_scale
st.session_state["seed"] = DefaultProjectSettingParams.batch_seed
st.session_state["num_inference_steps"] = DefaultProjectSettingParams.batch_num_inference_steps
st.session_state["transformation_stage"] = DefaultProjectSettingParams.batch_transformation_stage
if "current_frame_uuid" not in st.session_state and len(timing_list) > 0:
timing = timing_list[0]
st.session_state["current_frame_uuid"] = timing.uuid
st.session_state["current_frame_index"] = timing.aux_frame_index + 1
if "frame_styling_view_type" not in st.session_state:
st.session_state["frame_styling_view_type"] = "Generate"
st.session_state["frame_styling_view_type_index"] = 0
if "explorer_view" not in st.session_state:
st.session_state["explorer_view"] = "Explorations"
st.session_state["explorer_view_index"] = 0
if "shot_view" not in st.session_state:
st.session_state["shot_view"] = "Animate Frames"
st.session_state["shot_view_index"] = 0
if "styling_view" not in st.session_state:
st.session_state["styling_view"] = "Generate"
st.session_state["styling_view_index"] = 0
def copy_sample_assets(project_uuid):
import shutil
# copy sample video
source = "sample_assets/sample_videos/sample.mp4"
dest = "videos/" + project_uuid + "/assets/resources/input_videos/sample.mp4"
shutil.copyfile(source, dest)
# TODO: some of the folders are not in use, remove them
# TODO: a lot of things are directly going into the temp folder, put them in appropriate folder
# TODO: comfy gens are copied into videos folder (taking twice as much space) also sometimes save is directly triggered from the backend model
def create_working_assets(project_uuid):
if SERVER != ServerType.DEVELOPMENT.value:
return
new_project = True
if os.path.exists("videos/" + project_uuid):
new_project = False
directory_list = [
# project specific files
"videos/" + project_uuid,
"videos/" + project_uuid + "/temp",
"videos/" + project_uuid + "/assets",
"videos/" + project_uuid + "/assets/frames",
# these are user uploaded/generated base keyframes
"videos/" + project_uuid + "/assets/frames/base",
# these are modified frames (like zoom, crop, rotate)
"videos/" + project_uuid + "/assets/frames/modified",
# these are inpainted keyframes
"videos/" + project_uuid + "/assets/frames/inpainting",
"videos/" + project_uuid + "/assets/resources",
"videos/" + project_uuid + "/assets/resources/masks",
"videos/" + project_uuid + "/assets/resources/audio",
"videos/" + project_uuid + "/assets/resources/input_videos",
"videos/" + project_uuid + "/assets/resources/prompt_images",
"videos/" + project_uuid + "/assets/videos",
# for raw imports (not in use anymore)
"videos/" + project_uuid + "/assets/videos/raw",
# storing generated videos
"videos/" + project_uuid + "/assets/videos/completed",
# app data
"inference_log",
# temp folder
"videos/temp",
]
for directory in directory_list:
if not os.path.exists(directory):
os.makedirs(directory)
# copying sample assets for new project
# if new_project:
# copy_sample_assets(project_uuid)
def truncate_decimal(num: float, n: int = 2) -> float:
return int(num * 10**n) / 10**n
def get_current_user(invalidate_cache=False) -> InternalUserObject:
data_repo = DataRepo()
user = data_repo.get_first_active_user(invalidate_cache=invalidate_cache)
return user
def user_credits_available():
current_user = get_current_user()
return True if (current_user and current_user.total_credits > 0) else False
def get_current_user_uuid():
current_user = get_current_user()
if current_user:
return current_user.uuid
else:
return None
def reset_project_state():
keys_to_delete = [
"page",
"current_frame_uuid",
"which_number_for_starting_image",
"rotated_image",
"current_frame_index",
"prev_frame_index",
"zoom_level_input",
"rotation_angle_input",
"x_shift",
"y_shift",
"working_image",
"degrees_rotated_to",
"degree",
"edited_image",
"index_of_type_of_mask_selection",
"type_of_mask_replacement",
"which_layer",
"which_layer_index",
"drawing_input",
"image_created",
"precision_cropping_inpainted_image_uuid",
"transformation_stage",
"custom_pipeline",
"index_of_last_custom_pipeline",
"index_of_controlnet_adapter_type",
"lora_model_1",
"lora_model_2",
"lora_model_3",
"index_of_lora_model_1",
"index_of_lora_model_2",
"index_of_lora_model_3",
"custom_models",
"adapter_type",
"low_threshold",
"high_threshold",
"model",
"prompt",
"strength",
"guidance_scale",
"seed",
"num_inference_steps",
"dreambooth_model_uuid",
"seed",
"promote_new_generation",
"use_new_settings",
"shot_uuid",
"maintain_state",
"status_optn_index",
]
for k in keys_to_delete:
if k in st.session_state:
del st.session_state[k]
# numbered keys
numbered_keys_to_delete = ["animation_style_index_", "animation_style_"]
# TODO: remove hardcoded 20, find a better way to clear numbered state
for i in range(20):
for k in numbered_keys_to_delete:
key = k + str(i)
if key in st.session_state:
del st.session_state[key]
# reset cache
StCache.clear_entire_cache()
def reset_styling_settings(timing_uuid):
keys_to_delete = [
f"frame_styling_stage_index_{timing_uuid}",
"index_of_default_model",
"index_of_controlnet_adapter_type",
"index_of_dreambooth_model",
f"prompt_value_{timing_uuid}",
"negative_prompt_value",
]
for k in keys_to_delete:
if k in st.session_state:
del st.session_state[k]
def is_process_active(custom_process_name, custom_process_port):
# This caching assumes that the runner won't interrupt or break once started
cache_key = custom_process_name + "_process_state"
if cache_key in st.session_state and st.session_state[cache_key]:
return True
res = False
try:
if platform.system() == "Windows":
try:
client_socket = socket.create_connection(("localhost", custom_process_port))
client_socket.close()
res = True
# print("----------------- process is active")
except ConnectionRefusedError:
res = False
# print("----------------- process is NOT active")
else:
# Use 'ps' for Unix/Linux
ps_output = subprocess.check_output(["ps", "aux"]).decode("utf-8")
res = True if custom_process_name in ps_output else False
if res:
st.session_state[cache_key] = True
return res
except subprocess.CalledProcessError:
return False
# If the process is not found or an error occurs, assume it's not active
return False
def refresh_process_active(port):
url = f"http://localhost:{port}/health"
timeout = 5
try:
response = requests.get(url, timeout=timeout)
if response.status_code == 200:
data = response.json()
if data.get("status") == "healthy":
return True
print(f"Unexpected response: {response.text}")
return False
except requests.RequestException as e:
# print(f"Failed to connect to Flask app: {str(e)}")
return False
def padded_integer(integer, pad_length=4):
padded_string = str(integer).zfill(pad_length)
return padded_string
@contextmanager
def sqlite_atomic_transaction():
cursor = connection.cursor()
try:
cursor.execute("BEGIN IMMEDIATE")
yield cursor
connection.commit()
except Exception as e:
connection.rollback()
raise e
finally:
cursor.close()
def acquire_lock(key):
data_repo = DataRepo()
retries = 0
while retries < 1:
lock_status = data_repo.acquire_lock(key)
if lock_status:
return lock_status
retries += 1
time.sleep(0.2)
return False
def release_lock(key):
data_repo = DataRepo()
data_repo.release_lock(key)
return True
def get_toml_config(key=None, toml_file="config.toml"):
toml_config_path = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "scripts", toml_file))
toml_data = {}
with open(toml_config_path, "r") as f:
toml_data = toml.load(f)
if key and key in toml_data:
return toml_data[key]
return toml_data
def update_toml_config(toml_dict, toml_file="config.toml"):
toml_config_path = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "scripts", toml_file))
with open(toml_config_path, "wb") as f:
toml_content = toml.dumps(toml_dict)
f.write(toml_content.encode())
# 2024-08-19T17:02:37.593405 --> 5:02 PM 19/8
convert_timestamp_1 = lambda timestamp: datetime.strptime(timestamp, "%Y-%m-%dT%H:%M:%S.%f").strftime(
"%I:%M %p %d/%m"
)
def convert_timestamp_to_relative(timestamp_str):
now = datetime.now()
# Convert the timestamp string to a datetime object
try:
# Assuming the timestamp is in ISO format. Adjust the format if it's different.
timestamp = datetime.fromisoformat(timestamp_str)
except ValueError:
# If the conversion fails, return a default message
return "Unknown time"
diff = now - timestamp
if diff.days > 0:
return f"{diff.days} day{'s' if diff.days > 1 else ''} ago"
elif diff.seconds >= 3600:
hours = diff.seconds // 3600
return f"{hours} hr{'s' if hours > 1 else ''} ago"
elif diff.seconds >= 60:
minutes = diff.seconds // 60
return f"{minutes} min{'s' if minutes > 1 else ''} ago"
else:
return "Just now"