Skip to content

Commit

Permalink
Expand user directory for basepath in extra_models_paths.yaml (#4857)
Browse files Browse the repository at this point in the history
* Expand user path.

* Add test.

* Add unit test for expanding base path.

* Simplify unit test.

* Remove comment.

* Remove comment.

* Checkpoints.

* Refactor.
  • Loading branch information
robinjhuang authored Sep 10, 2024
1 parent 9a7444e commit 9fa8faa
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 24 deletions.
27 changes: 3 additions & 24 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ def execute_script(script_path):
import gc

import logging
from utils import extra_config

This comment has been minimized.

Copy link
@xxf1996

xxf1996 Sep 10, 2024

或许这里utils这个模块名跟下面的import comfy.utils发生冲突?我刚刚在获取了最新的ComfyUI代码后,发现无法正常运行了(不过我用的是sd-webui-comfyui插件),控制台出现了如下报错:

[sd-webui-comfyui] Started callback listeners for process webui
[sd-webui-comfyui] Starting subprocess for comfyui...
Startup time: 39.1s (prepare environment: 26.1s, import torch: 4.8s, import gradio: 0.8s, setup paths: 0.9s, initialize shared: 0.2s, other imports: 0.4s, load scripts: 2.3s, create ui: 0.5s, gradio launch: 2.5s, add APIs: 0.4s).
[ComfyUI] [sd-webui-comfyui] Setting up IPC...
[ComfyUI] [sd-webui-comfyui] Using inter-process communication strategy: Shared memory
[ComfyUI] [sd-webui-comfyui] Started callback listeners for process comfyui
[ComfyUI] [sd-webui-comfyui] Patching ComfyUI...
WARNING:xformers:A matching Triton is not available, some optimizations will not be enabled
[ComfyUI] Traceback (most recent call last):
[ComfyUI]   File "E:\stable-diffusion-webui\venv\lib\site-packages\xformers\__init__.py", line 57, in _is_triton_available
    import triton  # noqa
[ComfyUI] ModuleNotFoundError: No module named 'triton'
[ComfyUI] [sd-webui-comfyui] Launching ComfyUI with arguments: --listen 127.0.0.1 --port 8189
[ComfyUI] [START] Security scan
[ComfyUI] [DONE] Security scan
[ComfyUI] ## ComfyUI-Manager: installing dependencies done.
[ComfyUI] ** ComfyUI startup time: 2024-09-10 16:30:42.763843
[ComfyUI] ** Platform: Windows
[ComfyUI] ** Python version: 3.10.11 (tags/v3.10.11:7d4cc5a, Apr  5 2023, 00:38:17) [MSC v.1929 64 bit (AMD64)]
[ComfyUI] ** Python executable: E:\stable-diffusion-webui\venv\Scripts\Python.exe
[ComfyUI] ** ComfyUI Path: E:\stable-diffusion-webui
[ComfyUI] ** Log path: E:\stable-diffusion-webui\extensions\sd-webui-comfyui\ComfyUI\comfyui.log
Traceback (most recent call last):
  File "E:\stable-diffusion-webui\extensions\sd-webui-comfyui\lib_comfyui\comfyui\pre_main.py", line 98, in <module>
    main()
  File "E:\stable-diffusion-webui\extensions\sd-webui-comfyui\lib_comfyui\ipc\__init__.py", line 41, in wrapper
    return function(*args, **kwargs)
  File "E:\stable-diffusion-webui\extensions\sd-webui-comfyui\lib_comfyui\comfyui\pre_main.py", line 44, in main
    start_comfyui()
  File "E:\stable-diffusion-webui\extensions\sd-webui-comfyui\lib_comfyui\ipc\__init__.py", line 41, in wrapper
    return function(*args, **kwargs)
  File "E:\stable-diffusion-webui\extensions\sd-webui-comfyui\lib_comfyui\comfyui\pre_main.py", line 93, in start_comfyui
    runpy.run_path(os.path.join(os.getcwd(), 'main.py'), {'comfyui_print': comfyui_print}, '__main__')
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python310\lib\runpy.py", line 289, in run_path
    return _run_module_code(code, init_globals, run_name,
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python310\lib\runpy.py", line 96, in _run_module_code
    _run_code(code, mod_globals, init_globals,
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python310\lib\runpy.py", line 86, in _run_code
    exec(code, run_globals)
  File "E:\stable-diffusion-webui\extensions\sd-webui-comfyui\ComfyUI\main.py", line 66, in <module>
    from utils import extra_config
ImportError: cannot import name 'extra_config' from 'utils' (E:\stable-diffusion-webui\extensions\sd-webui-comfyui\ComfyUI\comfy\utils.py)

很显然这里utils被识别成comfy.utils了,所以我试着改了一下utils文件夹为其他的名称,然后在import就不会跟comfy.utils模块发生冲突了。


Maybe the module name utils here conflicts with import comfy.utils below? I just realized that after getting the latest ComfyUI code, it doesn't work anymore (but I'm using the sd-webui-comfyui plugin), and I get the following error in the console:

[sd-webui-comfyui] Started callback listeners for process webui
[sd-webui-comfyui] Starting subprocess for comfyui...
Startup time: 39.1s (prepare environment: 26.1s, import torch: 4.8s, import gradio: 0.8s, setup paths: 0.9s, initialize shared: 0.2s, other imports: 0.4s, load scripts: 2.3s, create ui: 0.5s, gradio launch: 2.5s, add APIs: 0.4s).
[ComfyUI] [sd-webui-comfyui] Setting up IPC...
[ComfyUI] [sd-webui-comfyui] Using inter-process communication strategy: Shared memory
[ComfyUI] [sd-webui-comfyui] Started callback listeners for process comfyui
[ComfyUI] [sd-webui-comfyui] Patching ComfyUI...
WARNING:xformers:A matching Triton is not available, some optimizations will not be enabled
[ComfyUI] Traceback (most recent call last):
[ComfyUI]   File "E:\stable-diffusion-webui\venv\lib\site-packages\xformers\__init__.py", line 57, in _is_triton_available
    import triton  # noqa
[ComfyUI] ModuleNotFoundError: No module named 'triton'
[ComfyUI] [sd-webui-comfyui] Launching ComfyUI with arguments: --listen 127.0.0.1 --port 8189
[ComfyUI] [START] Security scan
[ComfyUI] [DONE] Security scan
[ComfyUI] ## ComfyUI-Manager: installing dependencies done.
[ComfyUI] ** ComfyUI startup time: 2024-09-10 16:30:42.763843
[ComfyUI] ** Platform: Windows
[ComfyUI] ** Python version: 3.10.11 (tags/v3.10.11:7d4cc5a, Apr  5 2023, 00:38:17) [MSC v.1929 64 bit (AMD64)]
[ComfyUI] ** Python executable: E:\stable-diffusion-webui\venv\Scripts\Python.exe
[ComfyUI] ** ComfyUI Path: E:\stable-diffusion-webui
[ComfyUI] ** Log path: E:\stable-diffusion-webui\extensions\sd-webui-comfyui\ComfyUI\comfyui.log
Traceback (most recent call last):
  File "E:\stable-diffusion-webui\extensions\sd-webui-comfyui\lib_comfyui\comfyui\pre_main.py", line 98, in <module>
    main()
  File "E:\stable-diffusion-webui\extensions\sd-webui-comfyui\lib_comfyui\ipc\__init__.py", line 41, in wrapper
    return function(*args, **kwargs)
  File "E:\stable-diffusion-webui\extensions\sd-webui-comfyui\lib_comfyui\comfyui\pre_main.py", line 44, in main
    start_comfyui()
  File "E:\stable-diffusion-webui\extensions\sd-webui-comfyui\lib_comfyui\ipc\__init__.py", line 41, in wrapper
    return function(*args, **kwargs)
  File "E:\stable-diffusion-webui\extensions\sd-webui-comfyui\lib_comfyui\comfyui\pre_main.py", line 93, in start_comfyui
    runpy.run_path(os.path.join(os.getcwd(), 'main.py'), {'comfyui_print': comfyui_print}, '__main__')
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python310\lib\runpy.py", line 289, in run_path
    return _run_module_code(code, init_globals, run_name,
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python310\lib\runpy.py", line 96, in _run_module_code
    _run_code(code, mod_globals, init_globals,
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python310\lib\runpy.py", line 86, in _run_code
    exec(code, run_globals)
  File "E:\stable-diffusion-webui\extensions\sd-webui-comfyui\ComfyUI\main.py", line 66, in <module>
    from utils import extra_config
ImportError: cannot import name 'extra_config' from 'utils' (E:\stable-diffusion-webui\extensions\sd-webui-comfyui\ComfyUI\comfy\utils.py)

Obviously here utils is recognized as comfy.utils, so I tried changing the utils folder to something else, and then in import it doesn't conflict with the comfy.utils module.


if os.name == "nt":
logging.getLogger("xformers").addFilter(lambda record: 'A matching Triton is not available' not in record.getMessage())
Expand All @@ -85,7 +86,6 @@ def execute_script(script_path):
pass

import comfy.utils
import yaml

import execution
import server
Expand Down Expand Up @@ -180,27 +180,6 @@ def cleanup_temp():
shutil.rmtree(temp_dir, ignore_errors=True)


def load_extra_path_config(yaml_path):
with open(yaml_path, 'r') as stream:
config = yaml.safe_load(stream)
for c in config:
conf = config[c]
if conf is None:
continue
base_path = None
if "base_path" in conf:
base_path = conf.pop("base_path")
for x in conf:
for y in conf[x].split("\n"):
if len(y) == 0:
continue
full_path = y
if base_path is not None:
full_path = os.path.join(base_path, full_path)
logging.info("Adding extra search path {} {}".format(x, full_path))
folder_paths.add_model_folder_path(x, full_path)


if __name__ == "__main__":
if args.temp_directory:
temp_dir = os.path.join(os.path.abspath(args.temp_directory), "temp")
Expand All @@ -222,11 +201,11 @@ def load_extra_path_config(yaml_path):

extra_model_paths_config_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), "extra_model_paths.yaml")
if os.path.isfile(extra_model_paths_config_path):
load_extra_path_config(extra_model_paths_config_path)
extra_config.load_extra_path_config(extra_model_paths_config_path)

if args.extra_model_paths_config:
for config_path in itertools.chain(*args.extra_model_paths_config):
load_extra_path_config(config_path)
extra_config.load_extra_path_config(config_path)

nodes.init_extra_nodes(init_custom_nodes=not args.disable_all_custom_nodes)

Expand Down
69 changes: 69 additions & 0 deletions tests-unit/utils/extra_config_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import pytest
import yaml
import os
from unittest.mock import Mock, patch, mock_open

from utils.extra_config import load_extra_path_config
import folder_paths

@pytest.fixture
def mock_yaml_content():
return {
'test_config': {
'base_path': '~/App/',
'checkpoints': 'subfolder1',
}
}

@pytest.fixture
def mock_expanded_home():
return '/home/user'

@pytest.fixture
def mock_add_model_folder_path():
return Mock()

@pytest.fixture
def mock_expanduser(mock_expanded_home):
def _expanduser(path):
if path.startswith('~/'):
return os.path.join(mock_expanded_home, path[2:])
return path
return _expanduser

@pytest.fixture
def mock_yaml_safe_load(mock_yaml_content):
return Mock(return_value=mock_yaml_content)

@patch('builtins.open', new_callable=mock_open, read_data="dummy file content")
def test_load_extra_model_paths_expands_userpath(
mock_file,
monkeypatch,
mock_add_model_folder_path,
mock_expanduser,
mock_yaml_safe_load,
mock_expanded_home
):
# Attach mocks used by load_extra_path_config
monkeypatch.setattr(folder_paths, 'add_model_folder_path', mock_add_model_folder_path)
monkeypatch.setattr(os.path, 'expanduser', mock_expanduser)
monkeypatch.setattr(yaml, 'safe_load', mock_yaml_safe_load)

dummy_yaml_file_name = 'dummy_path.yaml'
load_extra_path_config(dummy_yaml_file_name)

expected_calls = [
('checkpoints', os.path.join(mock_expanded_home, 'App', 'subfolder1')),
]

assert mock_add_model_folder_path.call_count == len(expected_calls)

# Check if add_model_folder_path was called with the correct arguments
for actual_call, expected_call in zip(mock_add_model_folder_path.call_args_list, expected_calls):
assert actual_call.args == expected_call

# Check if yaml.safe_load was called
mock_yaml_safe_load.assert_called_once()

# Check if open was called with the correct file path
mock_file.assert_called_once_with(dummy_yaml_file_name, 'r')
Empty file added utils/__init__.py
Empty file.
25 changes: 25 additions & 0 deletions utils/extra_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import os
import yaml
import folder_paths
import logging

def load_extra_path_config(yaml_path):
with open(yaml_path, 'r') as stream:
config = yaml.safe_load(stream)
for c in config:
conf = config[c]
if conf is None:
continue
base_path = None
if "base_path" in conf:
base_path = conf.pop("base_path")
base_path = os.path.expanduser(base_path)
for x in conf:
for y in conf[x].split("\n"):
if len(y) == 0:
continue
full_path = y
if base_path is not None:
full_path = os.path.join(base_path, full_path)
logging.info("Adding extra search path {} {}".format(x, full_path))
folder_paths.add_model_folder_path(x, full_path)

0 comments on commit 9fa8faa

Please sign in to comment.