-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathinstall.py
More file actions
772 lines (693 loc) · 29.2 KB
/
Copy pathinstall.py
File metadata and controls
772 lines (693 loc) · 29.2 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
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
# -*- coding: utf-8 -*-
"""
RVC 安装脚本
自动创建虚拟环境 → 安装依赖 → 启动应用
用法:
python install.py # 完整安装并启动
python install.py --check # 仅检查依赖
python install.py --no-run # 安装但不启动
python install.py --cpu # 安装 CPU 版本
python install.py --backend mps # Apple MPS
python install.py --backend directml # Windows DirectML
"""
import subprocess
import sys
import os
import platform
import json
from pathlib import Path
from lib.console_encoding import configure_console_encoding
from lib.console_i18n import console_print as print
configure_console_encoding()
ROOT_DIR = Path(__file__).parent
VENV_DIR = ROOT_DIR / "venv310"
PYTHON310_CANDIDATES = [
r"C:\Users\Administrator\AppData\Local\Programs\Python\Python310\python.exe",
r"C:\Python310\python.exe",
r"C:\Program Files\Python310\python.exe",
r"C:\Program Files (x86)\Python310\python.exe",
]
SUPPORTED_BACKENDS = ("auto", "cpu", "cuda", "rocm", "xpu", "directml", "mps")
BACKEND_SETTINGS = {
"cpu": {
"audio_extra": "cpu",
"runtime_dist": "onnxruntime",
"conflicting_runtime_dists": ("onnxruntime-gpu", "onnxruntime-directml"),
"runtime_device": "cpu",
"torch_install": "cpu",
},
"cuda": {
"audio_extra": "gpu",
"runtime_dist": "onnxruntime-gpu",
"conflicting_runtime_dists": ("onnxruntime", "onnxruntime-directml"),
"runtime_device": "cuda",
"torch_install": "cuda",
},
"rocm": {
"audio_extra": "cpu",
"runtime_dist": "onnxruntime",
"conflicting_runtime_dists": ("onnxruntime-gpu", "onnxruntime-directml"),
"runtime_device": "cuda",
"torch_install": "preinstalled",
},
"xpu": {
"audio_extra": "cpu",
"runtime_dist": "onnxruntime",
"conflicting_runtime_dists": ("onnxruntime-gpu", "onnxruntime-directml"),
"runtime_device": "xpu",
"torch_install": "preinstalled",
},
"directml": {
"audio_extra": "dml",
"runtime_dist": "onnxruntime-directml",
"conflicting_runtime_dists": ("onnxruntime", "onnxruntime-gpu"),
"runtime_device": "directml",
"torch_install": "preinstalled",
},
"mps": {
"audio_extra": "cpu",
"runtime_dist": "onnxruntime",
"conflicting_runtime_dists": ("onnxruntime-gpu", "onnxruntime-directml"),
"runtime_device": "mps",
"torch_install": "pypi",
},
}
TORCH_MIN_VERSION = (
"2.13.0"
if sys.platform == "darwin" and platform.machine().lower() in {"arm64", "aarch64"}
else "2.3.0"
)
TORCH_REQUIREMENT = f"torch>={TORCH_MIN_VERSION},<3"
TORCH_STACK_NAMES = ("torch", "torchvision", "torchaudio")
# TorchAudio 2.11 uses the stable ABI and supports PyTorch 2.11 and newer.
# https://docs.pytorch.org/audio/main/installation.html
TORCH_INSTALL_PACKAGES = (
("torch==2.13.0", "torchvision==0.28.0", "torchaudio==2.11.0")
if TORCH_MIN_VERSION == "2.13.0"
else ("torch==2.11.0", "torchvision==0.26.0", "torchaudio==2.11.0")
)
PACKAGES = {
"transformers": {
"import": "transformers", "name": "Transformers HuBERT",
"pip": "transformers==4.49.0", "dist": "transformers",
"required_version": "4.49.0",
},
"torch": {"import": "torch", "name": "PyTorch", "pip": "torch", "dist": "torch",
"min_version": TORCH_MIN_VERSION, "max_exclusive_version": "3.0.0"},
"torchvision": {"import": "torchvision", "name": "torchvision", "pip": "torchvision"},
"torchaudio": {
"import": "torchaudio", "name": "torchaudio", "pip": "torchaudio", "dist": "torchaudio",
"min_version": "2.11.0" if TORCH_MIN_VERSION == "2.13.0" else "2.0.0",
},
"gradio": {
"import": "gradio",
"name": "Gradio",
"pip": "gradio==5.49.1",
"dist": "gradio",
"required_version": "5.49.1",
},
"librosa": {"import": "librosa", "name": "librosa", "pip": "librosa"},
"soundfile": {"import": "soundfile", "name": "soundfile", "pip": "soundfile"},
"av": {"import": "av", "name": "PyAV", "pip": "av"},
"scipy": {"import": "scipy", "name": "scipy", "pip": "scipy"},
"numpy": {
"import": "numpy",
"name": "numpy",
"pip": "numpy>=2,<3",
"dist": "numpy",
"min_version": "2.0.0",
"max_exclusive_version": "3.0.0",
},
"yaml": {"import": "yaml", "name": "PyYAML", "pip": "PyYAML"},
"einops": {"import": "einops", "name": "einops", "pip": "einops"},
"parselmouth": {"import": "parselmouth", "name": "praat-parselmouth", "pip": "praat-parselmouth"},
"pyworld": {"import": "pyworld", "name": "pyworld", "pip": "pyworld"},
"torchcrepe": {"import": "torchcrepe", "name": "torchcrepe", "pip": "torchcrepe"},
"torchfcpe": {"import": "torchfcpe", "name": "torchfcpe", "pip": "torchfcpe==0.0.4",
"dist": "torchfcpe", "min_version": "0.0.4", "max_exclusive_version": "0.0.5"},
"faiss": {"import": "faiss", "name": "faiss-cpu", "pip": "faiss-cpu"},
"tqdm": {"import": "tqdm", "name": "tqdm", "pip": "tqdm"},
"requests": {"import": "requests", "name": "requests", "pip": "requests"},
"dotenv": {"import": "dotenv", "name": "python-dotenv", "pip": "python-dotenv"},
"colorama": {"import": "colorama", "name": "colorama", "pip": "colorama"},
"mcp": {"import": "mcp", "name": "mcp", "pip": "mcp"},
"jsonschema": {"import": "jsonschema", "name": "jsonschema", "pip": "jsonschema>=4,<5",
"dist": "jsonschema", "min_version": "4.0.0", "max_exclusive_version": "5.0.0"},
"gdown": {"import": "gdown", "name": "gdown", "pip": "gdown==6.0.0",
"dist": "gdown", "min_version": "6.0.0", "max_exclusive_version": "6.0.1"},
"demucs": {"import": "demucs", "name": "demucs", "pip": "demucs"},
"audio_separator": {
"import": "audio_separator",
"name": "audio-separator",
"pip": "audio-separator",
"dist": "audio-separator",
"required_version": "0.47.0",
},
"huggingface_hub": {
"import": "huggingface_hub",
"name": "huggingface_hub",
"pip": "huggingface_hub>=0.19.0,<1.0",
"dist": "huggingface_hub",
"min_version": "0.19.0",
"max_exclusive_version": "1.0",
},
"pedalboard": {"import": "pedalboard", "name": "pedalboard", "pip": "pedalboard"},
"ffmpeg": {"import": "ffmpeg", "name": "ffmpeg-python", "pip": "ffmpeg-python"},
"fairseq": {
"import": "fairseq",
"name": "fairseq",
"pip": "fairseq==0.12.2",
"dist": "fairseq",
"required_version": "0.12.2",
},
}
# === 虚拟环境 ===
def find_python310():
"""查找系统中的 Python 3.10"""
if sys.version_info[:2] == (3, 10):
return sys.executable
for p in PYTHON310_CANDIDATES:
if os.path.isfile(p):
return p
try:
r = subprocess.run(
["py", "-3.10", "-c", "import sys; print(sys.executable)"],
capture_output=True, text=True, timeout=10,
)
if r.returncode == 0:
return r.stdout.strip()
except (FileNotFoundError, subprocess.TimeoutExpired):
pass
return None
def get_venv_python():
"""获取虚拟环境的 Python 路径"""
if os.name == "nt":
return str(VENV_DIR / "Scripts" / "python.exe")
return str(VENV_DIR / "bin" / "python")
def create_venv():
"""创建 Python 3.10 虚拟环境"""
venv_py = get_venv_python()
if os.path.isfile(venv_py):
r = subprocess.run([venv_py, "--version"], capture_output=True, text=True)
if r.returncode == 0 and "3.10" in r.stdout:
print(f" [OK] 虚拟环境已存在: {VENV_DIR}")
return prepare_install_tools(venv_py)
py310 = find_python310()
if not py310:
print(" [错误] 未找到 Python 3.10")
print(" 下载: https://www.python.org/downloads/release/python-31011/")
return False
print(f" 使用 Python: {py310}")
print(f" 创建虚拟环境: {VENV_DIR}")
r = subprocess.run([py310, "-m", "venv", str(VENV_DIR)], capture_output=True, text=True)
if r.returncode != 0:
print(f" [错误] 创建失败:\n{r.stderr}")
return False
print(" [OK] 虚拟环境创建成功")
return prepare_install_tools(venv_py)
def prepare_install_tools(venv_py):
"""Install the same fairseq-compatible build tools on every entry point."""
result = subprocess.run(
[venv_py, "-m", "pip", "install", "-r", str(ROOT_DIR / "pre-requirements.txt")],
capture_output=True,
text=True,
)
if result.returncode != 0:
print(" [失败] 安装工具准备失败")
print((result.stderr or result.stdout).strip())
return False
return True
# === 依赖检查与安装 ===
def check_package(venv_py, import_name):
"""用虚拟环境的 Python 检查包是否已安装"""
r = subprocess.run(
[venv_py, "-c", f"import {import_name}"],
capture_output=True, text=True,
)
return r.returncode == 0
def get_installed_version(venv_py, distribution_name):
"""返回虚拟环境中已安装发行包版本;无法读取时返回 None。"""
code = (
"from importlib.metadata import PackageNotFoundError, version\n"
f"dist = {distribution_name!r}\n"
"try:\n"
" print(version(dist))\n"
"except PackageNotFoundError:\n"
" raise SystemExit(1)\n"
)
r = subprocess.run([venv_py, "-c", code], capture_output=True, text=True)
if r.returncode != 0:
return None
return r.stdout.strip() or None
def _version_parts(version_text):
parts = []
for part in str(version_text or "").split("."):
digits = ""
for char in part:
if not char.isdigit():
break
digits += char
parts.append(int(digits or 0))
return tuple(parts)
def _version_at_least(installed, required):
installed_parts = _version_parts(installed)
required_parts = _version_parts(required)
width = max(len(installed_parts), len(required_parts))
installed_parts += (0,) * (width - len(installed_parts))
required_parts += (0,) * (width - len(required_parts))
return installed_parts >= required_parts
def _version_less_than(installed, upper_bound):
installed_parts = _version_parts(installed)
upper_parts = _version_parts(upper_bound)
width = max(len(installed_parts), len(upper_parts))
installed_parts += (0,) * (width - len(installed_parts))
upper_parts += (0,) * (width - len(upper_parts))
return installed_parts < upper_parts
def _version_matches(installed, required):
return str(installed or "").strip() == str(required or "").strip()
def _version_requirement_text(info):
required_version = info.get("required_version")
if required_version:
return f"== {required_version}"
requirements = []
if info.get("min_version"):
requirements.append(f">= {info['min_version']}")
if info.get("max_exclusive_version"):
requirements.append(f"< {info['max_exclusive_version']}")
return " 且 ".join(requirements)
def detect_cuda_version():
"""检测系统 CUDA 版本,返回对应的 PyTorch index-url"""
try:
r = subprocess.run(
["nvidia-smi", "--query-gpu=driver_version", "--format=csv,noheader"],
capture_output=True, text=True, timeout=10,
)
if r.returncode == 0:
# nvidia-smi 存在,尝试获取 CUDA 版本
r2 = subprocess.run(
["nvidia-smi"],
capture_output=True, text=True, timeout=10,
)
output = r2.stdout
# 从 nvidia-smi 输出中提取 CUDA Version
import re
match = re.search(r"CUDA Version:\s*(\d+)\.(\d+)", output)
if match:
major, minor = int(match.group(1)), int(match.group(2))
if (major, minor) < (12, 6):
print(" [错误] PyTorch 2.11 安装要求支持 CUDA 12.6 或更新版本的驱动。")
return None
devices = subprocess.run(
["nvidia-smi", "--query-gpu=compute_cap", "--format=csv,noheader"],
capture_output=True, text=True, timeout=10,
)
capabilities = devices.stdout.strip().splitlines()
if devices.returncode != 0 or not capabilities or any(
not re.fullmatch(r"\d+\.\d+", value.strip()) for value in capabilities
):
print(" [错误] 无法读取 NVIDIA 计算能力,无法选择兼容的 CUDA 运行时。")
return None
capabilities = [tuple(map(int, value.strip().split("."))) for value in capabilities]
if any(value >= (10, 0) for value in capabilities):
if (major, minor) < (12, 8) or any(value < (7, 5) for value in capabilities):
print(" [错误] 当前驱动或混合 GPU 架构不满足 Blackwell 的 CUDA 12.8 运行时要求。")
return None
return "https://download.pytorch.org/whl/cu128"
# CUDA 12.6 retains Maxwell/Pascal/Volta kernels in PyTorch 2.11.
return "https://download.pytorch.org/whl/cu126"
except (FileNotFoundError, subprocess.TimeoutExpired):
pass
return None
def resolve_install_backend(requested: str, venv_py: str = None) -> str:
"""Resolve an explicit installer backend; only ``auto`` performs detection."""
backend = str(requested or "auto").strip().lower()
if backend not in SUPPORTED_BACKENDS:
raise ValueError(f"不支持的安装后端: {requested!r}")
if backend != "auto":
return backend
if venv_py and os.path.isfile(venv_py):
probe = (
"import torch\n"
"backend = 'cpu'\n"
"if getattr(torch.version, 'hip', None) and torch.cuda.is_available(): backend = 'rocm'\n"
"elif hasattr(torch, 'xpu') and torch.xpu.is_available(): backend = 'xpu'\n"
"elif torch.cuda.is_available(): backend = 'cuda'\n"
"elif hasattr(torch.backends, 'mps') and torch.backends.mps.is_available(): backend = 'mps'\n"
"else:\n"
" try:\n"
" import torch_directml\n"
" torch.zeros(1).to(torch_directml.device(torch_directml.default_device()))\n"
" backend = 'directml'\n"
" except Exception:\n"
" pass\n"
"print(backend)\n"
)
result = subprocess.run(
[venv_py, "-c", probe],
capture_output=True,
text=True,
)
detected = result.stdout.strip().lower() if result.returncode == 0 else ""
if detected in BACKEND_SETTINGS and detected != "cpu":
return detected
if sys.platform == "darwin":
machine = platform.machine().lower()
return "mps" if machine in {"arm64", "aarch64"} else "cpu"
if detect_cuda_version():
return "cuda"
return "cpu"
def check_backend_available(venv_py: str, backend: str) -> bool:
"""Verify that the selected accelerator stack is importable and usable."""
checks = {
"cpu": "import torch; torch.zeros(1, device='cpu')",
"cuda": (
"import torch; "
"assert torch.version.hip is None, 'ROCm PyTorch cannot satisfy CUDA mode'; "
"assert torch.cuda.is_available(), 'CUDA is unavailable'; "
"torch.zeros(1, device='cuda')"
),
"rocm": (
"import torch; "
"assert torch.version.hip is not None, 'PyTorch is not a ROCm build'; "
"assert torch.cuda.is_available(), 'ROCm device is unavailable'; "
"torch.zeros(1, device='cuda')"
),
"xpu": (
"import torch; "
"assert hasattr(torch, 'xpu') and torch.xpu.is_available(), 'XPU is unavailable'; "
"torch.zeros(1, device='xpu')"
),
"directml": (
"import torch, torch_directml; "
"device = torch_directml.device(torch_directml.default_device()); "
"torch.zeros(1).to(device)"
),
"mps": (
"import torch; "
"assert hasattr(torch.backends, 'mps') and torch.backends.mps.is_available(), "
"'MPS is unavailable'; "
"torch.zeros(1, device='mps')"
),
}
result = subprocess.run(
[venv_py, "-c", checks[backend]],
capture_output=True,
text=True,
)
if result.returncode == 0:
print(f" [v] 计算后端可用: {backend}")
return True
details = (result.stderr or result.stdout).strip().splitlines()
print(f" [x] 计算后端不可用: {backend}")
if details:
print(f" {details[-1]}")
return False
def pip_install(venv_py, package, extra="", index_url=None, no_deps=False, version_spec="", pinned_packages=()):
"""用虚拟环境的 pip 安装包"""
target = f"{package}[{extra}]{version_spec}" if extra else f"{package}{version_spec}"
print(f" 安装 {target} ...")
cmd = [venv_py, "-m", "pip", "install", target, *pinned_packages]
if index_url:
cmd.extend(["--index-url", index_url])
if no_deps:
cmd.append("--no-deps")
r = subprocess.run(cmd, capture_output=True, text=True)
if r.returncode != 0:
print(f" [失败] {target}")
lines = r.stderr.strip().splitlines()
if lines:
print(f" {lines[-1]}")
return False
print(f" [完成] {target}")
return True
def pip_install_packages(venv_py, packages, index_url=None):
"""Install a compatibility-coupled package set in one resolver transaction."""
targets = [str(package) for package in packages]
print(f" 安装 {' '.join(targets)} ...")
cmd = [venv_py, "-m", "pip", "install", *targets]
if index_url:
cmd.extend(["--index-url", index_url])
result = subprocess.run(cmd, capture_output=True, text=True)
if result.returncode != 0:
print(f" [失败] {' '.join(targets)}")
lines = result.stderr.strip().splitlines()
if lines:
print(f" {lines[-1]}")
return False
print(f" [完成] {' '.join(targets)}")
return True
def check_dependency_consistency(venv_py):
"""Run pip's installed-distribution consistency check without hiding failures."""
result = subprocess.run(
[venv_py, "-m", "pip", "check"],
capture_output=True,
text=True,
)
if result.returncode == 0:
print(" [v] pip 依赖一致性检查通过")
return True
print(" [x] pip 依赖一致性检查失败:")
details = (result.stdout or result.stderr).strip()
if details:
for line in details.splitlines():
print(f" {line}")
return False
def install_project_dependencies(venv_py, backend, pinned_packages):
"""Resolve the whole application together, including already installed pins."""
settings = BACKEND_SETTINGS[backend]
separator_version = PACKAGES["audio_separator"]["required_version"]
return pip_install_packages(venv_py, (
"-r", str(ROOT_DIR / "requirements.txt"),
f"audio-separator[{settings['audio_extra']}]=={separator_version}",
*pinned_packages,
))
def check_all(venv_py):
"""检查所有依赖"""
print("=" * 50)
print("RVC 依赖检查")
print("=" * 50)
missing = []
for key, info in PACKAGES.items():
ok = check_package(venv_py, info["import"])
status = "OK" if ok else "未安装"
required_version = info.get("required_version")
min_version = info.get("min_version")
max_exclusive_version = info.get("max_exclusive_version")
if ok and (required_version or min_version or max_exclusive_version):
installed_version = get_installed_version(
venv_py,
info.get("dist", info["pip"]),
)
if not installed_version:
ok = False
status = f"需更新 (无法读取版本,要求 {_version_requirement_text(info)})"
elif required_version:
if _version_matches(installed_version, required_version):
status = f"OK ({installed_version})"
else:
ok = False
status = (
f"需更新 ({installed_version} 不等于 "
f"{required_version})"
)
elif (
(not min_version or _version_at_least(installed_version, min_version))
and (
not max_exclusive_version
or _version_less_than(installed_version, max_exclusive_version)
)
):
status = f"OK ({installed_version})"
else:
ok = False
status = (
f"需更新 ({installed_version} 不满足 "
f"{_version_requirement_text(info)})"
)
mark = "[v]" if ok else "[x]"
print(f" {mark} {info['name']:30s} {status}")
if not ok:
missing.append(info)
print("-" * 50)
if missing:
print(f"缺少 {len(missing)} 个依赖包")
else:
print("所有依赖已安装")
return missing
def install_all(venv_py, gpu=True, backend=None):
"""安装所有缺失依赖,并保持所选计算后端的运行时组合。"""
backend = str(backend or ("cuda" if gpu else "cpu")).strip().lower()
if backend not in BACKEND_SETTINGS:
raise ValueError(f"不支持的安装后端: {backend!r}")
settings = BACKEND_SETTINGS[backend]
missing = check_all(venv_py)
torch_index_url = None
if settings["torch_install"] == "cuda":
cuda_index_url = detect_cuda_version()
if cuda_index_url:
print(f"\n 检测到 CUDA,使用 PyTorch 源: {cuda_index_url}")
torch_index_url = cuda_index_url
else:
print("\n [错误] 未检测到支持的 CUDA,GPU 安装停止。")
print(" 如需 CPU 版 PyTorch,请显式使用 --cpu。")
return False
elif settings["torch_install"] == "cpu":
torch_index_url = "https://download.pytorch.org/whl/cpu"
runtime_dist = settings["runtime_dist"]
for conflicting_runtime_dist in settings["conflicting_runtime_dists"]:
if get_installed_version(venv_py, conflicting_runtime_dist):
print(
f"\n [错误] 检测到冲突的 {conflicting_runtime_dist}。"
f"当前安装模式只允许 {runtime_dist},请删除 venv310 后重新安装。"
)
return False
if not get_installed_version(venv_py, runtime_dist):
audio_separator_info = PACKAGES["audio_separator"]
if audio_separator_info not in missing:
missing.append(audio_separator_info)
if not missing:
if not check_backend_available(venv_py, backend):
return False
if check_dependency_consistency(venv_py):
print("\n无需安装,所有依赖已就绪。")
return True
print("\n开始解析并安装完整项目依赖...\n")
failed = []
torch_stack_missing = [info for info in missing if info["pip"] in TORCH_STACK_NAMES]
if torch_stack_missing:
if settings["torch_install"] == "preinstalled":
print(
f" [错误] {backend} 需要先安装并验证对应的 PyTorch 运行栈;"
"安装器不会用其他 PyTorch 版本覆盖它"
)
return False
elif not pip_install_packages(
venv_py,
TORCH_INSTALL_PACKAGES,
index_url=torch_index_url,
):
return False
missing = [info for info in missing if info["pip"] not in TORCH_STACK_NAMES]
# Keep the selected CPU/CUDA/ROCm/XPU/DirectML build in every later resolver
# transaction, including its local version suffix. Conflicts must fail before
# pip can replace one part of the already coupled runtime stack.
pinned_packages = []
for package in TORCH_STACK_NAMES:
version = get_installed_version(venv_py, package)
if not version:
print(f" [错误] 无法读取 {package} 版本,依赖安装已停止。")
return False
pinned_packages.append(f"{package}=={version}")
pinned_packages = tuple(pinned_packages)
if not install_project_dependencies(venv_py, backend, pinned_packages):
return False
if not get_installed_version(venv_py, runtime_dist):
failed.append(runtime_dist)
if not check_backend_available(venv_py, backend):
failed.append(f"{backend} backend")
if not check_dependency_consistency(venv_py):
failed.append("pip dependency consistency")
print("\n" + "=" * 50)
if failed:
print(f"安装完成,{len(failed)} 个包失败: {', '.join(failed)}")
return False
print("所有依赖安装成功!")
return True
def save_runtime_device(device: str) -> None:
"""把显式安装模式写入运行配置,避免设备被隐式改写。"""
if device not in {"cpu", "cuda", "xpu", "directml", "mps"}:
raise ValueError(f"安装脚本不支持写入设备: {device!r}")
config_path = ROOT_DIR / "configs" / "config.json"
with open(config_path, "r", encoding="utf-8") as handle:
config = json.load(handle)
config["device"] = device
with open(config_path, "w", encoding="utf-8", newline="\n") as handle:
json.dump(config, handle, ensure_ascii=False, indent=4)
handle.write("\n")
print(f"已写入运行设备: {device} ({config_path})")
def check_onnx_runtime_variant(venv_py: str, gpu: bool = None, backend: str = None) -> bool:
"""检查当前安装模式只存在对应的 ONNX Runtime 发行包。"""
backend = str(backend or ("cuda" if gpu else "cpu")).strip().lower()
if backend not in BACKEND_SETTINGS:
raise ValueError(f"不支持的安装后端: {backend!r}")
settings = BACKEND_SETTINGS[backend]
required_dist = settings["runtime_dist"]
for conflicting_dist in settings["conflicting_runtime_dists"]:
conflicting_version = get_installed_version(venv_py, conflicting_dist)
if conflicting_version:
print(
f" [x] ONNX Runtime 检测到冲突: {conflicting_dist} "
f"{conflicting_version};当前模式只允许 {required_dist}"
)
return False
required_version = get_installed_version(venv_py, required_dist)
if not required_version:
print(f" [x] ONNX Runtime 缺少 {required_dist}")
return False
print(f" [v] ONNX Runtime {required_dist} {required_version}")
return True
def launch_app(venv_py):
"""用虚拟环境启动应用"""
run_script = str(ROOT_DIR / "run.py")
print(f"\n启动应用: {run_script}")
print("=" * 50)
try:
result = subprocess.run([venv_py, run_script], cwd=str(ROOT_DIR))
if result.returncode:
raise SystemExit(result.returncode)
except KeyboardInterrupt:
print("\n已停止")
# === 主入口 ===
def main():
import argparse
parser = argparse.ArgumentParser(description="RVC 安装脚本")
parser.add_argument("--cpu", action="store_true", help="安装 CPU 版本(--backend cpu 的兼容别名)")
parser.add_argument(
"--backend",
choices=SUPPORTED_BACKENDS,
default=None,
help="安装后端: auto/cpu/cuda/rocm/xpu/directml/mps",
)
parser.add_argument("--check", action="store_true", help="仅检查依赖")
parser.add_argument("--no-run", action="store_true", help="安装后不启动")
args = parser.parse_args()
if args.cpu and args.backend not in {None, "cpu"}:
parser.error("--cpu 不能与非 CPU 的 --backend 同时使用")
requested_backend = "cpu" if args.cpu else (args.backend or "auto")
print("=" * 50)
print("RVC 安装程序")
print("=" * 50)
# 1. 创建虚拟环境
print("\n[1/3] 检查虚拟环境")
if args.check and not Path(get_venv_python()).is_file():
print("Virtual environment is missing; run install.py to create it.")
sys.exit(1)
if not args.check and not create_venv():
sys.exit(1)
venv_py = get_venv_python()
backend = resolve_install_backend(requested_backend, venv_py=venv_py)
settings = BACKEND_SETTINGS[backend]
print(f"安装后端: {backend}")
# 2. 安装依赖
print(f"\n[2/3] 检查依赖")
if args.check:
missing = check_all(venv_py)
runtime_ok = check_onnx_runtime_variant(venv_py, backend=backend)
backend_ok = check_backend_available(venv_py, backend)
dependencies_ok = check_dependency_consistency(venv_py)
if missing or not runtime_ok or not backend_ok or not dependencies_ok:
sys.exit(1)
return
if not install_all(venv_py, backend=backend):
print("\n部分依赖安装失败,可尝试手动安装。")
sys.exit(1)
save_runtime_device(settings["runtime_device"])
# 3. 启动应用
if args.no_run:
print("\n安装完成。运行方式:")
print(f" {venv_py} run.py")
return
print(f"\n[3/3] 启动应用")
launch_app(venv_py)
if __name__ == "__main__":
main()