-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstart_all_services.ps1
More file actions
264 lines (216 loc) · 9.35 KB
/
Copy pathstart_all_services.ps1
File metadata and controls
264 lines (216 loc) · 9.35 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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
<#
.SYNOPSIS
Complete ML System Launcher
.DESCRIPTION
Starts all components of the ML monitoring system:
- MLflow tracking server (port 5000)
- FastAPI prediction API (port 8000)
- Streamlit monitoring dashboard (port 8501)
.EXAMPLE
.\start_all_services.ps1
#>
param(
[switch]$SkipMLflow,
[switch]$SkipAPI,
[switch]$SkipDashboard
)
Write-Host "=" * 80 -ForegroundColor Cyan
Write-Host "ML SYSTEM LAUNCHER" -ForegroundColor Cyan
Write-Host "=" * 80 -ForegroundColor Cyan
Write-Host ""
# Activate virtual environment
$venvPath = ".venv\Scripts\Activate.ps1"
if (Test-Path $venvPath) {
Write-Host "Activating virtual environment..." -ForegroundColor Green
& $venvPath
} else {
Write-Host "Virtual environment not found. Using system Python..." -ForegroundColor Yellow
}
# Check Python
Write-Host "Checking Python installation..." -ForegroundColor Cyan
python --version
if ($LASTEXITCODE -ne 0) {
Write-Host "Python not found! Please install Python 3.11+" -ForegroundColor Red
exit 1
}
Write-Host ""
# Function to check if port is in use
function Test-Port {
param($Port)
$connection = Get-NetTCPConnection -LocalPort $Port -State Listen -ErrorAction SilentlyContinue
return $null -ne $connection
}
# Function to start process in new window
function Start-Service {
param(
[string]$Name,
[string]$Command,
[int]$Port,
[string]$Color = "Green"
)
Write-Host "[$Name] Starting on port $Port..." -ForegroundColor $Color
if (Test-Port $Port) {
Write-Host "[$Name] Port $Port already in use. Service may already be running." -ForegroundColor Yellow
return $null
}
$process = Start-Process powershell -ArgumentList "-NoExit", "-Command", $Command -PassThru -WindowStyle Normal
Write-Host "[$Name] Started (PID: $($process.Id))" -ForegroundColor $Color
Start-Sleep -Seconds 2
return $process
}
$processes = @()
# Start MLflow
if (-not $SkipMLflow) {
Write-Host ""
Write-Host "=== Starting MLflow Tracking Server ===" -ForegroundColor Cyan
$mlflowCmd = "Write-Host 'MLflow Tracking Server' -ForegroundColor Green; " +
"Write-Host '=====================' -ForegroundColor Green; " +
"Write-Host ''; " +
"mlflow server --host 127.0.0.1 --port 5000 --backend-store-uri sqlite:///mlflow.db --default-artifact-root ./mlruns"
$proc = Start-Service -Name "MLflow" -Command $mlflowCmd -Port 5000 -Color "Blue"
if ($proc) { $processes += $proc }
Write-Host "MLflow UI: http://localhost:5000" -ForegroundColor Blue
}
# Start API
if (-not $SkipAPI) {
Write-Host ""
Write-Host "=== Starting FastAPI Prediction Service ===" -ForegroundColor Cyan
# Check for model
$modelPath = "mlruns\best_model_artifacts\customer_churn_optimization"
if (-not (Test-Path $modelPath)) {
Write-Host "WARNING: No trained model found!" -ForegroundColor Red
Write-Host "API may fail to start. Train a model first:" -ForegroundColor Yellow
Write-Host " python Scripts\model_training.py" -ForegroundColor White
Write-Host ""
}
$apiCmd = "Write-Host 'FastAPI Prediction Service' -ForegroundColor Green; " +
"Write-Host '==========================' -ForegroundColor Green; " +
"Write-Host ''; " +
"python -m uvicorn api.main:app --host 127.0.0.1 --port 8000 --reload"
$proc = Start-Service -Name "API" -Command $apiCmd -Port 8000 -Color "Magenta"
if ($proc) { $processes += $proc }
Write-Host "API Docs: http://localhost:8000/docs" -ForegroundColor Magenta
}
# Start Dashboard
if (-not $SkipDashboard) {
Write-Host ""
Write-Host "=== Starting Streamlit Monitoring Dashboard ===" -ForegroundColor Cyan
# Install dashboard dependencies if needed
$streamlitCheck = python -c "import streamlit" 2>&1
if ($LASTEXITCODE -ne 0) {
Write-Host "Installing dashboard dependencies..." -ForegroundColor Yellow
pip install -q -r dashboard/requirements.txt
if ($LASTEXITCODE -ne 0) {
Write-Host "Failed to install dashboard dependencies!" -ForegroundColor Red
Write-Host "Skipping dashboard..." -ForegroundColor Yellow
} else {
Write-Host "Dependencies installed!" -ForegroundColor Green
}
}
if ($LASTEXITCODE -eq 0 -or $streamlitCheck -match "streamlit") {
$dashCmd = "Write-Host 'Streamlit Monitoring Dashboard' -ForegroundColor Green; " +
"Write-Host '=============================' -ForegroundColor Green; " +
"Write-Host ''; " +
"streamlit run dashboard\app.py"
$proc = Start-Service -Name "Dashboard" -Command $dashCmd -Port 8501 -Color "Green"
if ($proc) { $processes += $proc }
Write-Host "Dashboard: http://localhost:8501" -ForegroundColor Green
}
}
Write-Host ""
Write-Host "=" * 80 -ForegroundColor Cyan
Write-Host "SYSTEM READY" -ForegroundColor Green
Write-Host "=" * 80 -ForegroundColor Cyan
Write-Host ""
# Display service summary
Write-Host "Services Running:" -ForegroundColor Cyan
Write-Host ""
if (-not $SkipMLflow) {
$mlflowStatus = if (Test-Port 5000) { "✓ Running" } else { "✗ Failed" }
Write-Host " [MLflow] Port 5000 $mlflowStatus" -ForegroundColor $(if (Test-Port 5000) { "Green" } else { "Red" })
Write-Host " http://localhost:5000" -ForegroundColor Blue
Write-Host ""
}
if (-not $SkipAPI) {
$apiStatus = if (Test-Port 8000) { "✓ Running" } else { "✗ Failed" }
Write-Host " [API] Port 8000 $apiStatus" -ForegroundColor $(if (Test-Port 8000) { "Green" } else { "Red" })
Write-Host " http://localhost:8000/docs" -ForegroundColor Magenta
Write-Host ""
}
if (-not $SkipDashboard) {
$dashStatus = if (Test-Port 8501) { "✓ Running" } else { "✗ Failed" }
Write-Host " [Dashboard] Port 8501 $dashStatus" -ForegroundColor $(if (Test-Port 8501) { "Green" } else { "Red" })
Write-Host " http://localhost:8501" -ForegroundColor Green
Write-Host ""
}
Write-Host "=" * 80 -ForegroundColor Cyan
Write-Host ""
# Wait for services to initialize
Write-Host "Waiting for services to initialize..." -ForegroundColor Yellow
Start-Sleep -Seconds 5
# Health checks
Write-Host ""
Write-Host "Running health checks..." -ForegroundColor Cyan
Write-Host ""
if (-not $SkipAPI -and (Test-Port 8000)) {
try {
$health = Invoke-RestMethod -Uri "http://localhost:8000/health" -Method Get -TimeoutSec 5 -ErrorAction Stop
if ($health.status -eq "ok") {
Write-Host " [API] Health check: ✓ PASSED" -ForegroundColor Green
} else {
Write-Host " [API] Health check: ✗ FAILED (status: $($health.status))" -ForegroundColor Red
}
} catch {
Write-Host " [API] Health check: ⚠ NOT READY (still starting...)" -ForegroundColor Yellow
}
}
if (-not $SkipMLflow -and (Test-Port 5000)) {
try {
$mlflow = Invoke-WebRequest -Uri "http://localhost:5000" -Method Get -TimeoutSec 5 -ErrorAction Stop
if ($mlflow.StatusCode -eq 200) {
Write-Host " [MLflow] Health check: ✓ PASSED" -ForegroundColor Green
}
} catch {
Write-Host " [MLflow] Health check: ⚠ NOT READY (still starting...)" -ForegroundColor Yellow
}
}
if (-not $SkipDashboard -and (Test-Port 8501)) {
Write-Host " [Dashboard] Server check: ✓ RUNNING" -ForegroundColor Green
}
Write-Host ""
Write-Host "=" * 80 -ForegroundColor Cyan
Write-Host "USAGE GUIDE" -ForegroundColor Yellow
Write-Host "=" * 80 -ForegroundColor Cyan
Write-Host ""
Write-Host "1. MLflow (Model Tracking):" -ForegroundColor Blue
Write-Host " - View experiments and metrics" -ForegroundColor White
Write-Host " - Compare model versions" -ForegroundColor White
Write-Host " - Access artifacts" -ForegroundColor White
Write-Host ""
Write-Host "2. API (Predictions):" -ForegroundColor Magenta
Write-Host " - Make churn predictions" -ForegroundColor White
Write-Host " - Test endpoints interactively" -ForegroundColor White
Write-Host " - View API schema" -ForegroundColor White
Write-Host ""
Write-Host "3. Dashboard (Monitoring):" -ForegroundColor Green
Write-Host " - View model performance" -ForegroundColor White
Write-Host " - Analyze SHAP & LIME explanations" -ForegroundColor White
Write-Host " - Detect data drift" -ForegroundColor White
Write-Host " - Make live predictions" -ForegroundColor White
Write-Host ""
Write-Host "=" * 80 -ForegroundColor Cyan
Write-Host ""
Write-Host "To stop all services: Close the PowerShell windows or press Ctrl+C in each" -ForegroundColor Yellow
Write-Host ""
Write-Host "For detailed documentation:" -ForegroundColor Cyan
Write-Host " - Dashboard: DASHBOARD_GUIDE.md" -ForegroundColor White
Write-Host " - API: api/README.md" -ForegroundColor White
Write-Host " - MLflow: MLFLOW_GUIDE.md" -ForegroundColor White
Write-Host ""
# Keep main window open
Write-Host "Press any key to exit this launcher window (services will continue running)..." -ForegroundColor Yellow
$null = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
Write-Host ""
Write-Host "Launcher closed. Services are still running in separate windows." -ForegroundColor Green
Write-Host "To stop services, close their respective windows." -ForegroundColor White
Write-Host ""