diff options
| author | Hsieh Chin Fan <pham@topo.tw> | 2023-03-21 22:33:32 +0800 |
|---|---|---|
| committer | Hsieh Chin Fan <pham@topo.tw> | 2023-03-21 22:33:32 +0800 |
| commit | 3b8b1fcdb12c91d5a8d43f2006c9a7f9a83ad485 (patch) | |
| tree | 244041956040295284f555d243629fdb7fb1ac9b | |
| parent | 60f85d962924d6d11cc27d8cac6e08af9953c994 (diff) | |
Update
| -rwxr-xr-x | bin/gpt/gpt.image | 85 |
1 files changed, 85 insertions, 0 deletions
diff --git a/bin/gpt/gpt.image b/bin/gpt/gpt.image new file mode 100755 index 0000000..5f85fc0 --- /dev/null +++ b/bin/gpt/gpt.image | |||
| @@ -0,0 +1,85 @@ | |||
| 1 | #! /bin/bash | ||
| 2 | |||
| 3 | [ -z "$OPENAI_API_KEY" ] && OPENAI_API_KEY=$(token openai) | ||
| 4 | [ -z "$OPENAI_API_KEY" ] && echo API KEY not specified && exit 1 | ||
| 5 | |||
| 6 | _print_helper_message() { | ||
| 7 | cat <<EOF | ||
| 8 | Usage: gpt [-h] [-s size] [-r RESPONSE] [-n NUMBER] [PROMPT] | ||
| 9 | |||
| 10 | Options: | ||
| 11 | -h, --help show this help message and exit | ||
| 12 | |||
| 13 | -s|--size The size of the generated images. Must be one of 256x256, 512x512, or 1024x1024. | ||
| 14 | (Defaults to 1024x1024) | ||
| 15 | |||
| 16 | -r The format in which the generated images are returned. Must be one of url or b64_json. | ||
| 17 | --response_format (Defaults to url) | ||
| 18 | |||
| 19 | -n The number of images to generate. Must be between 1 and 10. | ||
| 20 | (Defaults to 1) | ||
| 21 | |||
| 22 | * The other arguments would be treated as prompt. | ||
| 23 | If no message is specified, user should type it by hands. | ||
| 24 | |||
| 25 | Reference: https://platform.openai.com/docs/api-reference/images/create | ||
| 26 | EOF | ||
| 27 | } | ||
| 28 | |||
| 29 | # Parse arguments | ||
| 30 | while [ "$#" -gt 0 ]; do | ||
| 31 | case "$1" in | ||
| 32 | -s|--size) | ||
| 33 | size="$2" | ||
| 34 | shift 2 | ||
| 35 | ;; | ||
| 36 | -r|--response_format) | ||
| 37 | response_format="$2" | ||
| 38 | shift 2 | ||
| 39 | ;; | ||
| 40 | -n) | ||
| 41 | n="$2" | ||
| 42 | shift 2 | ||
| 43 | ;; | ||
| 44 | -h|--help) | ||
| 45 | _print_helper_message | ||
| 46 | exit 0 | ||
| 47 | ;; | ||
| 48 | *) | ||
| 49 | content="$1" | ||
| 50 | shift 1 | ||
| 51 | ;; | ||
| 52 | esac | ||
| 53 | done | ||
| 54 | |||
| 55 | ROUTE=v1/images/generations | ||
| 56 | |||
| 57 | size=${size:-1024x1024} | ||
| 58 | response_format=${response_format:-url} | ||
| 59 | n=${n:-1} | ||
| 60 | |||
| 61 | # Read content from terminal | ||
| 62 | [ -z "$prompt" ] && read -r -p "What image you want? " prompt </dev/tty | ||
| 63 | |||
| 64 | # Create request body | ||
| 65 | body="$(cat <<EOF | ||
| 66 | { | ||
| 67 | "prompt": "$prompt", | ||
| 68 | "size": "$size", | ||
| 69 | "response_format": "$response_format", | ||
| 70 | "n": 2 | ||
| 71 | } | ||
| 72 | EOF | ||
| 73 | )" | ||
| 74 | |||
| 75 | # Add an empty line between prompt and response | ||
| 76 | echo | ||
| 77 | |||
| 78 | # API call | ||
| 79 | curl https://api.openai.com/$ROUTE \ | ||
| 80 | --silent \ | ||
| 81 | -H "Content-Type: application/json" \ | ||
| 82 | -H "Authorization: Bearer $OPENAI_API_KEY" \ | ||
| 83 | -d "$body" | \ | ||
| 84 | jq . | tee .gpt.image | \ | ||
| 85 | jq -r .data[0].url | ||