summaryrefslogtreecommitdiffstats
path: root/krebs/5pkgs/simple/stable-generate/default.nix
blob: 31cf6c23d9ba720b7fe880cc4e6d7d1316a5e62e (plain)
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
{ pkgs, lib, ... }:

pkgs.writers.writeBashBin "stable-generate" ''
  set -efu

  export PATH=${lib.makeBinPath [
    pkgs.coreutils
    pkgs.curl
    pkgs.jq
  ]}

  STABLE_URL=''${STABLE_URL:-http://stable-confusion.r}

  PAYLOAD=$(jq -cn --arg prompt "$*" '{
    prompt: $prompt
  }')

  filename=$(mktemp)
  curl -Ssf "$STABLE_URL/sdapi/v1/txt2img" \
    -X POST \
    --header 'Content-Type: application/json' \
    --data "$PAYLOAD" |
      jq -r '.images[0]' |
      base64 --decode > "$filename"

  if test -t 1; then
    echo "$filename"
  else
    cat "$filename"
    rm "$filename"
  fi
''