install_env.bat 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. @echo off
  2. chcp 65001
  3. :: 设置是否使用镜像站的标志,true 表示使用,false 表示不使用
  4. set USE_MIRROR=true
  5. echo use_mirror = %USE_MIRROR%
  6. set no_proxy="127.0.0.1, 0.0.0.0, localhost"
  7. setlocal enabledelayedexpansion
  8. cd /D "%~dp0"
  9. set PATH="%PATH%";%SystemRoot%\system32
  10. :: 安装Miniconda
  11. :: 检查是否有特殊字符
  12. echo "%CD%"| findstr /R /C:"[!#\$%&()\*+,;<=>?@\[\]\^`{|}~\u4E00-\u9FFF ] " >nul && (
  13. echo.
  14. echo 当前路径中存在特殊字符,请使fish-speech的路径不含特殊字符后再运行。 && (
  15. goto end
  16. )
  17. )
  18. :: 解决跨驱动器安装问题
  19. set TMP=%CD%\fishenv
  20. set TEMP=%CD%\fishenv
  21. :: 取消激活已经激活的环境
  22. (call conda deactivate && call conda deactivate && call conda deactivate) 2>nul
  23. :: 安装路径配置
  24. set INSTALL_DIR=%cd%\fishenv
  25. set CONDA_ROOT_PREFIX=%cd%\fishenv\conda
  26. set INSTALL_ENV_DIR=%cd%\fishenv\env
  27. set PIP_CMD=%cd%\fishenv\env\python -m pip
  28. set PYTHON_CMD=%cd%\fishenv\env\python
  29. set API_FLAG_PATH=%~dp0API_FLAGS.txt
  30. set MINICONDA_DOWNLOAD_URL=https://mirrors.tuna.tsinghua.edu.cn/anaconda/miniconda/Miniconda3-py310_23.3.1-0-Windows-x86_64.exe
  31. set MINICONDA_CHECKSUM=307194e1f12bbeb52b083634e89cc67db4f7980bd542254b43d3309eaf7cb358
  32. set conda_exists=F
  33. :: 确定是否要安装conda
  34. call "%CONDA_ROOT_PREFIX%\_conda.exe" --version >nul 2>&1
  35. if "%ERRORLEVEL%" EQU "0" set conda_exists=T
  36. :: 下载Miniconda
  37. if "%conda_exists%" == "F" (
  38. echo.
  39. echo 正在下载Miniconda...
  40. mkdir "%INSTALL_DIR%" 2>nul
  41. :: 使用curl下载Miniconda安装程序
  42. call curl -Lk "%MINICONDA_DOWNLOAD_URL%" > "%INSTALL_DIR%\miniconda_installer.exe"
  43. :: 检查下载是否成功
  44. if errorlevel 1 (
  45. echo.
  46. echo 下载Miniconda失败
  47. goto end
  48. )
  49. :: 哈希校验
  50. for /f %%a in ('
  51. certutil -hashfile "%INSTALL_DIR%\miniconda_installer.exe" sha256
  52. ^| find /i /v " "
  53. ^| find /i "%MINICONDA_CHECKSUM%"
  54. ') do (
  55. :: 如果哈希值匹配预设的校验和,将其存储在变量中
  56. set "hash=%%a"
  57. )
  58. if not defined hash (
  59. echo.
  60. echo Miniconda安装程序的哈希值不匹配
  61. del "%INSTALL_DIR%\miniconda_installer.exe"
  62. goto end
  63. ) else (
  64. echo.
  65. echo Miniconda安装程序的哈希值成功匹配
  66. )
  67. echo 下载完成,接下来安装Miniconda至"%CONDA_ROOT_PREFIX%"
  68. start /wait "" "%INSTALL_DIR%\miniconda_installer.exe" /InstallationType=JustMe /NoShortcuts=1 /AddToPath=0 /RegisterPython=0 /NoRegistry=1 /S /D=%CONDA_ROOT_PREFIX%
  69. :: 测试是否成功安装
  70. call "%CONDA_ROOT_PREFIX%\_conda.exe" --version
  71. if errorlevel 1 (
  72. echo.
  73. echo 未安装Miniconda
  74. goto end
  75. ) else (
  76. echo.
  77. echo Miniconda安装成功
  78. )
  79. :: 删除安装程序
  80. del "%INSTALL_DIR%\miniconda_installer.exe"
  81. )
  82. :: 创建conda环境
  83. if not exist "%INSTALL_ENV_DIR%" (
  84. echo.
  85. echo 正在创建conda环境...
  86. call "%CONDA_ROOT_PREFIX%\_conda.exe" create --no-shortcuts -y -k --prefix "%INSTALL_ENV_DIR%" -c https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/ python=3.10
  87. :: 检查环境创建是否成功
  88. if errorlevel 1 (
  89. echo.
  90. echo 创建conda环境失败
  91. goto end
  92. )
  93. )
  94. :: 检查是否真的创建了环境
  95. if not exist "%INSTALL_ENV_DIR%\python.exe" (
  96. echo.
  97. echo Conda环境不存在
  98. goto end
  99. )
  100. :: 环境隔离
  101. set PYTHONNOUSERSITE=1
  102. set PYTHONPATH=
  103. set PYTHONHOME=
  104. set "CUDA_PATH=%INSTALL_ENV_DIR%"
  105. set "CUDA_HOME=%CUDA_PATH%"
  106. :: 激活环境
  107. call "%CONDA_ROOT_PREFIX%\condabin\conda.bat" activate "%INSTALL_ENV_DIR%"
  108. :: 检查环境是否成功激活
  109. if errorlevel 1 (
  110. echo.
  111. echo 环境激活失败
  112. goto end
  113. ) else (
  114. echo.
  115. echo 环境激活成功
  116. )
  117. :: 安装依赖
  118. set "packages=torch torchvision torchaudio openai-whisper fish-speech"
  119. :: 检查包是否已安装,如果没有安装则添加到需要安装的包列表
  120. set "install_packages="
  121. for %%p in (%packages%) do (
  122. %PIP_CMD% show %%p >nul 2>&1
  123. if errorlevel 1 (
  124. set "install_packages=!install_packages! %%p"
  125. )
  126. )
  127. if not "!install_packages!"=="" (
  128. echo.
  129. echo 正在安装以下包: !install_packages!
  130. :: 针对不同的包使用不同的安装源
  131. for %%p in (!install_packages!) do (
  132. if "!USE_MIRROR!"=="true" (
  133. if "%%p"=="torch" (
  134. %PIP_CMD% install torch --index-url https://mirror.sjtu.edu.cn/pytorch-wheels/cu121 --no-warn-script-location
  135. ) else if "%%p"=="torchvision" (
  136. %PIP_CMD% install torchvision --index-url https://mirror.sjtu.edu.cn/pytorch-wheels/cu121 --no-warn-script-location
  137. ) else if "%%p"=="torchaudio" (
  138. %PIP_CMD% install torchaudio --index-url https://mirror.sjtu.edu.cn/pytorch-wheels/cu121 --no-warn-script-location
  139. ) else if "%%p"=="openai-whisper" (
  140. %PIP_CMD% install -i https://pypi.tuna.tsinghua.edu.cn/simple openai-whisper --no-warn-script-location
  141. ) else if "%%p"=="fish-speech" (
  142. %PIP_CMD% install -e . -i https://pypi.tuna.tsinghua.edu.cn/simple
  143. )
  144. ) else (
  145. if "%%p"=="torch" (
  146. %PIP_CMD% install torch --index-url https://download.pytorch.org/whl/cu121 --no-warn-script-location
  147. ) else if "%%p"=="torchvision" (
  148. %PIP_CMD% install torchvision --index-url https://download.pytorch.org/whl/cu121 --no-warn-script-location
  149. ) else if "%%p"=="torchaudio" (
  150. %PIP_CMD% install torchaudio --index-url https://download.pytorch.org/whl/cu121 --no-warn-script-location
  151. ) else if "%%p"=="openai-whisper" (
  152. %PIP_CMD% install openai-whisper --no-warn-script-location
  153. ) else if "%%p"=="fish-speech" (
  154. %PIP_CMD% install -e .
  155. )
  156. )
  157. )
  158. )
  159. echo 环境检查并安装完成
  160. endlocal
  161. pause