TheLastBen commited on
Commit
e656995
1 Parent(s): 7664958

Update Scripts/mainpaperspaceA1111.py

Browse files
Files changed (1) hide show
  1. Scripts/mainpaperspaceA1111.py +179 -122
Scripts/mainpaperspaceA1111.py CHANGED
@@ -1,94 +1,117 @@
1
  import os
2
  from IPython.display import clear_output
3
- from subprocess import call, getoutput, run
4
  import time
 
 
5
  import sys
6
  import fileinput
7
- import ipywidgets as widgets
8
  from torch.hub import download_url_to_file
9
  from urllib.parse import urlparse
10
  import re
11
 
12
 
13
-
14
  def Deps(force_reinstall):
15
 
16
- if not force_reinstall and os.path.exists('/usr/local/lib/python3.9/dist-packages/safetensors'):
17
- ntbk()
18
  print('Modules and notebooks updated, dependencies already installed')
19
-
20
  else:
21
  print('Installing the dependencies...')
22
- call("pip install --root-user-action=ignore --no-deps -q accelerate==0.12.0", shell=True, stdout=open('/dev/null', 'w'))
23
- if not os.path.exists('/usr/local/lib/python3.9/dist-packages/safetensors'):
24
- os.chdir('/usr/local/lib/python3.9/dist-packages')
25
- call("rm -r torch torch-1.12.1+cu116.dist-info torchaudio* torchvision* PIL Pillow* transformers* numpy* gdown*", shell=True, stdout=open('/dev/null', 'w'))
26
- ntbk()
27
- if not os.path.exists('/models'):
28
- call('mkdir /models', shell=True)
29
- if not os.path.exists('/notebooks/models'):
30
- call('ln -s /models /notebooks', shell=True)
31
- if os.path.exists('/deps'):
32
- call("rm -r /deps", shell=True)
33
- call('mkdir /deps', shell=True)
34
  if not os.path.exists('cache'):
35
  call('mkdir cache', shell=True)
36
- os.chdir('/deps')
37
- call('wget -q -i https://raw.githubusercontent.com/TheLastBen/fast-stable-diffusion/main/Dependencies/aptdeps.txt', shell=True)
38
- call('dpkg -i *.deb', shell=True, stdout=open('/dev/null', 'w'))
39
- call('wget -q https://huggingface.co/TheLastBen/dependencies/resolve/main/ppsdeps.tar.zst', shell=True, stdout=open('/dev/null', 'w'))
40
- call('tar -C / --zstd -xf ppsdeps.tar.zst', shell=True, stdout=open('/dev/null', 'w'))
41
- call("sed -i 's@~/.cache@/notebooks/cache@' /usr/local/lib/python3.9/dist-packages/transformers/utils/hub.py", shell=True)
42
- os.chdir('/notebooks')
43
- call("git clone --depth 1 -q --branch main https://github.com/TheLastBen/diffusers /diffusers", shell=True, stdout=open('/dev/null', 'w'))
44
- call("pip install --root-user-action=ignore -qq gradio==3.23", shell=True, stdout=open('/dev/null', 'w'))
45
- if not os.path.exists('/notebooks/diffusers'):
46
- call('ln -s /diffusers /notebooks', shell=True)
47
- call("rm -r /deps", shell=True)
48
- os.chdir('/notebooks')
49
  clear_output()
50
 
51
  done()
52
 
 
53
 
54
- def ntbk():
55
-
56
- os.chdir('/notebooks')
57
  if not os.path.exists('Latest_Notebooks'):
58
  call('mkdir Latest_Notebooks', shell=True)
59
  else:
60
  call('rm -r Latest_Notebooks', shell=True)
61
  call('mkdir Latest_Notebooks', shell=True)
62
- os.chdir('/notebooks/Latest_Notebooks')
63
- call('wget -q -i https://huggingface.co/datasets/TheLastBen/PPS/raw/main/Notebooks.txt', shell=True)
64
  call('rm Notebooks.txt', shell=True)
