fix incorrect handling of single quotes in prompts

This commit is contained in:
Lincoln Stein 2022-10-21 17:06:35 -04:00
parent 62e1cb48fd
commit f6b31d51e0
2 changed files with 37 additions and 20 deletions

View File

@ -172,6 +172,7 @@ class Args(object):
command = cmd_string.replace("'", "\\'") command = cmd_string.replace("'", "\\'")
try: try:
elements = shlex.split(command) elements = shlex.split(command)
elements = [x.replace("\\'","'") for x in elements]
except ValueError: except ValueError:
import sys, traceback import sys, traceback
print(traceback.format_exc(), file=sys.stderr) print(traceback.format_exc(), file=sys.stderr)
@ -935,7 +936,7 @@ def metadata_loads(metadata) -> list:
for image in images: for image in images:
# repack the prompt and variations # repack the prompt and variations
if 'prompt' in image: if 'prompt' in image:
image['prompt'] = ','.join([':'.join([x['prompt'], str(x['weight'])]) for x in image['prompt']]) image['prompt'] = repack_prompt(image['prompt'])
if 'variations' in image: if 'variations' in image:
image['variations'] = ','.join([':'.join([str(x['seed']),str(x['weight'])]) for x in image['variations']]) image['variations'] = ','.join([':'.join([str(x['seed']),str(x['weight'])]) for x in image['variations']])
# fix a bit of semantic drift here # fix a bit of semantic drift here
@ -943,12 +944,19 @@ def metadata_loads(metadata) -> list:
opt = Args() opt = Args()
opt._cmd_switches = Namespace(**image) opt._cmd_switches = Namespace(**image)
results.append(opt) results.append(opt)
except KeyError as e: except Exception as e:
import sys, traceback import sys, traceback
print('>> badly-formatted metadata',file=sys.stderr) print('>> could not read metadata',file=sys.stderr)
print(traceback.format_exc(), file=sys.stderr) print(traceback.format_exc(), file=sys.stderr)
return results return results
def repack_prompt(prompt_list:list)->str:
# in the common case of no weighting syntax, just return the prompt as is
if len(prompt_list) > 1:
return ','.join([':'.join([x['prompt'], str(x['weight'])]) for x in prompt_list])
else:
return prompt_list[0]['prompt']
# image can either be a file path on disk or a base64-encoded # image can either be a file path on disk or a base64-encoded
# representation of the file's contents # representation of the file's contents
def calculate_init_img_hash(image_string): def calculate_init_img_hash(image_string):

View File

@ -800,19 +800,33 @@ def retrieve_dream_command(opt,command,completer):
will retrieve and format the dream command used to generate the image, will retrieve and format the dream command used to generate the image,
and pop it into the readline buffer (linux, Mac), or print out a comment and pop it into the readline buffer (linux, Mac), or print out a comment
for cut-and-paste (windows) for cut-and-paste (windows)
Given a wildcard path to a folder with image png files, Given a wildcard path to a folder with image png files,
will retrieve and format the dream command used to generate the images, will retrieve and format the dream command used to generate the images,
and save them to a file commands.txt for further processing and save them to a file commands.txt for further processing
''' '''
if len(command) == 0: if len(command) == 0:
return return
tokens = command.split() tokens = command.split()
if len(tokens) > 1: if len(tokens) > 1:
outfilepath = tokens[1] return write_commands(opt,tokens)
else:
outfilepath = "commands.txt" try:
cmd = dream_cmd_from_png(tokens[0])
except OSError:
print(f'## {path}: file could not be read')
except (KeyError, AttributeError, IndexError):
print(f'## {path}: file has no metadata')
except:
print(f'## {path}: file could not be processed')
if len(cmd)>0:
completer.set_line(cmd)
def write_commands(opt, tokens:list):
file_path = tokens[0] file_path = tokens[0]
outfilepath = tokens[1]
dir,basename = os.path.split(file_path) dir,basename = os.path.split(file_path)
if len(dir) == 0: if len(dir) == 0:
dir = opt.outdir dir = opt.outdir
@ -827,28 +841,23 @@ def retrieve_dream_command(opt,command,completer):
return return
commands = [] commands = []
cmd = None
for path in paths: for path in paths:
try: try:
cmd = dream_cmd_from_png(path) cmd = dream_cmd_from_png(path)
except OSError: except OSError:
print(f'## {path}: file could not be read') print(f'## {path}: file could not be read')
continue
except (KeyError, AttributeError, IndexError): except (KeyError, AttributeError, IndexError):
print(f'## {path}: file has no metadata') print(f'## {path}: file has no metadata')
continue
except: except:
print(f'## {path}: file could not be processed') print(f'## {path}: file could not be processed')
continue if cmd:
commands.append(f'# {path}')
commands.append(f'# {path}') commands.append(cmd)
commands.append(cmd) if len(commands)>0:
with open(outfilepath, 'w', encoding='utf-8') as f:
with open(outfilepath, 'w', encoding='utf-8') as f: f.write('\n'.join(commands))
f.write('\n'.join(commands)) print(f'>> File {outfilepath} with commands created')
print(f'>> File {outfilepath} with commands created')
if len(commands) == 2:
completer.set_line(commands[1])
###################################### ######################################