1#!/usr/bin/env bash
2#
3# build.sh: the images for "Dark Night of the Soul"
4#
5# St. John of the Cross wrote eight five-line stanzas about a soul that slips out of her
6# quiet house at night, guided only by the ember in her chest, toward the Beloved who waits.
7# This script retells every stanza as dark-fantasy anime with rose.py and FLUX.2 on Replicate:
8# four shots per stanza, plus a fifth for stanzas 5-8, where the two lovers meet (36 candidates).
9# The published post keeps the best three per stanza (24); the shots listed in EXCLUDED below
10# were cut after review, and a normal run does not regenerate them.
11#
12# Poem: https://en.wikipedia.org/wiki/Dark_Night_of_the_Soul
13# Text: English translation by E. Allison Peers
14# Style: Bloodborne, Resident Evil, Anime, Devil May Cry
15#
16# Usage: ./build.sh [options] (REPLICATE_API_TOKEN must be set)
17# -s, --stanza N[,N...] only these stanzas (1-8)
18# -k, --shot N[,N...] only these shots (1-5) of the chosen stanzas; always regenerates them
19# with the seed they had in a full run, so a redo stays reproducible
20# -m, --model REF Replicate model (default black-forest-labs/flux-2-pro)
21# -a, --aspect RATIO aspect ratio (default 3:2)
22# -r, --resolution RES e.g. "2 MP" (default: the model's)
23# -S, --seed N base seed (default 1572; reproducible)
24# -j, --jobs N parallel predictions (default 4)
25# -f, --force regenerate stanzas whose images already exist
26# -n, --dry-run print the prompts and payloads, generate nothing
27# -h, --help
28#
29# Every stanza gets four shots (establishing, action, portrait, symbol); stanzas 5-8, where the
30# heroine and the Beloved are together, get a fifth (a two-shot). All share one style
31# block and one heroine, so the set stays visually consistent. Images land next to this file
32# as dark-night-<stanza>-<shot>.jpg. Existing stanzas are skipped, so an interrupted run
33# resumes where it stopped. When all kept images exist, the post's `draft` flag is cleared.
34
35set -Eeuo pipefail
36
37HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
38ROSE="${ROSE_PY:-$HERE/../../../rose.py}"
39
40MODEL="black-forest-labs/flux-2-pro"
41ASPECT="3:2"
42RESOLUTION=""
43SEED=1572
44JOBS=4
45FORMAT="jpg"
46QUALITY=95
47FORCE=0
48DRY=0
49ONLY=""
50SHOT_ONLY=""
51
52# Shots cut after review, as <stanza>-<shot>. A full run skips them; --shot can still redo one.
53EXCLUDED=(01-02 02-01 03-04 04-02 05-03 05-05 06-02 06-05 07-04 07-05 08-03 08-05)
54
55# ---------------------------------------------------------------------------------------
56# The look: one style block and one heroine, reused in every prompt
57# ---------------------------------------------------------------------------------------
58TITLE="Dark Night of the Soul"
59
60STYLE="Dark fantasy anime key visual, cel-shaded characters over painterly gothic backgrounds. \
61Bloodborne Victorian gothic horror and cosmic dread, Resident Evil survival-horror lighting \
62with desaturated shadows, Devil May Cry stylish demon-hunter flair. Deep blacks, volumetric \
63fog, cold moonlit blue against a single ember-red accent, razor-sharp linework, cinematic \
64composition, highly detailed. No text, no letters, no watermark, no logo."
65
66HEROINE="The heroine is a young woman with short silver-white hair and pale skin, wearing a \
67tattered black-and-crimson Victorian coat with a high collar and fingerless gloves, an ember \
68glowing softly in her chest."
69
70BELOVED="The Beloved is a tall, calm, pale figure in a long black coat with small curved \
71horns and quiet scarlet eyes, a gentle presence rather than a threat."
72
73# Four shots per stanza; @MOTIF@ in the last one is replaced by the stanza's symbol
74SHOT[1]="Wide cinematic establishing shot, the small figure dwarfed by the architecture."
75SHOT[2]="Dramatic low-angle action pose, dynamic diagonal composition, flowing coat."
76SHOT[3]="Intimate close-up portrait of her face and eyes lit by the ember glow, detailed anime face."
77SHOT[4]="Extreme close-up still life of @MOTIF@, shallow depth of field, moody rim light."
78SHOT[5]="Cinematic side-view two-shot of the heroine and the Beloved together, shallow depth of field, \
79rim light and painterly bokeh, full-frame single illustration."
80
81# ---------------------------------------------------------------------------------------
82# The poem, stanza by stanza (E. Allison Peers), each with its scene and symbol
83# ---------------------------------------------------------------------------------------
84
85# On a dark night, / Kindled in love with yearnings / —oh, happy chance!— /
86# I went forth without being observed, / My house being now at rest.
87SCENE[1]="At midnight she slips out through the iron gate of a silent gothic manor into the fog of a \
88Yharnam-like cobblestone street, every window of the house behind her dark and sleeping. $HEROINE"
89MOTIF[1]="a rusted gate latch, a drift of black feathers and a faint red glow"
90
91# In darkness and secure, / By the secret ladder, disguised / —oh, happy chance!— /
92# In darkness and in concealment, / My house being now at rest.
93SCENE[2]="Disguised in a ragged hooded cloak, she climbs down a hidden rusted iron ladder inside a \
94cathedral wall, chains and gargoyles all around her, the vault below swallowed by darkness. $HEROINE"
95MOTIF[2]="a gloved hand gripping a rusted iron ladder rung over a black abyss"
96
97# In the happy night, / In secret, when none saw me, / Nor I beheld aught, /
98# Without light or guide, / save that which burned in my heart.
99SCENE[3]="She walks without lantern through a ruined labyrinth of streets where faceless statues \
100and sleeping beasts crouch in the shadows, the only light the ember burning through her chest. $HEROINE"
101MOTIF[3]="a glowing ember cupped in two gloved hands, light spilling between the fingers, one \
102full-frame macro illustration with no inset panel, border or collage"
103
104# This light guided me / More surely than the light of noonday / To the place where he
105# (well I knew who!) / was awaiting me— / A place where none appeared.
106SCENE[4]="Led by her ember, she reaches a vast empty cathedral under a moonless sky; at the far end \
107of the nave a tall silhouette waits by a doorway where light seeps through the cracks. $HEROINE $BELOVED"
108MOTIF[4]="a heavy gothic door with warm red light pouring through its keyhole and seams"
109
110# Oh, night that guided me, / Oh, night more lovely than the dawn, / Oh, night that joined /
111# Beloved with lover, / Lover transformed in the Beloved!
112SCENE[5]="Beneath a blood-red moon and a spiral of cosmic stars, the heroine and the Beloved stand \
113face to face as their two silhouettes merge into one figure of light, a Bloodborne-style cosmic sky \
114above a still black lake. $HEROINE $BELOVED"
115MOTIF[5]="a blood-red moon ringed by a spiral of stars reflected in black water"
116
117# Upon my flowery breast, / Kept wholly for himself alone, / There he stayed sleeping, /
118# and I caressed him, / And the fanning of the cedars made a breeze.
119SCENE[6]="In an overgrown moonlit crypt garden of red roses and white lilies under tall cedars, the \
120Beloved sleeps with his head on her lap while she gently strokes his hair, a calm, tender, chaste \
121scene. $HEROINE $BELOVED"
122MOTIF[6]="red roses and white lilies growing through cracked stone, cedar needles in the air"
123
124# The breeze blew from the turret / As I parted his locks; / With his gentle hand /
125# he wounded my neck / And caused all my senses to be suspended.
126SCENE[7]="On a windswept castle turret at night the heroine parts the Beloved's long hair, his gloved \
127hand resting gently at her neck, her coat and petals frozen mid-air as time itself is suspended. \
128$HEROINE $BELOVED"
129MOTIF[7]="a black-gloved hand and a single crimson petal frozen in mid-air"
130
131# I remained, lost in oblivion; / My face I reclined on the Beloved. / All ceased and I
132# abandoned myself, / Leaving my cares / forgotten among the lilies.
133SCENE[8]="At the first grey of dawn she rests her face against the Beloved's chest among white lilies, \
134her cares dissolving into black butterflies and drifting ash, utterly at peace. $HEROINE $BELOVED"
135MOTIF[8]="white lilies with black butterflies rising from them in the pale dawn light"
136
137STANZAS=8
138shots_for() { if (($1 >= 5)); then echo 5; else echo 4; fi; } # stanzas 5-8 add a two-shot
139
140# ---------------------------------------------------------------------------------------
141# Helpers
142# ---------------------------------------------------------------------------------------
143if [[ -t 2 ]]; then
144 B=$'\033[1m' D=$'\033[2m' R=$'\033[31m' G=$'\033[32m' Y=$'\033[33m' C=$'\033[36m' Z=$'\033[0m'
145else
146 B="" D="" R="" G="" Y="" C="" Z=""
147fi
148say() { printf '%s\n' "$*" >&2; }
149die() { say "${R}error:${Z} $*"; exit 2; }
150usage() { sed -n '/^# Usage:/,/^# -h/p' "${BASH_SOURCE[0]}" | sed 's/^# \{0,1\}//'; }
151
152stanza_prompt() { # stanza shot -> one-line prompt
153 local s=$1 k=$2 shot=${SHOT[$2]}
154 shot=${shot//@MOTIF@/${MOTIF[$s]}}
155 printf '"%s", verse %d of %d. %s %s %s' "$TITLE" "$s" "$STANZAS" "$STYLE" "${SCENE[$s]}" "$shot" |
156 tr -s '[:space:]' ' '
157}
158
159run_rose() { # prompts_file seed out_dir
160 local args=(replicate "$MODEL" --prompts-file "$1" --jobs "$JOBS" --seed "$2"
161 --aspect-ratio "$ASPECT" --output-format "$FORMAT" --output-quality "$QUALITY"
162 --output-dir "$3" --no-meta)
163 [[ -n "$RESOLUTION" ]] && args+=(--resolution "$RESOLUTION")
164 ((DRY)) && args+=(--dry-run)
165 python3 "$ROSE" "${args[@]}"
166}
167
168excluded() { # stanza shot -> 0 when the shot was cut
169 local key
170 printf -v key '0%d-0%d' "$1" "$2"
171 [[ " ${EXCLUDED[*]} " == *" $key "* ]]
172}
173
174stanza_done() { # stanza -> 0 when all kept shots exist
175 local k
176 for ((k = 1; k <= $(shots_for "$1"); k++)); do
177 ! excluded "$1" "$k" || continue
178 compgen -G "$HERE/dark-night-0$1-0$k.*" >/dev/null || return 1
179 done
180}
181
182# ---------------------------------------------------------------------------------------
183# Options
184# ---------------------------------------------------------------------------------------
185while (($#)); do
186 case "$1" in
187 -s | --stanza) ONLY="${2:?--stanza needs a value}"; shift 2 ;;
188 -k | --shot) SHOT_ONLY="${2:?--shot needs a value}"; shift 2 ;;
189 -m | --model) MODEL="${2:?--model needs a value}"; shift 2 ;;
190 -a | --aspect) ASPECT="${2:?--aspect needs a value}"; shift 2 ;;
191 -r | --resolution) RESOLUTION="${2:?--resolution needs a value}"; shift 2 ;;
192 -S | --seed) SEED="${2:?--seed needs a value}"; shift 2 ;;
193 -j | --jobs) JOBS="${2:?--jobs needs a value}"; shift 2 ;;
194 -f | --force) FORCE=1; shift ;;
195 -n | --dry-run) DRY=1; shift ;;
196 -h | --help) usage; exit 0 ;;
197 *) die "unknown option: $1 (try --help)" ;;
198 esac
199done
200
201[[ -f "$ROSE" ]] || die "rose.py not found at $ROSE (set ROSE_PY)"
202command -v python3 >/dev/null || die "python3 is required"
203[[ -n "${REPLICATE_API_TOKEN:-}" ]] || die "REPLICATE_API_TOKEN is not set"
204
205if [[ -n "$ONLY" ]]; then
206 IFS=',' read -r -a WANT <<<"$ONLY"
207 for n in "${WANT[@]}"; do
208 [[ "$n" =~ ^[1-8]$ ]] || die "stanza must be 1-8, got '$n'"
209 done
210else
211 WANT=(1 2 3 4 5 6 7 8)
212fi
213if [[ -n "$SHOT_ONLY" ]]; then
214 IFS=',' read -r -a SHOT_WANT <<<"$SHOT_ONLY"
215 for n in "${SHOT_WANT[@]}"; do
216 [[ "$n" =~ ^[1-5]$ ]] || die "shot must be 1-5, got '$n'"
217 done
218fi
219
220TMP="$(mktemp -d)"
221trap 'rm -rf "$TMP"' EXIT
222
223# ---------------------------------------------------------------------------------------
224# Generate
225# ---------------------------------------------------------------------------------------
226say "${B}${TITLE}${Z} ${D}${MODEL} · ${ASPECT} · seed ${SEED} · ${#WANT[@]} stanza(s), 4-5 shots each${Z}"
227
228failed=0
229for s in "${WANT[@]}"; do
230 if [[ -n "$SHOT_ONLY" ]]; then
231 # Redo single shots, each with the seed it has in a full run
232 for k in "${SHOT_WANT[@]}"; do
233 if ((k > $(shots_for "$s"))); then
234 say "${Y}stanza $s has no shot $k, skipping${Z}"
235 continue
236 fi
237 say ""
238 say "${C}${B}stanza $s/$STANZAS, shot $k/$(shots_for "$s")${Z}"
239 one="$TMP/stanza-$s-$k.txt"
240 stanza_prompt "$s" "$k" >"$one"
241 printf '\n' >>"$one"
242 if ! run_rose "$one" $((SEED + (s - 1) * 10 + k - 1)) "$TMP/out-$s-$k"; then
243 say "${Y}shot $k of stanza $s failed${Z}"
244 failed=1
245 fi
246 ((DRY)) && continue
247 src="$(find "$TMP/out-$s-$k" -maxdepth 1 -type f -name '*-01.*' 2>/dev/null | head -n1 || true)"
248 [[ -n "$src" ]] || continue
249 rm -f "$HERE"/dark-night-0"$s"-0"$k".*
250 mv -f "$src" "$HERE/dark-night-0$s-0$k.${src##*.}"
251 say "${G}saved${Z} dark-night-0$s-0$k.${src##*.}"
252 done
253 continue
254 fi
255
256 if ((!FORCE && !DRY)) && stanza_done "$s"; then
257 say "${D}stanza $s/$STANZAS already done, skipping (use --force)${Z}"
258 continue
259 fi
260 say ""
261 say "${C}${B}stanza $s/$STANZAS${Z}"
262
263 prompts="$TMP/stanza-$s.txt"
264 : >"$prompts"
265 for ((k = 1; k <= $(shots_for "$s"); k++)); do
266 stanza_prompt "$s" "$k" >>"$prompts"
267 printf '\n' >>"$prompts"
268 done
269
270 if ! run_rose "$prompts" $((SEED + (s - 1) * 10)) "$TMP/out-$s"; then
271 say "${Y}stanza $s: some images failed, rerun to retry${Z}"
272 failed=1
273 fi
274 ((DRY)) && continue
275
276 # rose.py numbers results <model>-<timestamp>-01..04; name them dark-night-<stanza>-<shot>
277 for ((k = 1; k <= $(shots_for "$s"); k++)); do
278 ! excluded "$s" "$k" || continue
279 src="$(find "$TMP/out-$s" -maxdepth 1 -type f -name "*-0$k.*" 2>/dev/null | head -n1 || true)"
280 [[ -n "$src" ]] || continue
281 mv -f "$src" "$HERE/dark-night-0$s-0$k.${src##*.}"
282 say "${G}saved${Z} dark-night-0$s-0$k.${src##*.}"
283 done
284done
285
286# ---------------------------------------------------------------------------------------
287# Publish the post once everything exists
288# ---------------------------------------------------------------------------------------
289if ((!DRY)); then
290 complete=1
291 for ((s = 1; s <= STANZAS; s++)); do stanza_done "$s" || complete=0; done
292 if ((complete)); then
293 if grep -q '^draft: true$' "$HERE/index.md"; then
294 sed -i 's/^draft: true$/draft: false/' "$HERE/index.md"
295 say ""
296 total=0
297 for ((s = 1; s <= STANZAS; s++)); do
298 for ((k = 1; k <= $(shots_for "$s"); k++)); do excluded "$s" "$k" || total=$((total + 1)); done
299 done
300 say "${G}All $total images exist: draft flag cleared in index.md.${Z}"
301 fi
302 else
303 say ""
304 say "${Y}Not every stanza has its images yet, so the post stays a draft.${Z}"
305 fi
306fi
307
308exit "$failed"