(default_val=APP_CONFIG_DEFAULTS)
| 117 | |
| 118 | |
| 119 | def getConfig(default_val=APP_CONFIG_DEFAULTS): |
| 120 | config_yaml_path = os.path.join(CONFIG_DIR, "..", "config.yaml") |
| 121 | config_yaml_path = os.path.abspath(config_yaml_path) |
| 122 | |
| 123 | # migrate the old config yaml location |
| 124 | config_legacy_yaml = os.path.join(CONFIG_DIR, "config.yaml") |
| 125 | if os.path.isfile(config_legacy_yaml): |
| 126 | shutil.move(config_legacy_yaml, config_yaml_path) |
| 127 | |
| 128 | def set_config_on_startup(config: dict): |
| 129 | if getConfig.__use_backend_on_startup is None: |
| 130 | getConfig.__use_backend_on_startup = config.get("backend", "ed_diffusers") |
| 131 | config["config_on_startup"] = {"backend": getConfig.__use_backend_on_startup} |
| 132 | |
| 133 | if os.path.isfile(config_yaml_path): |
| 134 | try: |
| 135 | yaml = YAML() |
| 136 | with open(config_yaml_path, "r", encoding="utf-8") as f: |
| 137 | config = yaml.load(f) |
| 138 | if "net" not in config: |
| 139 | config["net"] = {} |
| 140 | if os.getenv("SD_UI_BIND_PORT") is not None: |
| 141 | config["net"]["listen_port"] = int(os.getenv("SD_UI_BIND_PORT")) |
| 142 | else: |
| 143 | config["net"]["listen_port"] = 9000 |
| 144 | if os.getenv("SD_UI_BIND_IP") is not None: |
| 145 | config["net"]["listen_to_network"] = os.getenv("SD_UI_BIND_IP") == "0.0.0.0" |
| 146 | else: |
| 147 | config["net"]["listen_to_network"] = True |
| 148 | |
| 149 | if "backend" not in config: |
| 150 | if "use_v3_engine" in config: |
| 151 | config["backend"] = "ed_diffusers" if config["use_v3_engine"] else "ed_classic" |
| 152 | else: |
| 153 | config["backend"] = "ed_diffusers" |
| 154 | # this default will need to be smarter when WebUI becomes the main backend, but needs to maintain backwards |
| 155 | # compatibility with existing ED 3.0 installations that haven't opted into the WebUI backend, and haven't |
| 156 | # set a "use_v3_engine" flag in their config |
| 157 | |
| 158 | set_config_on_startup(config) |
| 159 | |
| 160 | return config |
| 161 | except Exception as e: |
| 162 | log.warn(traceback.format_exc()) |
| 163 | set_config_on_startup(default_val) |
| 164 | return default_val |
| 165 | else: |
| 166 | try: |
| 167 | config_json_path = os.path.join(CONFIG_DIR, "config.json") |
| 168 | if not os.path.exists(config_json_path): |
| 169 | return default_val |
| 170 | |
| 171 | log.info("Converting old json config file to yaml") |
| 172 | with open(config_json_path, "r", encoding="utf-8") as f: |
| 173 | config = json.load(f) |
| 174 | # Save config in new format |
| 175 | setConfig(config) |
| 176 |
no test coverage detected