Recently Written · git

subplz-web

git clone https://github.com/equwal/subplz-web

Log | Files | Refs


run.ps1 (3588 bytes)

1 <#
2     Start SubPlz Web on localhost.
3 
4         .\run.ps1              # start on http://127.0.0.1:8420
5         .\run.ps1 -Port 9000
6         .\run.ps1 -NoBrowser
7 
8     Creates .venv and installs dependencies on first run, including the
9     alignment backend itself.
10 #>
11 [CmdletBinding()]
12 param(
13     [int]$Port = 8420,
14     [string]$BindHost = "127.0.0.1",
15     [switch]$NoBrowser,
16     [switch]$Reload
17 )
18 
19 $ErrorActionPreference = "Stop"
20 
21 $Root   = $PSScriptRoot
22 $Venv   = Join-Path $Root ".venv"
23 $Python = Join-Path $Venv "Scripts\python.exe"
24 
25 # Where to install the alignment backend from. Override to use a fork or a
26 # local checkout, e.g. -e C:\path\to\SubPlz
27 $SubPlzSpec = $env:SUBPLZ_INSTALL_SPEC
28 if (-not $SubPlzSpec) {
29     $SubPlzSpec = "git+https://github.com/kanjieater/SubPlz.git"
30 }
31 
32 function Write-Step($msg) { Write-Host "==> $msg" -ForegroundColor Cyan }
33 function Write-Warn($msg) { Write-Host "!!  $msg" -ForegroundColor Yellow }
34 
35 # --- ffmpeg is not optional ---------------------------------------------------
36 if (-not (Get-Command ffmpeg -ErrorAction SilentlyContinue)) {
37     Write-Warn "ffmpeg is not on PATH. It is needed to read audio and build video."
38     Write-Warn "Install it (winget install Gyan.FFmpeg) and reopen the terminal."
39 }
40 
41 # --- venv ---------------------------------------------------------------------
42 # subplz pins requires-python >=3.10,<3.12, so 3.11 it is.
43 if (-not (Test-Path $Python)) {
44     Write-Step "Creating .venv on Python 3.11"
45     & py -3.11 --version 2>&1 | Out-Null
46     if ($LASTEXITCODE -ne 0) {
47         throw "Python 3.11 not found. Install it, then rerun. (py -0 lists what you have.)"
48     }
49     & py -3.11 -m venv $Venv
50     if ($LASTEXITCODE -ne 0) { throw "venv creation failed" }
51 }
52 
53 # --- dependencies -------------------------------------------------------------
54 & $Python -c "import fastapi, uvicorn, sqlalchemy, multipart, pydantic_settings" 2>$null
55 if ($LASTEXITCODE -ne 0) {
56     Write-Step "Installing web dependencies"
57     & $Python -m pip install --upgrade pip
58     & $Python -m pip install -r (Join-Path $Root "requirements.txt")
59     if ($LASTEXITCODE -ne 0) { throw "dependency install failed" }
60 }
61 
62 & $Python -c "import subplz" 2>$null
63 if ($LASTEXITCODE -ne 0) {
64     Write-Step "Installing the alignment backend ($SubPlzSpec) - pulls torch, several minutes"
65     & $Python -m pip install $SubPlzSpec
66     if ($LASTEXITCODE -ne 0) { throw "alignment backend install failed" }
67 }
68 
69 # --- language registry --------------------------------------------------------
70 if (-not (Test-Path (Join-Path $Root "backend\languages.json"))) {
71     Write-Step "Generating the language registry"
72     Push-Location $Root
73     try { & $Python -m backend.gen_languages } finally { Pop-Location }
74 }
75 
76 # --- run ----------------------------------------------------------------------
77 # The backend prints emoji; without UTF-8 these die on a cp1252/cp932 console.
78 $env:PYTHONIOENCODING = "utf-8"
79 $env:PYTHONUTF8 = "1"
80 
81 # Put the venv's scripts first, so `subplz` resolves on PATH.
82 $env:PATH = (Join-Path $Venv "Scripts") + [IO.Path]::PathSeparator + $env:PATH
83 
84 $url = "http://${BindHost}:${Port}"
85 Write-Step "Starting SubPlz Web on $url"
86 Write-Host "    Ctrl+C to stop." -ForegroundColor DarkGray
87 
88 if (-not $NoBrowser) {
89     Start-Job -ScriptBlock { param($u) Start-Sleep -Seconds 3; Start-Process $u } `
90         -ArgumentList $url | Out-Null
91 }
92 
93 $uvicornArgs = @("-m", "uvicorn", "backend.main:app", "--host", $BindHost, "--port", $Port)
94 if ($Reload) { $uvicornArgs += "--reload" }
95 
96 Push-Location $Root
97 try { & $Python @uvicornArgs } finally { Pop-Location }