File size: 3,758 Bytes
3ca1d38
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env pwsh
<#
.SYNOPSIS
    Start MediGuard AI FastAPI server for local development.

.DESCRIPTION
    This script starts the FastAPI server with proper configuration
    for local development. It handles:
    - Environment variable loading from .env
    - Virtual environment activation
    - Server startup with uvicorn

.PARAMETER Port
    The port to run the server on (default: 8000)

.PARAMETER Host
    The host to bind to (default: 127.0.0.1)

.PARAMETER Reload
    Enable auto-reload on file changes (default: true)

.EXAMPLE
    .\scripts\start_server.ps1
    
.EXAMPLE
    .\scripts\start_server.ps1 -Port 8080 -Host 0.0.0.0
#>

param(
    [int]$Port = 8000,
    [string]$Host = "127.0.0.1",
    [bool]$Reload = $true
)

$ErrorActionPreference = "Stop"

Write-Host ""
Write-Host "========================================" -ForegroundColor Cyan
Write-Host "  MediGuard AI - Starting Server" -ForegroundColor Cyan
Write-Host "========================================" -ForegroundColor Cyan
Write-Host ""

# Change to project root
$ProjectRoot = Split-Path -Parent (Split-Path -Parent $PSScriptRoot)
if (Test-Path (Join-Path $PSScriptRoot "..")) {
    $ProjectRoot = Resolve-Path (Join-Path $PSScriptRoot "..")
}
Set-Location $ProjectRoot
Write-Host "[INFO] Project root: $ProjectRoot" -ForegroundColor Gray

# Check for virtual environment
$VenvPath = Join-Path $ProjectRoot ".venv"
$VenvActivate = Join-Path $VenvPath "Scripts\Activate.ps1"

if (Test-Path $VenvActivate) {
    Write-Host "[INFO] Activating virtual environment..." -ForegroundColor Gray
    & $VenvActivate
} else {
    Write-Host "[WARN] No virtual environment found at .venv" -ForegroundColor Yellow
    Write-Host "[WARN] Creating virtual environment..." -ForegroundColor Yellow
    python -m venv .venv
    & $VenvActivate
    pip install -r requirements.txt
}

# Load .env file if present
$EnvFile = Join-Path $ProjectRoot ".env"
if (Test-Path $EnvFile) {
    Write-Host "[INFO] Loading environment from .env..." -ForegroundColor Gray
    Get-Content $EnvFile | ForEach-Object {
        if ($_ -match "^\s*([^#][^=]+)=(.*)$") {
            $key = $matches[1].Trim()
            $value = $matches[2].Trim()
            # Remove quotes if present
            $value = $value -replace '^["'']|["'']$'
            [Environment]::SetEnvironmentVariable($key, $value, "Process")
        }
    }
}

# Check for required API keys
$HasGroq = $env:GROQ_API_KEY
$HasGoogle = $env:GOOGLE_API_KEY

if (-not $HasGroq -and -not $HasGoogle) {
    Write-Host ""
    Write-Host "[WARN] No LLM API key found!" -ForegroundColor Yellow
    Write-Host "  Set GROQ_API_KEY or GOOGLE_API_KEY in .env file" -ForegroundColor Yellow
    Write-Host "  Get a free Groq key: https://console.groq.com/keys" -ForegroundColor Yellow
    Write-Host ""
}

# Check for FAISS index
$FaissIndex = Join-Path $ProjectRoot "data\vector_stores\medical_knowledge.faiss"
if (-not (Test-Path $FaissIndex)) {
    Write-Host ""
    Write-Host "[WARN] FAISS index not found!" -ForegroundColor Yellow
    Write-Host "  Run: python -m src.pdf_processor" -ForegroundColor Yellow
    Write-Host "  to create the vector store from PDFs" -ForegroundColor Yellow
    Write-Host ""
}

# Build uvicorn command
$ReloadFlag = if ($Reload) { "--reload" } else { "" }

Write-Host ""
Write-Host "[INFO] Starting server at http://${Host}:${Port}" -ForegroundColor Green
Write-Host "[INFO] API docs available at http://${Host}:${Port}/docs" -ForegroundColor Green
Write-Host "[INFO] Press Ctrl+C to stop" -ForegroundColor Gray
Write-Host ""

# Start the server
$UvicornArgs = @(
    "-m", "uvicorn",
    "src.main:app",
    "--host", $Host,
    "--port", $Port
)
if ($Reload) {
    $UvicornArgs += "--reload"
}

python @UvicornArgs