TheLastBen commited on
Commit
911a983
1 Parent(s): 9b9604b

Upload 2 files

Browse files
Scripts/mainpaperspacev1.py CHANGED
@@ -20,6 +20,7 @@ from collections import defaultdict
20
  from math import log, sqrt
21
  import numpy as np
22
 
 
23
  from urllib.request import urlopen, Request
24
  import tempfile
25
  from tqdm import tqdm
@@ -94,7 +95,7 @@ def depsinst(url, dst):
94
  pbar.update(len(buffer))
95
  f.close()
96
 
97
-
98
  def ntbk():
99
 
100
  os.chdir('/notebooks')
@@ -109,6 +110,8 @@ def ntbk():
109
  os.chdir('/notebooks')
110
 
111
 
 
 
112
  def downloadmodel_hf(Path_to_HuggingFace):
113
  import wget
114
 
@@ -171,7 +174,38 @@ def downloadmodel_pth(Model_Path, safetensors):
171
  while not os.path.exists(str(Model_Path)):
172
  print('Wrong path, use the file explorer to copy the path')
173
  time.sleep(5)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
174
 
 
175
 
176
  def downloadmodel_lnk(Model_Link, safetensors):
177
 
@@ -205,6 +239,87 @@ def downloadmodel_lnk(Model_Link, safetensors):
205
  while os.path.getsize("/models/"+modelnm) < 1810671599:
206
  print('Wrong link, check that the link is valid')
207
  time.sleep(5)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
208
 
209
 
210
  def dl(Path_to_HuggingFace, Model_Path, Model_Link, safetensors):
@@ -223,6 +338,26 @@ def dl(Path_to_HuggingFace, Model_Path, Model_Link, safetensors):
223
  print('Using the original V1.5 model')
224
 
225
  return MODEL_NAME
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
226
 
227
 
228
  def sess(Session_Name, Session_Link_optional, MODEL_NAME):
 
20
  from math import log, sqrt
21
  import numpy as np
22
 
23
+ from urllib.parse import urlparse, parse_qs, unquote
24
  from urllib.request import urlopen, Request
25
  import tempfile
26
  from tqdm import tqdm
 
95
  pbar.update(len(buffer))
96
  f.close()
97
 
98
+
99
  def ntbk():
100
 
101
  os.chdir('/notebooks')
 
110
  os.chdir('/notebooks')
111
 
112
 
113
+
114
+
115
  def downloadmodel_hf(Path_to_HuggingFace):
116
  import wget
117
 
 
174
  while not os.path.exists(str(Model_Path)):
175
  print('Wrong path, use the file explorer to copy the path')
176
  time.sleep(5)
177
+
178
+
179
+
180
+
181
+ def downloadmodel_path(MODEL_PATH):
182
+
183
+ modelname=os.path.basename(MODEL_PATH)
184
+ sftnsr=""
185
+ if modelname.split('.')[-1]=='safetensors':
186
+ sftnsr="--from_safetensors"
187
+
188
+ import wget
189
+ os.chdir('/notebooks')
190
+ clear_output()
191
+ if os.path.exists(str(MODEL_PATH)):
192
+ call('wget -q -O config.yaml https://github.com/CompVis/stable-diffusion/raw/main/configs/stable-diffusion/v1-inference.yaml', shell=True)
193
+ call('python /diffusers/scripts/convert_original_stable_diffusion_to_diffusers.py --checkpoint_path '+MODEL_PATH+' --dump_path /models/stable-diffusion-custom --original_config_file config.yaml '+sftnsr, shell=True)
194
+ clear_output()
195
+ call('rm config.yaml', shell=True)
196
+ if os.path.exists('/models/stable-diffusion-custom/unet/diffusion_pytorch_model.bin'):
197
+ clear_output()
198
+ done()
199
+ while not os.path.exists('/models/stable-diffusion-custom/unet/diffusion_pytorch_model.bin'):
200
+ print('Conversion error')
201
+ time.sleep(5)
202
+
203
+ else:
204
+ while not os.path.exists(str(MODEL_PATH)):
205
+ print('Wrong path, use the file explorer to copy the path')
206
+ time.sleep(5)
207
 
