services(cartesia): make sure we close the client on exit
This commit is contained in:
@@ -8,7 +8,7 @@ from cartesia import AsyncCartesia
|
|||||||
|
|
||||||
from typing import AsyncGenerator
|
from typing import AsyncGenerator
|
||||||
|
|
||||||
from pipecat.frames.frames import AudioRawFrame, Frame
|
from pipecat.frames.frames import AudioRawFrame, CancelFrame, EndFrame, Frame, StartFrame
|
||||||
from pipecat.services.ai_services import TTSService
|
from pipecat.services.ai_services import TTSService
|
||||||
|
|
||||||
from loguru import logger
|
from loguru import logger
|
||||||
@@ -28,22 +28,33 @@ class CartesiaTTSService(TTSService):
|
|||||||
super().__init__(**kwargs)
|
super().__init__(**kwargs)
|
||||||
|
|
||||||
self._api_key = api_key
|
self._api_key = api_key
|
||||||
|
self._voice_id = voice_id
|
||||||
self._model_id = model_id
|
self._model_id = model_id
|
||||||
self._output_format = {
|
self._output_format = {
|
||||||
"container": "raw",
|
"container": "raw",
|
||||||
"encoding": encoding,
|
"encoding": encoding,
|
||||||
"sample_rate": sample_rate,
|
"sample_rate": sample_rate,
|
||||||
}
|
}
|
||||||
|
self._client = None
|
||||||
try:
|
|
||||||
self._client = AsyncCartesia(api_key=self._api_key)
|
|
||||||
self._voice = self._client.voices.get(id=voice_id)
|
|
||||||
except Exception as e:
|
|
||||||
logger.exception(f"{self} initialization error: {e}")
|
|
||||||
|
|
||||||
def can_generate_metrics(self) -> bool:
|
def can_generate_metrics(self) -> bool:
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
async def start(self, frame: StartFrame):
|
||||||
|
try:
|
||||||
|
self._client = AsyncCartesia(api_key=self._api_key)
|
||||||
|
self._voice = self._client.voices.get(id=self._voice_id)
|
||||||
|
except Exception as e:
|
||||||
|
logger.exception(f"{self} initialization error: {e}")
|
||||||
|
|
||||||
|
async def stop(self, frame: EndFrame):
|
||||||
|
if self._client:
|
||||||
|
await self._client.close()
|
||||||
|
|
||||||
|
async def cancel(self, frame: CancelFrame):
|
||||||
|
if self._client:
|
||||||
|
await self._client.close()
|
||||||
|
|
||||||
async def run_tts(self, text: str) -> AsyncGenerator[Frame, None]:
|
async def run_tts(self, text: str) -> AsyncGenerator[Frame, None]:
|
||||||
logger.debug(f"Generating TTS: [{text}]")
|
logger.debug(f"Generating TTS: [{text}]")
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user