From 7f436061b8d1fe2a437f20215208a66356ced36f Mon Sep 17 00:00:00 2001 From: JeLuF Date: Tue, 15 Nov 2022 23:29:32 +0100 Subject: [PATCH 1/9] =?UTF-8?q?=E2=9D=93RFC:=20how=20to=20handle=20%TMP%?= =?UTF-8?q?=20and=20%TEMP%=20-=20UNTESTED?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `conda activate` fails if %TMP% or %TEMP% contains spaces. So instead of setting the temp variables in the installer steps, we need to set it at the beginning of the script. Would this break anything? https://discord.com/channels/1014774730907209781/1042190192049668106/1042190192049668106 --- scripts/on_sd_start.bat | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/scripts/on_sd_start.bat b/scripts/on_sd_start.bat index 48894771..b4c4f69f 100644 --- a/scripts/on_sd_start.bat +++ b/scripts/on_sd_start.bat @@ -1,5 +1,8 @@ @echo off +@REM Caution, this file will make your eyes and brain bleed. It's such an unholy mess. +@REM Note to self: Please rewrite this in Python. For the sake of your own sanity. + @copy sd-ui-files\scripts\on_env_start.bat scripts\ /Y @copy sd-ui-files\scripts\bootstrap.bat scripts\ /Y @@ -7,11 +10,17 @@ if exist "%cd%\profile" ( set USERPROFILE=%cd%\profile ) +@mkdir tmp +@set TMP=%cd%\tmp +@set TEMP=%cd%\tmp + @rem activate the installer env call conda activate - -@REM Caution, this file will make your eyes and brain bleed. It's such an unholy mess. -@REM Note to self: Please rewrite this in Python. For the sake of your own sanity. +@if "%ERRORLEVEL%" NEQ "0" ( + @echo. & echo "Error activating conda for Stable Diffusion. Sorry about that, please try to:" & echo " 1. Run this installer again." & echo " 2. If that doesn't fix it, please try the common troubleshooting steps at https://github.com/cmdr2/stable-diffusion-ui/wiki/Troubleshooting" & echo " 3. If those steps don't help, please copy *all* the error messages in this window, and ask the community at https://discord.com/invite/u9yhsFmEkB" & echo " 4. If that doesn't solve the problem, please file an issue at https://github.com/cmdr2/stable-diffusion-ui/issues" & echo "Thanks!" & echo. + pause + exit /b +) @REM remove the old version of the dev console script, if it's still present if exist "Open Developer Console.cmd" del "Open Developer Console.cmd" @@ -68,8 +77,6 @@ if exist "Open Developer Console.cmd" del "Open Developer Console.cmd" @set PYTHONNOUSERSITE=1 set USERPROFILE=%cd%\profile - set TMP=%cd%\tmp - set TEMP=%cd%\tmp set PYTHONPATH=%cd%;%cd%\env\lib\site-packages @@ -107,8 +114,6 @@ set PATH=C:\Windows\System32;%PATH% @set PYTHONNOUSERSITE=1 set USERPROFILE=%cd%\profile - set TMP=%cd%\tmp - set TEMP=%cd%\tmp set PYTHONPATH=%cd%;%cd%\env\lib\site-packages @@ -142,8 +147,6 @@ set PATH=C:\Windows\System32;%PATH% @set PYTHONNOUSERSITE=1 set USERPROFILE=%cd%\profile - set TMP=%cd%\tmp - set TEMP=%cd%\tmp set PYTHONPATH=%cd%;%cd%\env\lib\site-packages @@ -171,8 +174,6 @@ set PATH=C:\Windows\System32;%PATH% @set PYTHONNOUSERSITE=1 set USERPROFILE=%cd%\profile - set TMP=%cd%\tmp - set TEMP=%cd%\tmp set PYTHONPATH=%cd%;%cd%\env\lib\site-packages From 6c156380f926e8c5ce90ab470771b120a4d0b51a Mon Sep 17 00:00:00 2001 From: JeLuF Date: Sat, 19 Nov 2022 17:10:45 +0100 Subject: [PATCH 2/9] Add network settings to the UI Allow users to choose the uvicorn port Allow users to restrict uvicorn to only listen on localhost --- ui/media/js/parameters.js | 50 ++++++++++++++++++++++++++++++++------- ui/server.py | 40 +++++++++++++++++++++++-------- 2 files changed, 72 insertions(+), 18 deletions(-) diff --git a/ui/media/js/parameters.js b/ui/media/js/parameters.js index d592576d..2e5bc75c 100644 --- a/ui/media/js/parameters.js +++ b/ui/media/js/parameters.js @@ -114,6 +114,24 @@ var PARAMETERS = [ icon: "fa-gear", default: true, }, + { + id: "listen_to_network", + type: ParameterType.checkbox, + label: "Make Stable Diffusion available on your network", + note: "Other devices on your network can access this web page", + icon: "fa-network-wired", + default: true, + }, + { + id: "listen_port", + type: ParameterType.custom, + label: "Network port", + note: "Port that this server listens to. The '9000' part in 'http://localhost:9000'", + icon: "fa-anchor", + render: (parameter) => { + return `` + } + }, { id: "use_beta_channel", type: ParameterType.checkbox, @@ -176,6 +194,8 @@ let useGPUsField = document.querySelector('#use_gpus') let useFullPrecisionField = document.querySelector('#use_full_precision') let saveToDiskField = document.querySelector('#save_to_disk') let diskPathField = document.querySelector('#diskPath') +let listenToNetworkField = document.querySelector("#listen_to_network") +let listenPortField = document.querySelector("#listen_port") let useBetaChannelField = document.querySelector("#use_beta_channel") let uiOpenBrowserOnStartField = document.querySelector("#ui_open_browser_on_start") @@ -205,11 +225,17 @@ async function getAppConfig() { if (config.update_branch === 'beta') { useBetaChannelField.checked = true - document.querySelector("#updateBranchLabel").innerText = "(beta)" + document.querySelector("#updateBranchLabel").innerText = "(beta)" } if (config.ui && config.ui.open_browser_on_start === false) { uiOpenBrowserOnStartField.checked = false } + if (config.net && config.net.listen_to_network === false) { + listenToNetworkField.checked = false + } + if (config.net && config.net.listen_port !== undefined) { + listenPortField.value = config.net.listen_port + } console.log('get config status response', config) } catch (e) { @@ -336,12 +362,20 @@ async function getDevices() { saveSettingsBtn.addEventListener('click', function() { let updateBranch = (useBetaChannelField.checked ? 'beta' : 'main') - changeAppConfig({ - 'render_devices': getCurrentRenderDeviceSelection(), - 'update_branch': updateBranch, - 'ui_open_browser_on_start': uiOpenBrowserOnStartField.checked - }) + if (listenPortField.value == '') { + alert('The network port field must not be empty.') + } else if (listenPortField.value<1 || listenPortField.value>65535) { + alert('The network port must be a number from 1 to 65535') + } else { + changeAppConfig({ + 'render_devices': getCurrentRenderDeviceSelection(), + 'update_branch': updateBranch, + 'ui_open_browser_on_start': uiOpenBrowserOnStartField.checked, + 'listen_to_network': listenToNetworkField.checked, + 'listen_port': listenPortField.value + }) + } - saveSettingsBtn.classList.add('active') - asyncDelay(300).then(() => saveSettingsBtn.classList.remove('active')) + saveSettingsBtn.classList.add('active') + asyncDelay(300).then(() => saveSettingsBtn.classList.remove('active')) }) diff --git a/ui/server.py b/ui/server.py index 94b7fce8..61635f18 100644 --- a/ui/server.py +++ b/ui/server.py @@ -83,13 +83,21 @@ def getConfig(default_val=APP_CONFIG_DEFAULTS): if not os.path.exists(config_json_path): return default_val with open(config_json_path, 'r', encoding='utf-8') as f: - return json.load(f) + config = json.load(f) + if 'net' not in config: + config['net'] = {} + if os.getenv('SD_UI_BIND_PORT') is not None: + config['net']['listen_port'] = int(os.getenv('SD_UI_BIND_PORT')) + if os.getenv('SD_UI_BIND_IP') is not None: + config['net']['listen_to_network'] = ( os.getenv('SD_UI_BIND_IP') == '0.0.0.0' ) + return config except Exception as e: print(str(e)) print(traceback.format_exc()) return default_val def setConfig(config): + print( json.dumps(config) ) try: # config.json config_json_path = os.path.join(CONFIG_DIR, 'config.json') with open(config_json_path, 'w', encoding='utf-8') as f: @@ -103,10 +111,10 @@ def setConfig(config): if 'update_branch' in config: config_bat.append(f"@set update_branch={config['update_branch']}") - if os.getenv('SD_UI_BIND_PORT') is not None: - config_bat.append(f"@set SD_UI_BIND_PORT={os.getenv('SD_UI_BIND_PORT')}") - if os.getenv('SD_UI_BIND_IP') is not None: - config_bat.append(f"@set SD_UI_BIND_IP={os.getenv('SD_UI_BIND_IP')}") + + config_bat.append(f"@set SD_UI_BIND_PORT={config['net']['listen_port']}") + bind_ip = '0.0.0.0' if config['net']['listen_to_network'] else '127.0.0.1' + config_bat.append(f"@set SD_UI_BIND_IP={bind_ip}") if len(config_bat) > 0: with open(config_bat_path, 'w', encoding='utf-8') as f: @@ -120,10 +128,10 @@ def setConfig(config): if 'update_branch' in config: config_sh.append(f"export update_branch={config['update_branch']}") - if os.getenv('SD_UI_BIND_PORT') is not None: - config_sh.append(f"export SD_UI_BIND_PORT={os.getenv('SD_UI_BIND_PORT')}") - if os.getenv('SD_UI_BIND_IP') is not None: - config_sh.append(f"export SD_UI_BIND_IP={os.getenv('SD_UI_BIND_IP')}") + + config_sh.append(f"export SD_UI_BIND_PORT={config['net']['listen_port']}") + bind_ip = '0.0.0.0' if config['net']['listen_to_network'] else '127.0.0.1' + config_sh.append(f"export SD_UI_BIND_IP={bind_ip}") if len(config_sh) > 1: with open(config_sh_path, 'w', encoding='utf-8') as f: @@ -178,6 +186,8 @@ class SetAppConfigRequest(BaseModel): render_devices: Union[List[str], List[int], str, int] = None model_vae: str = None ui_open_browser_on_start: bool = None + listen_to_network: bool = None + listen_port: int = None @app.post('/app_config') async def setAppConfig(req : SetAppConfigRequest): @@ -190,6 +200,14 @@ async def setAppConfig(req : SetAppConfigRequest): if 'ui' not in config: config['ui'] = {} config['ui']['open_browser_on_start'] = req.ui_open_browser_on_start + if req.listen_to_network is not None: + if 'net' not in config: + config['net'] = {} + config['net']['listen_to_network'] = bool(req.listen_to_network) + if req.listen_port is not None: + if 'net' not in config: + config['net'] = {} + config['net']['listen_port'] = int(req.listen_port) try: setConfig(config) @@ -435,7 +453,9 @@ update_render_threads() def open_browser(): config = getConfig() ui = config.get('ui', {}) + net = config.get('net', {'listen_port':9000}) + port = net.get('listen_port', 9000) if ui.get('open_browser_on_start', True): - import webbrowser; webbrowser.open('http://localhost:9000') + import webbrowser; webbrowser.open(f"http://localhost:{port}") open_browser() From cde855e1dcf6e581fe09917924a7d9be832885b3 Mon Sep 17 00:00:00 2001 From: JeLuF Date: Sat, 19 Nov 2022 20:31:36 +0100 Subject: [PATCH 3/9] Change note --- CHANGES.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGES.md b/CHANGES.md index 280e7248..df1b09a6 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -21,6 +21,7 @@ - A `What's New?` tab in the UI ### Detailed changelog +* 2.4.11 - 19 Nov 2022 - New system settings to manage the network config (port number and whether to only listen on localhost) * 2.4.11 - 19 Nov 2022 - Address a regression in how long images take to generate. Use the previous code for moving a model to CPU. This improves things by a second or two per image, but we still have a regression (investigating). * 2.4.10 - 18 Nov 2022 - Textarea for negative prompts. Thanks @JeLuf * 2.4.10 - 18 Nov 2022 - Improved design for Settings, and rounded toggle buttons instead of checkboxes for a more modern look. Thanks @mdiller From ae409dd0ecd71e80256c73e6efa0206e1de85930 Mon Sep 17 00:00:00 2001 From: JeLuF Date: Sat, 19 Nov 2022 21:00:41 +0100 Subject: [PATCH 4/9] Prevent empty fields in make image request Prevent render jobs to fail with HTTP 422 due to empty fields in the image settings. https://discord.com/channels/1014774730907209781/1043481789706031215/1043481789706031215 https://discord.com/channels/1014774730907209781/1014774732018683927/1042768986871443516 minor change, no CHANGE.md entry --- ui/media/js/main.js | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/ui/media/js/main.js b/ui/media/js/main.js index bff548c0..2811a50c 100644 --- a/ui/media/js/main.js +++ b/ui/media/js/main.js @@ -823,16 +823,26 @@ function getCurrentUserRequest() { function makeImage() { if (!isServerAvailable()) { alert('The server is not available.') - return - } - const taskTemplate = getCurrentUserRequest() - const newTaskRequests = [] - getPrompts().forEach((prompt) => newTaskRequests.push(Object.assign({}, taskTemplate, { - reqBody: Object.assign({ prompt: prompt }, taskTemplate.reqBody) - }))) - newTaskRequests.forEach(createTask) + } else if (!randomSeedField.checked && seedField.value == '') { + alert('The "Seed" field must not be empty.') + } else if (numOutputsTotalField.value == '') { + alert('The "Number of Images" field must not be empty.') + } else if (numOutputsParallelField.value == '') { + alert('The "Number of parallel Images" field must not be empty.') + } else if (numInferenceStepsField.value == '') { + alert('The "Inference Steps" field must not be empty.') + } else if (guidanceScaleField.value == '') { + alert('The Guidance Scale field must not be empty.') + } else { + const taskTemplate = getCurrentUserRequest() + const newTaskRequests = [] + getPrompts().forEach((prompt) => newTaskRequests.push(Object.assign({}, taskTemplate, { + reqBody: Object.assign({ prompt: prompt }, taskTemplate.reqBody) + }))) + newTaskRequests.forEach(createTask) - initialText.style.display = 'none' + initialText.style.display = 'none' + } } function createTask(task) { @@ -1336,4 +1346,4 @@ window.addEventListener("beforeunload", function(e) { }); createCollapsibles() -prettifyInputs(document); \ No newline at end of file +prettifyInputs(document); From 5ebc6b698c9628df104843afd306b8d0473545ed Mon Sep 17 00:00:00 2001 From: JeLuF Date: Sun, 20 Nov 2022 18:48:48 +0100 Subject: [PATCH 5/9] Rewrite easy_install.pth on each start Fixes GFPGANer errors when the env has been moved. --- scripts/on_sd_start.bat | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/scripts/on_sd_start.bat b/scripts/on_sd_start.bat index abc63ef5..995b99cc 100644 --- a/scripts/on_sd_start.bat +++ b/scripts/on_sd_start.bat @@ -393,6 +393,14 @@ call python --version @set SD_UI_PATH=%cd%\ui @cd stable-diffusion +@rem +@rem Rewrite easy-install.pth. This fixes the installation if the user has relocated the SDUI installation +@rem +>env\Lib\site-packages\easy-install.pth echo %cd%\src\taming-transformers +>>env\Lib\site-packages\easy-install.pth echo %cd%\src\clip +>>env\Lib\site-packages\easy-install.pth echo %cd%\src\gfpgan +>>env\Lib\site-packages\easy-install.pth echo %cd%\src\realesrgan + @if NOT DEFINED SD_UI_BIND_PORT set SD_UI_BIND_PORT=9000 @if NOT DEFINED SD_UI_BIND_IP set SD_UI_BIND_IP=0.0.0.0 @uvicorn server:app --app-dir "%SD_UI_PATH%" --port %SD_UI_BIND_PORT% --host %SD_UI_BIND_IP% From 2b1f8533b02b9a5b901c25a8719f6fb304cdbcab Mon Sep 17 00:00:00 2001 From: JeLuF Date: Sun, 20 Nov 2022 23:07:44 +0100 Subject: [PATCH 6/9] Add --whitespace=fix to git apply For some users who have git preinstalled, `git apply` fails due to whitespace errors. Aracon found that applying `--whitespace=fix` to the `git apply` invocation fixes the problem. https://discord.com/channels/1014774730907209781/1036679816713359471/1037025435491516548 ryz confirmed that `--reject` wasn't needed for him to make it work, and this explanation from the "git apply" manpage suggests that we shouldn't include `--reject`: > For atomicity, git apply by default fails the whole patch and does not touch the working tree when some > of the hunks do not apply. This option makes it apply the parts of the patch that are applicable, and leave > the rejected hunks in corresponding *.rej files. After having a look at https://github.com/git/git/blob/master/apply.c, I think that they only check for `correct_ws_error` if they couldn't apply the patch. It doesn't impact 'normal' patching. If the patch can be applied, it will be done, and only if the apply fails, they'll check whether adding or removing WS might help. It should thus be save to be added and didn't produce any errors on my installation using SDUI-provided git. --- scripts/on_sd_start.bat | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/scripts/on_sd_start.bat b/scripts/on_sd_start.bat index abc63ef5..a73aabd3 100644 --- a/scripts/on_sd_start.bat +++ b/scripts/on_sd_start.bat @@ -28,8 +28,8 @@ if exist "Open Developer Console.cmd" del "Open Developer Console.cmd" @call git pull @call git -c advice.detachedHead=false checkout f6cfebffa752ee11a7b07497b8529d5971de916c - @call git apply ..\ui\sd_internal\ddim_callback.patch - @call git apply ..\ui\sd_internal\env_yaml.patch + @call git apply --whitespace=fix ..\ui\sd_internal\ddim_callback.patch + @call git apply --whitespace=fix ..\ui\sd_internal\env_yaml.patch @cd .. ) else ( @@ -46,8 +46,8 @@ if exist "Open Developer Console.cmd" del "Open Developer Console.cmd" @cd stable-diffusion @call git -c advice.detachedHead=false checkout f6cfebffa752ee11a7b07497b8529d5971de916c - @call git apply ..\ui\sd_internal\ddim_callback.patch - @call git apply ..\ui\sd_internal\env_yaml.patch + @call git apply --whitespace=fix ..\ui\sd_internal\ddim_callback.patch + @call git apply --whitespace=fix ..\ui\sd_internal\env_yaml.patch @cd .. ) From ed435d2b7215e2578b5fa18f016a4a9d05c2f55e Mon Sep 17 00:00:00 2001 From: JeLuF Date: Sun, 20 Nov 2022 23:09:27 +0100 Subject: [PATCH 7/9] Add --whitespace=fix also on Linux --- scripts/on_sd_start.sh | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/scripts/on_sd_start.sh b/scripts/on_sd_start.sh index adcba1f2..a4f92428 100755 --- a/scripts/on_sd_start.sh +++ b/scripts/on_sd_start.sh @@ -30,8 +30,8 @@ if [ -e "scripts/install_status.txt" ] && [ `grep -c sd_git_cloned scripts/insta git pull git -c advice.detachedHead=false checkout f6cfebffa752ee11a7b07497b8529d5971de916c - git apply ../ui/sd_internal/ddim_callback.patch || fail "ddim patch failed" - git apply ../ui/sd_internal/env_yaml.patch || fail "yaml patch failed" + git apply --whitespace=fix ../ui/sd_internal/ddim_callback.patch || fail "ddim patch failed" + git apply --whitespace=fix ../ui/sd_internal/env_yaml.patch || fail "yaml patch failed" cd .. else @@ -46,8 +46,8 @@ else cd stable-diffusion git -c advice.detachedHead=false checkout f6cfebffa752ee11a7b07497b8529d5971de916c - git apply ../ui/sd_internal/ddim_callback.patch || fail "ddim patch failed" - git apply ../ui/sd_internal/env_yaml.patch || fail "yaml patch failed" + git apply --whitespace=fix ../ui/sd_internal/ddim_callback.patch || fail "ddim patch failed" + git apply --whitespace=fix ../ui/sd_internal/env_yaml.patch || fail "yaml patch failed" cd .. fi From 5fe7807462e30f1db410d8c0ca13247dc80e6467 Mon Sep 17 00:00:00 2001 From: cmdr2 Date: Mon, 21 Nov 2022 12:38:46 +0530 Subject: [PATCH 8/9] ERRORLEVEL is unreliable when conda is run --- scripts/on_sd_start.bat | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/scripts/on_sd_start.bat b/scripts/on_sd_start.bat index acb4ca82..79a51205 100644 --- a/scripts/on_sd_start.bat +++ b/scripts/on_sd_start.bat @@ -16,11 +16,11 @@ if exist "%cd%\profile" ( @rem activate the installer env call conda activate -@if "%ERRORLEVEL%" NEQ "0" ( - @echo. & echo "Error activating conda for Stable Diffusion. Sorry about that, please try to:" & echo " 1. Run this installer again." & echo " 2. If that doesn't fix it, please try the common troubleshooting steps at https://github.com/cmdr2/stable-diffusion-ui/wiki/Troubleshooting" & echo " 3. If those steps don't help, please copy *all* the error messages in this window, and ask the community at https://discord.com/invite/u9yhsFmEkB" & echo " 4. If that doesn't solve the problem, please file an issue at https://github.com/cmdr2/stable-diffusion-ui/issues" & echo "Thanks!" & echo. - pause - exit /b -) +@rem @if "%ERRORLEVEL%" NEQ "0" ( +@rem @echo. & echo "Error activating conda for Stable Diffusion. Sorry about that, please try to:" & echo " 1. Run this installer again." & echo " 2. If that doesn't fix it, please try the common troubleshooting steps at https://github.com/cmdr2/stable-diffusion-ui/wiki/Troubleshooting" & echo " 3. If those steps don't help, please copy *all* the error messages in this window, and ask the community at https://discord.com/invite/u9yhsFmEkB" & echo " 4. If that doesn't solve the problem, please file an issue at https://github.com/cmdr2/stable-diffusion-ui/issues" & echo "Thanks!" & echo. +@rem pause +@rem exit /b +@rem) @REM remove the old version of the dev console script, if it's still present if exist "Open Developer Console.cmd" del "Open Developer Console.cmd" From 2edf64985dff7ad9af30c86b43be18f5a8de5ec8 Mon Sep 17 00:00:00 2001 From: cmdr2 Date: Mon, 21 Nov 2022 12:41:53 +0530 Subject: [PATCH 9/9] Update CHANGES.md --- CHANGES.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGES.md b/CHANGES.md index df1b09a6..80c1c64a 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -21,6 +21,8 @@ - A `What's New?` tab in the UI ### Detailed changelog +* 2.4.11 - 21 Nov 2022 - Installer improvements: avoid crashing if the username contains a space or special characters, allow moving/renaming the folder after installation on Windows, whitespace fix on git apply +* 2.4.11 - 21 Nov 2022 - Validate inputs before submitting the Image request * 2.4.11 - 19 Nov 2022 - New system settings to manage the network config (port number and whether to only listen on localhost) * 2.4.11 - 19 Nov 2022 - Address a regression in how long images take to generate. Use the previous code for moving a model to CPU. This improves things by a second or two per image, but we still have a regression (investigating). * 2.4.10 - 18 Nov 2022 - Textarea for negative prompts. Thanks @JeLuf