208
+
209
 
210
  def downloadmodel_lnk(Model_Link, safetensors):
211
 
 
239
  while os.path.getsize("/models/"+modelnm) < 1810671599:
240
  print('Wrong link, check that the link is valid')
241
  time.sleep(5)
242
+
243
+
244
+
245
+ def downloadmodel_link(MODEL_LINK):
246
+
247
+ import wget
248
+ import gdown
249
+ from gdown.download import get_url_from_gdrive_confirmation
250
+
251
+
252
+ def getsrc(url):
253
+ parsed_url = urlparse(url)
254
+ if parsed_url.netloc == 'civitai.com':
255
+ src='civitai'
256
+ elif parsed_url.netloc == 'drive.google.com':
257
+ src='gdrive'
258
+ elif parsed_url.netloc == 'huggingface.co':
259
+ src='huggingface'
260
+ else:
261
+ src='others'
262
+ return src
263
+
264
+ src=getsrc(MODEL_LINK)
265
+
266
+ def get_name(url, gdrive):
267
+ if not gdrive:
268
+ response = requests.get(url, allow_redirects=False)
269
+ if "Location" in response.headers:
270
+ redirected_url = response.headers["Location"]
271
+ quer = parse_qs(urlparse(redirected_url).query)
272
+ if "response-content-disposition" in quer:
273
+ disp_val = quer["response-content-disposition"][0].split(";")
274
+ for vals in disp_val:
275
+ if vals.strip().startswith("filename="):
276
+ filenm=unquote(vals.split("=", 1)[1].strip())
277
+ return filenm.replace("\"","")
278
+ else:
279
+ headers = {"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36"}
280
+ lnk="https://drive.google.com/uc?id={id}&export=download".format(id=url[url.find("/d/")+3:url.find("/view")])
281
+ res = requests.session().get(lnk, headers=headers, stream=True, verify=True)
282
+ res = requests.session().get(get_url_from_gdrive_confirmation(res.text), headers=headers, stream=True, verify=True)
283
+ content_disposition = six.moves.urllib_parse.unquote(res.headers["Content-Disposition"])
284
+ filenm = re.search(r"filename\*=UTF-8''(.*)", content_disposition).groups()[0].replace(os.path.sep, "_")
285
+ return filenm
286
+
287
+ if src=='civitai':
288
+ modelname=get_name(MODEL_LINK, False)
289
+ elif src=='gdrive':
290
+ modelname=get_name(MODEL_LINK, True)
291
+ else:
292
+ modelname=os.path.basename(MODEL_LINK)
293
+
294
+ sftnsr=""
295
+ if modelname.split('.')[-1]!='safetensors':
296
+ modelnm="model.ckpt"
297
+ else:
298
+ modelnm="model.safetensors"
299
+ sftnsr="--from_safetensors"
300
+
301
+ os.chdir('/notebooks')
302
+ call("gdown --fuzzy " +MODEL_LINK+ " -O "+modelnm, shell=True)
303
+
304
+ if os.path.exists(modelnm):
305
+ if os.path.getsize(modelnm) > 1810671599:
306
+ call('wget -q -O config.yaml https://github.com/CompVis/stable-diffusion/raw/main/configs/stable-diffusion/v1-inference.yaml', shell=True)
307
+ call('python /diffusers/scripts/convert_original_stable_diffusion_to_diffusers.py --checkpoint_path '+modelnm+' --dump_path /models/stable-diffusion-custom --original_config_file config.yaml '+sftnsr, shell=True)
308
+ clear_output()
309
+ call('rm config.yaml', shell=True)
310
+ if os.path.exists('/models/stable-diffusion-custom/unet/diffusion_pytorch_model.bin'):
311
+ call('rm '+modelnm, shell=True)
312
+ clear_output()
313
+ done()
314
+ else:
315
+ while not os.path.exists('/models/stable-diffusion-custom/unet/diffusion_pytorch_model.bin'):
316
+ print('Conversion error')
317
+ time.sleep(5)
318
+ else:
319
+ while os.path.getsize(modelnm) < 1810671599:
320
+ print('Wrong link, check that the link is valid')
321
+ time.sleep(5)
322
+
323
 
