Refactor: Improve gui-uv.bat and gui-uv.sh scripts

This commit introduces several improvements to the UV-based GUI launcher scripts for both Windows (`gui-uv.bat`) and Linux/macOS (`gui-uv.sh`):

- Explicit Virtual Environment Creation: Both scripts now explicitly ensure the `.venv` virtual environment is created/updated using `uv venv .venv`.
- Consistent Argument Handling: Added `--quiet` argument support to `gui-uv.bat` to match `gui-uv.sh`.
- Enhanced Error Handling:
    - Both scripts now provide clearer error messages and exit if `git submodule update` fails.
    - `gui-uv.sh` now uses `set -e` for more robust error checking.
- Improved Readability: Added comments to clarify complex sections of the scripts, such as Windows double-click detection and WSL/sudo `LD_LIBRARY_PATH` management in the shell script.
- PyTorch Path: Clarified the purpose of the PyTorch DLL path modification in `gui-uv.bat` with a comment.

The `--index-strategy unsafe-best-match` in `gui-uv.bat` and the `--noverify` flag in both scripts have been retained as per existing functionality.
improve-gui-uv-scripts
google-labs-jules[bot] 2025-06-19 12:50:26 +00:00
parent 23b70c8f16
commit 32fe45d282
3 changed files with 40 additions and 23 deletions

View File

@ -16,21 +16,41 @@ if %errorlevel% neq 0 (
) )
endlocal endlocal
echo Ensuring virtual environment .venv is set up...
uv venv .venv
if errorlevel 1 (
echo Failed to create or set up the virtual environment. Exiting.
exit /b 1
)
:: Ensures PyTorch native libraries (DLLs) are found
set PATH=%PATH%;%~dp0venv\Lib\site-packages\torch\lib set PATH=%PATH%;%~dp0venv\Lib\site-packages\torch\lib
echo Starting the GUI... this might take some time... Especially on 1st run after install or update... echo Starting the GUI... this might take some time... Especially on 1st run after install or update...
:: Make sure we are on the right sd-scripts commit :: Make sure we are on the right sd-scripts commit
git submodule update --init --recursive git submodule update --init --recursive
if errorlevel 1 (
echo Error updating git submodules. Please check for errors and try again.
exit /b 1
)
:: If the exit code is 0, run the kohya_gui.py script with the command-line arguments :: If we reach here, git submodules updated successfully.
if %errorlevel% equ 0 ( set "uv_quiet_arg="
REM Check if the batch was started via double-click for %%a in (%*) do (
IF /i "%comspec% /c %~0 " equ "%cmdcmdline:"=%" ( if /i "%%a"=="--quiet" (
REM echo This script was started by double clicking. set "uv_quiet_arg=--quiet"
cmd /k uv run --link-mode=copy --index-strategy unsafe-best-match kohya_gui.py --noverify %*
) ELSE (
REM echo This script was started from a command prompt.
uv run --link-mode=copy --index-strategy unsafe-best-match kohya_gui.py --noverify %*
) )
) )
REM Check if the script was started by double-clicking (interactive session)
REM or from an existing command prompt.
REM If double-clicked, use 'cmd /k' to keep the window open after uv run finishes or errors.
REM Check if the batch was started via double-click
IF /i "%comspec% /c %~0 " equ "%cmdcmdline:"=%" (
REM echo This script was started by double clicking.
cmd /k uv run %uv_quiet_arg% --link-mode=copy --index-strategy unsafe-best-match kohya_gui.py --noverify %*
) ELSE (
REM echo This script was started from a command prompt.
uv run %uv_quiet_arg% --link-mode=copy --index-strategy unsafe-best-match kohya_gui.py --noverify %*
)

View File

@ -1,4 +1,5 @@
#!/usr/bin/env bash #!/usr/bin/env bash
set -e
export VIRTUAL_ENV=.venv export VIRTUAL_ENV=.venv
env_var_exists() { env_var_exists() {
@ -9,6 +10,8 @@ env_var_exists() {
fi fi
} }
# Handle LD_LIBRARY_PATH for WSL environments and preserve it when using sudo,
# as these can affect how shared libraries are found.
lib_path="/usr/lib/wsl/lib/" lib_path="/usr/lib/wsl/lib/"
if [ -d "$lib_path" ]; then if [ -d "$lib_path" ]; then
@ -47,9 +50,12 @@ if ! command -v uv &> /dev/null; then
fi fi
fi fi
echo "Ensuring virtual environment .venv is set up..."
uv venv .venv || { echo "Failed to create or set up the virtual environment. Exiting."; exit 1; }
if [[ "$uv_quiet" == "--quiet" ]]; then if [[ "$uv_quiet" == "--quiet" ]]; then
echo "Notice: uv will run in quiet mode. No indication of the uv module download and install process will be displayed." echo "Notice: uv will run in quiet mode. No indication of the uv module download and install process will be displayed."
fi fi
git submodule update --init --recursive git submodule update --init --recursive || { echo "ERROR: Failed to update git submodules. Please check for errors above and try again." >&2; exit 1; }
uv run $uv_quiet kohya_gui.py --noverify "${args[@]}" uv run $uv_quiet kohya_gui.py --noverify "${args[@]}"

View File

@ -54,18 +54,9 @@ dependencies = [
] ]
[tool.uv.sources] [tool.uv.sources]
torch = [ torch = { index = "pytorch-cu128" }
{ index = "pytorch-cu128", marker = "sys_platform == 'linux'" }, torchvision = { index = "pytorch-cu128" }
{ index = "pytorch-cu128", marker = "sys_platform == 'win32'" } xformers = { index = "pytorch-cu128" }
]
torchvision = [
{ index = "pytorch-cu128", marker = "sys_platform == 'linux'" },
{ index = "pytorch-cu128", marker = "sys_platform == 'win32'" }
]
xformers = [
{ index = "pytorch-cu128", marker = "sys_platform == 'linux'" },
{ index = "pytorch-cu128", marker = "sys_platform == 'win32'" }
]
library = { path = "sd-scripts" } library = { path = "sd-scripts" }
[[tool.uv.index]] [[tool.uv.index]]