Merge pull request #208 from pipecat-ai/aleix/cartesia-voice-load-startup

services(cartesia): load voices on startup
This commit is contained in:
Aleix Conchillo Flaqué
2024-06-04 22:54:25 +08:00
committed by GitHub
2 changed files with 14 additions and 13 deletions

View File

@@ -11,6 +11,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Fixed an issue with Deepgram TTS that was introduced in the previous release. - Fixed an issue with Deepgram TTS that was introduced in the previous release.
### Performance
- Load Cartesia voice on startup.
## [0.0.25] - 2024-05-31 ## [0.0.25] - 2024-05-31
### Added ### Added

View File

@@ -6,10 +6,9 @@
from cartesia.tts import AsyncCartesiaTTS from cartesia.tts import AsyncCartesiaTTS
import time
from typing import AsyncGenerator from typing import AsyncGenerator
from pipecat.frames.frames import AudioRawFrame, ErrorFrame, Frame from pipecat.frames.frames import AudioRawFrame, Frame
from pipecat.services.ai_services import TTSService from pipecat.services.ai_services import TTSService
from loguru import logger from loguru import logger
@@ -28,18 +27,18 @@ class CartesiaTTSService(TTSService):
self._api_key = api_key self._api_key = api_key
self._voice_name = voice_name self._voice_name = voice_name
self._client = None try:
self._client = AsyncCartesiaTTS(api_key=self._api_key)
voices = self._client.get_voices()
voice_id = voices[self._voice_name]["id"]
self._voice = self._client.get_voice_embedding(voice_id=voice_id)
except Exception as e:
logger.error(f"Cartesia initialization error: {e}")
async def run_tts(self, text: str) -> AsyncGenerator[Frame, None]: async def run_tts(self, text: str) -> AsyncGenerator[Frame, None]:
logger.debug(f"Transcribing text: [{text}]") logger.debug(f"Transcribing text: [{text}]")
try: try:
if self._client is None:
self._client = AsyncCartesiaTTS(api_key=self._api_key)
voices = self._client.get_voices()
self._voice_id = voices[self._voice_name]["id"]
self._voice = self._client.get_voice_embedding(voice_id=self._voice_id)
chunk_generator = await self._client.generate( chunk_generator = await self._client.generate(
transcript=text, voice=self._voice, stream=True, transcript=text, voice=self._voice, stream=True,
model_id="upbeat-moon", data_rtype='array', output_format='pcm_16000', model_id="upbeat-moon", data_rtype='array', output_format='pcm_16000',
@@ -49,8 +48,6 @@ class CartesiaTTSService(TTSService):
) )
async for chunk in chunk_generator: async for chunk in chunk_generator:
# print(f"") yield AudioRawFrame(chunk['audio'], 16000, 1)
frame = AudioRawFrame(chunk['audio'], 16000, 1)
yield frame
except Exception as e: except Exception as e:
logger.error(f"Exception {e}") logger.error(f"Cartesia error: {e}")