From b27d0694c4fcca63ac7cb6b375c95ddbf2666ebe Mon Sep 17 00:00:00 2001 From: bmaltais Date: Tue, 12 Mar 2024 20:31:22 -0400 Subject: [PATCH] Improve setup. Add --headless option --- setup-runpod.sh | 4 - setup.bat | 18 +---- setup.ps1 | 15 +--- setup.sh | 4 - setup/setup_common.py | 44 +++-------- setup/setup_linux.py | 2 +- setup/setup_runpod.py | 2 +- setup/setup_windows.py | 161 ++++++++++++++++++++++------------------- 8 files changed, 99 insertions(+), 151 deletions(-) diff --git a/setup-runpod.sh b/setup-runpod.sh index 56052cc..4654248 100755 --- a/setup-runpod.sh +++ b/setup-runpod.sh @@ -21,10 +21,6 @@ fi echo "Activating venv..." source "$SCRIPT_DIR/venv/bin/activate" || exit 1 -# Install packaging -echo "Installing the python packaging module..." -pip install packaging - # Run setup_linux.py script with platform requirements echo "Running setup_linux.py..." python "$SCRIPT_DIR/setup/setup_linux.py" --platform-requirements-file=requirements_runpod.txt --show_stdout --no_run_accelerate diff --git a/setup.bat b/setup.bat index 79ff65c..2b95e27 100644 --- a/setup.bat +++ b/setup.bat @@ -1,13 +1,5 @@ @echo off -set PYTHON_VER=3.10.9 - -:: Check if Python version meets the recommended version -python --version 2>nul | findstr /b /c:"Python %PYTHON_VER%" >nul -if errorlevel 1 ( - echo Warning: Python version %PYTHON_VER% is required. Kohya_ss GUI will most likely fail to run. -) - IF NOT EXIST venv ( echo Creating venv... python -m venv venv @@ -16,24 +8,18 @@ IF NOT EXIST venv ( :: Create the directory if it doesn't exist mkdir ".\logs\setup" > nul 2>&1 -:: Deactivate the virtual environment +:: Deactivate the virtual environment to prevent error call .\venv\Scripts\deactivate.bat -:: Calling external python program to check for local modules -:: python .\setup\check_local_modules.py - call .\venv\Scripts\activate.bat -echo "Installing packaging python module..." -pip install packaging - 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 python .\setup\setup_windows.py ) ELSE ( REM echo This script was started from a command prompt. - python .\setup\setup_windows.py + python .\setup\setup_windows.py %* ) :: Deactivate the virtual environment diff --git a/setup.ps1 b/setup.ps1 index 5459b5f..8237643 100644 --- a/setup.ps1 +++ b/setup.ps1 @@ -1,10 +1,3 @@ - -# Check if Python version meets the recommended version -$pythonVersion = & .\venv\Scripts\python.exe --version 2>$null -if ($pythonVersion -notmatch "^Python $PYTHON_VER") { - Write-Host "Warning: Python version $PYTHON_VER is recommended." -} - if (-not (Test-Path -Path "venv")) { Write-Host "Creating venv..." python -m venv venv @@ -16,15 +9,9 @@ $null = New-Item -ItemType Directory -Force -Path ".\logs\setup" # Deactivate the virtual environment & .\venv\Scripts\deactivate.bat -# Calling external python program to check for local modules -# & .\venv\Scripts\python.exe .\setup\check_local_modules.py - & .\venv\Scripts\activate.bat -Write-Host "Installing python packaging module..." -& pip install packaging - -& .\venv\Scripts\python.exe .\setup\setup_windows.py +& .\venv\Scripts\python.exe .\setup\setup_windows.py $args # Deactivate the virtual environment & .\venv\Scripts\deactivate.bat diff --git a/setup.sh b/setup.sh index 70db7a1..3df651d 100755 --- a/setup.sh +++ b/setup.sh @@ -201,10 +201,6 @@ install_python_dependencies() { source "$DIR/venv/bin/activate" fi - # Install packaging - echo "Installing the python packaging module..." - pip install packaging - case "$OSTYPE" in "lin"*) if [ "$RUNPOD" = true ]; then diff --git a/setup/setup_common.py b/setup/setup_common.py index 52f4894..15173bd 100644 --- a/setup/setup_common.py +++ b/setup/setup_common.py @@ -2,16 +2,11 @@ import subprocess import os import re import sys -import filecmp import logging import shutil -import sysconfig import datetime -import platform import pkg_resources -from packaging import version - errors = 0 # Define the 'errors' variable before using it log = logging.getLogger('sd') @@ -25,6 +20,8 @@ def check_python_version(): min_version = (3, 10, 9) max_version = (3, 11, 0) + from packaging import version + try: current_version = sys.version_info log.info(f"Python version is {sys.version}") @@ -584,42 +581,19 @@ def ensure_base_requirements(): import rich # pylint: disable=unused-import except ImportError: install('--upgrade rich', 'rich') + + try: + import packaging + except ImportError: + install('packaging') def run_cmd(run_cmd): try: subprocess.run(run_cmd, shell=True, check=False, env=os.environ) except subprocess.CalledProcessError as e: - print(f'Error occurred while running command: {run_cmd}') - print(f'Error: {e}') - - -# check python version -def check_python(ignore=True, skip_git=False): - # - # This function was adapted from code written by vladimandic: https://github.com/vladmandic/automatic/commits/master - # - - supported_minors = [9, 10] - log.info(f'Python {platform.python_version()} on {platform.system()}') - if not ( - int(sys.version_info.major) == 3 - and int(sys.version_info.minor) in supported_minors - ): - log.error( - f'Incompatible Python version: {sys.version_info.major}.{sys.version_info.minor}.{sys.version_info.micro} required 3.{supported_minors}' - ) - if not ignore: - sys.exit(1) - if not skip_git: - git_cmd = os.environ.get('GIT', 'git') - if shutil.which(git_cmd) is None: - log.error('Git not found') - if not ignore: - sys.exit(1) - else: - git_version = git('--version', folder=None, ignore=False) - log.debug(f'Git {git_version.replace("git version", "").strip()}') + log.error(f'Error occurred while running command: {run_cmd}') + log.error(f'Error: {e}') def delete_file(file_path): diff --git a/setup/setup_linux.py b/setup/setup_linux.py index 6d35cdb..9698382 100644 --- a/setup/setup_linux.py +++ b/setup/setup_linux.py @@ -15,7 +15,7 @@ def main_menu(platform_requirements_file, show_stdout: bool = False, no_run_acce log.info("If this operation ever runs too long, you can rerun this script in verbose mode to check.") setup_common.check_repo_version() - setup_common.check_python() + # setup_common.check_python() # Upgrade pip if needed setup_common.install('pip') diff --git a/setup/setup_runpod.py b/setup/setup_runpod.py index ee57076..3cf38ce 100644 --- a/setup/setup_runpod.py +++ b/setup/setup_runpod.py @@ -50,7 +50,7 @@ def main_menu(platform_requirements_file): log.info("If this operation ever runs too long, you can rerun this script in verbose mode to check.") setup_common.check_repo_version() - setup_common.check_python() + # setup_common.check_python() # Upgrade pip if needed setup_common.install('pip') diff --git a/setup/setup_windows.py b/setup/setup_windows.py index ab766d7..13a1697 100644 --- a/setup/setup_windows.py +++ b/setup/setup_windows.py @@ -5,6 +5,7 @@ import logging import shutil import sysconfig import setup_common +import argparse errors = 0 # Define the 'errors' variable before using it log = logging.getLogger("sd") @@ -105,9 +106,9 @@ def sync_bits_and_bytes_files(): log.error(f"An unexpected error occurred: {e}") -def install_kohya_ss_torch2(): +def install_kohya_ss_torch2(headless: bool = False): setup_common.check_repo_version() - setup_common.check_python() + setup_common.check_python_version() setup_common.update_submodule() @@ -117,12 +118,9 @@ def install_kohya_ss_torch2(): setup_common.install_requirements( "requirements_windows_torch2.txt", check_no_verify_flag=False ) - - # sync_bits_and_bytes_files() - setup_common.configure_accelerate(run_accelerate=True) - - # run_cmd(f'accelerate config') + if not headless: + setup_common.configure_accelerate(run_accelerate=True) def install_bitsandbytes_0_35_0(): @@ -158,79 +156,90 @@ def install_bitsandbytes_0_41_2(): reinstall=True, ) -def main_menu(): - setup_common.clear_screen() - while True: - print("\nKohya_ss GUI setup menu:\n") - print("1. Install kohya_ss gui") - print("2. (Optional) Install cudnn files (if you want to use latest supported cudnn version)") - print("3. (Optional) Install specific bitsandbytes versions") - print("4. (Optional) Manually configure accelerate") - print("5. (Optional) Start Kohya_ss GUI in browser") - print("6. Quit") +def main_menu(headless: bool = False): + setup_common.ensure_base_requirements() + + if headless: + install_kohya_ss_torch2(headless=headless) + else: + setup_common.clear_screen() + while True: + print("\nKohya_ss GUI setup menu:\n") + print("1. Install kohya_ss gui") + print("2. (Optional) Install cudnn files (if you want to use latest supported cudnn version)") + print("3. (Optional) Install specific bitsandbytes versions") + print("4. (Optional) Manually configure accelerate") + print("5. (Optional) Start Kohya_ss GUI in browser") + print("6. Quit") - choice = input("\nEnter your choice: ") - print("") + choice = input("\nEnter your choice: ") + print("") - if choice == "1": - install_kohya_ss_torch2() - elif choice == "2": - cudnn_install() - elif choice == "3": - while True: - print("1. (Optional) Force installation of bitsandbytes 0.35.0") - print( - "2. (Optional) Force installation of bitsandbytes 0.40.1 for new optimizer options support and pre-bugfix results" - ) - print( - "3. (Optional) Force installation of bitsandbytes 0.41.1 for new optimizer options support" - ) - print( - "4. (Recommended) Force installation of bitsandbytes 0.41.2 for new optimizer options support" - ) - print( - "5. (Danger) Install bitsandbytes-windows (this package has been reported to cause issues for most... avoid...)" - ) - print("6. Exit") - choice_torch = input("\nEnter your choice: ") - print("") - - if choice_torch == "1": - install_bitsandbytes_0_35_0() - break - elif choice_torch == "2": - install_bitsandbytes_0_40_1() - break - elif choice_torch == "3": - install_bitsandbytes_0_41_1() - break - elif choice_torch == "3": - install_bitsandbytes_0_41_2() - break - elif choice_torch == "5": - setup_common.install( - "--upgrade bitsandbytes-windows", reinstall=True + if choice == "1": + install_kohya_ss_torch2() + elif choice == "2": + cudnn_install() + elif choice == "3": + while True: + print("1. (Optional) Force installation of bitsandbytes 0.35.0") + print( + "2. (Optional) Force installation of bitsandbytes 0.40.1 for new optimizer options support and pre-bugfix results" ) - break - elif choice_torch == "6": - break - else: - print("Invalid choice. Please enter a number between 1-3.") - elif choice == "4": - setup_common.run_cmd("accelerate config") - elif choice == "5": - subprocess.Popen( - "start cmd /k .\gui.bat --inbrowser", shell=True - ) # /k keep the terminal open on quit. /c would close the terminal instead - elif choice == "6": - print("Quitting the program.") - break - else: - print("Invalid choice. Please enter a number between 1-5.") + print( + "3. (Optional) Force installation of bitsandbytes 0.41.1 for new optimizer options support" + ) + print( + "4. (Recommended) Force installation of bitsandbytes 0.41.2 for new optimizer options support" + ) + print( + "5. (Danger) Install bitsandbytes-windows (this package has been reported to cause issues for most... avoid...)" + ) + print("6. Exit") + choice_torch = input("\nEnter your choice: ") + print("") + + if choice_torch == "1": + install_bitsandbytes_0_35_0() + break + elif choice_torch == "2": + install_bitsandbytes_0_40_1() + break + elif choice_torch == "3": + install_bitsandbytes_0_41_1() + break + elif choice_torch == "3": + install_bitsandbytes_0_41_2() + break + elif choice_torch == "5": + setup_common.install( + "--upgrade bitsandbytes-windows", reinstall=True + ) + break + elif choice_torch == "6": + break + else: + print("Invalid choice. Please enter a number between 1-3.") + elif choice == "4": + setup_common.run_cmd("accelerate config") + elif choice == "5": + subprocess.Popen( + "start cmd /k .\gui.bat --inbrowser", shell=True + ) # /k keep the terminal open on quit. /c would close the terminal instead + elif choice == "6": + print("Quitting the program.") + break + else: + print("Invalid choice. Please enter a number between 1-5.") if __name__ == "__main__": - python_ver = setup_common.check_python_version() - setup_common.ensure_base_requirements() setup_common.setup_logging() - main_menu() + + # Setup argument parser + parser = argparse.ArgumentParser(description="Your Script Description") + parser.add_argument('--headless', action='store_true', help='Run in headless mode') + + # Parse arguments + args = parser.parse_args() + + main_menu(headless=args.headless)