Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion backends/moonshine-server
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ case "${1:-}" in
if [ ! -S "$SOCK" ]; then
"$0" start >&2 || exit 1
fi
echo "$2" | socat -T 30 - UNIX-CONNECT:"$SOCK"
echo "$2" | socat -t 120 -T 120 - UNIX-CONNECT:"$SOCK"
;;
*)
echo "Usage: moonshine-server {start|stop|transcribe <audio.wav>}" >&2
Expand Down
2 changes: 1 addition & 1 deletion backends/parakeet-server
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ case "${1:-}" in
if [ ! -S "$SOCK" ]; then
"$0" start >&2 || exit 1
fi
echo "$2" | socat -T 30 - UNIX-CONNECT:"$SOCK"
echo "$2" | socat -t 120 -T 120 - UNIX-CONNECT:"$SOCK"
;;
*)
echo "Usage: parakeet-server {start|stop|transcribe <audio.wav>}" >&2
Expand Down
11 changes: 9 additions & 2 deletions transcribe-server
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,20 @@ case "${1:-}" in
;;
transcribe)
# Ensure daemon is alive (not just a stale socket from a crash)
if [ -S "$SOCK" ] && [ -f "$PIDFILE" ] && ! kill -0 "$(cat "$PIDFILE")" 2>/dev/null; then
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
echo "$2" | socat -T 30 - UNIX-CONNECT:"$SOCK"
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
Expand Down
26 changes: 24 additions & 2 deletions whisper-daemon.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import sys
import socket
import signal
import logging
import time
from faster_whisper import WhisperModel

SOCK_PATH = sys.argv[1]
Expand All @@ -11,10 +13,19 @@
DEVICE = sys.argv[4]
COMPUTE = sys.argv[5]

LOG_PATH = os.path.join(os.environ.get("XDG_RUNTIME_DIR", "/tmp"), "talktype-whisper.log")
logging.basicConfig(
filename=LOG_PATH, level=logging.INFO,
format="%(asctime)s %(levelname)s %(message)s",
)
log = logging.getLogger("whisper-daemon")

# Load model once
log.info("Loading faster-whisper %s (device=%s, compute=%s)...", MODEL_NAME, DEVICE, COMPUTE)
print(f"Loading faster-whisper {MODEL_NAME}...", flush=True)
model = WhisperModel(MODEL_NAME, device=DEVICE, compute_type=COMPUTE)
print("Model loaded.", flush=True)
log.info("Model loaded.")


def transcribe(audio_path):
Expand All @@ -23,6 +34,7 @@ def transcribe(audio_path):


def cleanup(*_):
log.info("Shutting down (signal).")
try:
os.unlink(SOCK_PATH)
except OSError:
Expand All @@ -45,12 +57,22 @@ def cleanup(*_):
try:
audio_path = conn.recv(4096).decode().strip()
if audio_path and os.path.isfile(audio_path):
file_size = os.path.getsize(audio_path)
log.info("Transcribing %s (%d bytes)...", audio_path, file_size)
t0 = time.monotonic()
text = transcribe(audio_path)
elapsed = time.monotonic() - t0
log.info("Done in %.1fs, %d chars: %s", elapsed, len(text),
text[:200] if text else "(empty)")
conn.sendall(text.encode())
else:
log.warning("Bad path: %r", audio_path)
conn.sendall(b"")
except Exception as e:
print(f"Error: {e}", file=sys.stderr, flush=True)
conn.sendall(b"")
log.exception("Error during transcription")
try:
conn.sendall(b"")
except Exception:
pass
finally:
conn.close()
Loading