summaryrefslogtreecommitdiffstats
path: root/krebs/5pkgs/simple/stable-generate/default.nix
blob: dc9c826f9ef9775aa6a95019f2d9ff1e1f81d816 (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
''