Skip to content

Instantly share code, notes, and snippets.

@anzz1
Last active April 8, 2023 16:35
Show Gist options
  • Save anzz1/1a0d0673d7765f115d57170b675e19da to your computer and use it in GitHub Desktop.
Save anzz1/1a0d0673d7765f115d57170b675e19da to your computer and use it in GitHub Desktop.
one-click-gpt4x
<# :
@echo off
cd /d "%~dp0"
md bin 2>nul
md models\gpt4-x-alpaca-13b-ggml 2>nul
md prompts 2>nul
if not exist "bin\wget.exe" (
powershell -nol -noni -nop -ex bypass -c "&{[ScriptBlock]::Create((cat '%~f0') -join [Char[]]10).Invoke()}"
)
if not exist "bin\main.exe" (
del llama-master-f2d1c47-bin-win-avx2-x64.zip 2>nul
bin\wget.exe -nv --show-progress --no-check-certificate "https://github.com/ggerganov/llama.cpp/releases/download/master-f2d1c47/llama-master-f2d1c47-bin-win-avx2-x64.zip" && (
powershell -nol -noni -nop -ex bypass -c "&{[Reflection.Assembly]::LoadWithPartialName('System.IO.Compression.FileSystem');[System.IO.Compression.ZipFile]::ExtractToDirectory($(join-path $PWD 'llama-master-f2d1c47-bin-win-avx2-x64.zip'), $(join-path $PWD 'bin'))}"
)
)
if not exist "models\gpt4-x-alpaca-13b-ggml\ggml-model-q4_1.bin" (
bin\wget.exe -nv --show-progress --no-check-certificate ^
"https://huggingface.co/anon8231489123/gpt4-x-alpaca-13b-native-4bit-128g/resolve/main/gpt4-x-alpaca-13b-ggml-q4_1-from-gptq-4bit-128g/ggml-model-q4_1.bin" ^
-O "./models/gpt4-x-alpaca-13b-ggml/ggml-model-q4_1.bin"
)
if not exist "prompts\alpaca.txt" (
echo Below is an instruction that describes a task. Write a response that appropriately completes the request.> prompts\alpaca.txt
)
bin\main.exe -m ./models/gpt4-x-alpaca-13b-ggml/ggml-model-q4_1.bin -f ./prompts/alpaca.txt --temp 0.71 --repeat_last_n 256 --repeat_penalty 1.1764705882352942 --top_k 40 --top_p 0.1 -ins -b 256 -c 2024 -n -1 --n_parts 1 %*
exit /b
#>
function filesize($length) {
$gb = [math]::pow(2, 30)
$mb = [math]::pow(2, 20)
$kb = [math]::pow(2, 10)
if($length -gt $gb) {
"{0:n1} GB" -f ($length / $gb)
} elseif($length -gt $mb) {
"{0:n1} MB" -f ($length / $mb)
} elseif($length -gt $kb) {
"{0:n1} KB" -f ($length / $kb)
} else {
"$($length) B"
}
}
function url_remote_filename($url) {
$uri = (New-Object URI $url)
$basename = Split-Path $uri.PathAndQuery -Leaf
If ($basename -match ".*[?=]+([\w._-]+)") {
$basename = $matches[1]
}
If (($basename -notlike "*.*") -or ($basename -match "^[v.\d]+$")) {
$basename = Split-Path $uri.AbsolutePath -Leaf
}
If (($basename -notlike "*.*") -and ($uri.Fragment -ne "")) {
$basename = $uri.Fragment.Trim('/', '#')
}
return $basename
}
function dl($url, $to, $progress) {
[System.Net.ServicePointManager]::SecurityProtocol = 3072 -bor 768 -bor 192
$reqUrl = ($url -split "#")[0]
$wreq = [net.webrequest]::create($reqUrl)
try {
$wres = $wreq.GetResponse()
} catch [System.Net.WebException] {
$exc = $_.Exception
$handledCodes = @(
[System.Net.HttpStatusCode]::MovedPermanently, # HTTP 301
[System.Net.HttpStatusCode]::Found, # HTTP 302
[System.Net.HttpStatusCode]::SeeOther, # HTTP 303
[System.Net.HttpStatusCode]::TemporaryRedirect # HTTP 307
)
$redirectRes = $exc.Response
if ($handledCodes -notcontains $redirectRes.StatusCode) {
throw $exc
}
if ((-not $redirectRes.Headers) -or ($redirectRes.Headers -notcontains 'Location')) {
throw $exc
}
$newUrl = $redirectRes.Headers['Location']
if ($url -like '*#/*') {
$null, $postfix = $url -split '#/'
$newUrl = "$newUrl#/$postfix"
}
dl $newUrl $to $progress
return
}
$total = $wres.ContentLength
if($total -eq -1 -and $wreq -is [net.ftpwebrequest]) {
$total = ftp_file_size($url)
}
if ($progress -and ($total -gt 0)) {
[console]::CursorVisible = $false
function dl_onProgress($read) {
dl_progress $read $total $url
}
} else {
write-host "Downloading $url ($(filesize $total))..."
function dl_onProgress {
#no op
}
}
try {
$s = $wres.getresponsestream()
$fs = [io.file]::openwrite($to)
$buffer = new-object byte[] 2048
$totalRead = 0
$sw = [diagnostics.stopwatch]::StartNew()
dl_onProgress $totalRead
while(($read = $s.read($buffer, 0, $buffer.length)) -gt 0) {
$fs.write($buffer, 0, $read)
$totalRead += $read
if ($sw.elapsedmilliseconds -gt 100) {
$sw.restart()
dl_onProgress $totalRead
}
}
$sw.stop()
dl_onProgress $totalRead
} finally {
if ($progress) {
[console]::CursorVisible = $true
write-host
}
if ($fs) {
$fs.close()
}
if ($s) {
$s.close();
}
$wres.close()
}
}
function dl_progress_output($url, $read, $total, $console) {
$filename = url_remote_filename $url
$p = [math]::Round($read / $total * 100, 0)
$left = "$filename ($(filesize $total))"
$right = [string]::Format("{0,3}%", $p)
$midwidth = $console.BufferSize.Width - ($left.Length + $right.Length + 8)
$completed = [math]::Abs([math]::Round(($p / 100) * $midwidth, 0) - 1)
if ($completed -gt 1) {
$dashes = [string]::Join("", ((1..$completed) | ForEach-Object {"="}))
}
$dashes += switch($p) {
100 {"="}
default {">"}
}
$spaces = switch($dashes.Length) {
$midwidth {[string]::Empty}
default {
[string]::Join("", ((1..($midwidth - $dashes.Length)) | ForEach-Object {" "}))
}
}
"$left [$dashes$spaces] $right"
}
function dl_progress($read, $total, $url) {
$console = $host.UI.RawUI;
$left = $console.CursorPosition.X;
$top = $console.CursorPosition.Y;
$width = $console.BufferSize.Width;
if($read -eq 0) {
$maxOutputLength = $(dl_progress_output $url 100 $total $console).length
if (($left + $maxOutputLength) -gt $width) {
write-host
$left = 0
$top = $top + 1
if($top -gt $console.CursorPosition.Y) { $top = $console.CursorPosition.Y }
}
}
write-host $(dl_progress_output $url $read $total $console) -nonewline
[console]::SetCursorPosition($left, $top)
}
dl "https://eternallybored.org/misc/wget/1.21.3/64/wget.exe" "bin\wget.exe" $(!([console]::isoutputredirected))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment