-
Notifications
You must be signed in to change notification settings - Fork 397
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #189 from pipecat-ai/khk-cartesia-etc
Cartesia TTS
- Loading branch information
Showing
7 changed files
with
212 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
# | ||
# Copyright (c) 2024, Daily | ||
# | ||
# SPDX-License-Identifier: BSD 2-Clause License | ||
# | ||
|
||
import asyncio | ||
import aiohttp | ||
import os | ||
import sys | ||
|
||
from pipecat.frames.frames import LLMMessagesFrame | ||
from pipecat.pipeline.pipeline import Pipeline | ||
from pipecat.pipeline.runner import PipelineRunner | ||
from pipecat.pipeline.task import PipelineParams, PipelineTask | ||
from pipecat.processors.aggregators.llm_response import ( | ||
LLMAssistantResponseAggregator, LLMUserResponseAggregator) | ||
from pipecat.services.cartesia import CartesiaTTSService | ||
from pipecat.services.openai import OpenAILLMService | ||
from pipecat.transports.services.daily import DailyParams, DailyTransport | ||
from pipecat.vad.silero import SileroVADAnalyzer | ||
|
||
from runner import configure | ||
|
||
from loguru import logger | ||
|
||
from dotenv import load_dotenv | ||
load_dotenv(override=True) | ||
|
||
logger.remove(0) | ||
logger.add(sys.stderr, level="DEBUG") | ||
|
||
|
||
async def main(room_url: str, token): | ||
async with aiohttp.ClientSession() as session: | ||
transport = DailyTransport( | ||
room_url, | ||
token, | ||
"Respond bot", | ||
DailyParams( | ||
audio_out_enabled=True, | ||
transcription_enabled=True, | ||
vad_enabled=True, | ||
vad_analyzer=SileroVADAnalyzer() | ||
) | ||
) | ||
|
||
tts = CartesiaTTSService( | ||
api_key=os.getenv("CARTESIA_API_KEY"), | ||
voice_name="Barbershop Man" | ||
) | ||
|
||
llm = OpenAILLMService( | ||
api_key=os.getenv("OPENAI_API_KEY"), | ||
model="gpt-4o") | ||
|
||
messages = [ | ||
{ | ||
"role": "system", | ||
"content": "You are a helpful LLM in a WebRTC call. Your goal is to demonstrate your capabilities in a succinct way. Your output will be converted to audio so don't include special characters in your answers. Respond to what the user said in a creative and helpful way.", | ||
}, | ||
] | ||
|
||
tma_in = LLMUserResponseAggregator(messages) | ||
tma_out = LLMAssistantResponseAggregator(messages) | ||
|
||
pipeline = Pipeline([ | ||
transport.input(), # Transport user input | ||
tma_in, # User responses | ||
llm, # LLM | ||
tts, # TTS | ||
transport.output(), # Transport bot output | ||
tma_out # Assistant spoken responses | ||
]) | ||
|
||
task = PipelineTask(pipeline, PipelineParams(allow_interruptions=True)) | ||
|
||
@transport.event_handler("on_first_participant_joined") | ||
async def on_first_participant_joined(transport, participant): | ||
transport.capture_participant_transcription(participant["id"]) | ||
# Kick off the conversation. | ||
messages.append( | ||
{"role": "system", "content": "Please introduce yourself to the user."}) | ||
await task.queue_frames([LLMMessagesFrame(messages)]) | ||
|
||
runner = PipelineRunner() | ||
|
||
await runner.run(task) | ||
|
||
|
||
if __name__ == "__main__": | ||
(url, token) = configure() | ||
asyncio.run(main(url, token)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
# | ||
# Copyright (c) 2024, Daily | ||
# | ||
# SPDX-License-Identifier: BSD 2-Clause License | ||
# | ||
|
||
from cartesia.tts import AsyncCartesiaTTS | ||
|
||
import time | ||
from typing import AsyncGenerator | ||
|
||
from pipecat.frames.frames import AudioRawFrame, ErrorFrame, Frame | ||
from pipecat.services.ai_services import TTSService | ||
|
||
from loguru import logger | ||
|
||
|
||
class CartesiaTTSService(TTSService): | ||
|
||
def __init__( | ||
self, | ||
*, | ||
api_key: str, | ||
voice_name: str, | ||
**kwargs): | ||
super().__init__(**kwargs) | ||
|
||
self._api_key = api_key | ||
self._voice_name = voice_name | ||
|
||
self._client = None | ||
|
||
async def run_tts(self, text: str) -> AsyncGenerator[Frame, None]: | ||
logger.debug(f"Transcribing text: [{text}]") | ||
|
||
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( | ||
transcript=text, voice=self._voice, stream=True, | ||
model_id="upbeat-moon", data_rtype='array', output_format='pcm_16000', | ||
# a chunk_time of 0.1 seems to be the default. there are small audio pops/gaps which | ||
# we need to debug | ||
chunk_time=0.1 | ||
) | ||
|
||
async for chunk in chunk_generator: | ||
# print(f"") | ||
frame = AudioRawFrame(chunk['audio'], 16000, 1) | ||
yield frame | ||
except Exception as e: | ||
logger.error(f"Exception {e}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters