Spaces:
Sleeping
Sleeping
File size: 6,414 Bytes
0c0ff58 18f4c73 0c0ff58 18f4c73 0c0ff58 18f4c73 0c0ff58 18f4c73 0c0ff58 18f4c73 0c0ff58 18f4c73 0c0ff58 18f4c73 0c0ff58 18f4c73 0c0ff58 18f4c73 0c0ff58 18f4c73 0c0ff58 18f4c73 0c0ff58 18f4c73 0c0ff58 18f4c73 0c0ff58 18f4c73 0c0ff58 18f4c73 0c0ff58 18f4c73 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
@echo off
chcp 65001
set PYTHONUTF8=1
:: The original source of the webui.bat file is stable-diffusion-webui
:: Modified and enhanced by Gemini with features for venv management and requirements handling.
:: --------- Configuration ---------
set COMMANDLINE_ARGS=
:: Define the application directory (folder name)
:: Leave empty if the app is in the root directory.
set APP_DIR=
:: Define the name of the Launch application
set APPLICATION_NAME=app.py
:: Define the requirements filename, default is requirements.txt
set REQUIREMENTS_FILE=requirements.txt
:: Define the name of the virtual environment directory
set VENV_NAME=venv
:: Set to 1 to always attempt to update packages from requirements.txt on every launch
set ALWAYS_UPDATE_REQS=1
:: ---------------------------------
:: --------- Path Setup Logic ---------
:: Logic to handle paths based on whether APP_DIR is set
if defined APP_DIR (
set "TARGET_REQ=%~dp0%APP_DIR%\%REQUIREMENTS_FILE%"
set "TARGET_SCRIPT=%~dp0%APP_DIR%\%APPLICATION_NAME%"
echo Working in subdirectory: %APP_DIR%
) else (
set "TARGET_REQ=%~dp0%REQUIREMENTS_FILE%"
set "TARGET_SCRIPT=%~dp0%APPLICATION_NAME%"
echo Working in root directory.
)
:: ------------------------------------
:: Set PYTHON executable if not already defined
if not defined PYTHON (set PYTHON=python)
:: Set VENV_DIR using VENV_NAME if not already defined
if not defined VENV_DIR (set "VENV_DIR=%~dp0%VENV_NAME%")
mkdir tmp 2>NUL
:: Check if Python is callable
%PYTHON% -c "" >tmp/stdout.txt 2>tmp/stderr.txt
if %ERRORLEVEL% == 0 goto :check_pip
echo Couldn't launch python
goto :show_stdout_stderr
:check_pip
:: Check if pip is available
%PYTHON% -mpip --help >tmp/stdout.txt 2>tmp/stderr.txt
if %ERRORLEVEL% == 0 goto :start_venv
:: If pip is not available and PIP_INSTALLER_LOCATION is set, try to install pip
if "%PIP_INSTALLER_LOCATION%" == "" goto :show_stdout_stderr
%PYTHON% "%PIP_INSTALLER_LOCATION%" >tmp/stdout.txt 2>tmp/stderr.txt
if %ERRORLEVEL% == 0 goto :start_venv
echo Couldn't install pip
goto :show_stdout_stderr
:start_venv
:: Skip venv creation/activation if VENV_DIR is explicitly set to "-"
if ["%VENV_DIR%"] == ["-"] goto :skip_venv_entirely
:: Skip venv creation/activation if SKIP_VENV is set to "1"
if ["%SKIP_VENV%"] == ["1"] goto :skip_venv_entirely
:: Check if the venv already exists by looking for Python.exe in its Scripts directory
dir "%VENV_DIR%\Scripts\Python.exe" >tmp/stdout.txt 2>tmp/stderr.txt
if %ERRORLEVEL% == 0 goto :activate_venv_and_maybe_update
:: Venv does not exist, create it
echo Virtual environment not found in "%VENV_DIR%". Creating a new one.
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% NEQ 0 (
echo Unable to create venv in directory "%VENV_DIR%"
goto :show_stdout_stderr
)
echo Venv created.
:: Install requirements for the first time if venv was just created
:: This section handles the initial installation of packages from requirements.txt
:: immediately after a new virtual environment is created.
echo Checking for %REQUIREMENTS_FILE% for initial setup...
if exist "%TARGET_REQ%" (
echo Found %REQUIREMENTS_FILE% at "%TARGET_REQ%", attempting to install for initial setup...
call "%VENV_DIR%\Scripts\activate.bat"
echo Installing packages from %REQUIREMENTS_FILE% ^(initial setup^)...
"%VENV_DIR%\Scripts\python.exe" -m pip install -r "%TARGET_REQ%"
if %ERRORLEVEL% NEQ 0 (
echo Failed to install requirements during initial setup. Please check the output above.
pause
goto :show_stdout_stderr_custom_pip_initial
)
echo Initial requirements installed successfully.
call "%VENV_DIR%\Scripts\deactivate.bat"
) else (
echo No %REQUIREMENTS_FILE% found at "%TARGET_REQ%", skipping package installation.
)
goto :activate_venv_and_maybe_update
:activate_venv_and_maybe_update
:: This label is reached if the venv exists or was just created.
:: Set PYTHON to point to the venv's Python interpreter.
set PYTHON="%VENV_DIR%\Scripts\Python.exe"
echo Activating venv: %PYTHON%
:: Always update requirements if ALWAYS_UPDATE_REQS is 1
:: This section allows for updating packages from requirements.txt on every launch
:: if the ALWAYS_UPDATE_REQS variable is set to 1.
if defined ALWAYS_UPDATE_REQS (
if "%ALWAYS_UPDATE_REQS%"=="1" (
echo ALWAYS_UPDATE_REQS is enabled.
if exist "%TARGET_REQ%" (
echo Attempting to update packages from "%TARGET_REQ%"...
REM No need to call activate.bat here again, PYTHON is already set to the venv's python
%PYTHON% -m pip install -r "%TARGET_REQ%"
if %ERRORLEVEL% NEQ 0 (
echo Failed to update requirements. Please check the output above.
pause
goto :endofscript
)
echo Requirements updated successfully.
) else (
echo ALWAYS_UPDATE_REQS is enabled, but no %REQUIREMENTS_FILE% found. Skipping update.
)
) else (
echo ALWAYS_UPDATE_REQS is not enabled or not set to 1. Skipping routine update.
)
)
goto :launch
:skip_venv_entirely
:: This label is reached if venv usage is explicitly skipped.
echo Skipping venv.
goto :launch
:launch
:: Launch the main application
echo Launching Web UI with arguments: %COMMANDLINE_ARGS% %*
echo Script path: %TARGET_SCRIPT%
%PYTHON% "%TARGET_SCRIPT%" %COMMANDLINE_ARGS% %*
echo Launch finished.
pause
exit /b
:show_stdout_stderr_custom_pip_initial
:: Custom error handler for failures during the initial pip install process.
echo.
echo exit code ^(pip initial install^): %errorlevel%
echo Errors during initial pip install. See output above.
echo.
echo Launch unsuccessful. Exiting.
pause
exit /b
:show_stdout_stderr
:: General error handler: displays stdout and stderr from the tmp directory.
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 :endofscript
echo.
echo stderr:
type tmp\stderr.txt
:endofscript
echo.
echo Launch unsuccessful. Exiting.
pause
exit /b |