-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtranscribe-server
More file actions
executable file
·83 lines (78 loc) · 2.9 KB
/
transcribe-server
File metadata and controls
executable file
·83 lines (78 loc) · 2.9 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
#!/usr/bin/env bash
# Whisper transcription server — keeps the model loaded in memory.
#
# Usage:
# transcribe-server start # start the server (loads model)
# transcribe-server stop # stop the server
# TALKTYPE_CMD="transcribe-server transcribe" talktype
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
# Source user config so env vars (WHISPER_MODEL, etc.) are available
# even when invoked directly (not via talktype).
TALKTYPE_CONFIG="${TALKTYPE_CONFIG:-${XDG_CONFIG_HOME:-$HOME/.config}/talktype/config}"
# shellcheck disable=SC1090
[ -f "$TALKTYPE_CONFIG" ] && source "$TALKTYPE_CONFIG"
VENV="${TALKTYPE_VENV:-$SCRIPT_DIR/.venv}"
SOCK="${XDG_RUNTIME_DIR:-/tmp}/talktype-whisper.sock"
PIDFILE="${XDG_RUNTIME_DIR:-/tmp}/talktype-whisper.pid"
WHISPER_MODEL="${WHISPER_MODEL:-base}"
WHISPER_LANG="${WHISPER_LANG:-en}"
WHISPER_DEVICE="${WHISPER_DEVICE:-cuda}"
WHISPER_COMPUTE="${WHISPER_COMPUTE:-float16}"
case "${1:-}" in
start)
if [ -f "$PIDFILE" ] && kill -0 "$(cat "$PIDFILE")" 2>/dev/null; then
echo "Already running (PID $(cat "$PIDFILE"))"
exit 0
fi
if [ ! -x "$VENV/bin/python3" ]; then
echo "Whisper backend not installed. Run: make install" >&2
exit 1
fi
rm -f "$PIDFILE" "$SOCK"
echo "Starting whisper server (loading $WHISPER_MODEL model)..."
"$VENV/bin/python3" "$SCRIPT_DIR/whisper-daemon.py" "$SOCK" "$WHISPER_MODEL" "$WHISPER_LANG" "$WHISPER_DEVICE" "$WHISPER_COMPUTE" &
PID=$!
disown "$PID"
echo "$PID" > "$PIDFILE"
for _ in $(seq 1 60); do
[ -S "$SOCK" ] && break
sleep 1
done
if [ -S "$SOCK" ]; then
echo "Ready (PID $PID)"
else
echo "Failed to start" >&2
exit 1
fi
;;
stop)
if [ -f "$PIDFILE" ]; then
kill "$(cat "$PIDFILE")" 2>/dev/null || true
rm -f "$PIDFILE" "$SOCK"
echo "Stopped"
else
echo "Not running"
fi
;;
transcribe)
# Ensure daemon is alive (not just a stale socket from a crash)
if [ -f "$PIDFILE" ] && ! kill -0 "$(cat "$PIDFILE")" 2>/dev/null; then
rm -f "$PIDFILE" "$SOCK"
fi
if [ ! -S "$SOCK" ]; then
"$0" start >&2 || exit 1
fi
TEXT=$(echo "$2" | socat -t 120 -T 120 - UNIX-CONNECT:"$SOCK" 2>/dev/null) || true
if [ -z "$TEXT" ] && [ -f "$PIDFILE" ] && ! kill -0 "$(cat "$PIDFILE")" 2>/dev/null; then
# Daemon died during transcription — restart and retry once
rm -f "$PIDFILE" "$SOCK"
"$0" start >&2 || exit 1
TEXT=$(echo "$2" | socat -t 120 -T 120 - UNIX-CONNECT:"$SOCK" 2>/dev/null) || true
fi
printf '%s' "$TEXT"
;;
*)
echo "Usage: transcribe-server {start|stop|transcribe <audio.wav>}" >&2
exit 1
;;
esac