Skip to content

Commit ad8f2ba

Browse files
authored
Merge pull request #10 from csheaff/fix/socat-shutdown-timeout
Fix socat dropping results on longer recordings
2 parents c4937fd + f4994ca commit ad8f2ba

4 files changed

Lines changed: 35 additions & 6 deletions

File tree

backends/moonshine-server

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ case "${1:-}" in
6262
if [ ! -S "$SOCK" ]; then
6363
"$0" start >&2 || exit 1
6464
fi
65-
echo "$2" | socat -T 30 - UNIX-CONNECT:"$SOCK"
65+
echo "$2" | socat -t 120 -T 120 - UNIX-CONNECT:"$SOCK"
6666
;;
6767
*)
6868
echo "Usage: moonshine-server {start|stop|transcribe <audio.wav>}" >&2

backends/parakeet-server

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ case "${1:-}" in
6161
if [ ! -S "$SOCK" ]; then
6262
"$0" start >&2 || exit 1
6363
fi
64-
echo "$2" | socat -T 30 - UNIX-CONNECT:"$SOCK"
64+
echo "$2" | socat -t 120 -T 120 - UNIX-CONNECT:"$SOCK"
6565
;;
6666
*)
6767
echo "Usage: parakeet-server {start|stop|transcribe <audio.wav>}" >&2

transcribe-server

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,20 @@ case "${1:-}" in
6161
;;
6262
transcribe)
6363
# Ensure daemon is alive (not just a stale socket from a crash)
64-
if [ -S "$SOCK" ] && [ -f "$PIDFILE" ] && ! kill -0 "$(cat "$PIDFILE")" 2>/dev/null; then
64+
if [ -f "$PIDFILE" ] && ! kill -0 "$(cat "$PIDFILE")" 2>/dev/null; then
6565
rm -f "$PIDFILE" "$SOCK"
6666
fi
6767
if [ ! -S "$SOCK" ]; then
6868
"$0" start >&2 || exit 1
6969
fi
70-
echo "$2" | socat -T 30 - UNIX-CONNECT:"$SOCK"
70+
TEXT=$(echo "$2" | socat -t 120 -T 120 - UNIX-CONNECT:"$SOCK" 2>/dev/null) || true
71+
if [ -z "$TEXT" ] && [ -f "$PIDFILE" ] && ! kill -0 "$(cat "$PIDFILE")" 2>/dev/null; then
72+
# Daemon died during transcription — restart and retry once
73+
rm -f "$PIDFILE" "$SOCK"
74+
"$0" start >&2 || exit 1
75+
TEXT=$(echo "$2" | socat -t 120 -T 120 - UNIX-CONNECT:"$SOCK" 2>/dev/null) || true
76+
fi
77+
printf '%s' "$TEXT"
7178
;;
7279
*)
7380
echo "Usage: transcribe-server {start|stop|transcribe <audio.wav>}" >&2

whisper-daemon.py

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import sys
44
import socket
55
import signal
6+
import logging
7+
import time
68
from faster_whisper import WhisperModel
79

810
SOCK_PATH = sys.argv[1]
@@ -11,10 +13,19 @@
1113
DEVICE = sys.argv[4]
1214
COMPUTE = sys.argv[5]
1315

16+
LOG_PATH = os.path.join(os.environ.get("XDG_RUNTIME_DIR", "/tmp"), "talktype-whisper.log")
17+
logging.basicConfig(
18+
filename=LOG_PATH, level=logging.INFO,
19+
format="%(asctime)s %(levelname)s %(message)s",
20+
)
21+
log = logging.getLogger("whisper-daemon")
22+
1423
# Load model once
24+
log.info("Loading faster-whisper %s (device=%s, compute=%s)...", MODEL_NAME, DEVICE, COMPUTE)
1525
print(f"Loading faster-whisper {MODEL_NAME}...", flush=True)
1626
model = WhisperModel(MODEL_NAME, device=DEVICE, compute_type=COMPUTE)
1727
print("Model loaded.", flush=True)
28+
log.info("Model loaded.")
1829

1930

2031
def transcribe(audio_path):
@@ -23,6 +34,7 @@ def transcribe(audio_path):
2334

2435

2536
def cleanup(*_):
37+
log.info("Shutting down (signal).")
2638
try:
2739
os.unlink(SOCK_PATH)
2840
except OSError:
@@ -45,12 +57,22 @@ def cleanup(*_):
4557
try:
4658
audio_path = conn.recv(4096).decode().strip()
4759
if audio_path and os.path.isfile(audio_path):
60+
file_size = os.path.getsize(audio_path)
61+
log.info("Transcribing %s (%d bytes)...", audio_path, file_size)
62+
t0 = time.monotonic()
4863
text = transcribe(audio_path)
64+
elapsed = time.monotonic() - t0
65+
log.info("Done in %.1fs, %d chars: %s", elapsed, len(text),
66+
text[:200] if text else "(empty)")
4967
conn.sendall(text.encode())
5068
else:
69+
log.warning("Bad path: %r", audio_path)
5170
conn.sendall(b"")
5271
except Exception as e:
53-
print(f"Error: {e}", file=sys.stderr, flush=True)
54-
conn.sendall(b"")
72+
log.exception("Error during transcription")
73+
try:
74+
conn.sendall(b"")
75+
except Exception:
76+
pass
5577
finally:
5678
conn.close()

0 commit comments

Comments
 (0)