Repository navigation
Expand file tree
/
Copy pathrun-doc-reader.ps1
More file actions
169 lines (156 loc) · 7.19 KB
/
Copy pathrun-doc-reader.ps1
File metadata and controls
169 lines (156 loc) · 7.19 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
# Doc Reader launcher for Windows.
#
# Prepares a Python 3.12 virtual environment in .venv (PyTorch with CUDA when an
# NVIDIA GPU is present, plus Kokoro, faster-whisper, PySide6, pynput, sounddevice),
# then hands off to `python -m doc_reader.windows_app <command>`.
#
# .\run-doc-reader.ps1 # start everything and open the web app
# .\run-doc-reader.ps1 status # health of speech service, web app, helper
# .\run-doc-reader.ps1 stop
# .\run-doc-reader.ps1 doctor
# .\run-doc-reader.ps1 enable-startup # launch at login
# .\run-doc-reader.ps1 --prepare-only # just build the environment
# .\run-doc-reader.ps1 cli <file.pdf> [--mode smart] # command-line reader
#
# Environment overrides:
# DOC_READER_VENV_DIR alternate venv location
# DOC_READER_TORCH "cuda" | "cpu" (default: auto-detect NVIDIA)
# DOC_READER_CUDA_INDEX PyTorch wheel index (default cu124)
[CmdletBinding()]
param(
[Parameter(ValueFromRemainingArguments = $true)]
[string[]] $Args
)
$ErrorActionPreference = "Stop"
$ScriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path
$VenvDir = if ($env:DOC_READER_VENV_DIR) { $env:DOC_READER_VENV_DIR } else { Join-Path $ScriptDir ".venv" }
$VenvPython = Join-Path $VenvDir "Scripts\python.exe"
$ReqFile = Join-Path $ScriptDir "requirements-windows.txt"
$BaseReqFile = Join-Path $ScriptDir "requirements.txt"
$StampFile = Join-Path $VenvDir ".requirements-windows.sha256"
$TorchVersion = "2.6.0"
$TorchVisionVersion = "0.21.0"
$TorchAudioVersion = "2.6.0"
$CudaIndex = if ($env:DOC_READER_CUDA_INDEX) { $env:DOC_READER_CUDA_INDEX } else { "cu124" }
function Write-Log([string] $Message) {
Write-Host "[doc-reader] $Message"
}
function Find-Command([string] $Name) {
$cmd = Get-Command $Name -ErrorAction SilentlyContinue
if ($cmd) { return $cmd.Source }
return $null
}
function Test-NvidiaGpu {
if ($env:DOC_READER_TORCH -eq "cpu") { return $false }
if ($env:DOC_READER_TORCH -eq "cuda") { return $true }
if (Find-Command "nvidia-smi") { return $true }
try {
$gpus = Get-CimInstance Win32_VideoController -ErrorAction Stop | Select-Object -ExpandProperty Name
foreach ($gpu in $gpus) { if ($gpu -match "NVIDIA") { return $true } }
} catch {}
return $false
}
function Invoke-Checked([string] $Exe, [string[]] $Arguments) {
& $Exe @Arguments
if ($LASTEXITCODE -ne 0) {
throw "$Exe $($Arguments -join ' ') failed with exit code $LASTEXITCODE"
}
}
$CommandName = if ($Args.Count) { $Args[0] } else { "start" }
$NeedsSetup = $CommandName -notin @("stop", "status", "doctor", "disable-startup", "remove-shortcuts", "web-stop", "tts-stop", "helper-stop")
if (-not $NeedsSetup -and -not (Test-Path $VenvPython)) {
throw "Doc Reader is not installed. Run read-docs install first."
}
# ---------------------------------------------------------------- create venv
$uv = Find-Command "uv"
if (-not (Test-Path $VenvPython)) {
Write-Log "Creating virtual environment at $VenvDir ..."
if ($uv) {
Invoke-Checked $uv @("venv", "--python", "3.12", $VenvDir)
} else {
$py = Find-Command "py"
$python = $null
if ($py) {
foreach ($version in @("3.12", "3.11", "3.10")) {
& $py "-$version" -c "import sys" 2>$null
if ($LASTEXITCODE -eq 0) { $python = @($py, "-$version"); break }
}
}
if (-not $python) {
$candidate = Find-Command "python"
if ($candidate) {
$ver = & $candidate -c "import sys; print('%d.%d' % sys.version_info[:2])"
if ($ver -in @("3.10", "3.11", "3.12")) { $python = @($candidate) }
}
}
if (-not $python) {
throw "This installer uses Python 3.10-3.12 with the pinned PyTorch version. Install uv (winget install astral-sh.uv) or Python 3.12 from python.org and re-run."
}
$pythonPrefix = @($python | Select-Object -Skip 1)
Invoke-Checked $python[0] ($pythonPrefix + @("-m", "venv", $VenvDir))
}
}
function Invoke-Pip([string[]] $PipArgs) {
if ($uv) {
Invoke-Checked $uv (@("pip", "install", "--python", $VenvPython) + $PipArgs)
} else {
Invoke-Checked $VenvPython (@("-m", "pip", "install") + $PipArgs)
}
}
# ---------------------------------------------------------------- install deps
$reqHash = (Get-FileHash -Algorithm SHA256 $ReqFile).Hash + (Get-FileHash -Algorithm SHA256 $BaseReqFile).Hash
$installedHash = if (Test-Path $StampFile) { (Get-Content $StampFile -Raw).Trim() } else { "" }
if ($NeedsSetup -and $reqHash -ne $installedHash) {
if (-not $uv) {
Invoke-Checked $VenvPython @("-m", "pip", "install", "--upgrade", "pip")
}
$hasTorch = $false
& $VenvPython -c "import torch" 2>$null
if ($LASTEXITCODE -eq 0) { $hasTorch = $true }
if (-not $hasTorch) {
if (Test-NvidiaGpu) {
Write-Log "NVIDIA GPU detected: installing PyTorch $TorchVersion ($CudaIndex) ..."
Invoke-Pip @("--index-url", "https://download.pytorch.org/whl/$CudaIndex", "torch==$TorchVersion", "torchvision==$TorchVisionVersion", "torchaudio==$TorchAudioVersion")
} else {
Write-Log "No NVIDIA GPU detected: installing CPU PyTorch $TorchVersion ..."
Invoke-Pip @("--index-url", "https://download.pytorch.org/whl/cpu", "torch==$TorchVersion", "torchvision==$TorchVisionVersion", "torchaudio==$TorchAudioVersion")
}
}
Write-Log "Installing/updating dependencies from requirements-windows.txt ..."
if ($uv) {
Invoke-Pip @("--extra-index-url", "https://download.pytorch.org/whl/$CudaIndex", "--index-strategy", "unsafe-best-match", "-r", $ReqFile)
} else {
Invoke-Pip @("-r", $ReqFile)
}
Set-Content -Path $StampFile -Value $reqHash -Encoding ascii
}
# ---------------------------------------------------------------- system tools (optional)
$winget = Find-Command "winget"
if ($NeedsSetup -and -not (Find-Command "ffplay")) {
$ffplayGlob = Join-Path $env:LOCALAPPDATA "Microsoft\WinGet\Packages\Gyan.FFmpeg*\*\bin\ffplay.exe"
if (-not (Get-ChildItem $ffplayGlob -ErrorAction SilentlyContinue)) {
if ($winget -and $env:DOC_READER_SKIP_WINGET -ne "1") {
Write-Log "ffmpeg not found: installing with winget (needed for audio playback and dictation)."
& $winget install --id Gyan.FFmpeg -e --accept-source-agreements --accept-package-agreements --silent | Out-Null
} else {
Write-Log "ffmpeg (ffplay) not found. Install it with: winget install Gyan.FFmpeg"
}
}
}
# ---------------------------------------------------------------- dispatch
if ($Args.Count -gt 0 -and $Args[0] -eq "--prepare-only") {
Write-Log "Environment ready: $VenvDir"
exit 0
}
$env:PYTHONPATH = if ($env:PYTHONPATH) { "$ScriptDir;$env:PYTHONPATH" } else { $ScriptDir }
if ($Args.Count -gt 0 -and $Args[0] -eq "cli") {
$cliArgs = @($Args | Select-Object -Skip 1)
& $VenvPython -m doc_reader @cliArgs
exit $LASTEXITCODE
}
if ($Args.Count -gt 0 -and (Test-Path $Args[0]) -and ($Args[0] -match "\.(pdf|docx|txt|md|markdown)$")) {
& $VenvPython -m doc_reader @Args
exit $LASTEXITCODE
}
& $VenvPython -m doc_reader.windows_app @Args
exit $LASTEXITCODE