File size: 1,014 Bytes
0bdbec3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
from __future__ import annotations

import json
from pathlib import Path

from ..state import AppState


def run(state: AppState) -> AppState:
    outdir = Path(state["outdir"])  # ensured by CLI
    outdir.mkdir(parents=True, exist_ok=True)

    # dump spec
    if state.get("spec"):
        (outdir / "spec.json").write_text(json.dumps(state["spec"], ensure_ascii=False, indent=2))
    if state.get("spec_text"):
        (outdir / "spec.txt").write_text(state["spec_text"]) 

    # dump prompts
    if state.get("prompts"):
        (outdir / "prompts.json").write_text(json.dumps(state["prompts"], ensure_ascii=False, indent=2))

    # dump scores
    if state.get("scores"):
        (outdir / "scores.json").write_text(json.dumps(state["scores"], ensure_ascii=False, indent=2))

    # copy/rename final image
    if state.get("best_image"):
        src = Path(state["best_image"].path)
        dst = outdir / "final.png"
        if src.exists():
            dst.write_bytes(src.read_bytes())

    return state