34 lines
2.0 KiB
PowerShell
34 lines
2.0 KiB
PowerShell
param([switch]$Sam2, [switch]$Cpu)
|
|
$ErrorActionPreference = 'Stop'
|
|
$projectRoot = Split-Path $PSScriptRoot -Parent
|
|
$env:PIP_CACHE_DIR = Join-Path $projectRoot '.cache/pip'
|
|
$runtimeRoot = Join-Path $projectRoot 'python'
|
|
$pythonPath = Join-Path $runtimeRoot 'python.exe'
|
|
function Invoke-Checked([string]$Program, [string[]]$Arguments) {
|
|
& $Program @Arguments
|
|
if ($LASTEXITCODE -ne 0) { throw "执行失败: $Program $($Arguments -join ' ')" }
|
|
}
|
|
if (!(Test-Path -LiteralPath $pythonPath)) {
|
|
# Copy a complete runtime, independent of the system Python and venv home.
|
|
$baseRoot = & python -c "import sys; print(sys.base_prefix)"
|
|
if ($LASTEXITCODE -ne 0) { throw 'Cannot locate bootstrap Python' }
|
|
New-Item -ItemType Directory -Force -Path $runtimeRoot | Out-Null
|
|
& robocopy $baseRoot $runtimeRoot /E /XD 'site-packages' '__pycache__' /XF '*.pyc' '*.lnk' /NFL /NDL /NJH /NJS
|
|
if ($LASTEXITCODE -ge 8) { throw 'Failed to copy Python runtime' }
|
|
Invoke-Checked $pythonPath @('-m', 'ensurepip', '--upgrade')
|
|
}
|
|
Invoke-Checked $pythonPath @('-m', 'pip', 'install', '--upgrade', 'pip')
|
|
if ($Cpu) {
|
|
Invoke-Checked $pythonPath @('-m', 'pip', 'install', 'torch', 'torchvision', '--index-url', 'https://download.pytorch.org/whl/cpu')
|
|
} else {
|
|
Invoke-Checked $pythonPath @('-m', 'pip', 'install', 'torch', 'torchvision', '--index-url', 'https://download.pytorch.org/whl/cu128')
|
|
}
|
|
Invoke-Checked $pythonPath @('-m', 'pip', 'install', '-r', (Join-Path $PSScriptRoot 'requirements.txt'))
|
|
if ($Sam2) {
|
|
# SAM2's optional CUDA extension is unnecessary for basic box propagation on Windows.
|
|
$env:SAM2_BUILD_CUDA = '0'
|
|
Invoke-Checked $pythonPath @('-m', 'pip', 'install', 'wheel', 'huggingface_hub')
|
|
Invoke-Checked $pythonPath @('-m', 'pip', 'install', '--no-build-isolation', 'git+https://github.com/facebookresearch/sam2.git@2b90b9f5ceec907a1c18123530e92e794ad901a4')
|
|
}
|
|
Write-Host "完成。内置环境: $pythonPath;请重新构建/发布程序以携带 python 子目录。"
|