From 8670b2d9944e822a8f440278c1bf49415d3bf421 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aleix=20Conchillo=20Flaqu=C3=A9?= Date: Thu, 15 Aug 2024 11:26:25 -0700 Subject: [PATCH] utils: add match_endofsentence and use it in processors --- .../processors/aggregators/sentence.py | 13 ++++------ src/pipecat/services/ai_services.py | 20 +--------------- src/pipecat/utils/string.py | 24 +++++++++++++++++++ 3 files changed, 30 insertions(+), 27 deletions(-) create mode 100644 src/pipecat/utils/string.py diff --git a/src/pipecat/processors/aggregators/sentence.py b/src/pipecat/processors/aggregators/sentence.py index a7992eace..7ee641826 100644 --- a/src/pipecat/processors/aggregators/sentence.py +++ b/src/pipecat/processors/aggregators/sentence.py @@ -4,10 +4,9 @@ # SPDX-License-Identifier: BSD 2-Clause License # -import re - from pipecat.frames.frames import EndFrame, Frame, InterimTranscriptionFrame, TextFrame from pipecat.processors.frame_processor import FrameDirection, FrameProcessor +from pipecat.utils.string import match_endofsentence class SentenceAggregator(FrameProcessor): @@ -40,12 +39,10 @@ class SentenceAggregator(FrameProcessor): return if isinstance(frame, TextFrame): - m = re.search("(.*[?.!])(.*)", frame.text) - if m: - await self.push_frame(TextFrame(self._aggregation + m.group(1))) - self._aggregation = m.group(2) - else: - self._aggregation += frame.text + self._aggregation += frame.text + if match_endofsentence(self._aggregation): + await self.push_frame(TextFrame(self._aggregation)) + self._aggregation = "" elif isinstance(frame, EndFrame): if self._aggregation: await self.push_frame(TextFrame(self._aggregation)) diff --git a/src/pipecat/services/ai_services.py b/src/pipecat/services/ai_services.py index 485ae85a4..33abf4e15 100644 --- a/src/pipecat/services/ai_services.py +++ b/src/pipecat/services/ai_services.py @@ -27,28 +27,10 @@ from pipecat.frames.frames import ( from pipecat.processors.async_frame_processor import AsyncFrameProcessor from pipecat.processors.frame_processor import FrameDirection, FrameProcessor from pipecat.utils.audio import calculate_audio_volume +from pipecat.utils.string import match_endofsentence from pipecat.utils.utils import exp_smoothing from pipecat.processors.aggregators.openai_llm_context import OpenAILLMContext -import re - - -ENDOFSENTENCE_PATTERN_STR = r""" - (? bool: - return ENDOFSENTENCE_PATTERN.search(text.rstrip()) is not None - class AIService(FrameProcessor): def __init__(self, **kwargs): diff --git a/src/pipecat/utils/string.py b/src/pipecat/utils/string.py new file mode 100644 index 000000000..a47db6c5c --- /dev/null +++ b/src/pipecat/utils/string.py @@ -0,0 +1,24 @@ +# +# Copyright (c) 2024, Daily +# +# SPDX-License-Identifier: BSD 2-Clause License +# + +import re + + +ENDOFSENTENCE_PATTERN_STR = r""" + (? bool: + return ENDOFSENTENCE_PATTERN.search(text.rstrip()) is not None