324
 
325
  def dl(Path_to_HuggingFace, Model_Path, Model_Link, safetensors):
 
338
  print('Using the original V1.5 model')
339
 
340
  return MODEL_NAME
341
+
342
+
343
+
344
+ def dls(Path_to_HuggingFace, Model_Path, Model_Link):
345
+
346
+ if Path_to_HuggingFace != "":
347
+ downloadmodel_hf(Path_to_HuggingFace)
348
+ MODEL_NAME="/models/stable-diffusion-custom"
349
+ elif Model_Path !="":
350
+ downloadmodel_pth(Model_Path)
351
+ MODEL_NAME="/models/stable-diffusion-custom"
352
+ elif Model_Link !="":
353
+ downloadmodel_lnk(Model_Link)
354
+ MODEL_NAME="/models/stable-diffusion-custom"
355
+ else:
356
+ MODEL_NAME="/datasets/stable-diffusion-diffusers/stable-diffusion-v1-5"
357
+ print('Using the original V1.5 model')
358
+
359
+ return MODEL_NAME
360
+
361
 
362
 
363
  def sess(Session_Name, Session_Link_optional, MODEL_NAME):
Scripts/mainpaperspacev2.py CHANGED
@@ -21,6 +21,8 @@ from math import log, sqrt
21
  import numpy as np
22
  from subprocess import check_output
23
 
 
 
24
  from urllib.request import urlopen, Request
25
  import tempfile
26
  from tqdm import tqdm
@@ -184,6 +186,51 @@ def downloadmodel_pthv2(Model_Path, safetensors):
184
  print('Wrong path, use the file explorer to copy the path')
185
  os.chdir('/notebooks')
186
  time.sleep(5)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
187
 
188
 
189
 
@@ -237,6 +284,103 @@ def downloadmodel_lnkv2(Model_Link, safetensors):
237
 
238
 
239
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
240
  def dlv2(Path_to_HuggingFace, Model_Path, Model_Link, Model_Version, safetensors):
241
 
242
  if Path_to_HuggingFace != "":
