mirror of
https://github.com/invoke-ai/InvokeAI
synced 2024-08-30 20:32:17 +00:00
Improve !fetch, add !replay
Allow save fetched commands to a file Replay command to get commands from file inside of interactive prompt Related to #871
This commit is contained in:
parent
33162355be
commit
93b1298d46
@ -16,6 +16,7 @@ from ldm.dream.image_util import make_grid
|
|||||||
from ldm.dream.log import write_log
|
from ldm.dream.log import write_log
|
||||||
from omegaconf import OmegaConf
|
from omegaconf import OmegaConf
|
||||||
from backend.invoke_ai_web_server import InvokeAIWebServer
|
from backend.invoke_ai_web_server import InvokeAIWebServer
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
@ -123,6 +124,7 @@ def main():
|
|||||||
def main_loop(gen, opt, infile):
|
def main_loop(gen, opt, infile):
|
||||||
"""prompt/read/execute loop"""
|
"""prompt/read/execute loop"""
|
||||||
done = False
|
done = False
|
||||||
|
doneAfterInFile = infile is not None
|
||||||
path_filter = re.compile(r'[<>:"/\\|?*]')
|
path_filter = re.compile(r'[<>:"/\\|?*]')
|
||||||
last_results = list()
|
last_results = list()
|
||||||
model_config = OmegaConf.load(opt.conf)[opt.model]
|
model_config = OmegaConf.load(opt.conf)[opt.model]
|
||||||
@ -150,7 +152,8 @@ def main_loop(gen, opt, infile):
|
|||||||
try:
|
try:
|
||||||
command = get_next_command(infile)
|
command = get_next_command(infile)
|
||||||
except EOFError:
|
except EOFError:
|
||||||
done = True
|
done = doneAfterInFile
|
||||||
|
infile = None
|
||||||
continue
|
continue
|
||||||
|
|
||||||
# skip empty lines
|
# skip empty lines
|
||||||
@ -175,10 +178,16 @@ def main_loop(gen, opt, infile):
|
|||||||
operation = 'postprocess'
|
operation = 'postprocess'
|
||||||
|
|
||||||
elif subcommand.startswith('fetch'):
|
elif subcommand.startswith('fetch'):
|
||||||
file_path = command.replace('!fetch ','',1)
|
file_path = command.replace('!fetch','',1).strip()
|
||||||
retrieve_dream_command(opt,file_path,completer)
|
retrieve_dream_command(opt,file_path,completer)
|
||||||
continue
|
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'):
|
elif subcommand.startswith('history'):
|
||||||
completer.show_history()
|
completer.show_history()
|
||||||
continue
|
continue
|
||||||
@ -510,18 +519,40 @@ def retrieve_dream_command(opt,file_path,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,
|
||||||
|
will retrieve and format the dream command used to generate the images,
|
||||||
|
and save them to a file commands.txt for further processing
|
||||||
'''
|
'''
|
||||||
|
|
||||||
dir,basename = os.path.split(file_path)
|
dir,basename = os.path.split(file_path)
|
||||||
if len(dir) == 0:
|
if len(dir) == 0:
|
||||||
path = os.path.join(opt.outdir,basename)
|
dir = opt.outdir
|
||||||
else:
|
|
||||||
path = file_path
|
|
||||||
try:
|
try:
|
||||||
cmd = dream_cmd_from_png(path)
|
paths = list(Path(dir).glob(basename))
|
||||||
except FileNotFoundError:
|
except ValueError:
|
||||||
print(f'** {path}: file not found')
|
print(f'## "{basename}": unacceptable pattern')
|
||||||
return
|
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):
|
||||||
|
print(f'## {path}: file has no metadata')
|
||||||
|
continue
|
||||||
|
|
||||||
|
commands.append(cmd)
|
||||||
|
|
||||||
|
outfile = os.path.join(dir,'commands.txt')
|
||||||
|
with open(outfile, 'w', encoding='utf-8') as f:
|
||||||
|
f.write('\n'.join(commands))
|
||||||
|
print(f'>> File {outfile} with commands created')
|
||||||
|
|
||||||
|
if len(commands) == 1:
|
||||||
|
completer.set_line(commands[0])
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
main()
|
||||||
|
Loading…
Reference in New Issue
Block a user