Upload paths.py
Browse files
paths.py
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import argparse
|
| 2 |
+
import os
|
| 3 |
+
import sys
|
| 4 |
+
import modules.safe
|
| 5 |
+
|
| 6 |
+
script_path = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
|
| 7 |
+
|
| 8 |
+
# Parse the --data-dir flag first so we can use it as a base for our other argument default values
|
| 9 |
+
parser = argparse.ArgumentParser(add_help=False)
|
| 10 |
+
parser.add_argument("--data-dir", type=str, default=os.path.dirname(os.path.dirname(os.path.realpath(__file__))), help="base path where all user data is stored",)
|
| 11 |
+
cmd_opts_pre = parser.parse_known_args()[0]
|
| 12 |
+
data_path = cmd_opts_pre.data_dir
|
| 13 |
+
models_path = os.path.join(data_path, "models")
|
| 14 |
+
|
| 15 |
+
# data_path = cmd_opts_pre.data
|
| 16 |
+
sys.path.insert(0, script_path)
|
| 17 |
+
|
| 18 |
+
# search for directory of stable diffusion in following places
|
| 19 |
+
sd_path = None
|
| 20 |
+
possible_sd_paths = [os.path.join(script_path, '/content/cos/MyDrive/sd/stablediffusion'), '.', os.path.dirname(script_path)]
|
| 21 |
+
for possible_sd_path in possible_sd_paths:
|
| 22 |
+
if os.path.exists(os.path.join(possible_sd_path, 'ldm/models/diffusion/ddpm.py')):
|
| 23 |
+
sd_path = os.path.abspath(possible_sd_path)
|
| 24 |
+
break
|
| 25 |
+
|
| 26 |
+
assert sd_path is not None, "Couldn't find Stable Diffusion in any of: " + str(possible_sd_paths)
|
| 27 |
+
|
| 28 |
+
path_dirs = [
|
| 29 |
+
(sd_path, 'ldm', 'Stable Diffusion', []),
|
| 30 |
+
(os.path.join(sd_path, 'src/taming-transformers'), 'taming', 'Taming Transformers', []),
|
| 31 |
+
(os.path.join(sd_path, 'src/codeformer'), 'inference_codeformer.py', 'CodeFormer', []),
|
| 32 |
+
(os.path.join(sd_path, 'src/blip'), 'models/blip.py', 'BLIP', []),
|
| 33 |
+
(os.path.join(sd_path, 'src/k-diffusion'), 'k_diffusion/sampling.py', 'k_diffusion', ["atstart"]),
|
| 34 |
+
]
|
| 35 |
+
|
| 36 |
+
paths = {}
|
| 37 |
+
|
| 38 |
+
for d, must_exist, what, options in path_dirs:
|
| 39 |
+
must_exist_path = os.path.abspath(os.path.join(script_path, d, must_exist))
|
| 40 |
+
if not os.path.exists(must_exist_path):
|
| 41 |
+
print(f"Warning: {what} not found at path {must_exist_path}", file=sys.stderr)
|
| 42 |
+
else:
|
| 43 |
+
d = os.path.abspath(d)
|
| 44 |
+
if "atstart" in options:
|
| 45 |
+
sys.path.insert(0, d)
|
| 46 |
+
else:
|
| 47 |
+
sys.path.append(d)
|
| 48 |
+
paths[what] = d
|
| 49 |
+
|
| 50 |
+
class Prioritize:
|
| 51 |
+
def __init__(self, name):
|
| 52 |
+
self.name = name
|
| 53 |
+
self.path = None
|
| 54 |
+
|
| 55 |
+
def __enter__(self):
|
| 56 |
+
self.path = sys.path.copy()
|
| 57 |
+
sys.path = [paths[self.name]] + sys.path
|
| 58 |
+
|
| 59 |
+
def __exit__(self, exc_type, exc_val, exc_tb):
|
| 60 |
+
sys.path = self.path
|
| 61 |
+
self.path = None
|