Last active
May 29, 2025 13:04
-
-
Save Birch-san/5eb62a4a5e4a1c4447a55e3a9faf8988 to your computer and use it in GitHub Desktop.
Example API request for encoding a NAIv4 vibe. Doesn't include all the metadata (e.g. image thumbnail and which model it was encoded for) that the UI adds
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
set -eo pipefail | |
# https://stackoverflow.com/a/12194427/5257399 | |
create() { # fd base [qualifier [suffix [max]]] | |
local fd="$1" base="$2" qualifier="${3-}" suffix="${4-.png}" max="${5-}" | |
local n=0 file | |
local - # ash-style local scoping of options in 4.4+ | |
set -o noclobber | |
REPLY= | |
# Add a dot to qualifier if it's provided and doesn't already start with one | |
[[ -n "$qualifier" && "$qualifier" != .* ]] && qualifier=".$qualifier" | |
# First pass: find the highest existing index | |
while true; do | |
PAD=$(printf '%03d' "$n") | |
# Check for ANY file with this index (regardless of qualifier) | |
if ls "${base}_${PAD}"* &>/dev/null; then | |
((n++)) | |
((max > 0 && n > max)) && return 1 | |
else | |
break | |
fi | |
done | |
# Create the file with the next available index | |
file="${base}_${PAD}${qualifier}${suffix}" | |
eval 'command exec '"$fd"'> "$file"' 2>/dev/null || return 1 | |
REPLY=$file | |
} | |
# closes file descriptor 3 | |
cleanup() { | |
local exit_code=$1 | |
# echo "Performing cleanup..." >&2 | |
exec 3>&- 2>/dev/null || true # Close FD 3, suppress errors if already closed | |
# Add any other cleanup tasks here | |
# echo "Cleanup completed with exit code: $exit_code" >&2 | |
exit $exit_code # Preserve the original exit code | |
} | |
: "${IN_IMG:=mycoolvibe.png}" | |
: "${OUT_RESULT:=}" | |
: "${INFO_EXTRACTED:=1.0}" | |
IMG_B64_IN="$(mktemp)" | |
base64 -w 0 -i "$IN_IMG" >"$IMG_B64_IN" | |
: "${API_TOKEN:=put yer token here}" | |
: "${MODEL:=nai-diffusion-4-curated-preview}" | |
: "${ENDPOINT:=https://image.novelai.net/ai/encode-vibe}" | |
REQUEST_BODY_IN="$(mktemp)" | |
RESPONSE_HEADERS_OUT="$(mktemp)" | |
RESPONSE_BODY_OUT="$(mktemp)" | |
jq -rMn \ | |
--rawfile image_base64 "$IMG_B64_IN" \ | |
--argjson infoExtracted "$INFO_EXTRACTED" \ | |
--arg model "$MODEL" \ | |
"${JQOPTS[@]}" \ | |
'{ | |
"image": $image_base64, | |
"information_extracted": $infoExtracted, | |
"model": $model | |
}' >"$REQUEST_BODY_IN" | |
curl -sX POST "$ENDPOINT" \ | |
-H "Authorization: Bearer $API_TOKEN" \ | |
-H 'Content-Type: application/json' \ | |
-H 'Accept: application/json, text/plain, application/x-zip-compressed' \ | |
-d "@$REQUEST_BODY_IN" \ | |
-D "$RESPONSE_HEADERS_OUT" \ | |
-o "$RESPONSE_BODY_OUT" | |
STATUS="$(tac "$RESPONSE_HEADERS_OUT" | grep --line-buffered -m1 HTTP/1.1 | awk '$0 = $2')" | |
echo "Status: $STATUS" >&2 | |
if [[ "$STATUS" =~ ^2[0-9]{2}$ ]]; then | |
mkdir -p out_vibe | |
if [[ -n "$OUT_RESULT" ]]; then | |
cp "$RESPONSE_BODY_OUT" "$OUT_RESULT" | |
echo "Saved $OUT_RESULT" >&2 | |
else | |
pushd out_vibe > /dev/null | |
create 3 out "" .nometa-naiv4vibe || exit | |
# create 3 out "${SAMPLER}_${PORT}" .png || exit | |
# EXIT includes normal exit (code 0), error exits (non-zero), and when terminated by signals like SIGINT (Ctrl+C) | |
# Track the exit code to preserve it through the trap | |
trap 'cleanup $?' EXIT | |
cat "$RESPONSE_BODY_OUT" >&3 | |
echo "Saved $REPLY" >&2 | |
fi | |
else | |
jq . <"$RESPONSE_BODY_OUT" 2>/dev/null || cat "$RESPONSE_BODY_OUT" | |
fi | |
# exec 3>&- # close the file |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment