forked from Huanshere/VideoLingo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.py
277 lines (237 loc) · 11.7 KB
/
install.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
import os
import platform
import subprocess
import sys
import zipfile
import shutil
import locale
import requests
import time
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
def install_package(*packages):
subprocess.check_call([sys.executable, "-m", "pip", "install", *packages])
install_package("requests", "rich", "ruamel.yaml")
from core.pypi_autochoose.pypi_autochoose import main as choose_mirror
def load_language(lang='en'):
from ruamel.yaml import YAML
yaml = YAML(typ='safe')
try:
with open(f'language/lang_{lang}.yml', 'r', encoding='utf-8') as f:
return yaml.load(f)
except FileNotFoundError:
print(f"Language file for {lang} not found. Falling back to English.")
with open('language/lang_en.yml', 'r', encoding='utf-8') as f:
return yaml.load(f)
def main():
from rich.console import Console
from rich.table import Table
from rich.panel import Panel
console = Console()
# Determine language based on system locale
system_lang = locale.getdefaultlocale()[0]
lang = 'zh' if system_lang.startswith('zh') else 'en'
# Load language strings
strings = load_language(lang)
console.print(Panel.fit(strings['starting_installation'], style="bold magenta"))
# 直接执行镜像选择
console.print(Panel(strings['configuring_mirror'], style="bold yellow"))
choose_mirror()
def init_language():
from core.config_utils import load_key, update_key
system_lang = locale.getdefaultlocale()[0]
lang_map = {
'zh_CN': 'zh_CN', 'zh_TW': 'zh_CN',
'en_US': 'en_US', 'ja_JP': 'ja_JP'
}
display_language = lang_map.get(system_lang, 'en_US')
if load_key("display_language") == "auto":
update_key("display_language", display_language)
console.print(Panel.fit(f"{strings['display_language_set']}{display_language}", style="bold green"))
def install_requirements():
if os.path.exists("requirements.txt"):
print(strings['converting_requirements'])
try:
with open("requirements.txt", "r", encoding="utf-8") as file:
content = file.read()
with open("requirements.txt", "w", encoding="gbk") as file:
file.write(content)
print(strings['conversion_completed'])
except UnicodeDecodeError:
print(strings['already_gbk'])
except Exception as e:
print(f"{strings['conversion_error']}: {str(e)}")
print(strings['installing_dependencies'])
subprocess.check_call([sys.executable, "-m", "pip", "install", "-r", "requirements.txt"])
else:
print(strings['requirements_not_found'])
def test_mirror_speed(name, base_url):
test_url = f"{base_url}lj1995/VoiceConversionWebUI/resolve/main/README.md"
max_retries = 3
timeout = 10
for attempt in range(max_retries):
try:
start_time = time.time()
response = requests.head(test_url, timeout=timeout)
end_time = time.time()
if response.status_code == 200:
speed = (end_time - start_time) * 1000
return name, speed
except requests.RequestException:
if attempt == max_retries - 1:
return name, float('inf')
time.sleep(1) # Wait 1 second before retrying
return name, float('inf')
def download_uvr_model():
models = {
"HP2_all_vocals.pth": "lj1995/VoiceConversionWebUI/resolve/e992cb1bc5d777fcddce20735a899219b1d46aba/uvr5_weights/HP2_all_vocals.pth",
"VR-DeEchoAggressive.pth": "lj1995/VoiceConversionWebUI/resolve/main/uvr5_weights/VR-DeEchoAggressive.pth"
}
mirrors = {
"Official": "https://huggingface.co/",
"Mirror": "https://hf-mirror.com/"
}
os.makedirs("_model_cache/uvr5_weights", exist_ok=True)
for model_name, model_path in models.items():
model_file_path = f"_model_cache/uvr5_weights/{model_name}"
if not os.path.exists(model_file_path):
print(f"{strings['downloading_uvr_model']}{model_name}...")
# Test speed for each mirror
speeds = []
for mirror_name, mirror_url in mirrors.items():
name, speed = test_mirror_speed(mirror_name, mirror_url)
speeds.append((name, speed))
print(f"{mirror_name} {strings['mirror_speed']} {speed:.2f} ms")
# Choose the fastest mirror
fastest_mirror = min(speeds, key=lambda x: x[1])[0]
print(f"{strings['choosing_mirror']} {fastest_mirror}")
# Download from the fastest mirror
url = mirrors[fastest_mirror] + model_path
try:
response = requests.get(url, stream=True)
response.raise_for_status()
total_size = int(response.headers.get('content-length', 0))
with open(model_file_path, "wb") as file:
downloaded_size = 0
for data in response.iter_content(chunk_size=8192):
size = file.write(data)
downloaded_size += size
if total_size:
percent = (downloaded_size / total_size) * 100
print(f"{strings['download_progress']} {percent:.2f}%", end="\r")
print(f"\n{model_name} {strings['model_downloaded']}")
except requests.RequestException as e:
print(f"{strings['download_failed']} {model_name}: {str(e)}")
else:
print(f"{model_name} {strings['model_exists']}")
def download_and_extract_ffmpeg():
system = platform.system()
if system == "Windows":
ffmpeg_exe = "ffmpeg.exe"
url = "https://github.com/BtbN/FFmpeg-Builds/releases/download/latest/ffmpeg-master-latest-win64-gpl.zip"
elif system == "Darwin":
ffmpeg_exe = "ffmpeg"
url = "https://evermeet.cx/ffmpeg/getrelease/zip"
elif system == "Linux":
ffmpeg_exe = "ffmpeg"
url = "https://johnvansickle.com/ffmpeg/builds/ffmpeg-git-amd64-static.tar.xz"
else:
return
if os.path.exists(ffmpeg_exe):
print(f"{ffmpeg_exe} {strings['ffmpeg_exists']}")
return
print(strings['downloading_ffmpeg'])
response = requests.get(url)
if response.status_code == 200:
filename = "ffmpeg.zip" if system in ["Windows", "Darwin"] else "ffmpeg.tar.xz"
with open(filename, 'wb') as f:
f.write(response.content)
print(f"{strings['ffmpeg_downloaded']}{filename}")
print(strings['extracting_ffmpeg'])
if system == "Linux":
import tarfile
with tarfile.open(filename) as tar_ref:
for member in tar_ref.getmembers():
if member.name.endswith("ffmpeg"):
member.name = os.path.basename(member.name)
tar_ref.extract(member)
else:
with zipfile.ZipFile(filename, 'r') as zip_ref:
for file in zip_ref.namelist():
if file.endswith(ffmpeg_exe):
zip_ref.extract(file)
shutil.move(os.path.join(*file.split('/')[:-1], os.path.basename(file)), os.path.basename(file))
print(strings['cleaning_up'])
os.remove(filename)
if system == "Windows":
for item in os.listdir():
if os.path.isdir(item) and "ffmpeg" in item.lower():
shutil.rmtree(item)
print(strings['ffmpeg_extraction_completed'])
else:
print(strings['failed_download_ffmpeg'])
def install_noto_font():
if platform.system() == 'Linux':
subprocess.run(['sudo', 'apt-get', 'install', '-y', 'fonts-noto'], check=True)
# User selects Whisper model
table = Table(title=strings['whisper_model_selection'])
table.add_column(strings['option'], style="cyan", no_wrap=True)
table.add_column(strings['model'], style="magenta")
table.add_column(strings['description'], style="green")
table.add_row("1", "whisperX 💻", strings['whisperx_local'])
table.add_row("2", "whisperXapi ☁️", strings['whisperxapi_cloud'])
console.print(table)
console.print(strings['model_difference_info'])
if len(sys.argv) > 1:
choice = sys.argv[1]
else:
choice = console.input(strings['enter_option'])
if platform.system() == 'Darwin':
console.print(Panel(strings['installing_cpu_pytorch'], style="cyan"))
subprocess.check_call([sys.executable, "-m", "pip", "install", "torch", "torchaudio"])
if choice == '1':
print(strings['installing_whisperx'])
current_dir = os.getcwd()
whisperx_dir = os.path.join(current_dir, "third_party", "whisperX")
os.chdir(whisperx_dir)
subprocess.check_call([sys.executable, "-m", "pip", "install", "-e", "."])
os.chdir(current_dir)
else:
if choice == '1':
console.print(Panel(strings['installing_cuda_pytorch'], style="cyan"))
subprocess.check_call([sys.executable, "-m", "pip", "install", "torch==2.0.0", "torchaudio==2.0.0", "--index-url", "https://download.pytorch.org/whl/cu118"])
print(strings['installing_whisperx'])
current_dir = os.getcwd()
whisperx_dir = os.path.join(current_dir, "third_party", "whisperX")
os.chdir(whisperx_dir)
subprocess.check_call([sys.executable, "-m", "pip", "install", "-e", "."])
os.chdir(current_dir)
elif choice == '2':
table = Table(title=strings['pytorch_version_selection'])
table.add_column(strings['option'], style="cyan", no_wrap=True)
table.add_column(strings['model'], style="magenta")
table.add_column(strings['description'], style="green")
table.add_row("1", "CPU", strings['cpu_version'])
table.add_row("2", "GPU", strings['gpu_version'])
console.print(table)
torch_choice = console.input(strings['enter_pytorch_option'])
if torch_choice == '1':
console.print(Panel(strings['installing_cpu_pytorch_message'], style="cyan"))
subprocess.check_call([sys.executable, "-m", "pip", "install", "torch", "torchaudio"])
elif torch_choice == '2':
console.print(Panel(strings['installing_gpu_pytorch_message'], style="cyan"))
subprocess.check_call([sys.executable, "-m", "pip", "install", "torch", "torchaudio", "--index-url", "https://download.pytorch.org/whl/cu118"])
else:
console.print(strings['invalid_choice'])
subprocess.check_call([sys.executable, "-m", "pip", "install", "torch", "torchaudio"])
else:
raise ValueError(strings['invalid_choice_retry'])
init_language()
install_noto_font()
install_requirements()
download_uvr_model()
download_and_extract_ffmpeg()
console.print(Panel.fit(strings['installation_completed'], style="bold green"))
console.print(strings['start_streamlit_command'])
console.print("[bold cyan]streamlit run st.py[/bold cyan]")
if __name__ == "__main__":
main()