28
28
def main ():
29
29
"""Initialize command-line parsers and the diffusion model"""
30
30
global infile
31
+ print ('* Initializing, be patient...' )
31
32
32
33
opt = Args ()
33
34
args = opt .parse_args ()
@@ -45,7 +46,6 @@ def main():
45
46
print ('--max_loaded_models must be >= 1; using 1' )
46
47
args .max_loaded_models = 1
47
48
48
- print ('* Initializing, be patient...' )
49
49
from ldm .generate import Generate
50
50
51
51
# these two lines prevent a horrible warning message from appearing
@@ -89,7 +89,12 @@ def main():
89
89
safety_checker = opt .safety_checker ,
90
90
max_loaded_models = opt .max_loaded_models ,
91
91
)
92
- except (FileNotFoundError , IOError , KeyError ) as e :
92
+ except FileNotFoundError :
93
+ print ('** You appear to be missing configs/models.yaml' )
94
+ print ('** You can either exit this script and run scripts/preload_models.py, or fix the problem now.' )
95
+ emergency_model_create (opt )
96
+ sys .exit (- 1 )
97
+ except (IOError , KeyError ) as e :
93
98
print (f'{ e } . Aborting.' )
94
99
sys .exit (- 1 )
95
100
@@ -276,7 +281,7 @@ def image_writer(image, seed, upscaled=False, first_seed=None, use_prefix=None):
276
281
filename = f'{ prefix } .{ use_prefix } .{ seed } .png'
277
282
tm = opt .text_mask [0 ]
278
283
th = opt .text_mask [1 ] if len (opt .text_mask )> 1 else 0.5
279
- formatted_dream_prompt = f'!mask { opt .prompt } -tm { tm } { th } '
284
+ formatted_dream_prompt = f'!mask { opt .input_file_path } -tm { tm } { th } '
280
285
path = file_writer .save_image_and_prompt_to_png (
281
286
image = image ,
282
287
dream_prompt = formatted_dream_prompt ,
@@ -316,7 +321,7 @@ def image_writer(image, seed, upscaled=False, first_seed=None, use_prefix=None):
316
321
tool = re .match ('postprocess:(\w+)' ,opt .last_operation ).groups ()[0 ]
317
322
add_postprocessing_to_metadata (
318
323
opt ,
319
- opt .prompt ,
324
+ opt .input_file_path ,
320
325
filename ,
321
326
tool ,
322
327
formatted_dream_prompt ,
@@ -481,6 +486,7 @@ def do_command(command:str, gen, opt:Args, completer) -> tuple:
481
486
command = '-h'
482
487
return command , operation
483
488
489
+
484
490
def add_weights_to_config (model_path :str , gen , opt , completer ):
485
491
print (f'>> Model import in process. Please enter the values needed to configure this model:' )
486
492
print ()
@@ -577,7 +583,7 @@ def write_config_file(conf_path, gen, model_name, new_config, clobber=False, mak
577
583
578
584
try :
579
585
print ('>> Verifying that new model loads...' )
580
- yaml_str = gen .model_cache .add_model (model_name , new_config , clobber )
586
+ gen .model_cache .add_model (model_name , new_config , clobber )
581
587
assert gen .set_model (model_name ) is not None , 'model failed to load'
582
588
except AssertionError as e :
583
589
print (f'** aborting **' )
@@ -613,10 +619,16 @@ def do_textmask(gen, opt, callback):
613
619
)
614
620
615
621
def do_postprocess (gen , opt , callback ):
616
- file_path = opt .prompt # treat the prompt as the file pathname
622
+ file_path = opt .prompt # treat the prompt as the file pathname
623
+ if opt .new_prompt is not None :
624
+ opt .prompt = opt .new_prompt
625
+ else :
626
+ opt .prompt = None
627
+
617
628
if os .path .dirname (file_path ) == '' : #basename given
618
629
file_path = os .path .join (opt .outdir ,file_path )
619
630
631
+ opt .input_file_path = file_path
620
632
tool = None
621
633
if opt .facetool_strength > 0 :
622
634
tool = opt .facetool
@@ -655,7 +667,10 @@ def do_postprocess (gen, opt, callback):
655
667
def add_postprocessing_to_metadata (opt ,original_file ,new_file ,tool ,command ):
656
668
original_file = original_file if os .path .exists (original_file ) else os .path .join (opt .outdir ,original_file )
657
669
new_file = new_file if os .path .exists (new_file ) else os .path .join (opt .outdir ,new_file )
658
- meta = retrieve_metadata (original_file )['sd-metadata' ]
670
+ try :
671
+ meta = retrieve_metadata (original_file )['sd-metadata' ]
672
+ except AttributeError :
673
+ meta = retrieve_metadata (new_file )['sd-metadata' ]
659
674
if 'image' not in meta :
660
675
meta = metadata_dumps (opt ,seeds = [opt .seed ])['image' ]
661
676
meta ['image' ] = {}
@@ -703,7 +718,7 @@ def prepare_image_metadata(
703
718
elif len (prior_variations ) > 0 :
704
719
formatted_dream_prompt = opt .dream_prompt_str (seed = first_seed )
705
720
elif operation == 'postprocess' :
706
- formatted_dream_prompt = '!fix ' + opt .dream_prompt_str (seed = seed )
721
+ formatted_dream_prompt = '!fix ' + opt .dream_prompt_str (seed = seed , prompt = opt . input_file_path )
707
722
else :
708
723
formatted_dream_prompt = opt .dream_prompt_str (seed = seed )
709
724
return filename ,formatted_dream_prompt
@@ -877,6 +892,36 @@ def write_commands(opt, file_path:str, outfilepath:str):
877
892
f .write ('\n ' .join (commands ))
878
893
print (f'>> File { outfilepath } with commands created' )
879
894
895
+ def emergency_model_create (opt :Args ):
896
+ completer = get_completer (opt )
897
+ completer .complete_extensions (('.yaml' ,'.yml' ,'.ckpt' ,'.vae.pt' ))
898
+ completer .set_default_dir ('.' )
899
+ valid_path = False
900
+ while not valid_path :
901
+ weights_file = input ('Enter the path to a downloaded models file, or ^C to exit: ' )
902
+ valid_path = os .path .exists (weights_file )
903
+ dir ,basename = os .path .split (weights_file )
904
+
905
+ valid_name = False
906
+ while not valid_name :
907
+ name = input ('Enter a short name for this model (no spaces): ' )
908
+ name = 'unnamed model' if len (name )== 0 else name
909
+ valid_name = ' ' not in name
910
+
911
+ description = input ('Enter a description for this model: ' )
912
+ description = 'no description' if len (description )== 0 else description
913
+
914
+ with open (opt .conf , 'w' , encoding = 'utf-8' ) as f :
915
+ f .write (f'{ name } :\n ' )
916
+ f .write (f' description: { description } \n ' )
917
+ f .write (f' weights: { weights_file } \n ' )
918
+ f .write (f' config: ./configs/stable-diffusion/v1-inference.yaml\n ' )
919
+ f .write (f' width: 512\n ' )
920
+ f .write (f' height: 512\n ' )
921
+ f .write (f' default: true\n ' )
922
+ print (f'Config file { opt .conf } is created. This script will now exit.' )
923
+ print (f'After restarting you may examine the entry with !models and edit it with !edit.' )
924
+
880
925
######################################
881
926
882
927
if __name__ == '__main__' :
0 commit comments