File size: 4,023 Bytes
e93bfbd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
<#
.SYNOPSIS
  One-click launcher for MedicalAI - Light Weight
.DESCRIPTION
  Checks for Python, sets up venv, installs deps, and launches the app.
  Double-click this file or run: powershell -File launch.ps1
#>

$ErrorActionPreference = "Stop"
$ProjectRoot = Split-Path -Parent $MyInvocation.MyCommand.Path
Set-Location $ProjectRoot

$Host.UI.RawUI.WindowTitle = "MedicalAI - Light Weight"

# ── Check Python ──
$python = $null
foreach ($cmd in @("python", "python3", "py")) {
    try {
        $v = & $cmd --version 2>&1
        if ($v -match "Python 3\.(1[0-9]|[0-9]+)") {
            $python = $cmd
            break
        }
    } catch {}
}
if (-not $python) {
    Write-Host "Python 3.10+ is required but not found." -ForegroundColor Red
    Write-Host "Download from: https://www.python.org/downloads/" -ForegroundColor Yellow
    Write-Host "Make sure to check 'Add Python to PATH' during installation." -ForegroundColor Yellow
    Read-Host "Press Enter to exit"
    exit 1
}
Write-Host "Using: $(& $python --version)" -ForegroundColor Green

# ── Virtual Environment ──
$venvPath = Join-Path $ProjectRoot ".venv"
if (-not (Test-Path $venvPath)) {
    Write-Host "Creating virtual environment..." -ForegroundColor Cyan
    & $python -m venv $venvPath
    if (-not $?) { throw "Failed to create venv" }
}

# ── Activate ──
$activate = Join-Path $venvPath "Scripts\Activate.ps1"
. $activate

# ── Install Dependencies ──
$reqPath = Join-Path $ProjectRoot "requirements.txt"
if (Test-Path $reqPath) {
    Write-Host "Installing dependencies..." -ForegroundColor Cyan
    pip install -q -r $reqPath 2>&1 | Out-Null
    if (-not $?) {
        Write-Host "Retrying with full output..." -ForegroundColor Yellow
        pip install -r $reqPath
    }
}

# ── Check / Generate Default Models ──
$defaultClassifier = Join-Path $ProjectRoot "models\default\fusion_classifier.onnx"
$checkpoint = Join-Path $ProjectRoot "checkpoints\fusion_model.pth"
$onnxFull = Join-Path $ProjectRoot "checkpoints\onnx_full\fusion_full.onnx"

if ((-not (Test-Path $checkpoint)) -and (-not (Test-Path $onnxFull)) -and (-not (Test-Path $defaultClassifier))) {
    Write-Host "No models found. Generating default models..." -ForegroundColor Yellow
    python setup_default.py
    Write-Host "Default models generated. The app will work immediately." -ForegroundColor Green
}

if (Test-Path $checkpoint) {
    Write-Host "Trained model found." -ForegroundColor Green
} elseif (Test-Path $onnxFull) {
    Write-Host "ONNX pipeline found." -ForegroundColor Green
} elseif (Test-Path $defaultClassifier) {
    Write-Host "Default model found. Train a proper model for accurate results." -ForegroundColor Yellow
}

# ── Ask how to launch ──
Write-Host ""
Write-Host "MedicalAI - Light Weight" -ForegroundColor Cyan
Write-Host "========================" -ForegroundColor Cyan
Write-Host "1) Web UI (recommended - opens in browser)"
Write-Host "2) Command-line interface (CLI)"
Write-Host "3) API Server (for website/app integration)"
Write-Host ""

$choice = Read-Host "Select (1, 2, or 3)"

if ($choice -eq "2") {
    Write-Host "Launching CLI..." -ForegroundColor Green
    python run.py
} elseif ($choice -eq "3") {
    # Ensure API deps are installed
    try {
        Import-Module python -ErrorAction Stop
        python -c "import fastapi" 2>$null
    } catch {
        Write-Host "Installing API dependencies..." -ForegroundColor Cyan
        python -m pip install "fastapi[standard]" uvicorn 2>&1 | Out-Null
    }
    Write-Host "Launching API server on http://127.0.0.1:8000 ..." -ForegroundColor Green
    Write-Host "Your website can connect to: http://127.0.0.1:8000/api" -ForegroundColor Yellow
    python quantization.py --mode serve-api
} else {
    Write-Host "Launching web UI..." -ForegroundColor Green
    python web_ui.py
}

# ── Keep window open on crash ──
if (-not $?) {
    Write-Host "App exited with error. Press Enter to close." -ForegroundColor Red
    Read-Host
}