From cd1fe6a77f897226053febb36934c612573dac0c Mon Sep 17 00:00:00 2001 From: d8ahazard Date: Thu, 15 Dec 2022 17:07:28 -0600 Subject: [PATCH 1/4] Add powershell launch scripts. --- webui-user.ps1 | 43 +++++++++++++++++++++++ webui.ps1 | 94 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 137 insertions(+) create mode 100644 webui-user.ps1 create mode 100644 webui.ps1 diff --git a/webui-user.ps1 b/webui-user.ps1 new file mode 100644 index 00000000..8766d704 --- /dev/null +++ b/webui-user.ps1 @@ -0,0 +1,43 @@ +$env:PYTHON= '' +$env:GIT= '' +$env:VENV_DIR= '' + +# Commandline arguments for webui.py, for example: $env:COMMANDLINE_ARGS="--medvram --opt-split-attention" +$env:COMMANDLINE_ARGS="" + +# script to launch to start the app +# $env:LAUNCH_SCRIPT="launch.py" + +# install command for torch +# $env:TORCH_COMMAND="pip install torch==1.12.1+cu113 --extra-index-url https://download.pytorch.org/whl/cu113" + +# Requirements file to use for stable-diffusion-webui +# $env:REQS_FILE="requirements_versions.txt" + +# $env:GFPGAN_PACKAGE="" +# $env:CLIP_PACKAGE="" +# $env:OPENCLIP_PACKAGE="" + +# URL to a WHL if you wish to override default xformers windows +# $env:XFORMERS_WINDOWS_PACKAGE="" + +# Uncomment and set to enable an alternate repository URL +# $env:STABLE_DIFFUSION_REPO="" +# $env:TAMING_TRANSFORMERS_REPO="" +# $env:K_DIFFUSION_REPO="" +# $env:CODEFORMER_REPO="" +# $env:BLIP_REPO="" + +# Uncomment and set to enable a specific revision of a repository +# $env:STABLE_DIFFUSION_COMMIT_HASH="" +# $env:TAMING_TRANSFORMERS_COMMIT_HASH="" +# $env:K_DIFFUSION_COMMIT_HASH="" +# $env:CODEFORMER_COMMIT_HASH="" +# $env:BLIP_COMMIT_HASH="" + + +# Uncomment to enable accelerated launch +# $env:ACCELERATE="True" + +$SCRIPT = "$PSScriptRoot\webui.ps1" +Invoke-Expression "$SCRIPT" diff --git a/webui.ps1 b/webui.ps1 new file mode 100644 index 00000000..de6916a7 --- /dev/null +++ b/webui.ps1 @@ -0,0 +1,94 @@ +if ($env:PYTHON -eq "" -or $env:PYTHON -eq $null) { + $PYTHON = "Python.exe" +} else { + $PYTHON = $env:PYTHON +} + +if ($env:VENV_DIR -eq "" -or $env:VENV_DIR -eq $null) { + $VENV_DIR = "$PSScriptRoot\venv" +} else { + $VENV_DIR = $env:VENV_DIR +} + +if ($env:LAUNCH_SCRIPT -eq "" -or $env:LAUNCH_SCRIPT -eq $null) { + $LAUNCH_SCRIPT = "$PSScriptRoot\launch.py" +} else { + $LAUNCH_SCRIPT = $env:LAUNCH_SCRIPT +} + +$ERROR_REPORTING = $false + +mkdir tmp 2>$null + +function Start-Venv { + if ($VENV_DIR -eq '-') { + Skip-Venv + } + + if (Test-Path -Path "$VENV_DIR\Scripts\$python") { + Activate-Venv + } else { + $PYTHON_FULLNAME = & $PYTHON -c "import sys; print(sys.executable)" + Write-Output "Creating venv in directory $VENV_DIR using python $PYTHON_FULLNAME" + Invoke-Expression "$PYTHON_FULLNAME -m venv $VENV_DIR > tmp/stdout.txt 2> tmp/stderr.txt" + if ($LASTEXITCODE -eq 0) { + Activate-Venv + } else { + Write-Output "Unable to create venv in directory $VENV_DIR" + } + } +} + +function Activate-Venv { + $PYTHON = "$VENV_DIR\Scripts\Python.exe" + $ACTIVATE = "$VENV_DIR\Scripts\activate.bat" + Invoke-Expression "cmd.exe /c $ACTIVATE" + Write-Output "Venv set to $VENV_DIR." + if ($ACCELERATE -eq 'True') { + Check-Accelerate + } else { + Launch-App + } +} + +function Skip-Venv { + Write-Output "Venv set to $VENV_DIR." + if ($ACCELERATE -eq 'True') { + Check-Accelerate + } else { + Launch-App + } +} + +function Check-Accelerate { + Write-Output 'Checking for accelerate' + $ACCELERATE = "$VENV_DIR\Scripts\accelerate.exe" + if (Test-Path -Path $ACCELERATE) { + Accelerate-Launch + } else { + Launch-App + } +} + +function Launch-App { + Write-Output "Launching with python" + Invoke-Expression "$PYTHON $LAUNCH_SCRIPT" + #pause + exit +} + +function Accelerate-Launch { + Write-Output 'Accelerating' + Invoke-Expression "$ACCELERATE launch --num_cpu_threads_per_process=6 $LAUNCH_SCRIPT" + #pause + exit +} + + +try { + if(Get-Command $PYTHON){ + Start-Venv + } +} Catch { + Write-Output "Couldn't launch python." +} \ No newline at end of file From 99c8ffc6dcecd6c613231b69b45b894d632bd01b Mon Sep 17 00:00:00 2001 From: d8ahazard Date: Wed, 28 Dec 2022 14:27:41 -0600 Subject: [PATCH 2/4] Fix env variables not being passed. For some reason, commandline_args would work, but not torch command. this fixes it so everything sticks. --- webui-user.ps1 | 46 +++++++++++++++++++++++----------------------- 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/webui-user.ps1 b/webui-user.ps1 index 8766d704..10781bb4 100644 --- a/webui-user.ps1 +++ b/webui-user.ps1 @@ -1,43 +1,43 @@ -$env:PYTHON= '' -$env:GIT= '' -$env:VENV_DIR= '' +[Environment]::SetEnvironmentVariable("PYTHON", "") +[Environment]::SetEnvironmentVariable("GIT", "") +[Environment]::SetEnvironmentVariable("VENV_DIR","") -# Commandline arguments for webui.py, for example: $env:COMMANDLINE_ARGS="--medvram --opt-split-attention" -$env:COMMANDLINE_ARGS="" +# Commandline arguments for webui.py, for example: [Environment]::SetEnvironmentVariable("COMMANDLINE_ARGS", "--medvram --opt-split-attention") +[Environment]::SetEnvironmentVariable("COMMANDLINE_ARGS", "") # script to launch to start the app -# $env:LAUNCH_SCRIPT="launch.py" +# [Environment]::SetEnvironmentVariable("LAUNCH_SCRIPT", "launch.py") # install command for torch -# $env:TORCH_COMMAND="pip install torch==1.12.1+cu113 --extra-index-url https://download.pytorch.org/whl/cu113" +# [Environment]::SetEnvironmentVariable("TORCH_COMMAND", "pip install torch==1.12.1+cu113 torchvision==0.13.1+cu113 --extra-index-url https://download.pytorch.org/whl/cu113") # Requirements file to use for stable-diffusion-webui -# $env:REQS_FILE="requirements_versions.txt" +# [Environment]::SetEnvironmentVariable("REQS_FILE", "requirements_versions.txt") -# $env:GFPGAN_PACKAGE="" -# $env:CLIP_PACKAGE="" -# $env:OPENCLIP_PACKAGE="" +# [Environment]::SetEnvironmentVariable("GFPGAN_PACKAGE", "") +# [Environment]::SetEnvironmentVariable("CLIP_PACKAGE", "") +# [Environment]::SetEnvironmentVariable("OPENCLIP_PACKAGE", "") # URL to a WHL if you wish to override default xformers windows -# $env:XFORMERS_WINDOWS_PACKAGE="" +# [Environment]::SetEnvironmentVariable("XFORMERS_WINDOWS_PACKAGE", "") # Uncomment and set to enable an alternate repository URL -# $env:STABLE_DIFFUSION_REPO="" -# $env:TAMING_TRANSFORMERS_REPO="" -# $env:K_DIFFUSION_REPO="" -# $env:CODEFORMER_REPO="" -# $env:BLIP_REPO="" +# [Environment]::SetEnvironmentVariable("STABLE_DIFFUSION_REPO", "") +# [Environment]::SetEnvironmentVariable("TAMING_TRANSFORMERS_REPO", "") +# [Environment]::SetEnvironmentVariable("K_DIFFUSION_REPO", "") +# [Environment]::SetEnvironmentVariable("CODEFORMER_REPO", "") +# [Environment]::SetEnvironmentVariable("BLIP_REPO", "") # Uncomment and set to enable a specific revision of a repository -# $env:STABLE_DIFFUSION_COMMIT_HASH="" -# $env:TAMING_TRANSFORMERS_COMMIT_HASH="" -# $env:K_DIFFUSION_COMMIT_HASH="" -# $env:CODEFORMER_COMMIT_HASH="" -# $env:BLIP_COMMIT_HASH="" +# [Environment]::SetEnvironmentVariable("STABLE_DIFFUSION_COMMIT_HASH", "") +# [Environment]::SetEnvironmentVariable("TAMING_TRANSFORMERS_COMMIT_HASH", "") +# [Environment]::SetEnvironmentVariable("K_DIFFUSION_COMMIT_HASH", "") +# [Environment]::SetEnvironmentVariable("CODEFORMER_COMMIT_HASH", "") +# [Environment]::SetEnvironmentVariable("BLIP_COMMIT_HASH", "") # Uncomment to enable accelerated launch -# $env:ACCELERATE="True" +# [Environment]::SetEnvironmentVariable("ACCELERATE", "True") $SCRIPT = "$PSScriptRoot\webui.ps1" Invoke-Expression "$SCRIPT" From d9b41a787ce4f4aa21ae242f77ec5bc7ad630293 Mon Sep 17 00:00:00 2001 From: d8ahazard Date: Fri, 6 Jan 2023 12:34:06 -0600 Subject: [PATCH 3/4] Handle spaces when creating venv. --- webui.ps1 | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/webui.ps1 b/webui.ps1 index de6916a7..98c01866 100644 --- a/webui.ps1 +++ b/webui.ps1 @@ -28,13 +28,15 @@ function Start-Venv { if (Test-Path -Path "$VENV_DIR\Scripts\$python") { Activate-Venv } else { - $PYTHON_FULLNAME = & $PYTHON -c "import sys; print(sys.executable)" - Write-Output "Creating venv in directory $VENV_DIR using python $PYTHON_FULLNAME" - Invoke-Expression "$PYTHON_FULLNAME -m venv $VENV_DIR > tmp/stdout.txt 2> tmp/stderr.txt" - if ($LASTEXITCODE -eq 0) { - Activate-Venv - } else { + $global:LASTEXITCODE = 0 + try { + $PYTHON_FULLNAME = & $PYTHON -c "import sys; print(sys.executable)" + Write-Output "Creating venv in directory $VENV_DIR using $PYTHON_FULLNAME" + Invoke-Expression "& '$PYTHON_FULLNAME' -m venv $VENV_DIR" + Activate-Venv + } Catch { Write-Output "Unable to create venv in directory $VENV_DIR" + Write-Output $_ } } } From 286497d8cf386a37643e9eefff605a75badf270d Mon Sep 17 00:00:00 2001 From: d8ahazard Date: Fri, 6 Jan 2023 12:34:43 -0600 Subject: [PATCH 4/4] No preference then. --- webui-user.bat | 8 ------ webui.bat | 74 -------------------------------------------------- 2 files changed, 82 deletions(-) delete mode 100644 webui-user.bat delete mode 100644 webui.bat diff --git a/webui-user.bat b/webui-user.bat deleted file mode 100644 index e5a257be..00000000 --- a/webui-user.bat +++ /dev/null @@ -1,8 +0,0 @@ -@echo off - -set PYTHON= -set GIT= -set VENV_DIR= -set COMMANDLINE_ARGS= - -call webui.bat diff --git a/webui.bat b/webui.bat deleted file mode 100644 index d4d626e2..00000000 --- a/webui.bat +++ /dev/null @@ -1,74 +0,0 @@ -@echo off - -if not defined PYTHON (set PYTHON=python) -if not defined VENV_DIR (set VENV_DIR=venv) - -set ERROR_REPORTING=FALSE - -mkdir tmp 2>NUL - -%PYTHON% -c "" >tmp/stdout.txt 2>tmp/stderr.txt -if %ERRORLEVEL% == 0 goto :start_venv -echo Couldn't launch python -goto :show_stdout_stderr - -:start_venv -if [%VENV_DIR%] == [-] goto :skip_venv - -dir %VENV_DIR%\Scripts\Python.exe >tmp/stdout.txt 2>tmp/stderr.txt -if %ERRORLEVEL% == 0 goto :activate_venv - -for /f "delims=" %%i in ('CALL %PYTHON% -c "import sys; print(sys.executable)"') do set PYTHON_FULLNAME="%%i" -echo Creating venv in directory %VENV_DIR% using python %PYTHON_FULLNAME% -%PYTHON_FULLNAME% -m venv %VENV_DIR% >tmp/stdout.txt 2>tmp/stderr.txt -if %ERRORLEVEL% == 0 goto :activate_venv -echo Unable to create venv in directory %VENV_DIR% -goto :show_stdout_stderr - -:activate_venv -set PYTHON="%~dp0%VENV_DIR%\Scripts\Python.exe" -echo venv %PYTHON% -if [%ACCELERATE%] == ["True"] goto :accelerate -goto :launch - -:skip_venv - -:accelerate -echo "Checking for accelerate" -set ACCELERATE="%~dp0%VENV_DIR%\Scripts\accelerate.exe" -if EXIST %ACCELERATE% goto :accelerate_launch - -:launch -%PYTHON% launch.py %* -pause -exit /b - -:accelerate_launch -echo "Accelerating" -%ACCELERATE% launch --num_cpu_threads_per_process=6 launch.py -pause -exit /b - -:show_stdout_stderr - -echo. -echo exit code: %errorlevel% - -for /f %%i in ("tmp\stdout.txt") do set size=%%~zi -if %size% equ 0 goto :show_stderr -echo. -echo stdout: -type tmp\stdout.txt - -:show_stderr -for /f %%i in ("tmp\stderr.txt") do set size=%%~zi -if %size% equ 0 goto :show_stderr -echo. -echo stderr: -type tmp\stderr.txt - -:endofscript - -echo. -echo Launch unsuccessful. Exiting. -pause