-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathlaunch_script.py
More file actions
121 lines (90 loc) · 4.27 KB
/
launch_script.py
File metadata and controls
121 lines (90 loc) · 4.27 KB
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
# this scripts installs necessary requirements and launches main program
import subprocess
import os
import importlib.util
import sys
# ****************************************************************************
# * UTIL *
# ****************************************************************************
python = sys.executable if os.getenv('SDGPYTHON') is None else os.getenv('SDGPYTHON')
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'"{python}" -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 torchaudio --index-url https://download.pytorch.org/whl/cu118" if platform == 'win32' else 'pip install torch torchaudio'
main_script_path = "app"
pre_torch_packages = []
post_torch_packages = [
'gradio',
'PySoundFile',
'black',
'diffusers',
'tqdm',
'v-diffusion-pytorch',
'k-diffusion',
'audio_diffusion_pytorch==0.0.96'
]
aliases = {
"PySoundFile": 'soundfile',
"v-diffusion-pytorch": 'diffusion',
"k-diffusion": 'k_diffusion',
"audio_diffusion_pytorch==0.0.96": 'audio_diffusion_pytorch'
}
prGreen('Starting launch script...')
if __name__ == "__main__":
# INSTALL
if not SKIP_INSTALL:
# 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:
print(torch_command)
run(f'"{python}" -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 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)
# LAUNCH
run(f'"{python}" -m {main_script_path}', "Starting main script..", "Couldn't start main script!", live=True)