-
Notifications
You must be signed in to change notification settings - Fork 126
Expand file tree
/
Copy pathrun_chatbot.sh
More file actions
executable file
·203 lines (177 loc) · 6.38 KB
/
Copy pathrun_chatbot.sh
File metadata and controls
executable file
·203 lines (177 loc) · 6.38 KB
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
#!/bin/bash
# Set working directory and environment
os_name=$(uname -s 2>/dev/null || echo "unknown")
is_linux=false
is_darwin=false
is_windows=false
case "$os_name" in
Linux*) is_linux=true ;;
Darwin*) is_darwin=true ;;
MINGW*|MSYS*|CYGWIN*) is_windows=true ;;
esac
if [ "$is_linux" = true ] &&
[ "${WHISPLAY_ALLOW_LEGACY_CHATBOT_SERVICE:-false}" != "true" ] &&
grep -q 'chatbot\.service' /proc/self/cgroup 2>/dev/null &&
{ systemctl is-active --quiet whisplay-daemon.service 2>/dev/null || [ -S /tmp/whisplay-daemon.sock ]; }; then
echo "whisplay-daemon is active; legacy chatbot.service mode is disabled to avoid duplicate ASR/TTS ports."
echo "Use the Whisplay daemon launcher, or run: whisplay service restart"
while true; do
sleep 3600
done
fi
export NVM_DIR="${NVM_DIR:-$HOME/.nvm}"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"
# Find the unified Whisplay sound card first; keep legacy names as fallback.
card_index=""
card_name=""
audio_supported=false
if [ "$is_linux" = true ] && [ -r "/proc/asound/cards" ] && command -v amixer >/dev/null 2>&1; then
card_info=$(awk '/whisplaysound|wm8960soundcard|es8389soundcard/ {print $1 " " $2; exit}' /proc/asound/cards)
if [ -n "$card_info" ]; then
card_index=$(echo "$card_info" | awk '{print $1}')
card_name=$(echo "$card_info" | awk '{print $2}' | tr -d '[]:')
audio_supported=true
echo "Using sound card: ${card_name:-unknown} (index ${card_index})"
else
echo "Whisplay sound card not found; using default audio devices."
fi
else
echo "Audio setup skipped for OS: $os_name"
fi
# Output current environment information (for debugging)
echo "===== Start time: $(date) ====="
echo "Current user: $(whoami)"
echo "Working directory: $(pwd)"
working_dir=$(pwd)
echo "PATH: $PATH"
if command -v python3 >/dev/null 2>&1; then
echo "Python version: $(python3 --version)"
else
echo "Python version: not found"
fi
if command -v node >/dev/null 2>&1; then
echo "Node version: $(node --version)"
else
echo "Node version: not found"
fi
# Start the service
echo "Starting Node.js application..."
cd $working_dir
get_env_value() {
if grep -Eq "^[[:space:]]*$1[[:space:]]*=" .env; then
val=$(grep -E "^[[:space:]]*$1[[:space:]]*=" .env | tail -n1 | cut -d'=' -f2-)
# trim whitespace and surrounding quotes
echo "$(echo "$val" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//' -e 's/^"//' -e 's/"$//' -e "s/^'//" -e "s/'$//")"
else
echo ""
fi
}
# load .env variables, exclude comments and empty lines
# check if .env file exists
initial_volume_percent=""
default_initial_volume_percent=80
serve_ollama=false
if [ -f ".env" ]; then
# Load only SERVE_OLLAMA from .env (ignore comments/other vars)
SERVE_OLLAMA=$(get_env_value "SERVE_OLLAMA")
[ -n "$SERVE_OLLAMA" ] && export SERVE_OLLAMA
CUSTOM_FONT_PATH=$(get_env_value "CUSTOM_FONT_PATH")
[ -n "$CUSTOM_FONT_PATH" ] && export CUSTOM_FONT_PATH
INITIAL_VOLUME_PERCENT=$(get_env_value "INITIAL_VOLUME_PERCENT")
INITIAL_VOLUME_LEVEL=$(get_env_value "INITIAL_VOLUME_LEVEL")
if [ -n "$INITIAL_VOLUME_LEVEL" ]; then
echo "[Volume] INITIAL_VOLUME_LEVEL is deprecated and ignored. Please use INITIAL_VOLUME_PERCENT (0-100) instead."
fi
WHISPER_MODEL_SIZE=$(get_env_value "WHISPER_MODEL_SIZE")
[ -n "$WHISPER_MODEL_SIZE" ] && export WHISPER_MODEL_SIZE
FASTER_WHISPER_MODEL_SIZE=$(get_env_value "FASTER_WHISPER_MODEL_SIZE")
[ -n "$FASTER_WHISPER_MODEL_SIZE" ] && export FASTER_WHISPER_MODEL_SIZE
ALSA_INPUT_DEVICE=$(get_env_value "ALSA_INPUT_DEVICE")
[ -n "$ALSA_INPUT_DEVICE" ] && export ALSA_INPUT_DEVICE
ALSA_OUTPUT_DEVICE=$(get_env_value "ALSA_OUTPUT_DEVICE")
[ -n "$ALSA_OUTPUT_DEVICE" ] && export ALSA_OUTPUT_DEVICE
echo ".env variables loaded."
# check if SERVE_OLLAMA is set to true
if [ "$SERVE_OLLAMA" = "true" ]; then
serve_ollama=true
fi
if [ -n "$INITIAL_VOLUME_PERCENT" ] && [ "$INITIAL_VOLUME_PERCENT" != "auto" ]; then
initial_volume_percent=$INITIAL_VOLUME_PERCENT
fi
else
echo ".env file not found, please create one based on .env.template."
exit 1
fi
# Adjust initial volume (Linux only). INITIAL_VOLUME_PERCENT is the percentage
# users see in alsamixer. The unified driver exposes a 0-100 speaker control;
# legacy WM8960 accepts percentages through amixer set Speaker.
if [ "$audio_supported" = true ]; then
if [ -z "$initial_volume_percent" ]; then
initial_volume_percent=$default_initial_volume_percent
fi
if [ "$card_name" = "whisplaysound" ]; then
amixer -c "$card_name" cset name='speaker' "$initial_volume_percent" >/dev/null 2>&1 || true
else
amixer -c "$card_index" set Speaker "${initial_volume_percent}%" >/dev/null 2>&1 || true
fi
fi
if [ -n "$card_name" ]; then
export SOUND_CARD_NAME="$card_name"
export SOUND_CARD_INDEX="$card_index"
export ALSA_INPUT_DEVICE="${ALSA_INPUT_DEVICE:-hw:${card_name},0}"
if [ -z "$ALSA_OUTPUT_DEVICE" ]; then
if [ "$card_name" = "whisplaysound" ]; then
export ALSA_OUTPUT_DEVICE="playback"
else
export ALSA_OUTPUT_DEVICE="plughw:${card_name},0"
fi
fi
echo "ALSA input device: $ALSA_INPUT_DEVICE"
echo "ALSA output device: $ALSA_OUTPUT_DEVICE"
fi
if [ "$serve_ollama" = true ]; then
echo "Starting Ollama server..."
export OLLAMA_KEEP_ALIVE=-1 # ensure Ollama server stays alive
OLLAMA_HOST=0.0.0.0:11434 ollama serve &
fi
# if file use_npm exists and is true, use npm
if [ -f "use_npm" ]; then
use_npm=true
else
use_npm=false
fi
if [ -f "dist/index.js" ] && command -v node >/dev/null 2>&1; then
echo "Starting compiled application directly..."
if [ -n "$card_index" ]; then
SOUND_CARD_INDEX=$card_index node dist/index.js
else
node dist/index.js
fi
elif [ "$use_npm" = true ]; then
echo "Using npm to start the application..."
if [ -n "$card_index" ]; then
SOUND_CARD_INDEX=$card_index npm start
else
npm start
fi
else
echo "Using yarn to start the application..."
if [ -n "$card_index" ]; then
SOUND_CARD_INDEX=$card_index yarn start
else
yarn start
fi
fi
# After the service ends, perform cleanup
echo "Cleaning up after service..."
if [ "$serve_ollama" = true ]; then
echo "Stopping Ollama server..."
if command -v pkill >/dev/null 2>&1; then
pkill ollama
else
echo "pkill not available; please stop ollama manually if needed."
fi
fi
# Record end status
echo "===== Service ended: $(date) ====="