File size: 3,677 Bytes
f063646
 
 
 
 
 
 
 
 
79f58fb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f063646
 
79f58fb
 
 
 
 
 
 
f063646
 
 
 
 
79f58fb
f063646
 
 
 
 
 
 
 
 
 
79f58fb
 
 
 
f063646
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
79f58fb
 
 
 
f063646
 
 
 
 
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
param(
    [string]$BindHost = "127.0.0.1",
    [int]$Port = 7860
)

$ErrorActionPreference = "Stop"
$Root = Resolve-Path "$PSScriptRoot/.."
Set-Location $Root

# ---------------------------------------------------------------------------
# Prereq check + optional winget install
# ---------------------------------------------------------------------------

function Test-Py311 {
    $py = Get-Command py -ErrorAction SilentlyContinue
    if (-not $py) { return $false }
    $listing = (& py -0 2>&1 | Out-String)
    return ($listing -match "3\.11")
}

function Test-Node {
    return [bool] (Get-Command npm -ErrorAction SilentlyContinue)
}

function Install-WithWinget($name, $id) {
    $w = Get-Command winget -ErrorAction SilentlyContinue
    if (-not $w) {
        Write-Host ""
        Write-Host "$name not found, and 'winget' is not available on this system."
        Write-Host "Install $name manually, then re-run scripts\start.bat."
        if ($name -eq "Python 3.11") {
            Write-Host "  -> https://www.python.org/downloads/"
        } elseif ($name -eq "Node.js LTS") {
            Write-Host "  -> https://nodejs.org/"
        }
        exit 1
    }
    Write-Host ""
    $resp = Read-Host "$name not found. Install now via winget ($id)? [Y/n]"
    if ($resp -and ($resp -ne "y") -and ($resp -ne "Y")) {
        Write-Host "Skipped. Install $name manually and re-run."
        exit 1
    }
    Write-Host "==> Installing $name (this can take a couple of minutes)…"
    & winget install --id $id -e --silent --accept-package-agreements --accept-source-agreements
    if ($LASTEXITCODE -ne 0) {
        Write-Error "winget install failed (exit $LASTEXITCODE)."
        exit 1
    }
    Write-Host ""
    Write-Host "==> $name installed."
    Write-Host "    Close this window and run scripts\start.bat again so the new PATH takes effect."
    exit 0
}

if (-not (Test-Py311)) { Install-WithWinget "Python 3.11" "Python.Python.3.11" }
if (-not (Test-Node))  { Install-WithWinget "Node.js LTS" "OpenJS.NodeJS.LTS" }

# ---------------------------------------------------------------------------
# venv + Python deps
# ---------------------------------------------------------------------------

if (-not (Test-Path ".venv")) {
    Write-Host "==> Creating venv (.venv)"
    & py -3.11 -m venv .venv
}

. .venv/Scripts/Activate.ps1

$reqHash = (Get-FileHash requirements.txt -Algorithm SHA1).Hash
$marker = ".venv/.installed-marker"
if (-not (Test-Path $marker) -or (Get-Content $marker) -ne $reqHash) {
    Write-Host "==> Installing python deps"
    pip install --upgrade pip
    pip install -r requirements.txt
    Set-Content $marker $reqHash
}

# ---------------------------------------------------------------------------
# web deps + build
# ---------------------------------------------------------------------------

if (-not (Test-Path "web/node_modules")) {
    Write-Host "==> Installing web deps"
    Push-Location web
    npm ci
    Pop-Location
}

if (-not (Test-Path "server/static/index.html")) {
    Write-Host "==> Building web"
    Push-Location web
    npm run build
    Pop-Location
    if (Test-Path "server/static") { Remove-Item -Recurse -Force "server/static" }
    New-Item -ItemType Directory -Force "server/static" | Out-Null
    Copy-Item -Recurse "web/dist/*" "server/static/"
}

# ---------------------------------------------------------------------------
# serve
# ---------------------------------------------------------------------------

$env:PYTORCH_ENABLE_MPS_FALLBACK = "1"
$Url = "http://${BindHost}:$Port"
Write-Host "==> Serving on $Url"
Start-Process $Url
uvicorn server.main:app --host $BindHost --port $Port