File size: 8,578 Bytes
e4460e1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
$huggingFaceRepoURL = "https://huggingface.co/bradhutchings/Brads-LLMs"
$llamafile = "Brads-LLMs-llamafile.exe"

$modelFile = 'Telugu-Indic-gemma-7b-finetuned-sft-Navarasa-2.0-q8_0.gguf'
$contextSize = 8192
$maxContextSize = 8192
$threads = 4
$parallel = 2
$ngl = 9999
$gpu = 'disable'           # disable, auto, amd, apple, nvidia

################################################################################
# Script location.
################################################################################

$myPath = $MyInvocation.MyCommand.Path
$myParent = Split-Path $mypath -Parent
$myGrandParent = Split-Path $myParent -Parent

<#
    "`$myPath: $myPath"
    "`$myParent: $myParent"
    "`$myGrandParent: $myGrandParent"
#>

################################################################################
# Overrides file.
################################################################################

$overridesJsonFilename = 'overrides.json'
$overridesJsonFile = "$myParent\$overridesJsonFilename"
if (Test-Path -Path $overridesJsonFile -PathType Leaf) {
    try {
        $contextSizeSave = $contextSize
        $threadsSave = $threads
        $parallelSave = $parallel
        $nglSave = $ngl
        $gpuSave = $gpu

        $overridesJsonFileData = Get-Content -Path $overridesJsonFile -Raw
        $jsonData = ConvertFrom-Json -InputObject $overridesJsonFileData

        if ($jsonData | Get-Member 'contextSize'){
            $contextSize = [Math]::Min($jsonData.contextSize, $maxContextSize)
        }
        if ($jsonData | Get-Member 'threads'){
            $threads = $jsonData.threads
        }
        if ($jsonData | Get-Member 'parallel'){
            $parallel = $jsonData.parallel
        }
        if ($jsonData | Get-Member 'ngl'){
            $ngl = $jsonData.ngl
        }

        if ($jsonData | Get-Member 'gpu'){
            $gpu = $jsonData.gpu
        }
    }
    catch {
        $contextSize = $contextSizeSave
        $threads = $threadsSave
        $parallel = $parallelSave
        $ngl = $nglSave
        $gpu = $gpuSave     
    }

    <#
    "overrides.json:"
    "- `$contextSize: $contextSize"
    "- `$threads: $threads"
    "- `$parallel: $parallel"
    "- `$ngl: $ngl"
    "- `$gpu: $gpu"
    #>
}

################################################################################
# Models folder.
################################################################################

$dev = ".dev"
$models = "models"
$myDev = "$myParent\$dev"
$myModels = "$myParent\$models"
$devModels = "$myGrandParent\$models"
$useModels = $myModels

<#
    echo "`$myDev: $myDev"
    echo "`$myModels: $myModels"
    echo "`$devModels: $devModels"
    echo "`$useModels: $useModels"

    "`$myDev exists: " + (Test-Path -Path $myDev -PathType Leaf)
    "`$devModels exists: " + (Test-Path -Path $devModels -PathType Container)
#>

if ((Test-Path -Path $myDev -PathType Leaf) -and (Test-Path -Path $devModels -PathType Container)) {
    $useModels = $devModels
}
else {
    if (!(Test-Path -Path $myModels -PathType Container)) {
        mkdir $myModels
    }
    $useModels = $myModels
}

<#
    echo "Models: $useModels"
    echo "--------------------"
#>

################################################################################
# Download llamafile.exe if not present.
################################################################################

$myLlamafile = "$myParent\$llamafile"
$myLlamafileTemp = "$myParent\$llamafile-temp"
$myLlamafileDownloadURL = "$huggingFaceRepoURL/resolve/main/$($llamafile)?download=true"

<#
    "`$llamafile: $llamafile" 
    "`$myLlamafile: $myLlamafile" 
    "`$myLlamafileTemp: $myLlamafileTemp" 
    "`$myLlamafileDownloadURL: $myLlamafileDownloadURL"
#>

