-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_demo.ps1
More file actions
124 lines (98 loc) · 3.73 KB
/
run_demo.ps1
File metadata and controls
124 lines (98 loc) · 3.73 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
$ErrorActionPreference = "Stop"
Set-Location -LiteralPath $PSScriptRoot
function Write-Step {
param([string]$Message)
Write-Host ""
Write-Host "==> $Message" -ForegroundColor Cyan
}
function Get-PythonCommand {
if (Get-Command python -ErrorAction SilentlyContinue) {
try {
& python --version | Out-Null
if ($LASTEXITCODE -eq 0) {
return @("python")
}
}
catch {
}
}
if (Get-Command py -ErrorAction SilentlyContinue) {
try {
& py -3 --version | Out-Null
if ($LASTEXITCODE -eq 0) {
return @("py", "-3")
}
}
catch {
}
}
throw "Python 3 was not found in PATH. Please install Python first."
}
function Invoke-Python {
param(
[string[]]$PythonCmd,
[string[]]$PythonArgs
)
$pythonExe = $PythonCmd[0]
$pythonPrefix = @()
if ($PythonCmd.Length -gt 1) {
$pythonPrefix = $PythonCmd[1..($PythonCmd.Length - 1)]
}
& $pythonExe @pythonPrefix @PythonArgs
if ($LASTEXITCODE -ne 0) {
throw "Command failed: $($PythonCmd -join ' ') $($PythonArgs -join ' ')"
}
}
function Ensure-EnvFile {
$envPath = Join-Path $PSScriptRoot ".env"
$examplePath = Join-Path $PSScriptRoot ".env.example"
if (-not (Test-Path $envPath) -and (Test-Path $examplePath)) {
Copy-Item -LiteralPath $examplePath -Destination $envPath
Write-Host "Created .env from .env.example" -ForegroundColor Yellow
}
if (-not (Test-Path $envPath)) {
Set-Content -LiteralPath $envPath -Value "DASHSCOPE_API_KEY="
}
$content = Get-Content -LiteralPath $envPath -Raw
$match = [regex]::Match($content, "(?m)^\s*DASHSCOPE_API_KEY\s*=\s*(.+?)\s*$")
$currentKey = if ($match.Success) { $match.Groups[1].Value.Trim() } else { "" }
if ([string]::IsNullOrWhiteSpace($currentKey) -or $currentKey -eq "sk-your-api-key-here") {
Write-Host ""
Write-Host "DASHSCOPE_API_KEY is missing." -ForegroundColor Yellow
$inputKey = Read-Host "Enter your DashScope API key (it will be saved to .env)"
if ([string]::IsNullOrWhiteSpace($inputKey)) {
throw "No DASHSCOPE_API_KEY was provided. Startup cancelled."
}
$newLine = "DASHSCOPE_API_KEY=$inputKey"
if ($match.Success) {
$updated = [regex]::Replace($content, "(?m)^\s*DASHSCOPE_API_KEY\s*=.*$", $newLine)
}
else {
$updated = $content.TrimEnd() + [Environment]::NewLine + $newLine + [Environment]::NewLine
}
Set-Content -LiteralPath $envPath -Value $updated -Encoding UTF8
Write-Host ".env updated." -ForegroundColor Green
}
}
function Ensure-Vectorstore {
$indexFile = Join-Path $PSScriptRoot "faiss_index\index.faiss"
$metaFile = Join-Path $PSScriptRoot "faiss_index\index.pkl"
if ((Test-Path $indexFile) -and (Test-Path $metaFile)) {
Write-Host "Existing vector index found. Skipping rebuild." -ForegroundColor Green
return
}
Write-Step "Vector index not found. Building now"
Invoke-Python -PythonCmd $script:PythonCmd -PythonArgs @("build_vectorstore.py")
}
$script:PythonCmd = Get-PythonCommand
Write-Step "Checking Python"
Invoke-Python -PythonCmd $PythonCmd -PythonArgs @("--version")
Write-Step "Checking API key"
Ensure-EnvFile
Write-Step "Installing dependencies"
Invoke-Python -PythonCmd $PythonCmd -PythonArgs @("-m", "pip", "install", "-r", "requirements.txt")
Write-Step "Checking vector index"
Ensure-Vectorstore
Write-Step "Starting Streamlit demo"
Write-Host "The browser should open shortly." -ForegroundColor Green
Invoke-Python -PythonCmd $PythonCmd -PythonArgs @("-m", "streamlit", "run", "app.py")