Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions docs/features/CLI.md
Original file line number Diff line number Diff line change
Expand Up @@ -246,19 +246,35 @@ Outputs:
## !fetch

This command retrieves the generation parameters from a previously
generated image and either loads them into the command line. You may
provide either the name of a file in the current output directory, or
a full file path.
generated image and either loads them into the command line
(Linux|Mac), or prints them out in a comment for copy-and-paste
(Windows). You may provide either the name of a file in the current
output directory, or a full file path.
Specify path to a folder with image png files, and wildcard *.png
to retrieve the dream command used to generate the images,
and save them to a file commands.txt for further processing.
Name of the saved file could be set as the second argument to !fetch

~~~
dream> !fetch 0000015.8929913.png
# the script returns the next line, ready for editing and running:
dream> a fantastic alien landscape -W 576 -H 512 -s 60 -A plms -C 7.5

dream> !fetch outputs\selected-imgs\*.png selected.txt
>> File outputs\selected-imgs\selected.txt with commands created
~~~

Note that this command may behave unexpectedly if given a PNG file that
was not generated by InvokeAI.

## !replay

This command replays a text file generated by !fetch or created manually

~~~
dream> !replay outputs\selected-imgs\selected.txt
~~~

## !history

The dream script keeps track of all the commands you issue during a
Expand Down
2 changes: 1 addition & 1 deletion ldm/dream/readline.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
'--skip_normalize','-x',
'--log_tokenization','-t',
'--hires_fix',
'!fix','!fetch','!history','!search','!clear',
'!fix','!fetch','!replay','!history','!search','!clear',
)
IMG_PATH_COMMANDS = (
'--outdir[=\s]',
Expand Down
69 changes: 56 additions & 13 deletions scripts/dream.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from ldm.dream.log import write_log
from omegaconf import OmegaConf
from backend.invoke_ai_web_server import InvokeAIWebServer
from pathlib import Path


def main():
Expand Down Expand Up @@ -123,6 +124,7 @@ def main():
def main_loop(gen, opt, infile):
"""prompt/read/execute loop"""
done = False
doneAfterInFile = infile is not None
path_filter = re.compile(r'[<>:"/\\|?*]')
last_results = list()
model_config = OmegaConf.load(opt.conf)[opt.model]
Expand Down Expand Up @@ -150,7 +152,8 @@ def main_loop(gen, opt, infile):
try:
command = get_next_command(infile)
except EOFError:
done = True
done = doneAfterInFile
infile = None
continue

# skip empty lines
Expand All @@ -175,10 +178,16 @@ def main_loop(gen, opt, infile):
operation = 'postprocess'

elif subcommand.startswith('fetch'):
file_path = command.replace('!fetch ','',1)
file_path = command.replace('!fetch','',1).strip()
retrieve_dream_command(opt,file_path,completer)
continue

elif subcommand.startswith('replay'):
file_path = command.replace('!replay','',1).strip()
if infile is None and os.path.isfile(file_path):
infile = open(file_path, 'r', encoding='utf-8')
continue

elif subcommand.startswith('history'):
completer.show_history()
continue
Expand Down Expand Up @@ -545,27 +554,61 @@ def split_variations(variations_string) -> list:
else:
return parts

def retrieve_dream_command(opt,file_path,completer):
def retrieve_dream_command(opt,command,completer):
'''
Given a full or partial path to a previously-generated image file,
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
for cut-and-paste (windows)
Given a wildcard path to a folder with image png files,
will retrieve and format the dream command used to generate the images,
and save them to a file commands.txt for further processing
'''
if len(command) == 0:
return
tokens = command.split()
if len(tokens) > 1:
outfilepath = tokens[1]
else:
outfilepath = "commands.txt"

file_path = tokens[0]
dir,basename = os.path.split(file_path)
if len(dir) == 0:
path = os.path.join(opt.outdir,basename)
else:
path = file_path
dir = opt.outdir

outdir,outname = os.path.split(outfilepath)
if len(outdir) == 0:
outfilepath = os.path.join(dir,outname)
try:
cmd = dream_cmd_from_png(path)
except OSError:
print(f'** {path}: file could not be read')
paths = list(Path(dir).glob(basename))
except ValueError:
print(f'## "{basename}": unacceptable pattern')
return
except (KeyError, AttributeError):
print(f'** {path}: file has no metadata')
return
completer.set_line(cmd)

commands = []
for path in paths:
try:
cmd = dream_cmd_from_png(path)
except OSError:
print(f'## {path}: file could not be read')
continue
except (KeyError, AttributeError, IndexError):
print(f'## {path}: file has no metadata')
continue
except:
print(f'## {path}: file could not be processed')
continue

commands.append(f'# {path}')
commands.append(cmd)

with open(outfilepath, 'w', encoding='utf-8') as f:
f.write('\n'.join(commands))
print(f'>> File {outfilepath} with commands created')

if len(commands) == 2:
completer.set_line(commands[1])

if __name__ == '__main__':
main()