Path: blob/main/AUTOMATIC1111_files/paths.py
540 views
import argparse1import os2import sys3from modules.paths_internal import models_path, script_path, data_path, extensions_dir, extensions_builtin_dir4import modules.safe56# data_path = cmd_opts_pre.data7sys.path.insert(0, script_path)89# search for directory of stable diffusion in following places10sd_path = None11possible_sd_paths = [os.path.join(script_path, '/content/gdrive/MyDrive/sd/stablediffusion'), '.', os.path.dirname(script_path)]12for possible_sd_path in possible_sd_paths:13if os.path.exists(os.path.join(possible_sd_path, 'ldm/models/diffusion/ddpm.py')):14sd_path = os.path.abspath(possible_sd_path)15break1617assert sd_path is not None, "Couldn't find Stable Diffusion in any of: " + str(possible_sd_paths)1819path_dirs = [20(sd_path, 'ldm', 'Stable Diffusion', []),21(os.path.join(sd_path, 'src/taming-transformers'), 'taming', 'Taming Transformers', []),22(os.path.join(sd_path, 'src/codeformer'), 'inference_codeformer.py', 'CodeFormer', []),23(os.path.join(sd_path, 'src/blip'), 'models/blip.py', 'BLIP', []),24(os.path.join(sd_path, 'src/k-diffusion'), 'k_diffusion/sampling.py', 'k_diffusion', ["atstart"]),25]2627paths = {}2829for d, must_exist, what, options in path_dirs:30must_exist_path = os.path.abspath(os.path.join(script_path, d, must_exist))31if not os.path.exists(must_exist_path):32print(f"Warning: {what} not found at path {must_exist_path}", file=sys.stderr)33else:34d = os.path.abspath(d)35if "atstart" in options:36sys.path.insert(0, d)37else:38sys.path.append(d)39paths[what] = d4041class Prioritize:42def __init__(self, name):43self.name = name44self.path = None4546def __enter__(self):47self.path = sys.path.copy()48sys.path = [paths[self.name]] + sys.path4950def __exit__(self, exc_type, exc_val, exc_tb):51sys.path = self.path52self.path = None535455