65
- os.chdir('/notebooks')
66
 
67
 
68
- def repo():
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
69
 
70
- print('Installing/Updating the repo...')
71
- os.chdir('/notebooks')
72
- if not os.path.exists('/notebooks/sd/stablediffusion'):
73
- call('wget -q -O sd_rep.tar.zst https://huggingface.co/TheLastBen/dependencies/resolve/main/sd_rep.tar.zst', shell=True)
74
- call('tar --zstd -xf sd_rep.tar.zst', shell=True)
75
- call('rm sd_rep.tar.zst', shell=True)
76
 
77
- os.chdir('/notebooks/sd')
78
- if not os.path.exists('stable-diffusion-webui'):
79
- call('git clone -q --depth 1 --branch master https://github.com/AUTOMATIC1111/stable-diffusion-webui', shell=True)
80
 
81
- os.chdir('/notebooks/sd/stable-diffusion-webui/')
82
- call('git reset --hard', shell=True, stdout=open('/dev/null', 'w'))
83
  print('')
84
- call('git pull', shell=True, stdout=open('/dev/null', 'w'))
85
- os.chdir('/notebooks')
86
  clear_output()
87
  done()
88
 
89
 
90
- def mdl(Original_Model_Version, Path_to_MODEL, MODEL_LINK, safetensors, Temporary_Storage):
 
 
91
  import gdown
 
 
 
 
 
92
  if Path_to_MODEL !='':
93
  if os.path.exists(str(Path_to_MODEL)):
94
  print('Using the trained model.')
@@ -98,10 +121,7 @@ def mdl(Original_Model_Version, Path_to_MODEL, MODEL_LINK, safetensors, Temporar
98
 
99
  elif MODEL_LINK != "":
100
  modelname="model.safetensors" if safetensors else "model.ckpt"
101
- if Temporary_Storage:
102
- model=f'/models/{modelname}'
103
- else:
104
- model=f'/notebooks/sd/stable-diffusion-webui/models/Stable-diffusion/{modelname}'
105
  if os.path.exists(model):
106
  call('rm '+model, shell=True)
107
  gdown.download(url=MODEL_LINK, output=model, quiet=False, fuzzy=True)
@@ -114,26 +134,34 @@ def mdl(Original_Model_Version, Path_to_MODEL, MODEL_LINK, safetensors, Temporar
114
 
115
  else:
116
  if Original_Model_Version == "v1.5":
117
- model="/datasets/stable-diffusion-classic/SDv1.5.ckpt"
118
  print('Using the original V1.5 model')
119
  elif Original_Model_Version == "v2-512":
120
- model="/datasets/stable-diffusion-v2-1-base-diffusers/stable-diffusion-2-1-base/v2-1_512-nonema-pruned.safetensors"
121
- print('Using the original V2-512 model')
 
 
 
 
 
 
 
122
  elif Original_Model_Version == "v2-768":
123
- model="/datasets/stable-diffusion-v2-1/stable-diffusion-2-1/v2-1_768-nonema-pruned.safetensors"
124
  print('Using the original V2-768 model')
125
  else:
126
- model=""
127
- print('Wrong model version')
128
  try:
129
  model
130
  except:
131
- model="/notebooks/sd/stable-diffusion-webui/models/Stable-diffusion"
132
 
133
  return model
134
 
135
 
136
- def CN(ControlNet_Model, ControlNet_v2_Model):
 
137
 
138
  def download(url, model_dir):
139
 
@@ -145,18 +173,18 @@ def CN(ControlNet_Model, ControlNet_v2_Model):
145
  else:
146
  print(f"The model {filename} already exists")
147
 
148
-
149
- os.chdir('/notebooks/sd/stable-diffusion-webui/extensions')
150
  if not os.path.exists("sd-webui-controlnet"):
151
  call('git clone https://github.com/Mikubill/sd-webui-controlnet.git', shell=True)
152
- os.chdir('/notebooks')
153
  else:
154
  os.chdir('sd-webui-controlnet')
155
  call('git reset --hard', shell=True, stdout=open('/dev/null', 'w'), stderr=open('/dev/null', 'w'))
156
  call('git pull', shell=True, stdout=open('/dev/null', 'w'), stderr=open('/dev/null', 'w'))
157
- os.chdir('/notebooks')
158
 
159
- mdldir="/notebooks/sd/stable-diffusion-webui/extensions/sd-webui-controlnet/models"
160
  for filename in os.listdir(mdldir):
161
  if "_sd14v1" in filename:
162
  renamed = re.sub("_sd14v1", "-fp16", filename)
@@ -164,7 +192,7 @@ def CN(ControlNet_Model, ControlNet_v2_Model):
164
 
165
  call('wget -q -O CN_models.txt https://github.com/TheLastBen/fast-stable-diffusion/raw/main/AUTOMATIC1111_files/CN_models.txt', shell=True)
166
  call('wget -q -O CN_models_v2.txt https://github.com/TheLastBen/fast-stable-diffusion/raw/main/AUTOMATIC1111_files/CN_models_v2.txt', shell=True)
167
-
168
  with open("CN_models.txt", 'r') as f:
169
  mdllnk = f.read().splitlines()
170
  with open("CN_models_v2.txt", 'r') as d:
@@ -172,10 +200,10 @@ def CN(ControlNet_Model, ControlNet_v2_Model):
172
  call('rm CN_models.txt CN_models_v2.txt', shell=True)
173
 
174
  cfgnames=[os.path.basename(url).split('.')[0]+'.yaml' for url in mdllnk_v2]
175
- os.chdir('/notebooks/sd/stable-diffusion-webui/extensions/sd-webui-controlnet/models')
176
  for name in cfgnames:
177
  run(['cp', 'cldm_v21.yaml', name])
178
- os.chdir('/notebooks')
179
 
180
  if ControlNet_Model == "All" or ControlNet_Model == "all" :
181
  for lnk in mdllnk:
@@ -190,7 +218,7 @@ def CN(ControlNet_Model, ControlNet_v2_Model):
190
  clear_output()
191
 
192
 
193
- elif ControlNet_Model.isdigit() and int(ControlNet_Model)-1<14:
194
  download(mdllnk[int(ControlNet_Model)-1], mdldir)
195
  clear_output()
196
 
@@ -200,22 +228,26 @@ def CN(ControlNet_Model, ControlNet_v2_Model):
200
 
201
  else:
202
  print('Wrong ControlNet V1 choice, try again')
 
203
 
204
 
205
  if ControlNet_v2_Model == "All" or ControlNet_v2_Model == "all" :
206
  for lnk_v2 in mdllnk_v2:
207
  download(lnk_v2, mdldir)
208
- clear_output()
 
209
  done()
210
 
211
  elif ControlNet_v2_Model.isdigit() and int(ControlNet_v2_Model)-1<5:
212
  download(mdllnk_v2[int(ControlNet_v2_Model)-1], mdldir)
213
- clear_output()
 
214
  done()
215
 
216
  elif ControlNet_v2_Model == "none":
217
  pass
218
- clear_output()
 
219
  done()
220
 
221
  else:
@@ -223,68 +255,93 @@ def CN(ControlNet_Model, ControlNet_v2_Model):
223
 
224
 
225
 
 
226
 
227
- def sd(User, Password, Use_localtunnel, model):
228
-
 
 
229
  auth=f"--gradio-auth {User}:{Password}"
230
  if User =="" or Password=="":
231
  auth=""
232
-
233
- if not os.path.exists('/usr/lib/node_modules/localtunnel'):
234
- call('npm install -g localtunnel --silent', shell=True, stdout=open('/dev/null', 'w'))
235
- clear_output()
236
-
237
-
238
- share=''
239
- 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)
240
-
241
- if not Use_localtunnel:
242
- share='--share'
243
-
244
- else:
245
- share=''
246
- os.chdir('/notebooks')
247
- call('nohup lt --port 7860 > srv.txt 2>&1 &', shell=True)
248
- time.sleep(2)
249
- call("grep -o 'https[^ ]*' /notebooks/srv.txt >srvr.txt", shell=True)
250
- time.sleep(2)
251
- srv= getoutput('cat /notebooks/srvr.txt')
252
-
253
- for line in fileinput.input('/usr/local/lib/python3.9/dist-packages/gradio/blocks.py', inplace=True):
254
- if line.strip().startswith('self.server_name ='):
255
- line = f' self.server_name = "{srv[8:]}"\n'
256
- if line.strip().startswith('self.server_port ='):
257
- line = ' self.server_port = 443\n'
258
- if line.strip().startswith('self.protocol = "https"'):
259
- line = ' self.protocol = "https"\n'
260
- if line.strip().startswith('if self.local_url.startswith("https") or self.is_colab'):
261
- line = ''
262
- if line.strip().startswith('else "http"'):
263
- line = ''
264
- sys.stdout.write(line)
265
-
266
- call('rm srv.txt srvr.txt', shell=True)
267
-
268
- os.chdir('/notebooks/sd/stable-diffusion-webui/modules')
269
  call('wget -q -O paths.py https://raw.githubusercontent.com/TheLastBen/fast-stable-diffusion/main/AUTOMATIC1111_files/paths.py', shell=True)
270
- call("sed -i 's@ui.create_ui().*@ui.create_ui();shared.demo.queue(concurrency_count=999999,status_update_rate=0.1)@' /notebooks/sd/stable-diffusion-webui/webui.py", shell=True)
271
- call("sed -i 's@/content/gdrive/MyDrive/sd/stablediffusion@/notebooks/sd/stablediffusion@' /notebooks/sd/stable-diffusion-webui/modules/paths.py", shell=True)
272
- call("sed -i 's@\"quicksettings\": OptionInfo(.*@\"quicksettings\": OptionInfo(\"sd_model_checkpoint, sd_vae, CLIP_stop_at_last_layers, inpainting_mask_weight, initial_noise_multiplier\", \"Quicksettings list\"),@' /notebooks/sd/stable-diffusion-webui/modules/shared.py", shell=True)
273
- os.chdir('/notebooks/sd/stable-diffusion-webui')
274
  clear_output()
275
 
276
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
277
  if os.path.isfile(model):
278
  mdlpth="--ckpt "+model
279
  else:
280
  mdlpth="--ckpt-dir "+model
281
 
282
-
283
- configf="--disable-console-progressbars --no-half-vae --disable-safe-unpickle --api --no-download-sd-model --xformers --enable-insecure-extension-access --skip-version-check "+auth+" "+share+" "+mdlpth
284
 
285
  return configf
 
 
 
286
 
 
 
287
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
288
  def done():
289
  done = widgets.Button(
290
  description='Done!',
 
1
  import os
2
  from IPython.display import clear_output
3
+ from subprocess import call, getoutput, Popen, run
4
  import time
5
+ import ipywidgets as widgets
6
+ import requests
7
  import sys
8
  import fileinput
 
9
  from torch.hub import download_url_to_file
10
  from urllib.parse import urlparse
11
  import re
12
 
13
 
 
14
  def Deps(force_reinstall):
15
 
16
+ if not force_reinstall and os.path.exists('/usr/local/lib/python3.10/dist-packages/safetensors'):
17
+ ntbks()
18
  print('Modules and notebooks updated, dependencies already installed')
19
+ os.environ['PYTHONWARNINGS'] = 'ignore'
20
  else:
21
  print('Installing the dependencies...')
22
+ call('pip install --root-user-action=ignore --disable-pip-version-check --no-deps -qq gdown numpy==1.23.5 accelerate==0.12.0 --force-reinstall', shell=True, stdout=open('/dev/null', 'w'))
23
+ ntbks()
24
+ if os.path.exists('deps'):
25
+ call("rm -r deps", shell=True)
26
+ if os.path.exists('diffusers'):
27
+ call("rm -r diffusers", shell=True)
28
+ call('mkdir deps', shell=True)
 
 
 
 
 
29
  if not os.path.exists('cache'):
30
  call('mkdir cache', shell=True)
31
+ os.chdir('deps')
32
+ call('wget -q https://huggingface.co/TheLastBen/dependencies/resolve/main/rnpddeps-t2.tar.zst', shell=True, stdout=open('/dev/null', 'w'))
33
+ call('tar -C / --zstd -xf rnpddeps-t2.tar.zst', shell=True, stdout=open('/dev/null', 'w'))
34
+ call("sed -i 's@~/.cache@/workspace/cache@' /usr/local/lib/python3.10/dist-packages/transformers/utils/hub.py", shell=True)
35
+ os.chdir('/workspace')
36
+ call("git clone --depth 1 -q --branch main https://github.com/TheLastBen/diffusers", shell=True, stdout=open('/dev/null', 'w'))
37
+ call("rm -r deps", shell=True)
38
+ os.chdir('/workspace')
39
+ os.environ['PYTHONWARNINGS'] = 'ignore'
 
 
 
 
40
  clear_output()
41
 
42
  done()
43
 
44
+ def ntbks():
45
 
46
+ os.chdir('/workspace')
 
 
47
  if not os.path.exists('Latest_Notebooks'):
48
  call('mkdir Latest_Notebooks', shell=True)
49
  else:
50
  call('rm -r Latest_Notebooks', shell=True)
51
  call('mkdir Latest_Notebooks', shell=True)
52
+ os.chdir('/workspace/Latest_Notebooks')
53
+ call('wget -q -i https://huggingface.co/datasets/TheLastBen/RNPD/raw/main/Notebooks.txt', shell=True)
54
  call('rm Notebooks.txt', shell=True)
55
+ os.chdir('/workspace')
56
 
57
 
58
+ def repo(Huggingface_token_optional):
59
+
60
+ from slugify import slugify
61
+ from huggingface_hub import HfApi, CommitOperationAdd, create_repo
62
+
63
+ os.chdir('/workspace')
64
+ if Huggingface_token_optional!="":
65
+ username = HfApi().whoami(Huggingface_token_optional)["name"]
66
+ backup=f"https://USER:{Huggingface_token_optional}@huggingface.co/datasets/{username}/fast-stable-diffusion/resolve/main/sd_backup_rnpd.tar.zst"
67
+ response = requests.head(backup)
68
+ if response.status_code == 302:
69
+ print('Restoring the SD folder...')
70
+ open('/workspace/sd_backup_rnpd.tar.zst', 'wb').write(requests.get(backup).content)
71
+ call('tar --zstd -xf sd_backup_rnpd.tar.zst', shell=True)
72
+ call('rm sd_backup_rnpd.tar.zst', shell=True)
73
+ else:
74
+ print('Backup not found, using a fresh/existing repo...')
75
+ time.sleep(2)
76
+ if not os.path.exists('/workspace/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
+ os.chdir('/workspace/sd')
81
+ if not os.path.exists('stable-diffusion-webui'):
82
+ call('git clone -q --depth 1 --branch master https://github.com/AUTOMATIC1111/stable-diffusion-webui', shell=True)
83
+
84
+ else:
85
+ print('Installing/Updating the repo...')
86
+ os.chdir('/workspace')
87
+ if not os.path.exists('/workspace/sd/stablediffusion'):
88
+ call('wget -q -O sd_rep.tar.zst https://huggingface.co/TheLastBen/dependencies/resolve/main/sd_rep.tar.zst', shell=True)
89
+ call('tar --zstd -xf sd_rep.tar.zst', shell=True)
90
+ call('rm sd_rep.tar.zst', shell=True)
91
 
92
+ os.chdir('/workspace/sd')
93
+ if not os.path.exists('stable-diffusion-webui'):
94
+ call('git clone -q --depth 1 --branch master https://github.com/AUTOMATIC1111/stable-diffusion-webui', shell=True)
 
 
 
95
 
 
 
 
96
 
97
+ os.chdir('/workspace/sd/stable-diffusion-webui/')
98
+ call('git reset --hard', shell=True)
99
  print('')
100
+ call('git pull', shell=True)
101
+ os.chdir('/workspace')
102
  clear_output()
103
  done()
104
 
105
 
106
+
107
+ def mdls(Original_Model_Version, Path_to_MODEL, MODEL_LINK, safetensors):
108
+
109
  import gdown
110
+
111
+ if os.path.exists('/workspace/auto-models/SDv1-5.ckpt'):
112
+ call('mv /workspace/auto-models/* /workspace/sd/stable-diffusion-webui/models/Stable-diffusion', shell=True)
113
+ call('rm -r /workspace/auto-models', shell=True)
114
+
115
  if Path_to_MODEL !='':
116
  if os.path.exists(str(Path_to_MODEL)):
117
  print('Using the trained model.')
 
121
 
122
  elif MODEL_LINK != "":
123
  modelname="model.safetensors" if safetensors else "model.ckpt"
124
+ model=f'/workspace/sd/stable-diffusion-webui/models/Stable-diffusion/{modelname}'
 
 
 
125
  if os.path.exists(model):
126
  call('rm '+model, shell=True)
127
  gdown.download(url=MODEL_LINK, output=model, quiet=False, fuzzy=True)
 
134
 
135
  else:
136
  if Original_Model_Version == "v1.5":
137
+ model="/workspace/sd/stable-diffusion-webui/models/Stable-diffusion/SDv1-5.ckpt"
138
  print('Using the original V1.5 model')
139
  elif Original_Model_Version == "v2-512":
140
+ if not os.path.exists('/workspace/sd/stable-diffusion-webui/models/Stable-diffusion/SDv2-512.ckpt'):
141
+ print('Downlading the V2-512 model...')
142
+ model='/workspace/sd/stable-diffusion-webui/models/Stable-diffusion/SDv2-512.ckpt'
143
+ call('gdown -O '+model+' https://huggingface.co/stabilityai/stable-diffusion-2-1-base/resolve/main/v2-1_512-nonema-pruned.ckpt', shell=True)
144
+ clear_output()
145
+ print('Using the original V2-512 model')
146
+ else:
147
+ print('Using the original V2-512 model')
148
+ model='/workspace/sd/stable-diffusion-webui/models/Stable-diffusion/SDv2-512.ckpt'
149
  elif Original_Model_Version == "v2-768":
150
+ model="/workspace/sd/stable-diffusion-webui/models/Stable-diffusion/SDv2-768.ckpt"
151
  print('Using the original V2-768 model')
152
  else:
153
+ model="/workspace/sd/stable-diffusion-webui/models/Stable-diffusion"
154
+ print('Wrong model version, try again')
155
  try:
156
  model
157
  except:
158
+ model="/workspace/sd/stable-diffusion-webui/models/Stable-diffusion"
159
 
160
  return model
161
 
162
 
163
+
164
+ def CNet(ControlNet_Model, ControlNet_v2_Model):
165
 
166
  def download(url, model_dir):
167
 
 
173
  else:
174
  print(f"The model {filename} already exists")
175
 
176
+ wrngv1=False
177
+ os.chdir('/workspace/sd/stable-diffusion-webui/extensions')
178
  if not os.path.exists("sd-webui-controlnet"):
179
  call('git clone https://github.com/Mikubill/sd-webui-controlnet.git', shell=True)
180
+ os.chdir('/workspace')
181
  else:
182
  os.chdir('sd-webui-controlnet')
183
  call('git reset --hard', shell=True, stdout=open('/dev/null', 'w'), stderr=open('/dev/null', 'w'))
184
  call('git pull', shell=True, stdout=open('/dev/null', 'w'), stderr=open('/dev/null', 'w'))
185
+ os.chdir('/workspace')
186
 
187
+ mdldir="/workspace/sd/stable-diffusion-webui/extensions/sd-webui-controlnet/models"
188
  for filename in os.listdir(mdldir):
189
  if "_sd14v1" in filename:
190
  renamed = re.sub("_sd14v1", "-fp16", filename)
 
192
 
193
  call('wget -q -O CN_models.txt https://github.com/TheLastBen/fast-stable-diffusion/raw/main/AUTOMATIC1111_files/CN_models.txt', shell=True)
194
  call('wget -q -O CN_models_v2.txt https://github.com/TheLastBen/fast-stable-diffusion/raw/main/AUTOMATIC1111_files/CN_models_v2.txt', shell=True)
195
+
196
  with open("CN_models.txt", 'r') as f:
197
  mdllnk = f.read().splitlines()
198
  with open("CN_models_v2.txt", 'r') as d:
 
200
  call('rm CN_models.txt CN_models_v2.txt', shell=True)
201
 
202
  cfgnames=[os.path.basename(url).split('.')[0]+'.yaml' for url in mdllnk_v2]
203
+ os.chdir('/workspace/sd/stable-diffusion-webui/extensions/sd-webui-controlnet/models')
204
  for name in cfgnames:
205
  run(['cp', 'cldm_v21.yaml', name])
206
+ os.chdir('/workspace')
207
 
208
  if ControlNet_Model == "All" or ControlNet_Model == "all" :
209
  for lnk in mdllnk:
 
218
  clear_output()
219
 
220
 
221
+ elif ControlNet_Model.isdigit() and int(ControlNet_Model)-1<14 and int(ControlNet_Model)>0:
222
  download(mdllnk[int(ControlNet_Model)-1], mdldir)
223
  clear_output()
224
 
 
228
 
229
  else:
230
  print('Wrong ControlNet V1 choice, try again')
231
+ wrngv1=True
232
 
233
 
234
  if ControlNet_v2_Model == "All" or ControlNet_v2_Model == "all" :
235
  for lnk_v2 in mdllnk_v2:
236
  download(lnk_v2, mdldir)
237
+ if not wrngv1:
238
+ clear_output()
239
  done()
240
 
241
  elif ControlNet_v2_Model.isdigit() and int(ControlNet_v2_Model)-1<5:
242
  download(mdllnk_v2[int(ControlNet_v2_Model)-1], mdldir)
243
+ if not wrngv1:
244
+ clear_output()
245
  done()
246
 
247
  elif ControlNet_v2_Model == "none":
248
  pass
249
+ if not wrngv1:
250
+ clear_output()
251
  done()
252
 
253
  else:
 
255
 
256
 
257
 
258
+ def sd(User, Password, model):
259
 
260
+ import gradio
261
+
262
+ gradio.close_all()
263
+
264
  auth=f"--gradio-auth {User}:{Password}"
265
  if User =="" or Password=="":
266
  auth=""
267
+
268
+ call('wget -q -O /usr/local/lib/python3.10/dist-packages/gradio/blocks.py https://raw.githubusercontent.com/TheLastBen/fast-stable-diffusion/main/AUTOMATIC1111_files/blocks.py', shell=True)
269
+
270
+ os.chdir('/workspace/sd/stable-diffusion-webui/modules')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
271
  call('wget -q -O paths.py https://raw.githubusercontent.com/TheLastBen/fast-stable-diffusion/main/AUTOMATIC1111_files/paths.py', shell=True)
272
+ call("sed -i 's@/content/gdrive/MyDrive/sd/stablediffusion@/workspace/sd/stablediffusion@' /workspace/sd/stable-diffusion-webui/modules/paths.py", shell=True)
273
+ call("sed -i 's@\"quicksettings\": OptionInfo(.*@\"quicksettings\": OptionInfo(\"sd_model_checkpoint, sd_vae, CLIP_stop_at_last_layers, inpainting_mask_weight, initial_noise_multiplier\", \"Quicksettings list\"),@' /workspace/sd/stable-diffusion-webui/modules/shared.py", shell=True)
274
+ call("sed -i 's@print(\"No module.*@@' /workspace/sd/stablediffusion/ldm/modules/diffusionmodules/model.py", shell=True)
275
+ os.chdir('/workspace/sd/stable-diffusion-webui')
276
  clear_output()
277
 
278
+ podid=os.environ.get('RUNPOD_POD_ID')
279
+ localurl=f"{podid}-3000.proxy.runpod.net"
280
+
281
+ for line in fileinput.input('/usr/local/lib/python3.10/dist-packages/gradio/blocks.py', inplace=True):
282
+ if line.strip().startswith('self.server_name ='):
283
+ line = f' self.server_name = "{localurl}"\n'
284
+ if line.strip().startswith('self.protocol = "https"'):
285
+ line = ' self.protocol = "https"\n'
286
+ if line.strip().startswith('if self.local_url.startswith("https") or self.is_colab'):
287
+ line = ''
288
+ if line.strip().startswith('else "http"'):
289
+ line = ''
290
+ sys.stdout.write(line)
291
+
292
+
293
  if os.path.isfile(model):
294
  mdlpth="--ckpt "+model
295
  else:
296
  mdlpth="--ckpt-dir "+model
297
 
298
+ configf="--disable-console-progressbars --no-half-vae --disable-safe-unpickle --api --no-download-sd-model --opt-sdp-attention --enable-insecure-extension-access --skip-version-check --listen --port 3000 "+auth+" "+mdlpth
 
299
 
300
  return configf
301
+
302
+
303
+ def save(Huggingface_Write_token):
304
 
305
+ from slugify import slugify
306
+ from huggingface_hub import HfApi, CommitOperationAdd, create_repo
307
 
308
+ if Huggingface_Write_token=="":
309
+ print('A huggingface write token is required')
310
+
311
+ else:
312
+ os.chdir('/workspace')
313
+
314
+ if os.path.exists('sd'):
315
+
316
+ call('tar --exclude="stable-diffusion-webui/models/*/*" --exclude="sd-webui-controlnet/models/*" --zstd -cf sd_backup_rnpd.tar.zst sd', shell=True)
317
+ api = HfApi()
318
+ username = api.whoami(token=Huggingface_Write_token)["name"]
319
+
320
+ repo_id = f"{username}/{slugify('fast-stable-diffusion')}"
321
+
322
+ print("Backing up...")
323
+
324
+ operations = [CommitOperationAdd(path_in_repo="sd_backup_rnpd.tar.zst", path_or_fileobj="/workspace/sd_backup_rnpd.tar.zst")]
325
+
326
+ create_repo(repo_id,private=True, token=Huggingface_Write_token, exist_ok=True, repo_type="dataset")
327
+
328
+ api.create_commit(
329
+ repo_id=repo_id,
330
+ repo_type="dataset",
331
+ operations=operations,
332
+ commit_message="SD folder Backup",
333
+ token=Huggingface_Write_token
334
+ )
335
+
336
+ call('rm sd_backup_rnpd.tar.zst', shell=True)
337
+ clear_output()
338
+
339
+ done()
340
+
341
+ else:
342
+ print('Nothing to backup')
343
+
344
+
345
  def done():
346
  done = widgets.Button(
347
  description='Done!',