Mercurial
annotate dictation/main.py @ 275:78699f810817
Add Qwen3-VL and WebRTC dictation services
Add Bazel targets for the CUDA-backed Qwen3-VL server and a local WebRTC faster-whisper dictation service.
Co-authored-by: Copilot <[email protected]>
Copilot-Session: e3d8cb06-6c95-4ae0-9757-651d3796ab00
| author | MrJuneJune <me@mrjunejune.com> |
|---|---|
| date | Mon, 17 Aug 2026 10:58:47 -0700 |
| parents | |
| children |
| rev | line source |
|---|---|
|
275
78699f810817
Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
1 from __future__ import annotations |
|
78699f810817
Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
2 |
|
78699f810817
Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
3 import argparse |
|
78699f810817
Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
4 import asyncio |
|
78699f810817
Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
5 import json |
|
78699f810817
Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
6 import os |
|
78699f810817
Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
7 from pathlib import Path |
|
78699f810817
Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
8 import sys |
|
78699f810817
Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
9 |
|
78699f810817
Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
10 from dictation.config import ( |
|
78699f810817
Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
11 DictationConfig, |
|
78699f810817
Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
12 MODEL_REPOSITORY, |
|
78699f810817
Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
13 MODEL_REVISION, |
|
78699f810817
Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
14 ) |
|
78699f810817
Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
15 |
|
78699f810817
Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
16 |
|
78699f810817
Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
17 def download_model(config: DictationConfig) -> None: |
|
78699f810817
Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
18 from huggingface_hub import snapshot_download |
|
78699f810817
Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
19 |
|
78699f810817
Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
20 config.model_dir.mkdir(parents=True, exist_ok=True) |
|
78699f810817
Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
21 snapshot_download( |
|
78699f810817
Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
22 repo_id=MODEL_REPOSITORY, |
|
78699f810817
Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
23 revision=MODEL_REVISION, |
|
78699f810817
Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
24 local_dir=config.model_dir, |
|
78699f810817
Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
25 allow_patterns=[ |
|
78699f810817
Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
26 "config.json", |
|
78699f810817
Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
27 "model.bin", |
|
78699f810817
Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
28 "tokenizer.json", |
|
78699f810817
Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
29 "vocabulary.txt", |
|
78699f810817
Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
30 ], |
|
78699f810817
Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
31 ) |
|
78699f810817
Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
32 marker = config.model_dir / ".model-revision" |
|
78699f810817
Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
33 marker.write_text(f"{MODEL_REPOSITORY}@{MODEL_REVISION}\n", encoding="ascii") |
|
78699f810817
Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
34 print(f"Model ready: {config.model_dir}") |
|
78699f810817
Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
35 |
|
78699f810817
Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
36 |
|
78699f810817
Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
37 def preflight(config: DictationConfig) -> None: |
|
78699f810817
Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
38 import ctranslate2 |
|
78699f810817
Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
39 |
|
78699f810817
Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
40 compute_types = sorted(ctranslate2.get_supported_compute_types("cuda")) |
|
78699f810817
Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
41 if config.compute_type not in compute_types: |
|
78699f810817
Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
42 raise RuntimeError( |
|
78699f810817
Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
43 f"{config.compute_type} is unsupported on CUDA; " |
|
78699f810817
Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
44 f"available: {', '.join(compute_types)}" |
|
78699f810817
Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
45 ) |
|
78699f810817
Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
46 result = { |
|
78699f810817
Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
47 "cudaDeviceCount": ctranslate2.get_cuda_device_count(), |
|
78699f810817
Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
48 "computeTypes": compute_types, |
|
78699f810817
Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
49 "selectedComputeType": config.compute_type, |
|
78699f810817
Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
50 "modelDir": str(config.model_dir), |
|
78699f810817
Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
51 "modelPresent": (config.model_dir / "model.bin").is_file(), |
|
78699f810817
Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
52 } |
|
78699f810817
Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
53 if result["cudaDeviceCount"] < 1: |
|
78699f810817
Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
54 raise RuntimeError("CTranslate2 did not detect a CUDA device") |
|
78699f810817
Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
55 print(json.dumps(result, indent=2)) |
|
78699f810817
Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
56 |
|
78699f810817
Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
57 |
|
78699f810817
Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
58 async def transcribe_file(config: DictationConfig, path: Path) -> None: |
|
78699f810817
Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
59 from dictation.transcriber import FasterWhisperTranscriber |
|
78699f810817
Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
60 |
|
78699f810817
Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
61 transcriber = FasterWhisperTranscriber( |
|
78699f810817
Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
62 config.model_dir, |
|
78699f810817
Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
63 config.compute_type, |
|
78699f810817
Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
64 ) |
|
78699f810817
Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
65 try: |
|
78699f810817
Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
66 from av import open as av_open |
|
78699f810817
Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
67 from av.audio.resampler import AudioResampler |
|
78699f810817
Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
68 import numpy as np |
|
78699f810817
Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
69 |
|
78699f810817
Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
70 samples = [] |
|
78699f810817
Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
71 resampler = AudioResampler(format="s16", layout="mono", rate=16000) |
|
78699f810817
Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
72 with av_open(str(path)) as container: |
|
78699f810817
Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
73 for frame in container.decode(audio=0): |
|
78699f810817
Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
74 for converted in resampler.resample(frame): |
|
78699f810817
Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
75 samples.append(converted.to_ndarray().reshape(-1)) |
|
78699f810817
Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
76 if not samples: |
|
78699f810817
Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
77 raise RuntimeError("Audio file contains no decodable samples") |
|
78699f810817
Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
78 audio = np.concatenate(samples).astype(np.float32) / 32768.0 |
|
78699f810817
Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
79 result = await transcriber.transcribe(audio, final=True) |
|
78699f810817
Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
80 print( |
|
78699f810817
Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
81 json.dumps( |
|
78699f810817
Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
82 { |
|
78699f810817
Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
83 "text": result.text, |
|
78699f810817
Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
84 "language": result.language, |
|
78699f810817
Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
85 "probability": result.probability, |
|
78699f810817
Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
86 }, |
|
78699f810817
Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
87 ensure_ascii=False, |
|
78699f810817
Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
88 ) |
|
78699f810817
Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
89 ) |
|
78699f810817
Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
90 finally: |
|
78699f810817
Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
91 transcriber.close() |
|
78699f810817
Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
92 |
|
78699f810817
Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
93 |
|
78699f810817
Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
94 def main() -> None: |
|
78699f810817
Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
95 parser = argparse.ArgumentParser() |
|
78699f810817
Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
96 parser.add_argument( |
|
78699f810817
Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
97 "command", |
|
78699f810817
Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
98 choices=["server", "preflight", "download_model", "transcribe"], |
|
78699f810817
Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
99 ) |
|
78699f810817
Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
100 parser.add_argument("path", nargs="?") |
|
78699f810817
Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
101 args = parser.parse_args() |
|
78699f810817
Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
102 try: |
|
78699f810817
Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
103 config = DictationConfig.from_environment() |
|
78699f810817
Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
104 if args.command == "download_model": |
|
78699f810817
Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
105 download_model(config) |
|
78699f810817
Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
106 elif args.command == "preflight": |
|
78699f810817
Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
107 preflight(config) |
|
78699f810817
Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
108 elif args.command == "transcribe": |
|
78699f810817
Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
109 if not args.path: |
|
78699f810817
Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
110 parser.error("transcribe requires an audio file path") |
|
78699f810817
Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
111 asyncio.run(transcribe_file(config, Path(args.path))) |
|
78699f810817
Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
112 else: |
|
78699f810817
Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
113 import uvicorn |
|
78699f810817
Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
114 from dictation.server import create_app |
|
78699f810817
Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
115 |
|
78699f810817
Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
116 uvicorn.run( |
|
78699f810817
Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
117 create_app(config), |
|
78699f810817
Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
118 host=config.host, |
|
78699f810817
Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
119 port=config.port, |
|
78699f810817
Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
120 access_log=False, |
|
78699f810817
Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
121 ) |
|
78699f810817
Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
122 except Exception as error: |
|
78699f810817
Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
123 print(f"dictation: {error}", file=sys.stderr) |
|
78699f810817
Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
124 raise SystemExit(1) from error |
|
78699f810817
Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
125 |
|
78699f810817
Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
126 |
|
78699f810817
Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
127 if __name__ == "__main__": |
|
78699f810817
Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
128 main() |