$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 }