aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rwxr-xr-xfanfictioner31
1 files changed, 24 insertions, 7 deletions
diff --git a/fanfictioner b/fanfictioner
index f5b3e3c..991f079 100755
--- a/fanfictioner
+++ b/fanfictioner
@@ -240,7 +240,10 @@ def run_sd(args, log):
with open(log, "w") as f:
f.write(shlex.join(args) + "\n")
f.flush()
- proc = subprocess.Popen(args, stdout=f, stderr=subprocess.STDOUT)
+ try:
+ proc = subprocess.Popen(args, stdout=f, stderr=subprocess.STDOUT)
+ except OSError as e:
+ return f"cannot run {args[0]}: {e}"
try:
while proc.poll() is None:
time.sleep(2)
@@ -259,7 +262,7 @@ def run_sd(args, log):
def editor(path):
- subprocess.run([*shlex.split(os.environ.get("EDITOR", "vi")), str(path)])
+ subprocess.run([*shlex.split(os.environ.get("EDITOR") or "vi"), str(path)])
def unload_llm():
@@ -285,9 +288,19 @@ def generate(d, profile, prompt, size, refs=()):
def show(files, seed, d):
m = d / f"{seed}_montage.png"
labelled = [x for i, f in enumerate(files) for x in ("-label", f"{i + 1} seed {seed + i}", str(f))]
- subprocess.run(["magick", "montage", "-pointsize", "32", *labelled,
- "-geometry", "+8+8", "-tile", "3x1", str(m)], check=True)
- subprocess.run(["kitty", "+kitten", "icat", str(m)])
+ try:
+ failed = subprocess.run(["magick", "montage", "-pointsize", "32", *labelled,
+ "-geometry", "+8+8", "-tile", "3x1", str(m)]).returncode
+ except OSError:
+ failed = True
+ if failed:
+ print("montage failed, candidates:", *files, sep="\n")
+ return
+ print(m)
+ try:
+ subprocess.run(["kitty", "+kitten", "icat", str(m)])
+ except OSError as e:
+ print(f"cannot show image: {e}")
def review(d, profile, prompt, size, refs=(), skip=False):
@@ -298,7 +311,10 @@ def review(d, profile, prompt, size, refs=(), skip=False):
if not err:
show(files, seed, d)
while True:
- k = input(f"[{keys}] > ").strip().lower()
+ try:
+ k = input(f"[{keys}] > ").strip().lower()
+ except EOFError:
+ k = "q"
if k in ("1", "2", "3") and not err:
return files[int(k) - 1]
if k == "r":
@@ -306,7 +322,7 @@ def review(d, profile, prompt, size, refs=(), skip=False):
if k == "p":
(d / "prompt.txt").write_text(prompt)
editor(d / "prompt.txt")
- prompt = (d / "prompt.txt").read_text().strip()
+ prompt = (d / "prompt.txt").read_text().strip() or prompt
break
if k == "s" and skip:
return None
@@ -424,6 +440,7 @@ def selftest():
assert (t / "ok.log").read_text().startswith("true")
assert run_sd(["sh", "-c", "echo '[ERROR] boom'"], t / "e.log") == "sd-cli failed (exit 0)"
assert run_sd(["false"], t / "f.log") == "sd-cli failed (exit 1)"
+ assert run_sd(["/nonexistent-sd-cli"], t / "n.log").startswith("cannot run /nonexistent-sd-cli")
print("selftest ok")