@@ -260,6 +404,35 @@ def dlv2(Path_to_HuggingFace, Model_Path, Model_Link, Model_Version, safetensors
260
  print('Wrong model version')
261
 
262
  return MODEL_NAMEv2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
263
 
264
 
265
  def sessv2(Session_Name, Session_Link_optional, MODEL_NAMEv2):
 
21
  import numpy as np
22
  from subprocess import check_output
23
 
24
+
25
+ from urllib.parse import urlparse, parse_qs, unquote
26
  from urllib.request import urlopen, Request
27
  import tempfile
28
  from tqdm import tqdm
 
186
  print('Wrong path, use the file explorer to copy the path')
187
  os.chdir('/notebooks')
188
  time.sleep(5)
189
+
190
+
191
+
192
+
193
+ def downloadmodel_path_v2(MODEL_PATH):
194
+
195
+ modelname=os.path.basename(MODEL_PATH)
196
+ sftnsr=""
197
+ if modelname.split('.')[-1]=='safetensors':
198
+ sftnsr="--from_safetensors"
199
+
200
+ import wget
201
+ os.chdir('/models')
202
+ clear_output()
203
+ if os.path.exists(str(MODEL_PATH)):
204
+
205
+ wget.download('https://github.com/TheLastBen/fast-stable-diffusion/raw/main/Dreambooth/det.py')
206
+ print('Detecting model version...')
207
+ Custom_Model_Version=check_output('python det.py '+sftnsr+' --MODEL_PATH '+MODEL_PATH, shell=True).decode('utf-8').replace('\n', '')
208
+ clear_output()
209
+ print(''+Custom_Model_Version+' Detected')
210
+ call('rm det.py', shell=True)
211
+
212
+ if Custom_Model_Version=='V2.1-512px':
213
+ call('wget -q -O convertodiffv2.py https://github.com/TheLastBen/fast-stable-diffusion/raw/main/Dreambooth/convertodiffv2.py', shell=True)
214
+ call('python convertodiffv2.py '+MODEL_PATH+' stable-diffusion-custom --v2 --reference_model stabilityai/stable-diffusion-2-1-base '+sftnsr, shell=True)
215
+
216
+ elif Custom_Model_Version=='V2.1-768px':
217
+ call('wget -q -O convertodiffv2.py https://github.com/TheLastBen/fast-stable-diffusion/raw/main/Dreambooth/convertodiffv2-768.py', shell=True)
218
+ call('python convertodiffv2.py '+MODEL_PATH+' stable-diffusion-custom --v2 --reference_model stabilityai/stable-diffusion-2-1 '+sftnsr, shell=True)
219
+
220
+ call('rm convertodiffv2.py', shell=True)
221
+ if os.path.exists('/models/stable-diffusion-custom/unet/diffusion_pytorch_model.bin'):
222
+ clear_output()
223
+ done()
224
+ while not os.path.exists('/models/stable-diffusion-custom/unet/diffusion_pytorch_model.bin'):
225
+ print('Conversion error')
226
+ os.chdir('/workspace')
227
+ time.sleep(5)
228
+ else:
229
+ while not os.path.exists(str(MODEL_PATH)):
230
+ print('Wrong path, use the file explorer to copy the path')
231
+ os.chdir('/workspace')
232
+ time.sleep(5)
233
+
234
 
235
 
236
 
 
284
 
285
 
286
 
287
+
288
+ def downloadmodel_link_v2(MODEL_LINK):
289
+
290
+ import wget
291
+ import gdown
292
+ from gdown.download import get_url_from_gdrive_confirmation
293
+
294
+ def getsrc(url):
295
+ parsed_url = urlparse(url)
296
+ if parsed_url.netloc == 'civitai.com':
297
+ src='civitai'
298
+ elif parsed_url.netloc == 'drive.google.com':
299
+ src='gdrive'
300
+ elif parsed_url.netloc == 'huggingface.co':
301
+ src='huggingface'
302
+ else:
303
+ src='others'
304
+ return src
305
+
306
+ src=getsrc(MODEL_LINK)
307
+
308
+ def get_name(url, gdrive):
309
+ if not gdrive:
310
+ response = requests.get(url, allow_redirects=False)
311
+ if "Location" in response.headers:
312
+ redirected_url = response.headers["Location"]
313
+ quer = parse_qs(urlparse(redirected_url).query)
314
+ if "response-content-disposition" in quer:
315
+ disp_val = quer["response-content-disposition"][0].split(";")
316
+ for vals in disp_val:
317
+ if vals.strip().startswith("filename="):
318
+ filenm=unquote(vals.split("=", 1)[1].strip())
319
+ return filenm.replace("\"","")
320
+ else:
321
+ headers = {"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36"}
322
+ lnk="https://drive.google.com/uc?id={id}&export=download".format(id=url[url.find("/d/")+3:url.find("/view")])
323
+ res = requests.session().get(lnk, headers=headers, stream=True, verify=True)
324
+ res = requests.session().get(get_url_from_gdrive_confirmation(res.text), headers=headers, stream=True, verify=True)
325
+ content_disposition = six.moves.urllib_parse.unquote(res.headers["Content-Disposition"])
326
+ filenm = re.search(r"filename\*=UTF-8''(.*)", content_disposition).groups()[0].replace(os.path.sep, "_")
327
+ return filenm
328
+
329
+ if src=='civitai':
330
+ modelname=get_name(MODEL_LINK, False)
331
+ elif src=='gdrive':
332
+ modelname=get_name(MODEL_LINK, True)
333
+ else:
334
+ modelname=os.path.basename(MODEL_LINK)
335
+
336
+ sftnsr=""
337
+ if modelname.split('.')[-1]!='safetensors':
338
+ modelnm="model.ckpt"
339
+ else:
340
+ modelnm="model.safetensors"
341
+ sftnsr="--from_safetensors"
342
+
343
+ os.chdir('/models')
344
+ call("gdown --fuzzy " +MODEL_LINK+ " -O "+modelnm, shell=True)
345
+
346
+ if os.path.exists(modelnm):
347
+ if os.path.getsize(modelnm) > 1810671599:
348
+
349
+ wget.download('https://github.com/TheLastBen/fast-stable-diffusion/raw/main/Dreambooth/det.py')
350
+ print('Detecting model version...')
351
+ Custom_Model_Version=check_output('python det.py '+sftnsr+' --MODEL_PATH '+modelnm, shell=True).decode('utf-8').replace('\n', '')
352
+ clear_output()
353
+ print(''+Custom_Model_Version+' Detected')
354
+ call('rm det.py', shell=True)
355
+
356
+ if Custom_Model_Version=='V2.1-512px':
357
+ call('wget -q -O convertodiffv2.py https://github.com/TheLastBen/fast-stable-diffusion/raw/main/Dreambooth/convertodiffv2.py', shell=True)
358
+ call('python convertodiffv2.py '+modelnm+' stable-diffusion-custom --v2 --reference_model stabilityai/stable-diffusion-2-1-base '+sftnsr, shell=True)
359
+
360
+ elif Custom_Model_Version=='V2.1-768px':
361
+ call('wget -q -O convertodiffv2.py https://github.com/TheLastBen/fast-stable-diffusion/raw/main/Dreambooth/convertodiffv2-768.py', shell=True)
362
+ call('python convertodiffv2.py '+modelnm+' stable-diffusion-custom --v2 --reference_model stabilityai/stable-diffusion-2-1 '+sftnsr, shell=True)
363
+ call('rm convertodiffv2.py', shell=True)
364
+
365
+ if os.path.exists('/models/stable-diffusion-custom/unet/diffusion_pytorch_model.bin'):
366
+ call('rm '+modelnm, shell=True)
367
+ os.chdir('/workspace')
368
+ clear_output()
369
+ done()
370
+ else:
371
+ while not os.path.exists('/models/stable-diffusion-custom/unet/diffusion_pytorch_model.bin'):
372
+ print('Conversion error')
373
+ os.chdir('/workspace')
374
+ time.sleep(5)
375
+ else:
376
+ while os.path.getsize(modelnm) < 1810671599:
377
+ print('Wrong link, check that the link is valid')
378
+ os.chdir('/workspace')
379
+ time.sleep(5)
380
+
381
+
382
+
383
+
384
  def dlv2(Path_to_HuggingFace, Model_Path, Model_Link, Model_Version, safetensors):
385
 
386
  if Path_to_HuggingFace != "":
 
404
  print('Wrong model version')
405
 
406
  return MODEL_NAMEv2
407
+
408
+
409
+
410
+
411
+ def dlsv2(Path_to_HuggingFace, Model_Path, Model_Link, Model_Version):
412
+
413
+ if Path_to_HuggingFace != "":
414
+ downloadmodel_hfv2(Path_to_HuggingFace)
415
+ MODEL_NAMEv2="/models/stable-diffusion-custom"
416
+ elif Model_Path !="":
417
+ downloadmodel_pthv2(Model_Path)
418
+ MODEL_NAMEv2="/models/stable-diffusion-custom"
419
+ elif Model_Link !="":
420
+ downloadmodel_lnkv2(Model_Link)
421
+ MODEL_NAMEv2="/models/stable-diffusion-custom"
422
+ else:
423
+ if Model_Version=="512":
424
+ MODEL_NAMEv2="/datasets/stable-diffusion-v2-1-base-diffusers/stable-diffusion-2-1-base"
425
+ print('Using the original V2-512 model')
426
+ elif Model_Version=="768":
427
+ MODEL_NAMEv2="/datasets/stable-diffusion-v2-1/stable-diffusion-2-1"
428
+ print('Using the original V2-768 model')
429
+ else:
430
+ MODEL_NAMEv2=""
431
+ print('Wrong model version')
432
+
433
+ return MODEL_NAMEv2
434
+
435
+
436
 
437
 
438
  def sessv2(Session_Name, Session_Link_optional, MODEL_NAMEv2):