Merge pull request #60 from Plaud-AI/feat/demo

feat: non-streaming ASR doesn't pass text length interruption strategy
This commit is contained in:
Harold-Guo 2025-12-21 13:57:21 +08:00 committed by GitHub
commit 04321383db
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 49 additions and 28 deletions

View file

@ -1535,7 +1535,8 @@ class ConnectionHandler:
2. TTS must be speaking (client_is_speaking = True) 2. TTS must be speaking (client_is_speaking = True)
3. Not in manual listen mode 3. Not in manual listen mode
4. Speech duration >= min_interrupt_speech_duration_ms 4. Speech duration >= min_interrupt_speech_duration_ms
5. ASR text buffer length >= min_interrupt_text_length 5. For streaming ASR: text buffer length >= min_interrupt_text_length
For non-streaming ASR: skip text check (not available during speech)
Args: Args:
speech_duration_ms: Current speech duration in milliseconds speech_duration_ms: Current speech duration in milliseconds
@ -1547,32 +1548,50 @@ class ConnectionHandler:
if self.client_listen_mode == "manual": if self.client_listen_mode == "manual":
return return
# Check if meeting interruption thresholds # Check speech duration threshold
words = tokenize.split_words(
self.asr_text_buffer,
ignore_punctuation=True,
split_character=True,
retain_format=False,
)
asr_text_len = len(words)
speech_ok = speech_duration_ms >= self.min_interrupt_speech_duration_ms speech_ok = speech_duration_ms >= self.min_interrupt_speech_duration_ms
text_ok = asr_text_len >= self.min_interrupt_text_length if not speech_ok:
return
if speech_ok and text_ok: # Check text length threshold (only for streaming ASR)
self.logger.bind(tag=TAG).info( # Non-streaming ASR doesn't have real-time text during speech
f"Interrupt triggered: speech={speech_duration_ms:.0f}ms >= {self.min_interrupt_speech_duration_ms:.0f}ms, " from core.providers.asr.dto import InterfaceType
is_streaming_asr = (
self.asr is not None
and hasattr(self.asr, 'interface_type')
and self.asr.interface_type == InterfaceType.STREAM
)
if is_streaming_asr:
words = tokenize.split_words(
self.asr_text_buffer,
ignore_punctuation=True,
split_character=True,
retain_format=False,
)
asr_text_len = len(words)
text_ok = asr_text_len >= self.min_interrupt_text_length
if not text_ok:
return
log_msg = (
f"Interrupt triggered (streaming): speech={speech_duration_ms:.0f}ms, "
f"text_len={asr_text_len} >= {self.min_interrupt_text_length}" f"text_len={asr_text_len} >= {self.min_interrupt_text_length}"
) )
# Trigger interrupt else:
self.client_abort = True log_msg = f"Interrupt triggered (non-streaming): speech={speech_duration_ms:.0f}ms >= {self.min_interrupt_speech_duration_ms:.0f}ms"
self.clear_queues()
# Send stop message to client self.logger.bind(tag=TAG).info(log_msg)
async def send_stop_message():
await self.websocket.send( # Trigger interrupt
json.dumps({"type": "tts", "state": "stop", "session_id": self.session_id}) self.client_abort = True
) self.clear_queues()
asyncio.create_task(send_stop_message()) # Send stop message to client
self.clearSpeakStatus() async def send_stop_message():
await self.websocket.send(
json.dumps({"type": "tts", "state": "stop", "session_id": self.session_id})
)
asyncio.create_task(send_stop_message())
self.clearSpeakStatus()
def chat_and_close(self, text): def chat_and_close(self, text):
"""Chat with the user and then close the connection""" """Chat with the user and then close the connection"""

View file

@ -14,7 +14,7 @@ from config.logger import setup_logging
from typing import Optional, Tuple, List from typing import Optional, Tuple, List
from core.handle.reportHandle import enqueue_asr_report from core.handle.reportHandle import enqueue_asr_report
from core.utils.util import remove_punctuation_and_length from core.utils.util import remove_punctuation_and_length
from .dto import ASRMessageType, ASRInputMessage from .dto import ASRMessageType, ASRInputMessage, InterfaceType
from queue import Queue, Empty from queue import Queue, Empty
TAG = __name__ TAG = __name__
@ -26,6 +26,8 @@ class ASRProviderBase(ABC):
def __init__(self): def __init__(self):
self.asr_input_queue = Queue[ASRInputMessage]() self.asr_input_queue = Queue[ASRInputMessage]()
self.asr_input_audio_format = "pcm" self.asr_input_audio_format = "pcm"
# Default to non-streaming, subclasses can override
self.interface_type = InterfaceType.NON_STREAM
# 打开音频通道 # 打开音频通道
async def open_audio_channels(self, conn): async def open_audio_channels(self, conn):

View file

@ -317,7 +317,7 @@ class VADStream(ABC):
asr_input_queue.put_nowait(asr_message) asr_input_queue.put_nowait(asr_message)
logger.bind(tag=TAG).info( logger.bind(tag=TAG).info(
f"ASR LAST message sent: total_speech={event.speech_duration:.2f}s, " f"ASR LAST message sent: total_speech={event.speech_duration:.0f}ms, "
f"audio={asr_message.audio_duration_ms:.0f}ms" f"audio={asr_message.audio_duration_ms:.0f}ms"
) )

View file

@ -567,9 +567,9 @@ TTS:
# ============================= Module Selection ================================ # ============================= Module Selection ================================
# =============================================================================== # ===============================================================================
selected_module: selected_module:
VAD: FsmnVAD # VAD: FsmnVAD
# VAD: SileroVAD VAD: SileroVAD
TurnDetection: TenTurnDetection # Set to TenTurnDetection to enable # TurnDetection: TenTurnDetection # Set to TenTurnDetection to enable
ASR: GroqASR ASR: GroqASR
# ASR: ByteplusStreamASR # ASR: ByteplusStreamASR
# LLM: GroqLLM # LLM: GroqLLM