52 lines
1.4 KiB
Python
52 lines
1.4 KiB
Python
#!/usr/bin/env python3
|
|
"""Veskslop STT helper — transcribe a 16-bit mono WAV with Vosk on the CPU."""
|
|
|
|
import json
|
|
import os
|
|
import sys
|
|
import wave
|
|
|
|
|
|
def main() -> int:
|
|
if len(sys.argv) < 2:
|
|
print("usage: stt-vosk.py <wav>", file=sys.stderr)
|
|
return 2
|
|
|
|
model_path = os.environ.get("VESKSLOP_VOSK_MODEL")
|
|
if not model_path:
|
|
print("VESKSLOP_VOSK_MODEL is not set", file=sys.stderr)
|
|
return 2
|
|
|
|
try:
|
|
from vosk import KaldiRecognizer, Model, SetLogLevel
|
|
except ImportError:
|
|
print("vosk is not installed (pip install vosk)", file=sys.stderr)
|
|
return 2
|
|
|
|
SetLogLevel(-1)
|
|
model = Model(model_path)
|
|
|
|
with wave.open(sys.argv[1], "rb") as wav:
|
|
if wav.getnchannels() != 1 or wav.getsampwidth() != 2:
|
|
print("input must be 16-bit mono WAV", file=sys.stderr)
|
|
return 2
|
|
|
|
recognizer = KaldiRecognizer(model, wav.getframerate())
|
|
parts = []
|
|
|
|
while True:
|
|
data = wav.readframes(4000)
|
|
if not data:
|
|
break
|
|
if recognizer.AcceptWaveform(data):
|
|
parts.append(json.loads(recognizer.Result()).get("text", ""))
|
|
|
|
parts.append(json.loads(recognizer.FinalResult()).get("text", ""))
|
|
|
|
print(" ".join(part for part in parts if part).strip())
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
sys.exit(main())
|