avans06/SeedVR2_Image_upscaler
8
1@echo off2 3chcp 650014set PYTHONUTF8=15:: The original source of the webui.bat file is stable-diffusion-webui6:: Modified and enhanced by Gemini with features for venv management and requirements handling.7 8:: --------- Configuration ---------9set COMMANDLINE_ARGS=10 11:: Define the application directory (folder name)12:: Leave empty if the app is in the root directory.13set APP_DIR=14 15:: Define the name of the Launch application16set APPLICATION_NAME=app.py17 18:: Define the requirements filename, default is requirements.txt19set REQUIREMENTS_FILE=requirements.txt20 21:: Define the name of the virtual environment directory22set VENV_NAME=venv23 24:: Set to 1 to always attempt to update packages from requirements.txt on every launch25set ALWAYS_UPDATE_REQS=126:: ---------------------------------27 28:: --------- Path Setup Logic ---------29:: Logic to handle paths based on whether APP_DIR is set30if defined APP_DIR (31 set "TARGET_REQ=%~dp0%APP_DIR%\%REQUIREMENTS_FILE%"32 set "TARGET_SCRIPT=%~dp0%APP_DIR%\%APPLICATION_NAME%"33 echo Working in subdirectory: %APP_DIR%34) else (35 set "TARGET_REQ=%~dp0%REQUIREMENTS_FILE%"36 set "TARGET_SCRIPT=%~dp0%APPLICATION_NAME%"37 echo Working in root directory.38)39:: ------------------------------------40 41:: Set PYTHON executable if not already defined42if not defined PYTHON (set PYTHON=python)43:: Set VENV_DIR using VENV_NAME if not already defined44if not defined VENV_DIR (set "VENV_DIR=%~dp0%VENV_NAME%")45 46mkdir tmp 2>NUL47 48:: Check if Python is callable49%PYTHON% -c "" >tmp/stdout.txt 2>tmp/stderr.txt50if %ERRORLEVEL% == 0 goto :check_pip51echo Couldn't launch python52goto :show_stdout_stderr53 54:check_pip55:: Check if pip is available56%PYTHON% -mpip --help >tmp/stdout.txt 2>tmp/stderr.txt57if %ERRORLEVEL% == 0 goto :start_venv58:: If pip is not available and PIP_INSTALLER_LOCATION is set, try to install pip59if "%PIP_INSTALLER_LOCATION%" == "" goto :show_stdout_stderr60%PYTHON% "%PIP_INSTALLER_LOCATION%" >tmp/stdout.txt 2>tmp/stderr.txt61if %ERRORLEVEL% == 0 goto :start_venv62echo Couldn't install pip63goto :show_stdout_stderr64 65:start_venv66:: Skip venv creation/activation if VENV_DIR is explicitly set to "-"67if ["%VENV_DIR%"] == ["-"] goto :skip_venv_entirely68:: Skip venv creation/activation if SKIP_VENV is set to "1"69if ["%SKIP_VENV%"] == ["1"] goto :skip_venv_entirely70 71:: Check if the venv already exists by looking for Python.exe in its Scripts directory72dir "%VENV_DIR%\Scripts\Python.exe" >tmp/stdout.txt 2>tmp/stderr.txt73if %ERRORLEVEL% == 0 goto :activate_venv_and_maybe_update74 75:: Venv does not exist, create it76echo Virtual environment not found in "%VENV_DIR%". Creating a new one.77for /f "delims=" %%i in ('CALL %PYTHON% -c "import sys; print(sys.executable)"') do set PYTHON_FULLNAME="%%i"78echo Creating venv in directory %VENV_DIR% using python %PYTHON_FULLNAME%79%PYTHON_FULLNAME% -m venv "%VENV_DIR%" >tmp/stdout.txt 2>tmp/stderr.txt80if %ERRORLEVEL% NEQ 0 (81 echo Unable to create venv in directory "%VENV_DIR%"82 goto :show_stdout_stderr83)84echo Venv created.85 86:: Install requirements for the first time if venv was just created87:: This section handles the initial installation of packages from requirements.txt88:: immediately after a new virtual environment is created.89echo Checking for %REQUIREMENTS_FILE% for initial setup...90if exist "%TARGET_REQ%" (91 echo Found %REQUIREMENTS_FILE% at "%TARGET_REQ%", attempting to install for initial setup...92 call "%VENV_DIR%\Scripts\activate.bat"93 echo Installing packages from %REQUIREMENTS_FILE% ^(initial setup^)...94 "%VENV_DIR%\Scripts\python.exe" -m pip install -r "%TARGET_REQ%"95 if %ERRORLEVEL% NEQ 0 (96 echo Failed to install requirements during initial setup. Please check the output above.97 pause98 goto :show_stdout_stderr_custom_pip_initial 99 )100 echo Initial requirements installed successfully.101 call "%VENV_DIR%\Scripts\deactivate.bat"102) else (103 echo No %REQUIREMENTS_FILE% found at "%TARGET_REQ%", skipping package installation.104)105goto :activate_venv_and_maybe_update106 107 108:activate_venv_and_maybe_update109:: This label is reached if the venv exists or was just created.110:: Set PYTHON to point to the venv's Python interpreter.111set PYTHON="%VENV_DIR%\Scripts\Python.exe"112echo Activating venv: %PYTHON%113 114:: Always update requirements if ALWAYS_UPDATE_REQS is 1115:: This section allows for updating packages from requirements.txt on every launch116:: if the ALWAYS_UPDATE_REQS variable is set to 1.117if defined ALWAYS_UPDATE_REQS (118 if "%ALWAYS_UPDATE_REQS%"=="1" (119 echo ALWAYS_UPDATE_REQS is enabled.120 if exist "%TARGET_REQ%" (121 echo Attempting to update packages from "%TARGET_REQ%"...122 REM No need to call activate.bat here again, PYTHON is already set to the venv's python123 %PYTHON% -m pip install -r "%TARGET_REQ%"124 if %ERRORLEVEL% NEQ 0 (125 echo Failed to update requirements. Please check the output above.126 pause127 goto :endofscript 128 )129 echo Requirements updated successfully.130 ) else (131 echo ALWAYS_UPDATE_REQS is enabled, but no %REQUIREMENTS_FILE% found. Skipping update.132 )133 ) else (134 echo ALWAYS_UPDATE_REQS is not enabled or not set to 1. Skipping routine update.135 )136)137 138goto :launch139 140:skip_venv_entirely141:: This label is reached if venv usage is explicitly skipped.142echo Skipping venv.143goto :launch144 145:launch146:: Launch the main application147echo Launching Web UI with arguments: %COMMANDLINE_ARGS% %*148echo Script path: %TARGET_SCRIPT%149%PYTHON% "%TARGET_SCRIPT%" %COMMANDLINE_ARGS% %*150echo Launch finished.151pause152exit /b153 154:show_stdout_stderr_custom_pip_initial155:: Custom error handler for failures during the initial pip install process.156echo.157echo exit code ^(pip initial install^): %errorlevel%158echo Errors during initial pip install. See output above.159echo.160echo Launch unsuccessful. Exiting.161pause162exit /b163 164 165:show_stdout_stderr166:: General error handler: displays stdout and stderr from the tmp directory.167echo.168echo exit code: %errorlevel%169 170for /f %%i in ("tmp\stdout.txt") do set size=%%~zi171if %size% equ 0 goto :show_stderr172echo.173echo stdout:174type tmp\stdout.txt175 176:show_stderr177for /f %%i in ("tmp\stderr.txt") do set size=%%~zi178if %size% equ 0 goto :endofscript179echo.180echo stderr:181type tmp\stderr.txt182 183:endofscript184echo.185echo Launch unsuccessful. Exiting.186pause187exit /b