-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.py
317 lines (273 loc) Β· 11.3 KB
/
main.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
# main.py
import config
import os
import sys
import time
import platform
from process import start_process, read_structure_parameters
from colorama import init, Fore, Style
from audio2txt import process_audio2txt
from datetime import datetime
import time # Assuming wait_time() uses time.sleep() under the hood
def wizard():
#### WIZARD STEP 1 : SCENE SOURCE ####
print(Fore.GREEN + Style.BRIGHT + "1 - Use your prompt to create your scene (/config-files/scene.csv)")
print("2 - Use a .wav audio file to create your scene (/input/*.wav)")
# print(Fore.GREEN + Style.BRIGHT + "3 - Use a story to create your scene (/config-files/conversation-capture.txt)")
choice = input(Style.RESET_ALL + "Enter your choice (1 to 2): ")
if choice == "3":
choice = "1" # Force to use the first option
# Choice Feedbacks
print(Style.RESET_ALL + "")
if choice == "1":
config.CONVERSATION_MODE = False
config.AUDIO_MODE = False
# Read the number of lines from config-files/scene.csv except the header
with open('config-files/scene.csv', 'r') as file:
lines = file.readlines()[1:] # Skip the header
number_of_lines = len(lines)
s = "s" if number_of_lines > 1 else ""
s2 = "s" if config.NUM_ITERATIONS > 1 else ""
print(f"A total of {number_of_lines} scene{s} will be rendered with {config.NUM_ITERATIONS} iteration{s2}")
elif choice == "2":
config.CONVERSATION_MODE = True
config.AUDIO_MODE = True
# Assuming 'config.INPUT_PATH' and other variables like 'file_path', 'model', 'language' are defined correctly
# Here you should process the audio file as needed; the example below may not directly apply to your logic.
print("A total of 1 file will be processed by ChatGPT to create your scenes")
elif choice == "3":
config.CONVERSATION_MODE = True
config.AUDIO_MODE = False
# Read the number of lines from /config-files/conversation-capture.txt
with open('config-files/conversation-capture.txt', 'r') as file:
lines = file.readlines()
number_of_lines = len(lines)
s = "s" if number_of_lines > 1 else ""
s2 = "s" if config.NUM_ITERATIONS > 1 else ""
print(f"A total of {number_of_lines} scene{s} will be rendered with {config.NUM_ITERATIONS} iteration{s2}")
else:
print("Invalid selection.")
exit(1)
s3 = "s" if config.RPS_Sleep > 1 else ""
s4 = "s" if config.MAX_CONCURRENT_JOBS > 1 else ""
print(f"We will wait {config.RPS_Sleep} second{s3} between API call")
print(f"And render {config.MAX_CONCURRENT_JOBS} scene{s4} at the same time")
wait_time(1)
#### WIZARD STEP 2 : SCENE RENDER VARIABLES ####
print(Fore.GREEN + Style.BRIGHT + "1 - Randomly combine suffix to research a style (/config-files/structure.csv)")
print("2 - Use a set prefix and suffix (/config-files/config.py)")
print("3 - Use only the scene with no prefix or suffix")
choice = input(Style.RESET_ALL + "Enter your choice (1 or 2): ")
# Choice Feedbacks
print(Style.RESET_ALL + "")
if choice == "1":
config.OVERRIDE_MODE = False
structure_params = read_structure_parameters(config.STRUCTURE_CSV_PATH)
print(f"A total of {len(structure_params)} dictionaries have been loaded.")
# Calculate the total number of unique combinations
total_combinations = 1 # Start with 1 since we're multiplying
for category in structure_params:
total_combinations *= len(structure_params[category])
print(f"This represents a total of {format_number(total_combinations)} unique possible combinations.")
elif choice == "2":
config.OVERRIDE_MODE = True
prefix = config.SCENE.split("<SCENE>")[0]
suffix = config.SCENE.split("<SCENE>")[1]
print("The scene will be rendered using your Override Value")
print("Prefix: " + prefix + " ...")
print("Suffix: ..." + suffix)
if config.REALISTIC_MODE == 1:
print("The scene will be rendered using the Realistic Model")
print("Model ID: " + config.OVERRIDE_MODEL)
else:
print("The scene will be rendered using the Creative Model")
print("Model ID: " + config.OVERRIDE_MODEL)
print("The render style " + config.OVERRIDE_STYLE + " will be apply to your scene")
elif choice == "3":
config.STRUCTURE_CSV_PATH = 'config-files/empty.csv'
else:
print("Invalid selection.")
exit(1)
wait_time(1)
#### WIZARD STEP 3 : SCENE LANDSCAPE OR PORTRAIT MODE ####
print(Fore.GREEN + Style.BRIGHT + "1 - Render Scene in Landscape Format (16:9)")
print("2 - Render Scene in Portrait Format (3:4)")
choice = input(Style.RESET_ALL + "Enter your choice (1 or 2): ")
# Choice Feedbacks
print(Style.RESET_ALL + "")
if choice == "1":
config.IMAGE_WIDTH = 1360
config.IMAGE_HEIGHT = 768
elif choice == "2":
config.IMAGE_WIDTH = 768
config.IMAGE_HEIGHT = 1024
else:
print(Fore.LIGHTBLACK_EX + Style.BRIGHT + "")
print("Invalid selection.")
exit(1)
if config.IMAGE_WIDTH == 768:
print("Render Mode = " + config.RENDER_MODE + " in Portrait Format (3:4)")
else:
print("Render Mode = " + config.RENDER_MODE + " in Landscape Format (16:9)")
wait_time(1)
#### WIZARD STEP 4 : RENDER ENGINE ####
print(Fore.GREEN + Style.BRIGHT + "1 - Render using Leonardo.AI API")
print("2 - Render using Stable Diffusion 3 API")
print("3 - Use preset in config (config.py)")
choice = input(Style.RESET_ALL + "Enter your choice (1, 2 or 3): ")
# Choice Feedbacks
print(Style.RESET_ALL + "")
if choice == "1":
config.RENDER_MODE = "Leonardo"
print("Render Mode will be Leonardo")
elif choice == "2":
config.RENDER_MODE = "sd3"
print("Render Mode will be Stable Diffusion 3")
elif choice == "3":
### No change to Render Mode
print("Render Mode will be " + config.RENDER_MODE + " API")
else:
print("Invalid selection.")
exit(1)
print(Fore.LIGHTYELLOW_EX + Style.BRIGHT + "Configuration Completed" + Style.RESET_ALL)
# press_any_key_to_continue()
# Function to check for missing configuration variables
def check_config_vars(module, vars_list):
missing_vars = []
# Check if RENDER_MODE is set and then call load_override_settings to initialize dynamic variables
if hasattr(module, 'RENDER_MODE'):
# Since load_override_settings updates module-level variables, ensure it's called before checking variables
config.load_override_settings(config.RENDER_MODE)
else:
# If RENDER_MODE itself is missing, add it to missing_vars
missing_vars.append('RENDER_MODE')
# Now check for both static and dynamic variables presence
for var in vars_list:
if not hasattr(module, var):
missing_vars.append(var)
return missing_vars
def format_number(num):
if num >= 1e9: # Billions
return f"{num / 1e9:.1f} billion"
elif num >= 1e6: # Millions
return f"{num / 1e6:.1f} million"
elif num >= 1e3: # Thousands
return f"{num / 1e3:.1f} thousand"
else:
return str(num)
# Clear console text
def clear_screen():
if platform.system() == "Windows":
os.system('cls')
else:
os.system('clear')
def press_any_key_to_continue():
"""Pause the program until the user presses any key, handling different OSes."""
print("")
try:
if platform.system() == "Windows":
# For Windows
import msvcrt
print(Fore.LIGHTBLACK_EX + Style.BRIGHT + "Press enter to start your EchoAI render...")
msvcrt.getch()
else:
# For Unix/Linux/Mac
print(Fore.LIGHTBLACK_EX + Style.BRIGHT + "Press enter to start your EchoAI render...")
os.system("read -n 1 -s -r")
except Exception as e:
print(f"Error waiting for key press: {e}")
print(Style.RESET_ALL + "")
def wait_time(seconds):
spinner = ['π ', 'π ', 'π ', 'π ', 'π ', 'π ', 'π ', 'π ']
end_time = time.time() + seconds
idx = 0 # Index for the spinner animation frame
while time.time() < end_time:
# Print the current spinner frame followed by a carriage return to overwrite it on the next print
sys.stdout.write('\r' + spinner[idx % len(spinner)])
sys.stdout.flush()
idx += 1
time.sleep(0.3) # Adjust for desired spinner speed
# Overwrite the spinner with a space to remove it, then return to the start of the line
sys.stdout.write('\r' + ' ' * len(spinner[idx % len(spinner)]) + '\r')
sys.stdout.flush()
print("")
def main():
#### SETTING UP ####
init() # Initialize colorama
os.environ['TERM'] = 'xterm-256color'
# Usage of clear_screen
clear_screen()
print(Fore.GREEN + Style.BRIGHT + "WELCOME TO ECHOAI" + Fore.LIGHTBLACK_EX)
# Checking that default config file exist
# Import the config module
try:
import config
except ImportError as e:
print("The config file cannot be found or has an error.")
raise
# List of required configuration variables
required_vars = [
"LEONARDO_TOKEN",
"OPENAI_API_KEY",
"RANDOM_MODE",
"BROADCAST_MODE",
"RENDER_MODE",
"NUM_ITERATIONS",
"MAX_CONCURRENT_JOBS",
"IMAGE_WIDTH",
"IMAGE_HEIGHT",
"CONVERSATION_MODE",
"AUDIO_MODE",
"RANDOM_READ",
"FORCE_UNIVERSE",
"NEGATIVE",
"SLIDESHOW_ENABLED",
"OVERRIDE_MODE",
"OVERRIDE_STRING",
"OVERRIDE_STYLE",
"OVERRIDE_MODEL",
"SCENE"
]
# Check for missing variables
missing = check_config_vars(config, required_vars)
# Notify the user if any variables are missing
if missing:
missing_str = ", ".join(missing)
print(f"The following configuration variables are missing: {missing_str}")
exit(0)
else:
print("All required configuration variables are set.")
print("Process Started")
if config.WIZARD:
wizard()
clear_screen()
# Start time tracker
start_time = datetime.now()
print(Fore.GREEN + Style.BRIGHT + "START TIME: " + start_time.strftime('%I:%M %p') + Fore.LIGHTBLACK_EX)
scenes = []
if config.AUDIO_MODE:
scenes = process_audio2txt()
print(scenes)
wait_time(3)
clear_screen()
start_process(scenes)
# End time tracker and calculate duration
end_time = datetime.now()
duration = end_time - start_time
# Format the duration in a readable format (e.g., "1h20 min" or "30 sec")
if duration.seconds >= 3600:
# More than an hour
hours = duration.seconds // 3600
minutes = (duration.seconds % 3600) // 60
duration_str = f"{hours}h{minutes} min"
elif duration.seconds >= 60:
# More than a minute but less than an hour
minutes = duration.seconds // 60
seconds = duration.seconds % 60
duration_str = f"{minutes} min {seconds} sec" if seconds > 0 else f"{minutes} min"
else:
# Less than a minute
duration_str = f"{duration.seconds} sec"
print(Fore.GREEN + Style.BRIGHT + "TOTAL DURATION: " + duration_str + Fore.LIGHTBLACK_EX)
if __name__ == "__main__":
main()