-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathlaunch_script.py
200 lines (163 loc) · 5.65 KB
/
launch_script.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
# this scripts installs necessary requirements and launches main program
import subprocess
import os
import importlib.util
import sys
import urllib.request
from shutil import which
# ****************************************************************************
# * UTIL *
# ****************************************************************************
python = sys.executable
print(f"Launching using Python: {python}")
platform = sys.platform
def prRed(skk):
print(f"\033[91m{skk}\033[00m")
def prGreen(skk):
print(f"\033[92m{skk}\033[00m")
def prYellow(skk):
print(f"\033[93m{skk}\033[00m")
def run(command, desc=None, errdesc=None, custom_env=None, live=False):
if desc is not None:
print(desc)
if live:
result = subprocess.run(
command, shell=True, env=os.environ if custom_env is None else custom_env
)
if result.returncode != 0:
raise RuntimeError(
f"""{errdesc or 'Error running command'}.
Command: {command}
Error code: {result.returncode}"""
)
return ""
result = subprocess.run(
command,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
shell=True,
env=os.environ if custom_env is None else custom_env,
)
if result.returncode != 0:
message = f"""{errdesc or 'Error running command'}.
Command: {command}
Error code: {result.returncode}
stdout: {result.stdout.decode(encoding="utf8", errors="ignore") if len(result.stdout)>0 else '<empty>'}
stderr: {result.stderr.decode(encoding="utf8", errors="ignore") if len(result.stderr)>0 else '<empty>'}
"""
raise RuntimeError(message)
return result.stdout.decode(encoding="utf8", errors="ignore")
def is_installed(package):
if package in aliases:
package = aliases[package]
try:
spec = importlib.util.find_spec(package)
except ModuleNotFoundError:
return False
return spec is not None
def run_pip(args, desc=None):
index_url = os.environ.get("INDEX_URL", "")
index_url_line = f" --index-url {index_url}" if index_url != "" else ""
return run(
f'"{sys.executable}" -m pip {args} --prefer-binary{index_url_line}',
desc=f"Installing {desc}",
errdesc=f"Couldn't install {desc}",
)
# ****************************************************************************
# * CONFIG *
# ****************************************************************************
SKIP_INSTALL = False
torch_command = (
"pip install torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/cu118"
if platform == "win32"
else "pip install torch torchaudio"
)
main_script_path = "main_gui.py"
pre_torch_packages = []
post_torch_packages = [
"black",
"diffusers",
"tqdm",
"PySimpleGUI",
"PySimpleGUIQt",
"PySide2",
"torchaudio",
"pydub",
"ema_pytorch",
"pytorch_lightning",
"pygame",
"soundfile",
"prefigure",
"wandb",
"einops",
"matplotlib",
"pandas",
"librosa",
"tqdm",
"k-diffusion",
"pyyaml",
"wandb",
"audio_diffusion_pytorch==0.0.96",
"v-diffusion-pytorch",
]
aliases = {
"k-diffusion": "k_diffusion",
"pyyaml": "yaml",
"audio_diffusion_pytorch==0.0.96": "audio_diffusion_pytorch",
"v-diffusion-pytorch": "diffusion",
}
prGreen("Starting launch script...")
if __name__ == "__main__":
# INSTALL
if not SKIP_INSTALL:
if not os.path.exists("sample_diffusion"):
run(
f"git clone https://github.com/sudosilico/sample-diffusion sample_diffusion",
"Cloning sample-diffusion repo.",
"Couldn't clone sample-diffusion repo",
live=True,
)
# pre torch packages
if pre_torch_packages:
for package in pre_torch_packages:
if not is_installed(package):
run_pip(f"install {package}", package)
# TORCH INSTALL
if not is_installed("torch") and torch_command is not None:
run(
f'"{sys.executable}" -m {torch_command}',
"Installing torch.",
"Couldn't install torch",
live=True,
)
# post torch packages
if post_torch_packages:
for package in post_torch_packages:
if not is_installed(package):
run_pip(f"install {package}", package)
if which("ffmpeg") is None:
if platform == "win32":
prYellow("No FFMPEG detected! Opening installer..")
urllib.request.urlretrieve(
"https://github.com/icedterminal/ffmpeg-installer/releases/download/5.1.0.20220727/FFmpeg_Essentials.msi",
"ffmpeg_installer.msi",
)
subprocess.run("ffmpeg_installer.msi", shell=True)
os.remove("ffmpeg_installer.msi")
elif platform == "linux":
prRed(
'No FFMPEG detected! Please update pkgs and run "sudo apt install ffmpeg" to install before using the GUI!'
)
exit()
elif platform == "darwin":
prRed(
'No FFMPEG detected! Please use homebrew "brew install ffmpeg" to install before using the GUI!'
)
exit()
# LAUNCH
run(
f'"{sys.executable}" {main_script_path}',
"Launch success! Starting main script, this might take a bit..",
"MAIN SCRIPT CRASH",
live=True,
)