-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun.ps1
More file actions
243 lines (206 loc) · 8.1 KB
/
run.ps1
File metadata and controls
243 lines (206 loc) · 8.1 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
# =============================================================
# HDJ RAG Evaluation - One-Click Launcher (Windows)
# Run this script to update and start the application.
# =============================================================
$ErrorActionPreference = "Continue"
# --- Configuration ---
$AppUrl = "http://localhost:8501"
$VenvDir = ".venv"
$StreamlitApp = "frontend/app.py"
# --- Helpers ---
function Print-Header {
Write-Host ""
Write-Host "=============================================="
Write-Host " Health Data Justice - RAG Evaluation"
Write-Host "=============================================="
Write-Host ""
}
function Print-Step($num, $msg) {
Write-Host " [$num/5] $msg"
}
function Print-Success {
Write-Host ""
Write-Host "=============================================="
Write-Host " All good! The app is running."
Write-Host " Open in your browser: $AppUrl"
Write-Host "=============================================="
Write-Host ""
Write-Host " Press Ctrl+C in this window to stop the app."
Write-Host ""
}
function Print-Error($problem, $whatToDo) {
Write-Host ""
Write-Host " ** Problem: $problem"
if ($whatToDo) {
Write-Host " ** What to do: $whatToDo"
}
Write-Host ""
}
# Move to the script's own directory (so it works from anywhere)
$ScriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path
Set-Location $ScriptDir
Print-Header
# ---------------------------------------------------------
# Step 1: Pull latest changes
# ---------------------------------------------------------
Print-Step 1 "Checking for updates..."
if (-not (Get-Command "git" -ErrorAction SilentlyContinue)) {
Print-Error "Git is not installed." "Install Git from https://git-scm.com and try again."
Read-Host "Press Enter to close"
exit 1
}
# Stash any local data changes so pull doesn't fail
git stash --quiet 2>$null
$pullOutput = git pull 2>&1 | Out-String
$pullExit = $LASTEXITCODE
git stash pop --quiet 2>$null
if ($pullExit -ne 0) {
Write-Host " Could not check for updates (no internet?)."
Write-Host " Continuing with the current version."
} elseif ($pullOutput -match "Already up to date") {
Write-Host " Already up to date."
} else {
Write-Host " Updated to the latest version."
}
# ---------------------------------------------------------
# Step 2: Check Python
# ---------------------------------------------------------
Print-Step 2 "Checking Python..."
$Python = $null
$PythonVersion = ""
foreach ($py in @("python3.13", "python3.12", "python3", "python")) {
$cmd = Get-Command $py -ErrorAction SilentlyContinue
if ($cmd) {
$versionOutput = & $cmd.Source --version 2>&1 | Out-String
if ($versionOutput -match '(\d+)\.(\d+)') {
$major = [int]$Matches[1]
$minor = [int]$Matches[2]
if ($major -eq 3 -and $minor -ge 12) {
$Python = $cmd.Source
$PythonVersion = "$major.$minor"
break
}
}
}
}
if (-not $Python) {
Print-Error "Python 3.12 or newer is required but was not found." `
"Download it from https://www.python.org/downloads/ and install it (make sure to check 'Add Python to PATH'), then restart this script."
Read-Host "Press Enter to close"
exit 1
}
Write-Host " Found Python $PythonVersion."
# ---------------------------------------------------------
# Step 3: Set up environment & dependencies
# ---------------------------------------------------------
Print-Step 3 "Preparing environment..."
if (-not (Test-Path $VenvDir)) {
Write-Host " Creating virtual environment (first run, one moment)..."
& $Python -m venv $VenvDir
if ($LASTEXITCODE -ne 0) {
Print-Error "Could not create the virtual environment." `
"Try deleting the .venv folder and running this script again."
Read-Host "Press Enter to close"
exit 1
}
}
# Activate
$ActivateScript = Join-Path $VenvDir "Scripts\Activate.ps1"
if (-not (Test-Path $ActivateScript)) {
Print-Error "Virtual environment is damaged." `
"Delete the .venv folder and run this script again."
Read-Host "Press Enter to close"
exit 1
}
& $ActivateScript
# Install/update dependencies
python -m pip install --quiet --upgrade pip uv 2>$null
$depOutput = uv pip install --quiet -e . 2>&1 | Out-String
if ($LASTEXITCODE -ne 0) {
Print-Error "Could not install dependencies." `
"Check your internet connection, then try again. If the problem persists, delete the .venv folder and re-run."
Write-Host " Details: $depOutput"
Read-Host "Press Enter to close"
exit 1
}
Write-Host " Environment ready."
# ---------------------------------------------------------
# Step 4: Check Ollama
# ---------------------------------------------------------
Print-Step 4 "Checking Ollama..."
if (-not (Get-Command "ollama" -ErrorAction SilentlyContinue)) {
Print-Error "Ollama is not installed." `
"Download it from https://ollama.com and install it, then run this script again."
Read-Host "Press Enter to close"
exit 1
}
# Helper: check if Ollama server is responsive using the CLI (more reliable
# than Invoke-WebRequest, which can fail due to proxy/TLS on Windows PS 5.1)
function Test-OllamaRunning {
$out = ollama list 2>&1 | Out-String
return ($LASTEXITCODE -eq 0 -and $out -notmatch "could not connect")
}
# Check if Ollama is running; if not, start it in the background
$ollamaRunning = Test-OllamaRunning
if (-not $ollamaRunning) {
Write-Host " Starting Ollama in the background..."
# Try 'ollama serve' — if it errors with "bind" it means the server
# is already running (port taken), which is fine.
$serveLog = Join-Path $env:TEMP "ollama_serve.log"
Start-Process "ollama" -ArgumentList "serve" -WindowStyle Hidden `
-RedirectStandardError $serveLog -ErrorAction SilentlyContinue
# Wait up to 30 seconds for Ollama to come online
for ($i = 0; $i -lt 30; $i++) {
Start-Sleep -Seconds 1
if (Test-OllamaRunning) { $ollamaRunning = $true; break }
# Check if 'ollama serve' failed because the port is already taken
# — that means Ollama IS running, just our CLI check hasn't caught up
if (($i -eq 5) -and (Test-Path $serveLog)) {
$errText = Get-Content $serveLog -Raw -ErrorAction SilentlyContinue
if ($errText -match "bind|address already in use") {
Write-Host " Ollama server is already running."
$ollamaRunning = $true
break
}
}
}
if (-not $ollamaRunning) {
Print-Error "Ollama did not start automatically." `
"Please open the Ollama app manually (search for 'Ollama' in the Start menu), wait until its icon appears in the system tray, then run this script again."
Read-Host "Press Enter to close"
exit 1
}
}
# Make sure the embedding model is available
$modelList = ollama list 2>&1 | Out-String
if ($modelList -notmatch "qwen3-embedding:4b") {
Write-Host " Downloading the embedding model (first run, this takes a few minutes)..."
ollama pull qwen3-embedding:4b
if ($LASTEXITCODE -ne 0) {
Print-Error "Could not download the embedding model." `
"Check your internet connection and try again."
Read-Host "Press Enter to close"
exit 1
}
}
Write-Host " Ollama is running."
# ---------------------------------------------------------
# Step 5: Launch the app
# ---------------------------------------------------------
Print-Step 5 "Starting the application..."
# Open the browser after a short delay
Start-Job -ScriptBlock {
Start-Sleep -Seconds 3
Start-Process $using:AppUrl
} | Out-Null
Print-Success
# Run Streamlit (this blocks until the user presses Ctrl+C)
try {
streamlit run $StreamlitApp --server.port 8501
} catch {
# Ctrl+C triggers this
}
Write-Host ""
Write-Host " App stopped. Close this window or run the script again to restart."
Write-Host ""
Read-Host "Press Enter to close"