paths.py 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import os
  2. import sys
  3. from modules.paths_internal import models_path, script_path, data_path, extensions_dir, extensions_builtin_dir # noqa: F401
  4. import modules.safe # noqa: F401
  5. def mute_sdxl_imports():
  6. """create fake modules that SDXL wants to import but doesn't actually use for our purposes"""
  7. class Dummy:
  8. pass
  9. module = Dummy()
  10. module.LPIPS = None
  11. sys.modules['taming.modules.losses.lpips'] = module
  12. module = Dummy()
  13. module.StableDataModuleFromConfig = None
  14. sys.modules['sgm.data'] = module
  15. # data_path = cmd_opts_pre.data
  16. sys.path.insert(0, script_path)
  17. # search for directory of stable diffusion in following places
  18. sd_path = None
  19. possible_sd_paths = [os.path.join(script_path, 'repositories/stable-diffusion-stability-ai'), '.', os.path.dirname(script_path)]
  20. for possible_sd_path in possible_sd_paths:
  21. if os.path.exists(os.path.join(possible_sd_path, 'ldm/models/diffusion/ddpm.py')):
  22. sd_path = os.path.abspath(possible_sd_path)
  23. break
  24. assert sd_path is not None, f"Couldn't find Stable Diffusion in any of: {possible_sd_paths}"
  25. mute_sdxl_imports()
  26. path_dirs = [
  27. (sd_path, 'ldm', 'Stable Diffusion', []),
  28. (os.path.join(sd_path, '../generative-models'), 'sgm', 'Stable Diffusion XL', ["sgm"]),
  29. (os.path.join(sd_path, '../CodeFormer'), 'inference_codeformer.py', 'CodeFormer', []),
  30. (os.path.join(sd_path, '../BLIP'), 'models/blip.py', 'BLIP', []),
  31. (os.path.join(sd_path, '../k-diffusion'), 'k_diffusion/sampling.py', 'k_diffusion', ["atstart"]),
  32. ]
  33. paths = {}
  34. for d, must_exist, what, options in path_dirs:
  35. must_exist_path = os.path.abspath(os.path.join(script_path, d, must_exist))
  36. if not os.path.exists(must_exist_path):
  37. print(f"Warning: {what} not found at path {must_exist_path}", file=sys.stderr)
  38. else:
  39. d = os.path.abspath(d)
  40. if "atstart" in options:
  41. sys.path.insert(0, d)
  42. elif "sgm" in options:
  43. # Stable Diffusion XL repo has scripts dir with __init__.py in it which ruins every extension's scripts dir, so we
  44. # import sgm and remove it from sys.path so that when a script imports scripts.something, it doesbn't use sgm's scripts dir.
  45. sys.path.insert(0, d)
  46. import sgm # noqa: F401
  47. sys.path.pop(0)
  48. else:
  49. sys.path.append(d)
  50. paths[what] = d