Repository navigation
Expand file tree
/
Copy pathdetect_qwen_with_ollama.ps1
More file actions
115 lines (93 loc) · 2.84 KB
/
Copy pathdetect_qwen_with_ollama.ps1
File metadata and controls
115 lines (93 loc) · 2.84 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
param(
[string]$Model = "qwen2.5-coder:latest",
[string]$VenvActivateScript = $null,
[string]$MainScript = $null
)
$startTime = Get-Date
$ScriptDir = $PSScriptRoot
$RootDir = (Resolve-Path (Join-Path $ScriptDir "..")).Path
if (-not $VenvActivateScript) {
$VenvActivateScript = Join-Path $RootDir ".venv\Scripts\Activate.ps1"
}
if (-not $MainScript) {
$MainScript = Join-Path $RootDir "scripts\detect_qwen.py"
}
Start-Transcript -Path (Join-Path $RootDir "detect_qwen_with_ollama.log") -Append
$ErrorActionPreference = "Stop"
$ollamaProcess = $null
$scriptSuccess = $true
function Stop-OllamaSession {
param(
[Parameter(Mandatory = $true)]
[System.Diagnostics.Process]$Process,
[Parameter(Mandatory = $true)]
[string]$Model
)
if (-not $Process -or $Process.HasExited) {
return
}
try {
$Process.StandardInput.WriteLine("/bye")
$Process.StandardInput.Flush()
$Process.StandardInput.Close()
}
catch {}
if ($Process.WaitForExit(15000)) {
return
}
try {
& ollama stop $Model | Out-Null
}
catch {}
if ($Process.WaitForExit(10000)) {
return
}
Write-Warning "Ollama no cerró tras /bye y stop; finalizando proceso."
$Process.Kill()
}
try {
Write-Host "[1/4] Iniciando Ollama en segundo plano con modelo '$Model'..."
$psi = New-Object System.Diagnostics.ProcessStartInfo
$psi.FileName = "ollama"
$psi.Arguments = "run $Model"
$psi.UseShellExecute = $false
$psi.RedirectStandardInput = $true
$psi.RedirectStandardOutput = $false
$psi.RedirectStandardError = $false
$psi.CreateNoWindow = $true
$ollamaProcess = New-Object System.Diagnostics.Process
$ollamaProcess.StartInfo = $psi
[void]$ollamaProcess.Start()
Start-Sleep -Seconds 15
Write-Host "[2/4] Activando entorno virtual..."
. $VenvActivateScript
Write-Host "[3/4] Ejecutando detección de alucinaciones con Qwen..."
python $MainScript --model $Model --overwrite
Write-Host "[4/4] Cerrando sesión de Ollama..."
if ($ollamaProcess -and -not $ollamaProcess.HasExited) {
Stop-OllamaSession -Process $ollamaProcess -Model $Model
}
Write-Host "Proceso completado."
}
catch {
Write-Error "Error durante la ejecución: $($_.Exception.Message)"
$scriptSuccess = $false
if ($ollamaProcess -and -not $ollamaProcess.HasExited) {
try {
Stop-OllamaSession -Process $ollamaProcess -Model $Model
}
catch {
try { $ollamaProcess.Kill() } catch {}
}
}
}
finally {
Stop-Transcript
if ($scriptSuccess) {
$endTime = Get-Date
$elapsed = $endTime - $startTime
$elapsedFormat = "{0:00}h {1:00}m {2:00}s" -f $elapsed.Hours, $elapsed.Minutes, $elapsed.Seconds
} else {
exit 1
}
}