if (!(Test-Path -Path $myLlamafile -PathType Leaf)) {
    # Download from Hugging Face

    "Downloading $llamafile from Hugging Face."
    # ""

    $progresspreference = 'SilentlyContinue'
    $result = Invoke-WebRequest -Method HEAD -Uri $myLlamafileDownloadURL -UseBasicParsing 
    $progresspreference = 'Continue'

    $downloadSizeRaw = $result.Headers."Content-Length"
    $downloadSize = [int]($downloadSizeRaw / (1024 * 1024))
    $downloadSize = $downloadSize.ToString("N0") + " MB"

    "- Preparing to download $downloadSize."
    # ""
    Start-Sleep -Seconds 2

    $sb = {
        $progresspreference = 'SilentlyContinue'
        Invoke-WebRequest -Uri $using:myLlamafileDownloadURL -Outfile $using:myLlamafileTemp | Out-Null
    }
    Start-Job -Name 'Download' -ScriptBlock $sb | Out-Null

    while ((Get-Job -Name 'Download').State -eq 'Running') {
        if (Test-Path -Path $myLlamafileTemp) {
            $downloadedBytesRaw = $((Get-ChildItem $myLlamafileTemp).Length)
            $downloadedBytes = [int]($downloadedBytesRaw / (1024 * 1024))
            $downloadedBytes = $downloadedBytes.ToString("N0") + " MB"
            $downloadPercent = [int](100 * ($downloadedBytesRaw / $downloadSizeRaw))

            Write-Progress -Activity "Downloading $llamafile" -Status ("Progress: " + $downloadPercent + "%") -CurrentOperation "$downloadedBytes / $downloadSize" -PercentComplete $downloadPercent
        }
        else {
            Write-Progress -Activity "Downloading $llamafile" -Status "Progress:" -CurrentOperation "Starting..."
        }
        Start-Sleep -Milliseconds 200
    }
    Write-Progress -Activity "Downloaded Bytes" -Completed
    Move-Item -Path $myLlamafileTemp -Destination $myLlamafile -Force

    "- Finished downloading $llamafile from Hugging Face."
    ""
    Start-Sleep -Seconds 2
}

################################################################################
# Download model file if not present.
################################################################################

$myModel = "$useModels\$modelFile"
$myModelTemp = "$useModels\$modelFile-temp"
$myModelDownloadURL = "$huggingFaceRepoURL/resolve/main/models/$($modelFile)?download=true"

<#
    "`$myModel: $myModel" 
    "`$myModelTemp: $myModelTemp" 
    "`$myModelDownloadURL: $myModelDownloadURL"
#>

if (!(Test-Path -Path $myModel -PathType Leaf)) {
    # Download from Hugging Face

    "Downloading $modelFile from Hugging Face."
    # ""
    $progresspreference = 'SilentlyContinue'
    $result = Invoke-WebRequest -Method HEAD -Uri $myModelDownloadURL -UseBasicParsing 
    $progresspreference = 'Continue'

    $downloadSizeRaw = $result.Headers."Content-Length"
    $downloadSize = [int]($downloadSizeRaw / (1024 * 1024))
    $downloadSize = $downloadSize.ToString("N0") + " MB"

    "- Preparing to download $downloadSize."
    # ""
    Start-Sleep -Seconds 2

    $sb = {
          $progresspreference = 'SilentlyContinue'
          Invoke-WebRequest -Uri $using:myModelDownloadURL -Outfile $using:myModelTemp | Out-Null
    }
    Start-Job -Name 'Download' -ScriptBlock $sb | Out-Null

    while ((Get-Job -Name 'Download').State -eq 'Running') {
        if (Test-Path -Path $myModelTemp) {
            $downloadedBytesRaw = $((Get-ChildItem $myModelTemp).Length)
            $downloadedBytes = [int]($downloadedBytesRaw / (1024 * 1024))
            $downloadedBytes = $downloadedBytes.ToString("N0") + " MB"
            $downloadPercent = [int](100 * ($downloadedBytesRaw / $downloadSizeRaw))

            Write-Progress -Activity "Downloading $modelFile" -Status ("Progress: " + $downloadPercent + "%") -CurrentOperation "$downloadedBytes / $downloadSize" -PercentComplete $downloadPercent
        }
        else {
            Write-Progress -Activity "Downloading $modelFile" -Status "Progress:" -CurrentOperation "Starting..."
        }
        Start-Sleep -Seconds 4
    }
    Write-Progress -Activity "Downloaded Bytes" -Completed
    Move-Item -Path $myModelTemp -Destination $myModel -Force
 
    "- Finished downloading $modelFile from Hugging Face."
    ""
    Start-Sleep -Seconds 2
}

################################################################################
# if llamafile and model are present, launch llamafile
################################################################################

if ((Test-Path -Path "$myLlamafile" -PathType Leaf) -and (Test-Path -Path "$myModel" -PathType Leaf)) {
    $process = Start-Process "$myLlamafile" -ArgumentList "-m `"$myModel`" --ctx-size $contextSize --threads $threads --parallel $parallel --server -ngl $ngl --gpu $gpu"  -PassThru -NoNewWindow
    $process.PriorityClass = [System.Diagnostics.ProcessPriorityClass]::High
}
else {
    "There was an unkown problem downloading or saving the model."
    Start-Sleep -s 20
}