Skip to content

Instantly share code, notes, and snippets.

@halr9000
Last active January 28, 2022 23:15
Show Gist options
  • Save halr9000/f893422291c2d01bc263b5f794eebc81 to your computer and use it in GitHub Desktop.
Save halr9000/f893422291c2d01bc263b5f794eebc81 to your computer and use it in GitHub Desktop.
Quick helper script for VQGAN-CLIP
## Basic helper script for VQGAN-CLIP generation
## Assumes you have all dependencies setup as per https://github.com/nerdyrodent/VQGAN-CLIP
## Start from an Anaconda PowerShell shell, such as the shortcut in your start menu
## Be sure to setup the VQGAN Python environment first, but the script will activate it for you
## Place this script in the base directory containing generate.py
##
## Example 1: Run three jobs from an array
## > $prompts = "prompt1", "prompt2|3|4", "prompt5"
## > $prompts | % { .\gen.ps1 -Prompt $_ -Path $_.replace("|","-") }
##
## Example 2: Run using PowerShell threaded jobs which automatically queue and dequeue
## using positional parameters. Be sure to set ThrottleLimit to 1, or your GPU
## will melt. (Limit is valid for entire session, subsequent calls can omit the setting.)
## > Start-ThreadJob { .\gen.ps1 "prompt1 | prompt 2" filename } -ThrottleLimit 1
## > Start-ThreadJob { .\gen1.ps1 "prompt3" "filename2" -Iteration 500 }
## TODO: proper PS help file with examples
## TODO: make the script more portable, robust. run from anywhere, check more dependencies, tell you how to solve them
[CmdletBinding()]
param (
[Parameter(Mandatory=$true)]
[string[]]
$Prompt,
[Parameter(Mandatory=$true)]
[string]
$Path,
[Parameter()]
[int]
$Iteration = 300,
[Parameter()]
[int]
$SaveInterval = 50
)
# Ensure we are in an Anaconda shell and able to activate the VQGAN Python environment
if ($env:CONDA_DEFAULT_ENV -eq 'vqgan') {
Write-Verbose "Correct environment, let's go"
} else {
try { conda activate vqgan }
catch {
throw "Failed to activate environment"
}
}
$Joined = $Prompt -join "|" # accepts array in typiucal PowerShell fashion, joins with pipe to match expected syntax
if (!$Path.EndsWith(".png")) { $Path+=".png" } # prevents error if .png suffix is not supplied
python .\generate.py -p $Joined -o $Path -se $SaveInterval -i $Iteration
@halr9000
Copy link
Author

Why PowerShell to call Python? Because I really like PowerShell.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment