-
Notifications
You must be signed in to change notification settings - Fork 250
/
Copy pathgpt.sh
executable file
·161 lines (133 loc) · 4.19 KB
/
gpt.sh
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#!/bin/bash
# Display banner
cat << "EOF"
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|P|e|n|t|e|s|t|P|a|c|k|a|g|e|
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Script: gpt.sh
Author: Leon Teale
Twitter: @leonteale
Website: cyberwolf-security.co.uk
Version: 1.0.0
EOF
# Set the OpenAI API authentication token
AUTH_TOKEN="Bearer <API-KEY>"
#This is the usage section
usage() {
cat <<EOF
Usage: $0 [OPTIONS] PROMPT
Generate text or images using OpenAI's GPT-3.5 Turbo & GPT-4 API.
Options:
--image PROMPT Generate and display images based on PROMPT.
Optional: --variations=NUM to generate NUM variations (default is 1).
--4 PROMPT Generate text using the GPT-4 API based on PROMPT. (Default uses GPT-3.5-Turbo)
--help Display this help message.
Positional arguments:
PROMPT The prompt to use for text or image generation.
API authentication:
This script requires an OpenAI API key, which should be set in the AUTH_TOKEN variable.
You can view your API keys here: https://platform.openai.com/account/api-keys
Examples:
./gpt.sh --image "a red apple on a white background"
./gpt.sh --image "a person walking a dog" --variations=5
./gpt.sh --4 "What is the meaning of life?"
./gpt.sh "Tell me a joke."
EOF
}
# Check if --help option is used
if [[ "$1" == "--help" ]]; then
usage
exit 0
fi
# Check if the user provides at least one argument
if [[ $# -eq 0 ]]; then
echo "Error: Missing arguments."
usage
exit 1
fi
# Check if the user provides a valid option
case "$1" in
--image|--4)
if [[ $# -lt 2 ]]; then
echo "Error: Missing prompt."
usage
exit 1
fi
;;
*)
if [[ $# -ne 1 ]]; then
echo "Error: Invalid option or too many arguments."
usage
exit 1
fi
;;
esac
#This now checks the passed to the script
#
#
# Check the arguments passed to the script
case "$1" in
"--image")
# Get the prompt from the arguments
PROMPT="$2"
# Set the number of variations to generate
VARIATIONS=1
if [[ "$3" =~ ^--variations=([1-9][0-9]*)$ ]]; then
VARIATIONS="${BASH_REMATCH[1]}"
fi
# Replace spaces and special characters in the prompt with underscores
SAFE_PROMPT=$(echo "$PROMPT" | tr '[:space:]' '_' | tr -dc '[:alnum:]-_')
# Generate and display the images
IMAGE_URLS=()
for (( i=1; i<=$VARIATIONS; i++ )); do
RESPONSE=$(curl -s https://api.openai.com/v1/images/generations \
-H "Content-Type: application/json" \
-H "Authorization: $AUTH_TOKEN" \
-d '{
"prompt": "'"$PROMPT"'",
"n": '"$VARIATIONS"',
"size": "1024x1024"
}')
IMAGE_URL=$(echo "$RESPONSE" | jq -r '.data[0].url')
mkdir -p ~/gptimages
curl -s -o ~/gptimages/"$SAFE_PROMPT"_$i.png "$IMAGE_URL"
IMAGE_URLS+=("$IMAGE_URL")
done
# Open all the generated images
for url in "${IMAGE_URLS[@]}"; do
xdg-open "$url" >/dev/null 2>&1
done
;;
"--4")
# Get the input message from the arguments
PROMPT="$*"
# Send a request to the OpenAI API to generate a response
RESPONSE=$(curl -s -H "Content-Type: application/json" \
-H "Authorization: $AUTH_TOKEN" \
-d '{
"model": "gpt-4",
"messages": [{"role": "user", "content": "'"$PROMPT"'"}],
"temperature": 0.7
}' https://api.openai.com/v1/chat/completions)
# Parse the response to get the generated text
GENERATED_TEXT=$(echo "$RESPONSE" | jq -r '.choices[0].message.content')
# Print the generated text
echo "$GENERATED_TEXT"
;;
*)
# Get the input message from the arguments
PROMPT="$*"
# Send a request to the OpenAI API to generate a response
RESPONSE=$(curl -s -H "Content-Type: application/json" \
-H "Authorization: $AUTH_TOKEN" \
-d '{
"model": "gpt-3.5-turbo",
"messages": [{"role": "user", "content": "'"$PROMPT"'"}],
"temperature": 0.7
}' https://api.openai.com/v1/chat/completions)
# Parse the response to get the generated text
GENERATED_TEXT=$(echo "$RESPONSE" | jq -r '.choices[0].message.content')
# Print the generated text
echo "$GENERATED_TEXT"
;;
esac