paths.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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. # data_path = cmd_opts_pre.data
  6. sys.path.insert(0, script_path)
  7. # search for directory of stable diffusion in following places
  8. sd_path = None
  9. possible_sd_paths = [os.path.join(script_path, 'repositories/stable-diffusion-stability-ai'), '.', os.path.dirname(script_path)]
  10. for possible_sd_path in possible_sd_paths:
  11. if os.path.exists(os.path.join(possible_sd_path, 'ldm/models/diffusion/ddpm.py')):
  12. sd_path = os.path.abspath(possible_sd_path)
  13. break
  14. assert sd_path is not None, f"Couldn't find Stable Diffusion in any of: {possible_sd_paths}"
  15. path_dirs = [
  16. (sd_path, 'ldm', 'Stable Diffusion', []),
  17. (os.path.join(sd_path, '../CodeFormer'), 'inference_codeformer.py', 'CodeFormer', []),
  18. (os.path.join(sd_path, '../BLIP'), 'models/blip.py', 'BLIP', []),
  19. (os.path.join(sd_path, '../k-diffusion'), 'k_diffusion/sampling.py', 'k_diffusion', ["atstart"]),
  20. ]
  21. paths = {}
  22. for d, must_exist, what, options in path_dirs:
  23. must_exist_path = os.path.abspath(os.path.join(script_path, d, must_exist))
  24. if not os.path.exists(must_exist_path):
  25. print(f"Warning: {what} not found at path {must_exist_path}", file=sys.stderr)
  26. else:
  27. d = os.path.abspath(d)
  28. if "atstart" in options:
  29. sys.path.insert(0, d)
  30. else:
  31. sys.path.append(d)
  32. paths[what] = d
  33. class Prioritize:
  34. def __init__(self, name):
  35. self.name = name
  36. self.path = None
  37. def __enter__(self):
  38. self.path = sys.path.copy()
  39. sys.path = [paths[self.name]] + sys.path
  40. def __exit__(self, exc_type, exc_val, exc_tb):
  41. sys.path = self.path
  42. self.path = None