-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathp2t
110 lines (81 loc) · 4.83 KB
/
p2t
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
# Small but handy functions. For Personal use only.
# Use it at your own risk!
# Author: Gabriele Risso License: Apache 2.0
session=${session:-$(echo "$(wget -qO - http://frightanic.com/goodies_content/docker-names.php)-$(date '+%Y-%m-%d-%H')" )};
promptg="/dev/shm/promptg_$session.txt"
textg="/dev/shm/textg_$session.txt"
input=""
output=""
if ! type p2t 2>/dev/null 1>&2;
then
p2t ()
{
if ! command -v docker --version 2>/dev/null 1>&2;
then
echo " Gum docker is not installed on your system. Install it: https://docs.docker.com/engine/install and re-try" >&2;
echo "p2t-sgpt is disabled" >&2;
elif ! command -v gum --version 2>/dev/null 1>&2;
then
echo " gum is not installed on your system. Install it: https://github.com/charmbracelet/gum#installation and re-try" >&2;
else
if ! type p2t-sgpt 2>/dev/null 1>&2;
then
echo '{{ Bold "[*] session: '$session' " }}' | gum format -t template >&2;
p2t-sgpt ()
{
if [ -z ${OPENAI_API_KEY+x} ]; then echo '{{ Bold "[!] OPENAI_API_KEY not set! type: export OPENAI_API_KEY=\"your-key-here\" " }}' | gum format -t template >&2; return 1; fi
echo '{{ Bold "[!] sgpt started" }}' | gum format -t template >&2;
local start=$SECONDS;
local input="$1";
local output="";
#select the lastly created file.txt (1 minute old) located in /dev/shm/ that macthces t or s as a first char fo the file name OR input prompt directly
local newest_file="";
newest_file=$(find /dev/shm/ -type f -cmin -1 -size +0 -regextype sed -regex ".*\(t\|s\)*.txt" -printf "%T@ %p\n" 2>/dev/null | sort -n | tail -1 | awk '{print $2}');
if [ -n "$newest_file" ]; then
echo '{{ Bold "[!] most recent text file --> '$newest_file'" }}' | gum format -t template >&2;
local input=${input:-$(echo $newest_file)};
else
echo '{{ Bold "[+] Input prompt" }}' | gum format -t template >&2;
local input=${input:-$(echo $(gum input --char-limit=4000 --placeholder="Type something here, just ask me!" --width="$(($COLUMNS-5))" ) )};
fi
#-------------------------------------x
gum spin -s points --show-output --spinner.foreground="50" --title.bold --spinner.margin="0 1" --title "using Docker to interact with sgpt..." --\
docker run --rm --env OPENAI_API_KEY -v gpt-cache:/tmp/shell_gpt ghcr.io/ther1d/shell_gpt --no-animation --chat "$session" "$input" > "$textg";
#-------------------------------------x
echo '{{ Bold "[t] completed in '$((SECONDS-start))' seconds" }}' | gum format -t template >&2;
echo $input > $promptg >&2;
output=$(cat $textg | tr -d '\n') >&2;
if [ -z "$output" ];
then
echo '{{ Bold "Openai key could be expired or Servers are probably down or unreachable, try again..." ' | gum format -t template >&2;
return 1;
fi
gum style "INPUT: saved at $promptg" " " "$input" "OUTPUT: saved at $textg" " " "$output" "SESSION: $session" --foreground 15 --border-foreground 50 --border rounded --width="$(($COLUMNS -4))" --italic --margin "1 1" --padding "0 1";
gum confirm --negative="no" --affirmative="yes, let's read!" "open pager to read output?" --timeout=15s --selected.foreground="0" --unselected.foreground="44" --selected.italic --unselected.italic --selected.background="50" --prompt.italic --prompt.margin="0 0 0 $(($COLUMNS -29))" \
&& gum pager --border-foreground="86" --help.foreground="200" --show-line-numbers < $textg;
echo '{{ Bold "[+] sgpt OK" }}' | gum format -t template >&2;
echo '{{ Bold "[?] What now? press ESC to exit." }}' | gum format -t template >&2;
echo ' ' >&2;
while read -r -n1 key
do
# if input == ESC key
if [[ $key == $'\e' ]];
then
break;
fi
CHOICE=$(gum choose --item.foreground 250 "edit output text in glow" "edit input text in glow" "help" "contribute to aiyu");
[[ "$CHOICE" == "edit output text in glow" ]] && cp $textg "${textg::-4}.md" && glow "/dev/shm/";
[[ "$CHOICE" == "edit input text in glow" ]] && cp $promptg "${promptg::-4}.md" && glow "/dev/shm/";
[[ "$CHOICE" == "help" ]] && glow -p README.md;
[[ "$CHOICE" == "contribute to aiyu" ]] && echo "Great! I thought so, thank you. Leave a $(gum style --bold "star") if you like $(gum style --bold "aiyu"). Go to: https://github.com/GabrieleRisso/aiyu";
done;
}
else
echo '{{ Bold "p2t-sgpt is set on the system." }}' | gum format -t template >&2;
fi
fi
p2t-sgpt "$1";
}
else
echo '{{ Bold "p2t is set on the system" }}' | gum format -t template >&2;
fi