-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathlaunch.py
244 lines (179 loc) · 6.22 KB
/
launch.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
# this scripts installs necessary requirements and launches main program in webui.py
import importlib
import importlib.util
import os
import shlex
import subprocess
import sys
python = sys.executable
git = os.environ.get("GIT", "git")
torch_command = os.environ.get(
"TORCH_COMMAND",
"pip install torch==1.12.1+cu113 torchvision==0.13.1+cu113 torchaudio==0.12.1+cu113 -f https://download.pytorch.org/whl/torch_stable.html",
)
requirements_file = os.environ.get("REQS_FILE", "requirements.txt")
commandline_args = os.environ.get("COMMANDLINE_ARGS", "")
args = shlex.split(commandline_args)
def extract_arg(args, name):
return [x for x in args if x != name], name in args
args, skip_torch_cuda_test = extract_arg(args, "--skip-torch-cuda-test")
# Get the current process directory
process_dir = os.path.dirname(os.path.abspath(__file__))
# Check if the process directory is in "Program Files" or "Program Files (x86)"
if "Program Files" in process_dir or "Program Files (x86)" in process_dir:
confirm = (
input(
"WARNING: The current process directory is in 'Program Files' or 'Program Files (x86)'. "
"Continuing may cause issues. Do you want to continue? (Y/N) "
)
.strip()
.lower()
)
if confirm != "y":
print("Exiting the program.")
exit()
def run(command, desc=None, errdesc=None, cwd=None):
if desc is not None:
print(desc)
result = subprocess.run(
command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True, cwd=cwd
)
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 run_python(code, desc=None, errdesc=None):
return run(f'"{python}" -c "{code}"', desc, errdesc)
def run_pip(args, desc=None):
return run(
f'"{python}" -m pip {args} --prefer-binary',
desc=f"Installing {desc}",
errdesc=f"Couldn't install {desc}",
)
def check_run(command):
result = subprocess.run(
command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True
)
return result.returncode == 0
def check_run_python(code):
return check_run(f'"{python}" -c "{code}"')
def is_installed(package):
try:
spec = importlib.util.find_spec(package)
except ModuleNotFoundError:
return False
return spec is not None
# def is_installed(package, version=None):
# try:
# module = importlib.import_module(package)
# if version is not None:
# module_version = version.parse(module.__version__)
# required_version = version.parse(version)
# if module_version < required_version:
# return False
# except ImportError:
# return False
# return True
try:
commit = run(f"{git} rev-parse HEAD").strip()
except Exception:
commit = "<none>"
print(f"Python {sys.version}")
print(f"Commit hash: {commit}")
if not is_installed("torch") or not is_installed("torchvision"):
run(
f'"{python}" -m {torch_command}',
"Installing torch and torchvision",
"Couldn't install torch",
)
else:
print("Check torch and torchvision")
# Only for pre-release
dir_repos = "repositories"
os.makedirs(dir_repos, exist_ok=True)
def repo_dir(name):
return os.path.join(dir_repos, name)
custom_gradio_commit_hash = os.environ.get(
"CUSTOM_GRADIO_COMMIT_HASH", "a3990a6111864e6f7602dd384c871bd054e69ba4"
)
custom_gradio_templates_commit_hash = os.environ.get(
"CUSTOM_GRADIO_TEMPLATES_COMMIT_HASH", "68df69b400ec2871bda9ce6a8dc93c3c30cfa0b5"
)
def git_clone(url, dir, name, commithash=None, branch=None):
if os.path.exists(dir):
if commithash is None:
return
current_hash = run(
f"{git} -C {dir} rev-parse HEAD",
None,
f"Couldn't determine {name}'s hash: {commithash}",
).strip()
if current_hash == commithash:
return
run(
f"{git} -C {dir} fetch",
f"Fetching updates for {name}...",
f"Couldn't fetch {name}",
)
run(
f"{git} -C {dir} checkout {commithash}",
f"Checking out commint for {name} with hash: {commithash}...",
f"Couldn't checkout commit {commithash} for {name}",
)
return
if branch is not None:
run(
f'{git} clone -b {branch} --single-branch "{url}" "{dir}"',
f"Cloning {name} into {dir}...",
f"Couldn't clone {name}",
)
else:
run(
f'{git} clone "{url}" "{dir}"',
f"Cloning {name} into {dir}...",
f"Couldn't clone {name}",
)
if commithash is not None:
run(
f"{git} -C {dir} checkout {commithash}",
None,
"Couldn't checkout {name}'s hash: {commithash}",
)
if not is_installed("gradio"):
git_clone(
"https://github.com/tomchang25/gradio.git",
repo_dir("gradio"),
"Custom Gradio",
custom_gradio_commit_hash,
branch="gradio-for-whisper-auto-transcribe",
)
run_pip(f"install {dir_repos}/gradio", "gradio")
git_clone(
"https://github.com/tomchang25/gradio-templates.git",
repo_dir("gradio-templates"),
"Custom Gradio templates",
custom_gradio_templates_commit_hash,
)
run(
rf"xcopy {dir_repos}\gradio-templates\templates venv\Lib\site-packages\gradio\templates\ /e/y/i",
"Building gardio front",
"Couldn't build gardio front",
)
else:
print("Check gradio")
run_pip(f"install -r {requirements_file}", "requirements for Web UI")
sys.argv += args
if "--exit" in args:
print("Exiting because of --exit argument")
exit(0)
def start_webui():
print(f"Launching Web UI with arguments: {' '.join(sys.argv[1:])}")
from gui import gui
gui().launch()
if __name__ == "__main__":
start_webui()