-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathllama-server-osx.sh
executable file
·133 lines (111 loc) · 2.86 KB
/
llama-server-osx.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
#!/bin/bash
#
# Llama Server OSX
#
# A shell script, in a shell script, in a Platypus wrapper
#
#
# Init / Config
#
config_dir="$HOME/.config/llamaserver"
config_options="$config_dir/options.sh"
if [ "$1" == "Create ~/.config/llamaserver" ]; then
mkdir -p "$config_dir"
app_dir=$(dirname "$(readlink -f "$0")")
cp "$app_dir/options.sh" "$config_dir"
cp "$app_dir/example.model.sh" "$config_dir"
cp "$app_dir/example.sandbox.sb" "$config_dir"
open "$config_dir"
exit 0
elif [ "$1" == "Edit ~/.config/llamaserver" ]; then
open "$config_dir"
exit 0
fi
if [ ! -f "$config_options" ]; then
echo "Create ~/.config/llamaserver"
exit 0
fi
source "$config_options"
if [[ "$LLAMA_SERVER_SCRIPT" == *"/path/to/llama.cpp/server" ]] || [[ ${#LLAMA_SERVER_MODELS[@]} -eq 0 ]]; then
echo "Edit ~/.config/llamaserver"
exit 0
fi
#
# Models
#
all_models=()
shopt -s nullglob
for dir in "${LLAMA_SERVER_MODELS[@]}"; do
for file in "$dir"/*.{gguf,model.sh}; do
all_models+=("$file")
done
done
shopt -u nullglob
if [ ${#all_models[@]} -eq 0 ]; then
echo "Edit ~/.config/llamaserver"
exit 0
fi
model_resolve() {
base=$1
for file in "${all_models[@]}"; do
if [ "$(basename "$file")" == "$base" ]; then
echo "$file"
return
fi
done
}
model_menu() {
for file in "${all_models[@]}"; do
echo "$(basename "$file")"
done | sort -V | paste -sd "|" -
}
#
# Commands
#
log_file="$config_dir/server.log"
server_options=("${LLAMA_SERVER_OPTIONS[@]}");
start_server() {
if [ -f "$LLAMA_SERVER_SANDBOX" ]; then
echo "sandbox-exec -f $LLAMA_SERVER_SANDBOX nohup $LLAMA_SERVER_SCRIPT ${server_options[@]}" >> "$log_file"
sandbox-exec -f "$LLAMA_SERVER_SANDBOX" nohup "$LLAMA_SERVER_SCRIPT" "${server_options[@]}" >> "$log_file" 2>&1 &
else
echo "nohup $LLAMA_SERVER_SCRIPT ${server_options[@]}" >> "$log_file"
nohup "$LLAMA_SERVER_SCRIPT" "${server_options[@]}" >> "$log_file" 2>&1 &
fi
}
# Command: Select model
if [[ "$1" == *.gguf ]]; then
echo "$1" > "$log_file"
server_options+=("--model $(model_resolve "$1")")
start_server
# Command: Select configured model
elif [[ "$1" == *.model.sh ]]; then
echo "$1" > "$log_file"
source $(model_resolve "$1")
server_options+=("${LLAMA_SERVER_MODEL_OPTIONS[@]}")
start_server
# Command: Stop server
elif [ "$1" == "Stop server" ]; then
rm "$log_file"
pkill -f "$LLAMA_SERVER_SCRIPT"
fi
#
# Render menu
#
# Server process is running
if pgrep -f "$LLAMA_SERVER_SCRIPT" > /dev/null; then
echo "STATUSTITLE|🦙🟢"
echo "Stop server"
echo "Edit ~/.config/llamaserver"
echo "----"
if [ -f "$log_file" ]; then
echo "DISABLED|Using: $(head -n 1 "$log_file")"
echo "----"
tail -n 5 "$log_file" | tr -s ' ' | fold -w 50 | sed 's/^/DISABLED|/'
fi
# Or not running
else
echo "STATUSTITLE|🦙"
echo "SUBMENU|Load model|$(model_menu)"
echo "Edit ~/.config/llamaserver"
fi