|
param ( |
|
[Parameter(Mandatory=$true)] |
|
[string]$outputFilePath, |
|
[string]$rentryUrl = "https://rentry.org/ponyxl_loras_n_stuff" |
|
) |
|
|
|
function Get-RemoteFileSize { |
|
param ( |
|
[string]$url |
|
) |
|
|
|
try { |
|
$ProgressPreference = 'SilentlyContinue' |
|
$response = Invoke-WebRequest -Uri $url -Method Head |
|
$ProgressPreference = 'Continue' |
|
$fileSize = [int]$response.Headers['Content-Length'] |
|
return $fileSize; |
|
} catch { |
|
Write-Error "Failed to retrieve file size. $_" |
|
} |
|
} |
|
|
|
function Create-Entry { |
|
param ( |
|
[string]$name, |
|
[string]$url, |
|
[int]$size, |
|
[string] $comment, |
|
[int]$status |
|
) |
|
return [pscustomobject]@{Name=$name;Url=$url;Size=$size;Status=$status;Comment=$comment;Selected=0} |
|
} |
|
|
|
Clear-Host |
|
|
|
|
|
$historyFile = Join-Path $outputFilePath "_history.txt" |
|
|
|
|
|
$historyDict = @{} |
|
|
|
if (Test-Path $historyFile -PathType Leaf) { |
|
|
|
Get-Content -Path $historyFile | ForEach-Object { |
|
|
|
$lineParts = $_ -split '\s+' |
|
|
|
|
|
if ($lineParts.Length -ge 2) { |
|
$key = $lineParts[0] |
|
|
|
|
|
|
|
|
|
|
|
|
|
$historyDict[$key] = $lineParts[1] |
|
} |
|
} |
|
} |
|
|
|
Write-Host "Getting details about the Loras" |
|
|
|
|
|
$downloadLinkPattern = '<a [^>]*href=["'']?(https?://(?:www\.)?[^\s]+safetensors\b[^"''\s>]*)[^>]*>([^<]*)</a>([^<]*)' |
|
|
|
|
|
$pageContent = Invoke-RestMethod -Uri $rentryUrl |
|
|
|
|
|
$matches = $pageContent | Select-String -Pattern $downloadLinkPattern -AllMatches | ForEach-Object { $_.Matches } |
|
|
|
|
|
$availableLoras = @() |
|
|
|
|
|
foreach ($match in $matches) { |
|
$url = $match.Groups[1].Value |
|
$htmlAfterA = $match.Groups[3].Value |
|
|
|
|
|
$trimmedString = $htmlAfterA.Trim() |
|
$firstWord = $trimmedString.Split(' ', 2)[0] |
|
$remainingText = $trimmedString.Split(' ', 2)[1] |
|
|
|
$outputFileWithPath = Join-Path "$outputFilePath" "ponyxl_$firstWord.safetensors" |
|
|
|
$comment = if($remainingText -ne $null) { $remainingText.Trim() } else { "" } |
|
|
|
if($historyDict[$firstWord] -eq $null -or -Not (Test-Path $outputFileWithPath -PathType Leaf)) { |
|
$fileSize = Get-RemoteFileSize -url $url |
|
|
|
|
|
$availableLoras += Create-Entry -name $firstWord -url $url -size $fileSize -comment $comment -status 0 |
|
} elseif(-Not ($historyDict[$firstWord] -eq $url)) { |
|
|
|
$fileSize = Get-RemoteFileSize -url $url |
|
|
|
$availableLoras += Create-Entry -name $firstWord -url $url -size $fileSize -comment $comment -status 1 |
|
} |
|
} |
|
|
|
|
|
if($availableLoras.Count -eq 0) { |
|
Write-Host "There are no available updates based on your history file." |
|
Exit |
|
} |
|
|
|
Do { |
|
Clear-Host |
|
Write-Host "Below are the available LoRAs, asterisk indicates a LoRA is queued." |
|
$index = 1 |
|
foreach ($availableLora in $availableLoras) { |
|
$name = $availableLora.Name |
|
$queued = if ($availableLora.Selected -eq 1) { "(*)" } else { "" } |
|
$size = $availableLora.Size / 1048576 |
|
$status = if ($availableLora.Status -eq 0) { "New" } else { "Updated" } |
|
|
|
$size = $size.ToString("F2") |
|
|
|
Write-Host "[$index]) $queued $name - $status ($size MB)" |
|
$index++; |
|
} |
|
|
|
Write-Host "Enter a number to toggle queued status, a to queue all, or c to continue to download the queued LoRAs." |
|
$input = Read-Host "Command" |
|
|
|
if($input -eq "a") { |
|
foreach ($availableLora in $availableLoras) { |
|
$availableLora.Selected = 1 |
|
} |
|
} elseif($input -ne "c") { |
|
$parsedInt = -1 |
|
$success = [int]::TryParse($input, [ref]$parsedInt) |
|
if($success) { |
|
$availableLoras[$parsedInt - 1].Selected = ($availableLoras[$parsedInt - 1].Selected + 1) % 2 |
|
} |
|
} |
|
} while($input -ne "c") |
|
|
|
if (-not (Test-Path $outputFilePath -PathType Container)) { |
|
|
|
New-Item -Path $outputFilePath -ItemType Directory |
|
} |
|
|
|
|
|
$downloadedLoras = 0 |
|
foreach ($availableLora in $availableLoras) { |
|
if($availableLora.Selected -eq 1) { |
|
try { |
|
$name = $availableLora.Name |
|
$url = $availableLora.Url.Trim() |
|
$comment = $availableLora.Comment |
|
$outputFileWithPath = Join-Path $outputFilePath "ponyxl_$name.safetensors" |
|
|
|
|
|
if (Test-Path $outputFileWithPath) { |
|
$counter = 1 |
|
$newName = "ponyxl_$($name)_old_$counter.safetensors" |
|
$newPath = Join-Path $outputFilePath $newName |
|
|
|
while (Test-Path $newPath) { |
|
$newName = "ponyxl_$($name)_old_$counter.safetensors" |
|
$newPath = Join-Path $outputFilePath $newName |
|
$counter++ |
|
} |
|
|
|
Rename-Item -LiteralPath $outputFileWithPath -NewName $newName |
|
Write-Host "Renamed old $name LoRA to: $newName" |
|
} |
|
|
|
Write-Host "Downloading lora $name with url $url" |
|
Invoke-WebRequest $url -OutFile $outputFileWithPath |
|
|
|
++$downloadedLoras |
|
$historyDict[$name] = $url |
|
|
|
if ($comment -ne $null -and $comment -ne "") { |
|
$commentFileWithPath = Join-Path "$outputFilePath" "ponyxl_$name.txt" |
|
|
|
if (Test-Path $commentFileWithPath -PathType Leaf) { |
|
Clear-Content -Path $commentFileWithPath |
|
|
|
|
|
Add-Content -Path $commentFileWithPath -Value $comment |
|
} |
|
} |
|
} catch { |
|
Write-Host "Error downloading lora $name, skipping, error was $_" |
|
} |
|
} |
|
} |
|
|
|
if (Test-Path $historyFile -PathType Leaf) { |
|
|
|
Clear-Content -Path $historyFile |
|
} |
|
|
|
|
|
foreach ($entry in $historyDict.GetEnumerator()) { |
|
$entryString = "{0} {1}" -f $entry.Key, $entry.Value |
|
Add-Content -Path $historyFile -Value $entryString |
|
} |
|
|
|
Write-Host "Downloaded $downloadedLoras LoRAs." |
|
Write-Host "Wrote the most recent urls for each model to _history.txt." |