-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvoicerefine_local_whisper.py
More file actions
71 lines (59 loc) · 2.24 KB
/
Copy pathvoicerefine_local_whisper.py
File metadata and controls
71 lines (59 loc) · 2.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/usr/bin/env python3
"""Optional local Whisper backend using faster-whisper.
Lazy-imported on first use so the package isn't a hard dependency of VoiceRefine.
Install with: pip install faster-whisper
"""
import io
import wave
class LocalWhisperUnavailable(RuntimeError):
"""Raised when the faster-whisper package is not installed."""
class LocalWhisperBackend:
"""Wraps a faster-whisper WhisperModel. One backend per process; loads on demand."""
_model_cache = {}
def __init__(self, model_size="base", device="auto", compute_type="auto"):
self.model_size = model_size or "base"
self.device = device or "auto"
self.compute_type = compute_type or "auto"
self._model = None
def _load(self):
if self._model is not None:
return self._model
key = (self.model_size, self.device, self.compute_type)
if key in self._model_cache:
self._model = self._model_cache[key]
return self._model
try:
from faster_whisper import WhisperModel
except ImportError as exc:
raise LocalWhisperUnavailable(
"faster-whisper is not installed. Install with: pip install faster-whisper"
) from exc
self._model = WhisperModel(
self.model_size,
device=self.device,
compute_type=self.compute_type,
)
self._model_cache[key] = self._model
return self._model
def transcribe(self, wav_bytes_io, language=None):
"""Transcribe a WAV BytesIO and return cleaned text.
Raises LocalWhisperUnavailable if the package is missing.
"""
model = self._load()
wav_bytes_io.seek(0)
# faster-whisper accepts a file-like object or path; BytesIO works.
segments, _info = model.transcribe(
wav_bytes_io,
language=language,
beam_size=1,
vad_filter=False,
)
text = " ".join(seg.text.strip() for seg in segments if seg.text)
return text.strip()
def is_available():
"""Cheap probe — returns True if faster-whisper can be imported."""
try:
import faster_whisper # noqa: F401
return True
except ImportError:
return False