From 932ee11c915989db2bb8a9255be83c3c251b10e9 Mon Sep 17 00:00:00 2001 From: JeLuF Date: Thu, 16 Feb 2023 01:19:36 +0100 Subject: [PATCH] Use yaml instead of json for the config file --- build.bat | 1 + build.sh | 1 + scripts/config.yaml.sample | 24 +++++++++++++ scripts/on_sd_start.bat | 17 +++++++++ ui/easydiffusion/app.py | 70 ++++++++++++++++++++++++-------------- 5 files changed, 88 insertions(+), 25 deletions(-) create mode 100644 scripts/config.yaml.sample diff --git a/build.bat b/build.bat index 6e3f3f81..b9c6b9ab 100644 --- a/build.bat +++ b/build.bat @@ -15,6 +15,7 @@ mkdir dist\win\stable-diffusion-ui\scripts copy scripts\on_env_start.bat dist\win\stable-diffusion-ui\scripts\ copy scripts\bootstrap.bat dist\win\stable-diffusion-ui\scripts\ +copy scripts\config.yaml.sample dist\win\stable-diffusion-ui\scripts\config.yaml copy "scripts\Start Stable Diffusion UI.cmd" dist\win\stable-diffusion-ui\ copy LICENSE dist\win\stable-diffusion-ui\ copy "CreativeML Open RAIL-M License" dist\win\stable-diffusion-ui\ diff --git a/build.sh b/build.sh index f4538e5c..a7ed152c 100755 --- a/build.sh +++ b/build.sh @@ -29,6 +29,7 @@ mkdir -p dist/linux-mac/stable-diffusion-ui/scripts cp scripts/on_env_start.sh dist/linux-mac/stable-diffusion-ui/scripts/ cp scripts/bootstrap.sh dist/linux-mac/stable-diffusion-ui/scripts/ cp scripts/functions.sh dist/linux-mac/stable-diffusion-ui/scripts/ +cp scripts/config.yaml.sample dist/linux-mac/stable-diffusion-ui/scripts/config.yaml cp scripts/start.sh dist/linux-mac/stable-diffusion-ui/ cp LICENSE dist/linux-mac/stable-diffusion-ui/ cp "CreativeML Open RAIL-M License" dist/linux-mac/stable-diffusion-ui/ diff --git a/scripts/config.yaml.sample b/scripts/config.yaml.sample new file mode 100644 index 00000000..9c2cc4a6 --- /dev/null +++ b/scripts/config.yaml.sample @@ -0,0 +1,24 @@ +# Change listen_port if port 9000 is already in use on your system +# Set listen_to_network to true to make Easy Diffusion accessibble on your local network +net: + listen_port: 9000 + listen_to_network: false + +# Multi GPU setup +render_devices: auto + +# Set open_browser_on_start to false to disable opening a new browser tab on each restart +ui: + open_browser_on_start: true + +# set update_branch to main to use the stable version, or to beta to use the experimental +# beta version. +update_branch: main + +# Set force_save_path to enforce an auto save path. Clients will not be able to change or +# disable auto save when this option is set. Please adapt the path in the examples to your +# needs. +# Windows: +# force_save_path: C:\\Easy Diffusion Images\\ +# Linux: +# force_save_path: /data/easy-diffusion-images/ diff --git a/scripts/on_sd_start.bat b/scripts/on_sd_start.bat index 821e24aa..fe6f20aa 100644 --- a/scripts/on_sd_start.bat +++ b/scripts/on_sd_start.bat @@ -135,6 +135,23 @@ if "%ERRORLEVEL%" EQU "0" ( ) ) +@rem install ruamel.yaml +call python ..\scripts\check_modules.py ruamel.yaml +if "%ERRORLEVEL%" EQU "0" ( + echo "ruamel.yaml has already been installed." +) else ( + echo "Installing ruamel.yaml.." + + set PYTHONNOUSERSITE=1 + set PYTHONPATH=%INSTALL_ENV_DIR%\lib\site-packages + + call python -m pip install ruamel.yaml==0.17.21 || ( + echo "Error installing ruamel.yaml. 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!" + pause + exit /b + ) +) + set PATH=C:\Windows\System32;%PATH% call python ..\scripts\check_modules.py uvicorn fastapi diff --git a/ui/easydiffusion/app.py b/ui/easydiffusion/app.py index 4369a488..92d2d596 100644 --- a/ui/easydiffusion/app.py +++ b/ui/easydiffusion/app.py @@ -5,6 +5,9 @@ import json import traceback import logging import shlex +from ruamel.yaml import YAML +yaml = YAML() + from rich.logging import RichHandler from sdkit.utils import log as sdkit_log # hack, so we can overwrite the log config @@ -54,33 +57,50 @@ def init(): update_render_threads() def getConfig(default_val=APP_CONFIG_DEFAULTS): - try: - config_json_path = os.path.join(CONFIG_DIR, 'config.json') - if not os.path.exists(config_json_path): - config = default_val - else: - with open(config_json_path, 'r', encoding='utf-8') as 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')) - else: - config['net']['listen_port'] = 9000 - 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') - else: - config['net']['listen_to_network'] = True - return config - except Exception as e: - log.warn(traceback.format_exc()) - return default_val + config_yaml_path = os.path.join(CONFIG_DIR, 'config.yaml') + if os.path.isfile(config_yaml_path): + try: + log.info('Loading config.yaml') + with open(config_yaml_path, 'r', encoding='utf-8') as f: + config = yaml.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')) + else: + config['net']['listen_port'] = 9000 + 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') + else: + config['net']['listen_to_network'] = True + return config + except Exception as e: + log.warn(traceback.format_exc()) + return default_val + else: + try: + config_json_path = os.path.join(CONFIG_DIR, 'config.json') + if not os.path.exists(config_json_path): + return default_val + else: + log.info('Converting old json config file to yaml') + with open(config_json_path, 'r', encoding='utf-8') as f: + config = json.load(f) + # Save config in new format + setConfig(config) + os.rename(config_json_path, config_json_path + '.bak') + log.info('Saved old config.json as config.json.bak') + return getConfig(default_val) + except Exception as e: + log.warn(traceback.format_exc()) + return default_val def setConfig(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: - json.dump(config, f) + try: # config.yaml + config_yaml_path = os.path.join(CONFIG_DIR, 'config.yaml') + yaml.indent(mapping=2, sequence=4, offset=2) + with open(config_yaml_path, 'w', encoding='utf-8') as f: + yaml.dump(config, f) except: log.error(traceback.format_exc())