TheLastBen commited on
Commit
e450b04
1 Parent(s): a4e0601

Create Scripts/mainpaperspaceA1111.py

Browse files
Files changed (1) hide show
  1. Scripts/mainpaperspaceA1111.py +197 -0
Scripts/mainpaperspaceA1111.py ADDED
@@ -0,0 +1,197 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ from IPython.display import clear_output
3
+ from subprocess import call, getoutput
4
+ import time
5
+ import sys
6
+ import fileinput
7
+ import ipywidgets as widgets
8
+
9
+
10
+
11
+ def Deps(force_reinstall):
12
+
13
+ if not force_reinstall and os.path.exists('/usr/local/lib/python3.9/dist-packages/safetensors'):
14
+ os.chdir('/notebooks')
15
+ if not os.path.exists('Latest_Notebooks'):
16
+ call('mkdir Latest_Notebooks', shell=True)
17
+ else:
18
+ call('rm -r Latest_Notebooks', shell=True)
19
+ call('mkdir Latest_Notebooks', shell=True)
20
+ os.chdir('/notebooks/Latest_Notebooks')
21
+ call('wget -q -i https://huggingface.co/datasets/TheLastBen/PPS/raw/main/Notebooks.txt', shell=True)
22
+ call('rm Notebooks.txt', shell=True)
23
+ os.chdir('/notebooks')
24
+ print('Modules and notebooks updated, dependencies already installed')
25
+
26
+ else:
27
+ print('Installing the dependencies...')
28
+ call("pip install --root-user-action=ignore --no-deps -q accelerate==0.12.0", shell=True, stdout=open('/dev/null', 'w'))
29
+ if not os.path.exists('/usr/local/lib/python3.9/dist-packages/safetensors'):
30
+ os.chdir('/usr/local/lib/python3.9/dist-packages')
31
+ call("rm -r torch torch-1.12.0+cu116.dist-info torchaudio* torchvision* PIL Pillow* transformers* numpy* gdown*", shell=True, stdout=open('/dev/null', 'w'))
32
+
33
+ os.chdir('/notebooks')
34
+ if not os.path.exists('Latest_Notebooks'):
35
+ call('mkdir Latest_Notebooks', shell=True)
36
+ else:
37
+ call('rm -r Latest_Notebooks', shell=True)
38
+ call('mkdir Latest_Notebooks', shell=True)
39
+ os.chdir('/notebooks/Latest_Notebooks')
40
+ call('wget -q -i https://huggingface.co/datasets/TheLastBen/PPS/raw/main/Notebooks.txt', shell=True)
41
+ call('rm Notebooks.txt', shell=True)
42
+ os.chdir('/notebooks')
43
+ call('wget -q -i https://raw.githubusercontent.com/TheLastBen/fast-stable-diffusion/main/Dependencies/aptdeps.txt', shell=True)
44
+ os.chdir('/notebooks')
45
+ if not os.path.exists('/models'):
46
+ call('mkdir /models', shell=True)
47
+ if not os.path.exists('/notebooks/models'):
48
+ call('ln -s /models /notebooks', shell=True)
49
+ if os.path.exists('/deps'):
50
+ call("rm -r /deps", shell=True)
51
+ call('mkdir /deps', shell=True)
52
+ if not os.path.exists('cache'):
53
+ call('mkdir cache', shell=True)
54
+ os.chdir('/deps')
55
+ call('wget -q -i https://raw.githubusercontent.com/TheLastBen/fast-stable-diffusion/main/Dependencies/aptdeps.txt', shell=True)
56
+ call('dpkg -i *.deb', shell=True, stdout=open('/dev/null', 'w'))
57
+ call('wget -q https://huggingface.co/TheLastBen/dependencies/resolve/main/pps.tar.zst', shell=True, stdout=open('/dev/null', 'w'))
58
+ call('tar -C / --zstd -xf pps.tar.zst', shell=True, stdout=open('/dev/null', 'w'))
59
+ call("sed -i 's@~/.cache@/notebooks/cache@' /usr/local/lib/python3.9/dist-packages/transformers/utils/hub.py", shell=True)
60
+ os.chdir('/notebooks')
61
+ call("git clone --depth 1 -q --branch updt https://github.com/TheLastBen/diffusers /diffusers", shell=True, stdout=open('/dev/null', 'w'))
62
+ if not os.path.exists('/notebooks/diffusers'):
63
+ call('ln -s /diffusers /notebooks', shell=True)
64
+ call("rm -r /deps", shell=True)
65
+ os.chdir('/notebooks')
66
+ clear_output()
67
+
68
+ done()
69
+
70
+
71
+
72
+ def repo():
73
+
74
+ print('Installing/Updating the repo...')
75
+ os.chdir('/notebooks')
76
+ if not os.path.exists('/notebooks/sd/stablediffusion'):
77
+ call('wget -q -O sd_rep.tar.zst https://huggingface.co/TheLastBen/dependencies/resolve/main/sd_rep.tar.zst', shell=True)
78
+ call('tar --zstd -xf sd_rep.tar.zst', shell=True)
79
+ call('rm sd_rep.tar.zst', shell=True)
80
+
81
+ os.chdir('/notebooks/sd')
82
+ if not os.path.exists('stable-diffusion-webui'):
83
+ call('git clone -q --depth 1 --branch master https://github.com/AUTOMATIC1111/stable-diffusion-webui', shell=True)
84
+
85
+ os.chdir('/notebooks/sd/stable-diffusion-webui/')
86
+ call('git reset --hard', shell=True, stdout=open('/dev/null', 'w'))
87
+ print('')
88
+ call('git pull', shell=True, stdout=open('/dev/null', 'w'))
89
+ os.chdir('/notebooks')
90
+ clear_output()
91
+ done()
92
+
93
+
94
+ def mdl(Original_Model_Version, Path_to_MODEL, MODEL_LINK, safetensors, Temporary_Storage):
95
+ import gdown
96
+ if Path_to_MODEL !='':
97
+ if os.path.exists(str(Path_to_MODEL)):
98
+ print('Using the trained model.')
99
+ model=Path_to_MODEL
100
+ else:
101
+ print('Wrong path, check that the path to the model is correct')
102
+
103
+ elif MODEL_LINK != "":
104
+ modelname="model.safetensors" if safetensors else "model.ckpt"
105
+ if Temporary_Storage:
106
+ model=f'/models/{modelname}'
107
+ else:
108
+ model=f'/notebooks/sd/stable-diffusion-webui/models/Stable-diffusion/{modelname}'
109
+ if os.path.exists(model):
110
+ call('rm '+model, shell=True)
111
+ gdown.download(url=MODEL_LINK, output=model, quiet=False, fuzzy=True)
112
+
113
+ if os.path.exists(model) and os.path.getsize(model) > 1810671599:
114
+ clear_output()
115
+ print('Model downloaded, using the trained model.')
116
+ else:
117
+ print('Wrong link, check that the link is valid')
118
+
119
+ else:
120
+ if Original_Model_Version == "v1.5":
121
+ model="/datasets/stable-diffusion-classic/v1-5-pruned-emaonly.ckpt"
122
+ print('Using the original V1.5 model')
123
+ elif Original_Model_Version == "v2-512":
124
+ model="dataset"
125
+ print('Using the original V2-512 model')
126
+ elif Original_Model_Version == "v2-768":
127
+ model="/datasets/stable-diffusion-v2-1/stable-diffusion-2-1/v2-1_768-nonema-pruned.safetensors"
128
+ print('Using the original V2-768 model')
129
+ else:
130
+ model=""
131
+ print('Wrong model version')
132
+
133
+ return model
134
+
135
+
136
+ def sd(User, Password, Use_localtunnel):
137
+
138
+ auth=f"--gradio-auth {User}:{Password}"
139
+ if User =="" or Password=="":
140
+ auth=""
141
+
142
+ if not os.path.exists('/usr/lib/node_modules/localtunnel'):
143
+ call('npm install -g localtunnel --silent', shell=True, stdout=open('/dev/null', 'w'))
144
+ clear_output()
145
+
146
+
147
+ share=''
148
+ call('wget -q -O /usr/local/lib/python3.9/dist-packages/gradio/blocks.py https://raw.githubusercontent.com/TheLastBen/fast-stable-diffusion/main/AUTOMATIC1111_files/blocks.py', shell=True)
149
+
150
+ if not Use_localtunnel:
151
+ share='--share'
152
+
153
+ else:
154
+ share=''
155
+ os.chdir('/notebooks')
156
+ call('nohup lt --port 7860 > srv.txt 2>&1 &', shell=True)
157
+ time.sleep(2)
158
+ call("grep -o 'https[^ ]*' /notebooks/srv.txt >srvr.txt", shell=True)
159
+ time.sleep(2)
160
+ srv= getoutput('cat /notebooks/srvr.txt')
161
+
162
+ for line in fileinput.input('/usr/local/lib/python3.9/dist-packages/gradio/blocks.py', inplace=True):
163
+ if line.strip().startswith('self.server_name ='):
164
+ line = f' self.server_name = "{srv[8:]}"\n'
165
+ if line.strip().startswith('self.server_port ='):
166
+ line = ' self.server_port = 443\n'
167
+ if line.strip().startswith('self.protocol = "https"'):
168
+ line = ' self.protocol = "https"\n'
169
+ if line.strip().startswith('if self.local_url.startswith("https") or self.is_colab'):
170
+ line = ''
171
+ if line.strip().startswith('else "http"'):
172
+ line = ''
173
+ sys.stdout.write(line)
174
+
175
+ call('rm /notebooks/srv.txt', shell=True)
176
+ call('rm /notebooks/srvr.txt', shell=True)
177
+
178
+ os.chdir('/notebooks/sd/stable-diffusion-webui/modules')
179
+ call('wget -q -O paths.py https://raw.githubusercontent.com/TheLastBen/fast-stable-diffusion/main/AUTOMATIC1111_files/paths.py', shell=True)
180
+ call("sed -i 's@/content/gdrive/MyDrive/sd/stablediffusion@/notebooks/sd/stablediffusion@' /notebooks/sd/stable-diffusion-webui/modules/paths.py", shell=True)
181
+ os.chdir('/notebooks/sd/stable-diffusion-webui')
182
+ clear_output()
183
+
184
+ configf="--disable-console-progressbars --no-half-vae --disable-safe-unpickle --api --xformers --medvram --skip-version-check "+auth+" "+share
185
+
186
+ return configf, auth, share
187
+
188
+
189
+ def done():
190
+ done = widgets.Button(
191
+ description='Done!',
192
+ disabled=True,
193
+ button_style='success',
194
+ tooltip='',
195
+ icon='check'
196
+ )
197
+ display(done)