forked from Huanshere/VideoLingo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.py
More file actions
223 lines (197 loc) · 8.6 KB
/
Copy pathinstall.py
File metadata and controls
223 lines (197 loc) · 8.6 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
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
import os, sys
import platform
import subprocess
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
# ASCII艺术字形式的项目Logo
ascii_logo = """
__ ___ _ _ _
\ \ / (_) __| | ___ ___ | | (_)_ __ __ _ ___
\ \ / /| |/ _` |/ _ \/ _ \| | | | '_ \ / _` |/ _ \
\ V / | | (_| | __/ (_) | |___| | | | | (_| | (_) |
\_/ |_|\__,_|\___|\___/|_____|_|_| |_|\__, |\___/
|___/
"""
def install_package(*packages):
"""
安装指定的Python包
参数:
*packages: 要安装的包名列表
"""
subprocess.check_call([sys.executable, "-m", "pip", "install", *packages])
def check_nvidia_gpu():
"""
检测系统是否有NVIDIA GPU
返回:
bool: 如果检测到NVIDIA GPU返回True,否则返回False
"""
install_package("pynvml")
import pynvml
from translations.translations import translate as t
initialized = False
try:
pynvml.nvmlInit()
initialized = True
device_count = pynvml.nvmlDeviceGetCount()
if device_count > 0:
print(t("Detected NVIDIA GPU(s)"))
for i in range(device_count):
handle = pynvml.nvmlDeviceGetHandleByIndex(i)
name = pynvml.nvmlDeviceGetName(handle)
print(f"GPU {i}: {name}")
return True
else:
print(t("No NVIDIA GPU detected"))
return False
except pynvml.NVMLError:
print(t("No NVIDIA GPU detected or NVIDIA drivers not properly installed"))
return False
finally:
if initialized:
pynvml.nvmlShutdown()
def check_ffmpeg():
"""
检查系统是否安装了FFmpeg
如果未安装,提供相应系统的安装指南
返回:
bool: 如果FFmpeg已安装返回True,否则退出程序
"""
from rich.console import Console
from rich.panel import Panel
from translations.translations import translate as t
console = Console()
try:
# 检查ffmpeg是否已安装
subprocess.run(['ffmpeg', '-version'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, check=True)
console.print(Panel(t("✅ FFmpeg is already installed"), style="green"))
return True
except (subprocess.CalledProcessError, FileNotFoundError):
system = platform.system()
install_cmd = ""
# 根据不同操作系统提供不同的安装命令
if system == "Windows":
install_cmd = "choco install ffmpeg"
extra_note = t("Install Chocolatey first (https://chocolatey.org/)")
elif system == "Darwin":
install_cmd = "brew install ffmpeg"
extra_note = t("Install Homebrew first (https://brew.sh/)")
elif system == "Linux":
install_cmd = "sudo apt install ffmpeg # Ubuntu/Debian\nsudo yum install ffmpeg # CentOS/RHEL"
extra_note = t("Use your distribution's package manager")
# 显示安装指南
console.print(Panel.fit(
t("❌ FFmpeg not found\n\n") +
f"{t('🛠️ Install using:')}\n[bold cyan]{install_cmd}[/bold cyan]\n\n" +
f"{t('💡 Note:')}\n{extra_note}\n\n" +
f"{t('🔄 After installing FFmpeg, please run this installer again:')}\n[bold cyan]python install.py[/bold cyan]",
style="red"
))
raise SystemExit(t("FFmpeg is required. Please install it and run the installer again."))
def main():
"""
主安装函数,执行以下步骤:
1. 安装基本依赖包
2. 设置用户界面语言
3. 配置PyPI镜像(可选)
4. 检测系统和GPU,安装适当版本的PyTorch
5. 安装项目依赖
6. 检查FFmpeg
7. 启动应用程序
"""
# 安装基础依赖包
install_package("requests", "rich", "ruamel.yaml", "InquirerPy")
from rich.console import Console
from rich.panel import Panel
from rich.box import DOUBLE
from InquirerPy import inquirer
from translations.translations import translate as t
from translations.translations import DISPLAY_LANGUAGES
from core.utils.config_utils import load_key, update_key
from core.utils.decorator import except_handler
console = Console()
# 显示欢迎面板
width = max(len(line) for line in ascii_logo.splitlines()) + 4
welcome_panel = Panel(
ascii_logo,
width=width,
box=DOUBLE,
title="[bold green]🌏[/bold green]",
border_style="bright_blue"
)
console.print(welcome_panel)
# 语言选择
current_language = load_key("display_language")
# 查找当前语言代码的显示名称
current_display = next((k for k, v in DISPLAY_LANGUAGES.items() if v == current_language), "🇬🇧 English")
selected_language = DISPLAY_LANGUAGES[inquirer.select(
message="Select language / 选择语言 / 選擇語言 / 言語を選択 / Seleccionar idioma / Sélectionner la langue / Выберите язык:",
choices=list(DISPLAY_LANGUAGES.keys()),
default=current_display
).execute()]
update_key("display_language", selected_language)
console.print(Panel.fit(t("🚀 Starting Installation"), style="bold magenta"))
# 配置镜像源
# 询问用户是否需要配置PyPI镜像
if inquirer.confirm(
message=t("Do you need to auto-configure PyPI mirrors? (Recommended if you have difficulty accessing pypi.org)"),
default=True
).execute():
from core.utils.pypi_autochoose import main as choose_mirror
choose_mirror()
# 检测系统和GPU
has_gpu = platform.system() != 'Darwin' and check_nvidia_gpu()
if has_gpu:
# 如果检测到NVIDIA GPU,安装CUDA版本的PyTorch
console.print(Panel(t("🎮 NVIDIA GPU detected, installing CUDA version of 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"])
else:
# 否则安装CPU版本的PyTorch
system_name = "🍎 MacOS" if platform.system() == 'Darwin' else "💻 No NVIDIA GPU"
console.print(Panel(t(f"{system_name} detected, installing CPU version of PyTorch... Note: it might be slow during whisperX transcription."), style="cyan"))
subprocess.check_call([sys.executable, "-m", "pip", "install", "torch==2.1.2", "torchaudio==2.1.2"])
@except_handler("Failed to install project")
def install_requirements():
"""安装项目依赖"""
console.print(Panel(t("Installing project in editable mode using `pip install -e .`"), style="cyan"))
subprocess.check_call([sys.executable, "-m", "pip", "install", "-e", "."], env={**os.environ, "PIP_NO_CACHE_DIR": "0", "PYTHONIOENCODING": "utf-8"})
@except_handler("Failed to install Noto fonts")
def install_noto_font():
"""在Linux系统上安装Noto字体"""
# 检测Linux发行版类型
if os.path.exists('/etc/debian_version'):
# Debian/Ubuntu系统
cmd = ['sudo', 'apt-get', 'install', '-y', 'fonts-noto']
pkg_manager = "apt-get"
elif os.path.exists('/etc/redhat-release'):
# RHEL/CentOS/Fedora系统
cmd = ['sudo', 'yum', 'install', '-y', 'google-noto*']
pkg_manager = "yum"
else:
console.print("Warning: Unrecognized Linux distribution, please install Noto fonts manually", style="yellow")
return
subprocess.run(cmd, check=True)
console.print(f"✅ Successfully installed Noto fonts using {pkg_manager}", style="green")
# 如果是Linux系统,安装Noto字体
if platform.system() == 'Linux':
install_noto_font()
# 安装项目依赖并检查FFmpeg
install_requirements()
check_ffmpeg()
# 显示安装完成和启动命令的面板
panel1_text = (
t("Installation completed") + "\n\n" +
t("Now I will run this command to start the application:") + "\n" +
"[bold]streamlit run st.py[/bold]\n" +
t("Note: First startup may take up to 1 minute")
)
console.print(Panel(panel1_text, style="bold green"))
# 显示故障排除提示的面板
panel2_text = (
t("If the application fails to start:") + "\n" +
"1. " + t("Check your network connection") + "\n" +
"2. " + t("Re-run the installer: [bold]python install.py[/bold]")
)
console.print(Panel(panel2_text, style="yellow"))
# 启动应用程序
subprocess.Popen(["streamlit", "run", "st.py"])
if __name__ == "__main__":
main()