Skip to content

Commit c1d830f

Browse files
committed
Standalone mode improvements
* Make standalone mode more self-aware if installed as webui extension * Fix commit retrieval for standalone mode if not installed as webui extension
1 parent 88a22c4 commit c1d830f

File tree

4 files changed

+47
-11
lines changed

4 files changed

+47
-11
lines changed

main.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,37 @@
33
# you may want to change the working directory to the stable-diffusion-webui root.
44

55
import argparse
6-
import src.common_ui
6+
import os
7+
import pathlib
8+
import builtins
9+
10+
import src.misc
11+
12+
def maybe_chdir():
13+
"""Detects if DepthMap was installed as a stable-diffusion-webui script, but run without current directory set to
14+
the stable-diffusion-webui root. Changes current directory if needed, to aviod clutter."""
15+
try:
16+
file_path = pathlib.Path(__file__)
17+
path = file_path.parts
18+
while len(path) > 0 and path[-1] != src.misc.REPOSITORY_NAME:
19+
path = path[:-1]
20+
if len(path) >= 2 and path[-1] == src.misc.REPOSITORY_NAME and path[-2] == "extensions":
21+
path = path[:-2]
22+
listdir = os.listdir(str(pathlib.Path(*path)))
23+
if 'launch.py' in listdir and 'webui.py':
24+
os.chdir(str(pathlib.Path(**path)))
25+
except:
26+
pass
27+
728

829
if __name__ == '__main__':
930
parser = argparse.ArgumentParser()
1031
parser.add_argument("--listen", help="Create public link")
32+
parser.add_argument("--no_chdir", help="Do not try to use the root of stable-diffusion-webui")
1133
args = parser.parse_args()
1234

35+
print(f"{src.misc.SCRIPT_FULL_NAME} starts in standalone mode!")
36+
import src.common_ui
37+
if not args.no_chdir:
38+
maybe_chdir()
1339
src.common_ui.on_ui_tabs().launch(share=args.listen)

src/backbone.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,9 @@ def get_hide_dirs():
6161
return modules.shared.hide_dirs
6262
except:
6363
# Standalone backbone
64-
print("DepthMap did not detect stable-duiffusion-webui; launching with the standalone backbone.\n"
65-
"The standalone backbone is not on par with the stable-duiffusion-webui backbone.\n"
66-
"Some features may be missing or work differently. Please report bugs.\n")
64+
print( # " DepthMap did not detect stable-duiffusion-webui; launching with the standalone backbone.\n"
65+
" The standalone mode is not on par with the stable-duiffusion-webui mode.\n"
66+
" Some features may be missing or work differently. Please report bugs.\n")
6767

6868
def save_image(image, path, basename, **kwargs):
6969
import os

src/depthmap_generation.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import numpy as np
77
import skimage.measure
88
from PIL import Image
9+
import torch
910
from torchvision.transforms import Compose, transforms
1011

1112
# midas imports

src/misc.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,36 @@
11
import subprocess
22
import os
33
import pathlib
4-
import torch
4+
import builtins
55

66
def get_commit_hash():
7-
try:
7+
def call_git(dir):
88
return subprocess.check_output(
99
[os.environ.get("GIT", "git"), "rev-parse", "HEAD"],
10-
cwd=pathlib.Path.cwd().joinpath('extensions/stable-diffusion-webui-depthmap-script/'),
11-
shell=False,
12-
stderr=subprocess.DEVNULL,
13-
encoding='utf8').strip()[0:8]
10+
cwd=dir, shell=False, stderr=subprocess.DEVNULL, encoding='utf8').strip()[0:8]
11+
12+
try:
13+
file_path = pathlib.Path(__file__)
14+
path = file_path.parts
15+
while len(path) > 0 and path[-1] != REPOSITORY_NAME:
16+
path = path[:-1]
17+
if len(path) >= 2 and path[-1] == REPOSITORY_NAME and path[-2] == "extensions":
18+
return call_git(str(pathlib.Path(*path)))
19+
20+
return call_git(pathlib.Path.cwd().joinpath('extensions/stable-diffusion-webui-depthmap-script/'))
1421
except Exception:
1522
return "<none>"
1623

1724

25+
REPOSITORY_NAME = "stable-diffusion-webui-depthmap-script"
1826
SCRIPT_NAME = "DepthMap"
1927
SCRIPT_VERSION = "v0.4.1"
2028
SCRIPT_FULL_NAME = f"{SCRIPT_NAME} {SCRIPT_VERSION} ({get_commit_hash()})"
2129

2230

2331
def ensure_file_downloaded(filename, url, sha256_hash_prefix=None):
24-
# Do not check the hash every time - it is somewhat time-consuming
32+
import torch
33+
# Do not check the hash every time - it is somewhat time-consumin
2534
if os.path.exists(filename):
2635
return
2736

0 commit comments

Comments
 (0)