Spaces:
Sleeping
Sleeping
Update stt.py
Browse files
stt.py
CHANGED
@@ -1,188 +1,187 @@
|
|
1 |
-
# stt.py
|
2 |
-
# -*- coding: utf-8 -*-
|
3 |
-
"""
|
4 |
-
stt.py — a module for Speech-to-Text via pywhispercpp
|
5 |
-
|
6 |
-
Classes
|
7 |
-
-------
|
8 |
-
SpeechToText
|
9 |
-
Encapsulates model loading, recording, saving, and transcription.
|
10 |
-
|
11 |
-
Usage (as a script)
|
12 |
-
-------------------
|
13 |
-
python -m stt --model tiny.en --duration 5
|
14 |
-
|
15 |
-
or in code
|
16 |
-
-----------
|
17 |
-
from stt import SpeechToText
|
18 |
-
stt = SpeechToText()
|
19 |
-
text = stt.transcribe()
|
20 |
-
"""
|
21 |
-
|
22 |
-
import os
|
23 |
-
import tempfile
|
24 |
-
import time
|
25 |
-
import datetime
|
26 |
-
import numpy as np
|
27 |
-
import sounddevice as sd
|
28 |
-
import
|
29 |
-
|
30 |
-
import
|
31 |
-
from
|
32 |
-
from qdrant_client import
|
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 |
-
self.
|
63 |
-
self.
|
64 |
-
self.
|
65 |
-
self.
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
)
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
)
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
parser
|
164 |
-
|
165 |
-
"
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
-
"
|
177 |
-
|
178 |
-
)
|
179 |
-
|
180 |
-
|
181 |
-
|
182 |
-
|
183 |
-
|
184 |
-
|
185 |
-
)
|
186 |
-
|
187 |
-
print(
|
188 |
-
print(text)
|
|
|
1 |
+
# stt.py
|
2 |
+
# -*- coding: utf-8 -*-
|
3 |
+
"""
|
4 |
+
stt.py — a module for Speech-to-Text via pywhispercpp
|
5 |
+
|
6 |
+
Classes
|
7 |
+
-------
|
8 |
+
SpeechToText
|
9 |
+
Encapsulates model loading, recording, saving, and transcription.
|
10 |
+
|
11 |
+
Usage (as a script)
|
12 |
+
-------------------
|
13 |
+
python -m stt --model tiny.en --duration 5
|
14 |
+
|
15 |
+
or in code
|
16 |
+
-----------
|
17 |
+
from stt import SpeechToText
|
18 |
+
stt = SpeechToText()
|
19 |
+
text = stt.transcribe()
|
20 |
+
"""
|
21 |
+
|
22 |
+
import os
|
23 |
+
import tempfile
|
24 |
+
import time
|
25 |
+
import datetime
|
26 |
+
import numpy as np
|
27 |
+
import sounddevice as sd
|
28 |
+
from scipy.io.wavfile import write as write_wav
|
29 |
+
import webrtcvad
|
30 |
+
from pywhispercpp.model import Model as Whisper
|
31 |
+
from qdrant_client import QdrantClient
|
32 |
+
from qdrant_client.http.models import Distance, VectorParams
|
33 |
+
|
34 |
+
|
35 |
+
class SpeechToText:
|
36 |
+
"""
|
37 |
+
A Speech-to-Text helper using pywhispercpp's Whisper + Qdrant for speaker metadata.
|
38 |
+
|
39 |
+
Parameters
|
40 |
+
----------
|
41 |
+
model_name : str
|
42 |
+
Whisper model to load (e.g. "tiny.en", "base", "small.en", etc.).
|
43 |
+
sample_rate : int
|
44 |
+
Audio sample rate (must match Whisper's 16 kHz).
|
45 |
+
record_duration : float
|
46 |
+
Default seconds to record when calling `.record_audio()`.
|
47 |
+
temp_dir : str
|
48 |
+
Directory for temporary WAV files.
|
49 |
+
verbose : bool
|
50 |
+
Print progress messages if True.
|
51 |
+
"""
|
52 |
+
|
53 |
+
def __init__(
|
54 |
+
self,
|
55 |
+
model_name: str = "tiny.en",
|
56 |
+
sample_rate: int = 16_000,
|
57 |
+
record_duration: float = 5.0,
|
58 |
+
temp_dir: str = None,
|
59 |
+
verbose: bool = True,
|
60 |
+
):
|
61 |
+
self.model_name = model_name
|
62 |
+
self.sample_rate = sample_rate
|
63 |
+
self.record_duration = record_duration
|
64 |
+
self.temp_dir = temp_dir or tempfile.gettempdir()
|
65 |
+
self.verbose = verbose
|
66 |
+
|
67 |
+
# load Whisper model
|
68 |
+
if self.verbose:
|
69 |
+
print(f"[STT] Loading Whisper model '{self.model_name}'...")
|
70 |
+
t0 = time.time()
|
71 |
+
self._model = Whisper(model=self.model_name)
|
72 |
+
if self.verbose:
|
73 |
+
print(f"[STT] Model loaded in {time.time() - t0:.2f}s")
|
74 |
+
|
75 |
+
def record_audio(self, duration: float = None) -> np.ndarray:
|
76 |
+
"""
|
77 |
+
Record from the default mic for `duration` seconds, return float32 mono waveform.
|
78 |
+
"""
|
79 |
+
duration = duration or self.record_duration
|
80 |
+
if self.verbose:
|
81 |
+
print(f"[STT] Recording for {duration}s at {self.sample_rate}Hz...")
|
82 |
+
frames = sd.rec(
|
83 |
+
int(duration * self.sample_rate),
|
84 |
+
samplerate=self.sample_rate,
|
85 |
+
channels=1,
|
86 |
+
dtype="int16",
|
87 |
+
)
|
88 |
+
sd.wait()
|
89 |
+
if self.verbose:
|
90 |
+
print("[STT] Recording finished.")
|
91 |
+
# convert to float32 in [-1, 1]
|
92 |
+
return (frames.astype(np.float32) / 32768.0).flatten()
|
93 |
+
|
94 |
+
def save_wav(self, audio: np.ndarray, filename: str = None) -> str:
|
95 |
+
"""
|
96 |
+
Save float32 waveform `audio` to an int16 WAV at `filename`.
|
97 |
+
If filename is None, create one in temp_dir.
|
98 |
+
Returns the path.
|
99 |
+
"""
|
100 |
+
filename = filename or os.path.join(
|
101 |
+
self.temp_dir,
|
102 |
+
f"stt_{datetime.datetime.now():%Y%m%d_%H%M%S}.wav"
|
103 |
+
)
|
104 |
+
os.makedirs(os.path.dirname(filename), exist_ok=True)
|
105 |
+
|
106 |
+
# convert back to int16
|
107 |
+
int16 = (audio * 32767).astype(np.int16)
|
108 |
+
write_wav(filename, self.sample_rate, int16)
|
109 |
+
if self.verbose:
|
110 |
+
print(f"[STT] Saved WAV to {filename}")
|
111 |
+
return filename
|
112 |
+
|
113 |
+
def transcribe_file(self, wav_path: str, n_threads: int = 4) -> str:
|
114 |
+
"""
|
115 |
+
Transcribe existing WAV file at `wav_path`. Returns the text.
|
116 |
+
"""
|
117 |
+
if not os.path.isfile(wav_path):
|
118 |
+
raise FileNotFoundError(f"No such file: {wav_path}")
|
119 |
+
if self.verbose:
|
120 |
+
print(f"[STT] Transcribing file {wav_path}…")
|
121 |
+
t0 = time.time()
|
122 |
+
|
123 |
+
# pywhispercpp API may return segments or text
|
124 |
+
result = self._model.transcribe(wav_path, n_threads=n_threads)
|
125 |
+
# cleanup temp if in our temp_dir
|
126 |
+
if wav_path.startswith(self.temp_dir):
|
127 |
+
try:
|
128 |
+
os.remove(wav_path)
|
129 |
+
except OSError:
|
130 |
+
pass
|
131 |
+
|
132 |
+
# collect text
|
133 |
+
if isinstance(result, list):
|
134 |
+
text = "".join([seg.text for seg in result])
|
135 |
+
else:
|
136 |
+
# assume Whisper stores text internally
|
137 |
+
text = self._model.get_text()
|
138 |
+
|
139 |
+
if self.verbose:
|
140 |
+
print(f"[STT] Transcription complete ({time.time() - t0:.2f}s).")
|
141 |
+
return text.strip()
|
142 |
+
|
143 |
+
def transcribe(
|
144 |
+
self,
|
145 |
+
duration: float = None,
|
146 |
+
save_temp: bool = False,
|
147 |
+
n_threads: int = 4,
|
148 |
+
) -> str:
|
149 |
+
"""
|
150 |
+
Record + save (optional) + transcribe in one call.
|
151 |
+
Returns the transcribed text.
|
152 |
+
"""
|
153 |
+
audio = self.record_audio(duration)
|
154 |
+
wav_path = self.save_wav(audio) if save_temp else self.save_wav(audio)
|
155 |
+
return self.transcribe_file(wav_path, n_threads=n_threads)
|
156 |
+
|
157 |
+
|
158 |
+
# Optional: make module runnable as a script
|
159 |
+
if __name__ == "__main__":
|
160 |
+
import argparse
|
161 |
+
|
162 |
+
parser = argparse.ArgumentParser(description="STT using pywhispercpp")
|
163 |
+
parser.add_argument(
|
164 |
+
"--model", "-m",
|
165 |
+
default="small.en",
|
166 |
+
help="Whisper model name (e.g. tiny.en, base, small.en)",
|
167 |
+
)
|
168 |
+
parser.add_argument(
|
169 |
+
"--duration", "-d",
|
170 |
+
type=float,
|
171 |
+
default=5.0,
|
172 |
+
help="Seconds to record",
|
173 |
+
)
|
174 |
+
parser.add_argument(
|
175 |
+
"--no-save", action="store_true",
|
176 |
+
help="Do not save the recorded WAV",
|
177 |
+
)
|
178 |
+
args = parser.parse_args()
|
179 |
+
|
180 |
+
stt = SpeechToText(
|
181 |
+
model_name=args.model,
|
182 |
+
record_duration=args.duration,
|
183 |
+
verbose=True
|
184 |
+
)
|
185 |
+
text = stt.transcribe(save_temp=not args.no_save)
|
186 |
+
print("\n=== Transcription ===")
|
187 |
+
print(text)
|
|