second commit, before making function of memory in api query
This commit is contained in:
21
stt.py
21
stt.py
@@ -34,12 +34,13 @@ class SpeechRecognizer:
|
||||
)
|
||||
print("✅ Модель Vosk загружена")
|
||||
|
||||
def listen(self, timeout_seconds: float = 5.0) -> str:
|
||||
def listen(self, timeout_seconds: float = 5.0, detection_timeout: float = None) -> str:
|
||||
"""
|
||||
Listen to microphone and transcribe speech.
|
||||
|
||||
Args:
|
||||
timeout_seconds: Maximum time to listen for speech
|
||||
detection_timeout: Time to wait for speech to start. If None, uses timeout_seconds.
|
||||
|
||||
Returns:
|
||||
Transcribed text from speech
|
||||
@@ -53,10 +54,13 @@ class SpeechRecognizer:
|
||||
self.recognizer = KaldiRecognizer(self.model, SAMPLE_RATE)
|
||||
|
||||
frames_to_read = int(SAMPLE_RATE * timeout_seconds / 4096)
|
||||
detection_frames = int(SAMPLE_RATE * detection_timeout / 4096) if detection_timeout else frames_to_read
|
||||
|
||||
silence_frames = 0
|
||||
max_silence_frames = 10 # About 2.5 seconds of silence
|
||||
speech_started = False
|
||||
|
||||
for _ in range(frames_to_read):
|
||||
for i in range(frames_to_read):
|
||||
data = self.stream.read(4096, exception_on_overflow=False)
|
||||
|
||||
if self.recognizer.AcceptWaveform(data):
|
||||
@@ -71,9 +75,14 @@ class SpeechRecognizer:
|
||||
partial = json.loads(self.recognizer.PartialResult())
|
||||
if partial.get("partial", ""):
|
||||
silence_frames = 0
|
||||
speech_started = True
|
||||
else:
|
||||
silence_frames += 1
|
||||
|
||||
# Check detection timeout
|
||||
if not speech_started and i > detection_frames:
|
||||
break
|
||||
|
||||
# Stop if too much silence after speech
|
||||
if silence_frames > max_silence_frames:
|
||||
break
|
||||
@@ -85,7 +94,9 @@ class SpeechRecognizer:
|
||||
if text:
|
||||
print(f"📝 Распознано: {text}")
|
||||
else:
|
||||
print("⚠️ Речь не распознана")
|
||||
# Only print if we weren't just checking for presence of speech
|
||||
if not detection_timeout or speech_started:
|
||||
print("⚠️ Речь не распознана")
|
||||
|
||||
return text
|
||||
|
||||
@@ -109,9 +120,9 @@ def get_recognizer() -> SpeechRecognizer:
|
||||
return _recognizer
|
||||
|
||||
|
||||
def listen(timeout_seconds: float = 5.0) -> str:
|
||||
def listen(timeout_seconds: float = 5.0, detection_timeout: float = None) -> str:
|
||||
"""Listen to microphone and return transcribed text."""
|
||||
return get_recognizer().listen(timeout_seconds)
|
||||
return get_recognizer().listen(timeout_seconds, detection_timeout)
|
||||
|
||||
|
||||
def cleanup():
|
||||
|
||||
Reference in New Issue
Block a user