forked from rbbrdckybk/dream-factory
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.py
201 lines (174 loc) · 6.52 KB
/
setup.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# Copyright 2021 - 2023, Bill Kennedy
# SPDX-License-Identifier: MIT
# Dream Factory setup script
# https://github.com/rbbrdckybk/dream-factory
import argparse
import datetime
import shlex
import subprocess
import shutil
import glob
import os
from os.path import exists, isdir, basename
from datetime import datetime as dt
# use this to give short messages summarizing what's
# happening when verbose = False
def msg(text, verbose = False):
if not verbose:
print(text)
# execute subprocess
def exec(command, verbose = False, shell = False):
if verbose:
subprocess.run(shlex.split(command), shell=shell)
else:
subprocess.run(shlex.split(command), stdout=subprocess.DEVNULL, shell=shell)
# execute subprocess in another working dir
def exec_cwd(command, wd, verbose = False, shell = False):
if verbose:
subprocess.run(shlex.split(command), cwd=(wd), shell=shell)
else:
subprocess.run(shlex.split(command), cwd=(wd), stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, shell=shell)
# grabs all required git repos
def clone_repos(verbose = False, shell = False):
print('\nCloning all required repositories:')
base = 'git clone https://github.com/'
repos = [ \
'CompVis/taming-transformers', \
'openai/CLIP', \
'rbbrdckybk/stable-diffusion', \
'xinntao/Real-ESRGAN' \
]
for repo in repos:
msg(' fetching ' + repo + '...', verbose)
exec(base+repo, verbose, shell)
# installs all dependancies via pip
def install_pytorch(verbose = False, shell = False):
print('\nInstalling Pytorch (this may take several minutes - be patient do not abort!):')
cmd = 'conda install -y pandas pytorch torchvision torchaudio pytorch-cuda=11.7 -c pytorch -c nvidia'
exec(cmd, verbose, shell)
# installs all dependancies via pip
def install_dependencies(verbose = False, shell = False):
print('\nChecking for required dependencies:')
base = 'pip install --no-input '
packages = [ \
#'transformers', \
#'diffusers', \
'cherrypy', \
#'ftfy', \
#'numpy', \
'pillow', \
#'omegaconf', \
#'pytorch-lightning', \
#'kornia', \
#'imageio', \
#'imageio-ffmpeg', \
#'einops', \
#'torch-fidelity', \
#'opencv-python', \
'datetime', \
'psutil', \
#'basicsr', \
#'facexlib', \
#'gfpgan', \
'requests', \
'IPTCInfo3'
]
packages.sort()
for package in packages:
msg(' installing ' + package + '...', verbose)
exec(base+package, verbose, shell)
# perform real-esrgan setup
def setup_esrgan(verbose = False, shell = False):
print('\nSetting up Real-ESRGAN:')
cmd = 'curl -L -o Real-ESRGAN/experiments/pretrained_models/RealESRGAN_x4plus.pth -C - https://github.com/xinntao/Real-ESRGAN/releases/download/v0.1.0/RealESRGAN_x4plus.pth'
msg(' downloading pre-trained models...', verbose)
exec(cmd, verbose, shell)
cwd = os.getcwd()
cmd = 'python setup.py develop'
msg(' running ESRGAN setup script...', verbose)
exec_cwd(cmd, cwd + os.path.sep + 'Real-ESRGAN', verbose, shell)
# for debugging
def test(verbose = False, shell = False):
print('\nTesting dummy call:')
base = 'thiswillfail'
exec(base, verbose, shell)
# create config.txt file
def create_config():
if not os.path.exists('config.txt'):
print('\nCreating config.txt containing default settings...')
shutil.copy2('config-default.txt', 'config.txt')
else:
print('\nExisting config.txt found, leaving it intact...')
# updates these repos to the latest version
def update(shell = False):
print('\nChecking for updates:')
cmd = 'git pull'
cwd = os.getcwd()
try:
print('updating dream-factory...')
exec(cmd, True, shell)
#print('updating stable-diffusion...')
#exec_cwd(cmd, cwd + os.path.sep + 'stable-diffusion', True, shell)
install_dependencies(False, shell)
except FileNotFoundError:
print('\nUnable to find git executable - try re-running update with the --shell option, e.g.:')
print(' python setup.py --shell --update\n')
exit(-1)
# entry point
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument(
"--verbose",
action='store_true',
help="display all output from subprocesses"
)
parser.add_argument(
"--force",
action='store_true',
help="force setup to run over a previous install"
)
parser.add_argument(
"--shell",
action='store_true',
help="enable shell calls; try this if setup cannot find your executables"
)
parser.add_argument(
"--update",
action='store_true',
help="updates a previous installation"
)
opt = parser.parse_args()
verbose = opt.verbose
shell = opt.shell
if opt.update:
if os.path.exists('logs'):
# user is requesting an update
update(shell)
else:
print('\nNo previous installation detected; aborting update.')
print('Run without the --update switch first.\n')
else:
if opt.force or not os.path.exists('stable-diffusion'):
# either no previous installation or the user is forcing re-install
try:
#clone_repos(verbose, shell)
install_pytorch(verbose, shell)
install_dependencies(verbose, shell)
#setup_esrgan(verbose, shell)
except FileNotFoundError:
print('\nUnable to find executable - try re-running setup with the --shell option, e.g.:')
print(' python setup.py --shell --force\n')
exit(-1)
create_config()
#checkpoint_path = 'stable-diffusion\models\ldm\stable-diffusion-v1'
#if not os.path.exists(checkpoint_path):
# os.makedirs(checkpoint_path)
#print('\n\nAll done - don\'t forget to place your model.ckpt file in this directory! : ')
#print(checkpoint_path + '\n')
if not os.path.exists('output'):
os.makedirs('output')
print('\n\nAll done! - don\'t forget to add your automatic1111 repo location to config.txt!')
else:
print('\nPrevious installation detected; aborting setup.')
print('If you really want to run setup again, use the --force switch.')
print('If you want to update this installation, use the --update switch.\n')