services(tts): strip before TTS and cleanup when interrupted

This commit is contained in:
Aleix Conchillo Flaqué
2024-06-25 10:21:21 -07:00
parent 0ddc5721b4
commit c31f2ab285
2 changed files with 16 additions and 9 deletions

View File

@@ -9,6 +9,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed ### Fixed
- Fixed an issue that could mix new LLM responses with previous ones when
handling interruptions.
- Fixed a Daily transport blocking situation that occurred while reading audio - Fixed a Daily transport blocking situation that occurred while reading audio
frames after a participant left the room. Needs daily-python >= 0.10.1. frames after a participant left the room. Needs daily-python >= 0.10.1.

View File

@@ -16,7 +16,9 @@ from pipecat.frames.frames import (
EndFrame, EndFrame,
ErrorFrame, ErrorFrame,
Frame, Frame,
LLMFullResponseStartFrame,
StartFrame, StartFrame,
StartInterruptionFrame,
TTSStartedFrame, TTSStartedFrame,
TTSStoppedFrame, TTSStoppedFrame,
TextFrame, TextFrame,
@@ -114,13 +116,17 @@ class TTSService(AIService):
if self._current_sentence.strip().endswith( if self._current_sentence.strip().endswith(
(".", "?", "!")) and not self._current_sentence.strip().endswith( (".", "?", "!")) and not self._current_sentence.strip().endswith(
("Mr,", "Mrs.", "Ms.", "Dr.")): ("Mr,", "Mrs.", "Ms.", "Dr.")):
text = self._current_sentence.strip() text = self._current_sentence
self._current_sentence = "" self._current_sentence = ""
if text: if text:
await self._push_tts_frames(text) await self._push_tts_frames(text)
async def _push_tts_frames(self, text: str): async def _push_tts_frames(self, text: str):
text = text.strip()
if not text:
return
await self.push_frame(TTSStartedFrame()) await self.push_frame(TTSStartedFrame())
await self.process_generator(self.run_tts(text)) await self.process_generator(self.run_tts(text))
await self.push_frame(TTSStoppedFrame()) await self.push_frame(TTSStoppedFrame())
@@ -133,14 +139,12 @@ class TTSService(AIService):
if isinstance(frame, TextFrame): if isinstance(frame, TextFrame):
await self._process_text_frame(frame) await self._process_text_frame(frame)
elif isinstance(frame, EndFrame): elif isinstance(frame, StartInterruptionFrame):
if self._current_sentence: self._current_sentence = ""
await self._push_tts_frames(self._current_sentence) await self.push_frame(frame, direction)
await self.push_frame(frame) elif isinstance(frame, LLMFullResponseEndFrame) or isinstance(frame, EndFrame):
elif isinstance(frame, LLMFullResponseEndFrame): self._current_sentence = ""
if self._current_sentence: await self._push_tts_frames(self._current_sentence)
await self._push_tts_frames(self._current_sentence.strip())
self._current_sentence = ""
await self.push_frame(frame) await self.push_frame(frame)
else: else:
await self.push_frame(frame, direction) await self.push_frame(frame, direction)