python-voiceio 0.2.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
voiceio/worker.py ADDED
@@ -0,0 +1,39 @@
1
+ """Whisper transcription worker that runs in a subprocess.
2
+
3
+ Communicates with the parent process via stdin (JSON requests) and
4
+ stdout (JSON responses). Loads the model once, then processes requests
5
+ in a loop for low-latency repeated transcriptions.
6
+
7
+ Usage: python3 -m voiceio.worker '{"model": "base", ...}'
8
+ """
9
+ from __future__ import annotations
10
+
11
+ import base64
12
+ import json
13
+ import sys
14
+
15
+ import numpy as np
16
+ from faster_whisper import WhisperModel
17
+
18
+
19
+ def main() -> None:
20
+ args = json.loads(sys.argv[1])
21
+ model = WhisperModel(args["model"], device=args["device"], compute_type=args["compute_type"])
22
+
23
+ # Warmup: first transcription is always slow
24
+ segs, _ = model.transcribe(np.zeros(16000, dtype=np.float32), language=args.get("language"), beam_size=1)
25
+ list(segs)
26
+ print("READY", flush=True)
27
+
28
+ for line in sys.stdin:
29
+ if line.strip() == "QUIT":
30
+ break
31
+ req = json.loads(line)
32
+ audio = np.frombuffer(base64.b64decode(req["audio_b64"]), dtype=np.float32)
33
+ segs, _ = model.transcribe(audio, language=args.get("language"), beam_size=1, best_of=1)
34
+ text = " ".join(s.text.strip() for s in segs).strip()
35
+ print(json.dumps({"text": text}), flush=True)
36
+
37
+
38
+ if __name__ == "__main__":
39
+ main()