This repository has been archived by the owner on Feb 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dekuai.sh
executable file
Β·486 lines (398 loc) Β· 16.4 KB
/
dekuai.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
#!/usr/bin/sh
sleep 0.03;clear
############################
### TERMINAL SCREEN SIZE ###
############################
WIDTH=$(tput cols)
HEIGHT=$(tput lines)
SYSTEM=$(uname -o)
#####################
### HELP & USAGE ####
#####################
usage() {
glow -p github.com/hishantik/openAI-shell-cli || error "connection error...."
}
###########################
### CUSTOM ERROR OUTPUT ###
###########################
error () {
gum style --border rounded --border-foreground "#EF233C" --foreground "#FFFFFF" "$*π" >&2
}
#####################
## EXITING ERROR ####
#####################
exiting () {
error "$*"
exit 1
}
######################
#### CUSTOM OUTPUT ###
######################
info () {
gum style --border rounded --border-foreground "#1B998B" --foreground "#F6AA1C" --bold \
--padding "1 1 0 1" --margin "1 1"\
"$1" "$2"
}
######################
### UNINSTALL CLI ####
######################
uninstall(){
spinner "monkey" "Uninstalling DekuAI...."
case "$SYSTEM" in
"Android" ) rm $"0" && exit 0;;
"GNU/Linux" ) sudo rm $"0" && exit 0;;
"Darwin" ) sudo rm $"0" && exit 0;;
esac
}
#################
### LOGIN #######
#################
provideToken(){
clear
gum style\
--foreground "#1B998B" --width $WIDTH --border hidden\
--align center --bold "WELCOME TO $(gum style --foreground "#FFB703" "π DekuAI") $(gum style --foreground "#FFFFFF" "AUTH SECTION")" \
"$(gum style --faint --foreground "#FFFFFF" "Visit π https://github.com/hishantik/openAI-shell-cli")"
gum style\
--border thick --border-foreground "#1B998B"\
--foreground "#F6AA1C" --align center --width 18 --height 1\
--bold "AUTHENTICATE" --padding "0 0" --margin "1 $((($WIDTH-18)/2))"
TOKEN=$(gum input --cursor.foreground "#F6AA1C" --prompt.foreground "#1B998B"\
--prompt.margin "0 1 0 5" --prompt "Enter your openai token π" --password --placeholder " openai token"
)
if [ -z "$TOKEN" ];then
echo "" &> /dev/null
else
if [ -n "$1" ]; then
if [ -f $HOME/.zshrc ]; then
sed -i '/OPENAI_TOKEN/c\export OPENAI_TOKEN=$TOKEN' $HOME/.zshrc
source $HOME/.zshrc
else
if [ -f $HOME/.bashrc ]; then
sed -i '/OPENAI_TOKEN/c\export OPENAI_TOKEN=$TOKEN' $HOME/.bashrc
source $HOME/.bashrc
else
sed -i '/OPENAI_TOKEN/c\export OPENAI_TOKEN=$TOKEN' $HOME/.profile
source $HOME/.profile
fi
fi
else
if [ -f $HOME/.zshrc ]; then
echo "export OPENAI_TOKEN=$TOKEN" >> $HOME/.zshrc
source $HOME/.zshrc
else
if [ -f $HOME/.bashrc ]; then
echo "export OPENAI_TOKEN=$TOKEN" >> $HOME/.bashrc
source $HOME/.bashrc
else
echo "export OPENAI_TOKEN=$TOKEN" >> $HOME/.profile
source $HOME/.profile
fi
fi
fi
fi
sleep 0.5; clear
}
######################
#### CHECK TOKEN #####
######################
checktoken(){
result=$(grep "OPENAI_TOKEN" $HOME/.zshrc || grep "OPENAI_TOKEN" $HOME/.bashrc)
if [ -n "$result" ]; then
echo "" &> /dev/null
else
gum style --foreground "Sorry could not find openai token.Please provide one."
provideToken $result
fi
}
#################
## CLI MENU #####
#################
menu(){
gum style\
--foreground "#1B998B" --bold "DekuAI MENU :"
gum style\
--faint --foreground "#E5E5E5" --margin "1 1" "press j β | k β β’ esc quit β’ enter choose" &
MENU=$(gum choose --item.margin "1 0 0 3" \
--item.border-foreground "#1B998B" --item.align center --item.width 11 --item.foreground "#FFFFFF" --item.border rounded --cursor "> " \
--cursor.background "#F6AA1C" --cursor.width 13 --cursor.align center --cursor.foreground "#001219" --cursor.bold\
--cursor.padding "0 1" --cursor.margin "1 0 0 3"\
--limit 1 "help" "options" "update" "version" "auth" "uninstall" "exit")
case "$MENU" in
"help") usage && menu;;
"options") option && menu;;
"update")checkupdate && dekuai;;
"version") version && menu;;
"auth") provideToken && menu;;
"uninstall") uninstall && exit 0;;
"exit") clear && quit ;;
esac
}
######################
### CLI OPTIONS ######
######################
option(){
clear
gum style\
--foreground "#1B998B" --bold "DekuAI OPTIONS :"
gum style\
--faint --foreground "#E5E5E5" --margin "1 1" "press j β | k β β’ esc quit β’ enter choose" &
OPTIONS=$(gum choose --item.margin "1 0 0 3" \
--item.border-foreground "#1B998B" --item.align center --item.width 11 --item.foreground "#FFFFFF" --item.border rounded --cursor "> " \
--cursor.background "#F6AA1C" --cursor.width 13 --cursor.align center --cursor.foreground "#001219" --cursor.bold\
--cursor.padding "0 1" --cursor.margin "1 0 0 3"\
--limit 1 "dekuai" "dalle" "back")
case "$OPTIONS" in
"dekuai") "$0" ;;
"dalle") dalle && option;;
"back") clear && menu ;;
esac
}
#####################################
## CHECK UPDATES DEPENDING ON OS ### (I dont know if it might work or not..... will be fixed later)
#####################################
checkupdate(){
case $SYSTEM in
"Android") update;;
"GNU/Linux") sudo update;;
"Darwin") sudo update;;
esac
}
#################
## UPDATE CLI ###
#################
update () {
update="$(curl -fsSL "https://raw.githubusercontent.com/Hishantik/openAI-shell-cli/main/dekuai.sh")" || error "Connection error..."
update="$(printf '%s\n' "$update" | diff -u "$0" -)"
if [ -z "$update" ]; then
info "DekuAI is up to date :)"
else
if printf '%s\n' "$update" | patch "$0" - ; then
info "DekuAI has been updated"
else
exiting "Can't update for some reason!"
fi
fi
exit 0
}
##########################
## OVERLOADING SPINNER ###
##########################
spinner(){
gum spin --spinner "$1" --title "$2π" --spinner.foreground "#F6AA1C" --title.bold --title.foreground "#1B998B" \
--spinner.margin "1 0 0 1" -- sleep 3
}
#################
## CLI VERSION ##
#################
VERSION="0.1.2"
version (){
sleep 1;clear
spinner "monkey" "loading please wait..."
info "π Version : $VERSION"
}
######################
## RESPONSE SPINNER ##
######################
loading(){
while kill -0 "$pid" 2> /dev/null; do
gum spin --spinner points --title "loading please wait...π" --spinner.foreground "#FFB703" --title.foreground "#1B998B" --spinner.margin "1 0 0 1" --title.bold -- sleep 5
done
}
######################
## QUITTING PROGRAM ##
######################
quit(){
running=false
spinner "monkey" "quitting...."
gum style \
--border rounded --padding "0 1 0 1" --margin "1 1 1 1" --border-foreground "#1B998B" --foreground "#F6AA1C"\
"π Bye have a great day!!"
exit 0
}
##############################
### DALLE IMAGE GENERATOR #### (BETA testing phase under development)
##############################
dalle(){
runnning=true
while $running;do
clear
gum style\
--foreground "#1B998B" --width $WIDTH --border hidden\
--align center --bold "WELCOME TO $(gum style --foreground "#FFB703" "π DekuAI") $(gum style --foreground "#FFFFFF" " DALLE IMAGE GENERATION")" \
"$(gum style --faint --foreground "#FFFFFF" "Visit π https://github.com/hishantik/openAI-shell-cli") for documentation."
DESCRIPTION=$(gum write\
--char-limit 0 --base.border rounded --width $WIDTH --base.padding "0 3"\
--prompt " " --placeholder "Give description of your image"\
--cursor.foreground "#FFB703" --cursor.background "#FFB703" --cursor.bold --base.border-foreground '#1B998B'\
--base.margin "1 $((($WIDTH-50)/2))" --base.width 50 --base.height 7 --placeholder.bold --base.padding "1 0"\
)
sleep 0.5; clear
DESC=$(gum style \
--border rounded "π.You searched for : $DESCRIPTION"\
--bold --width 2 --height 1\
--border-foreground "#FFB703" --foreground "#1B998B" --padding "0 1"
)
if [ -z "$DESCRIPTION" ]; then
spinner "monkey" "quitting...." && option
else
echo "please wait while generatng image"
curl -fsS https://api.openai.com/v1/images/generations \
-H 'Content-Type: application/json' \
-H "Authorization: Bearer $OPENAI_TOKEN" \
-d '{
"prompt": "'"$DESCRIPTION"'",
"n": 10,
"size": "1024x1024"
}'|jq -r '.data[].url'|while read url; do
if [ -n "$url" ]; then
no=1
curl -sS $url -o $HOME/.local/dekuai/search_history/image$no.png
xdg-open $HOME/.local/dekuai/search_history/image$no.png
gum confirm --selected.margin "1 1 1 4" --unselected.margin "1 1 1 4" --prompt.margin "2 0 0 4" --selected.background "#1B998B" --prompt.foreground "# F6AA1C" "π Next image ?" && continue || break
fi
done
rm $HOME/.local/dekuai/search_history/*
gum confirm --selected.margin "1 1 1 4" --unselected.margin "1 1 1 4" --prompt.margin "2 0 0 4" --selected.background "#1B998B" --prompt.foreground "# F6AA1C" "π Generate new image ?" && continue || running=false && spinner "monkey" "quitting...." && option
fi
done
}
########################
## COMMAND LINE ARGS ###
########################
OPT=$(getopt -o mvuUh -l menu,version,update,help,uninstall --n "$0" -- "$@")
eval set -- "$OPT"
unset OPT
while true; do
case $1 in
-m | --menu) menu && exit 0;;
-v | --version) version && exit 0 ;;
-u | --uninstall) uninstall && exit 0;;
-U | --update) update && exit 0 ;;
-h | --help) usage && exit 0 ;;
--) shift && break ;;
*) printf "\33[0:31mInvalid option: %s" "$1" && usage && exit 1 ;;
esac
shift
done
##########################
### HANDLING RESPONSE ####
##########################
# handleresponse(){
# if [ -z "$1" ]; then
# return 1
# else
# if [ $2 -eq 1 ]; then
# echo "$1" | jq -M -r '.choices[].text|gsub("\\\\n";"\n")' | awk '{ printf "%s\n", $0 }' > $HOME/.local/dekuai/answers.txt
# else
# echo "$1" | jq -M -r '.choices[].text|gsub("\\\\n";"\n")' | awk '{ printf "%s\n", $0 }' >> $HOME/.local/dekuai/answers.txt
# fi
# fi
# }
#####################
## MAIN GOES HERE ###
#####################
#Creating directory to store dekuai configs and file history
if [ -d $HOME/.local/dekuai/search_history ]; then
echo "" &> /dev/null
else
mkdir -p $HOME/.local/dekuai/search_history
fi
####################
## PROGRAM HEADER ##
####################
#first checks for token if it is available in rc file o r not
checktoken
gum style\
--foreground "#1B998B" --width $WIDTH --border hidden\
--align center --bold "WELCOME TO $(gum style --foreground "#FFB703" "π DekuAI")" \
"$(gum style --faint --foreground "#FFFFFF" "press esc β exit β’ ctrl + d β enter ")"
######################
## PROGRAM RUNNING ###
######################
running=true
while $running; do
gum style --margin "1 2 0 $((($WIDTH-50)/2))" --foreground "#1B998B" --bold "ASK π : "
QUESTION=$(gum write\
--char-limit 0 --base.border rounded --width $WIDTH --base.padding "0 3"\
--prompt " " --placeholder "Type your questions here......"\
--cursor.foreground "#FFB703" --cursor.background "#FFB703" --cursor.bold --base.border-foreground '#1B998B'\
--base.margin "1 $((($WIDTH-50)/2))" --base.width 50 --base.height 7 --placeholder.bold --base.padding "1 0"\
)
# splittting length of the string into substring according to the size of the terminal
QUESTION=$(echo "$QUESTION" | awk -v width="$(($WIDTH-28))" '{
len = length($0)
for (i = 1; i <= len; i += width) {
print substr($0, i, width) "\n"
}
}')
sleep 0.5; clear
QUE=$(gum style \
--border rounded "π. $(echo "$QUESTION"| fold -w $(($WIDTH-10)))"\
--bold \
--border-foreground "#FFB703" --foreground "#1B998B" --padding "0 1"
)
if [ -z "$QUESTION" ]; then
gum confirm --prompt.margin "2 0 0 2" --selected.background "#1B998B" --prompt.foreground "# F6AA1C" "π Are you sure you want to quit?" && quit || continue
else
if [ "$QUESTION" = "clear" ]; then
clear
else
if [ "$QUESTION" = "options" ]; then
option
else
RESPONSE=""
STATUS=0
#Check whether the user inputlenght is long or short
MAX_INPUT_LENGTH=1024
INPUT_CHUNK_SIZE=512
#dividing input into chunks of word according to input chuk size
if [ ${#QUESTION} -gt $MAX_INPUT_LENGTH ];then
QUESTION=$(echo "User input is too long...")
for i in $(seq 0 $INPUT_CHUNK_SIZE $((${#QUESTION}-1)));do
CHUNK=${QUESTION:$i:$INPUT_CHUNK_SIZE}
curl https://api.openai.com/v1/completions \
-sS \
-H 'Content-Type: application/json' \
-H "Authorization: Bearer $OPENAI_TOKEN" \
-d '{
"model": "text-davinci-003",
"prompt": "'"${CHUNK}"'",
"max_tokens": 4000,
"temperature": 0.7,
"best_of":1,
"top_p":1,
"frequency_penalty": 0.81,
"presence_penalty": 0.8
}' | jq -M -r '.choices[].text|gsub("\\\\n";"\n")' | awk '{ printf "%s\n", $0 }' >> $HOME/.local/dekuai/answers.txt
done
else
#if user input length is not too long ....
QUESTION=$(echo "$QUESTION" | grep -v '^ *$' |tr '\n' ' ')
curl https://api.openai.com/v1/completions \
-sS \
-H 'Content-Type: application/json' \
-H "Authorization: Bearer $OPENAI_TOKEN" \
-d '{
"model": "text-davinci-003",
"prompt": "'"${QUESTION}"'",
"max_tokens": 4000,
"temperature": 0.7,
"best_of":1,
"top_p":1,
"frequency_penalty": 0.81,
"presence_penalty": 0.8
}' | jq -M -r '.choices[].text|gsub("\\\\n";"\n")' | awk '{ printf "%s\n", $0 }' > $HOME/.local/dekuai/answers.txt
fi &
pid=$!
loading
RESPONSE=$(cat $HOME/.local/dekuai/answers.txt)
echo "DekuAI π΅ : $RESPONSE" > $HOME/.local/dekuai/answers.txt
OUTPUT=$(gum style --foreground "#83C5BE" "$(gum format --theme notty< ~/.local/dekuai/answers.txt)")
gum style \
--width $(($WIDTH-10)) --foreground "#83C5BE" --padding "1 4 2 4" --margin "0 0 0 5" --border thick --border-foreground "#1B998B" "${QUE}" "${OUTPUT}"
gum confirm --selected.margin "1 1 1 4" --unselected.margin "1 1 1 4" --prompt.margin "2 0 0 4" --selected.background "#1B998B" --prompt.foreground "# F6AA1C" "π Next Question?" && continue || quit
fi
fi
fi
done