Add voice options and make to use InputParams for Cartesia.
This commit is contained in:
@@ -52,7 +52,9 @@ async def main():
|
|||||||
tts = CartesiaTTSService(
|
tts = CartesiaTTSService(
|
||||||
api_key=os.getenv("CARTESIA_API_KEY"),
|
api_key=os.getenv("CARTESIA_API_KEY"),
|
||||||
voice_id="a0e99841-438c-4a64-b679-ae501e7d6091", # Barbershop Man
|
voice_id="a0e99841-438c-4a64-b679-ae501e7d6091", # Barbershop Man
|
||||||
sample_rate=44100,
|
params=CartesiaTTSService.InputParams(
|
||||||
|
sample_rate=44100,
|
||||||
|
),
|
||||||
)
|
)
|
||||||
|
|
||||||
llm = OpenAILLMService(
|
llm = OpenAILLMService(
|
||||||
|
|||||||
@@ -78,7 +78,9 @@ async def main():
|
|||||||
tts = CartesiaTTSService(
|
tts = CartesiaTTSService(
|
||||||
api_key=os.getenv("CARTESIA_API_KEY"),
|
api_key=os.getenv("CARTESIA_API_KEY"),
|
||||||
voice_id="79a125e8-cd45-4c13-8a67-188112f4dd22", # British Lady
|
voice_id="79a125e8-cd45-4c13-8a67-188112f4dd22", # British Lady
|
||||||
sample_rate=16000,
|
params=CartesiaTTSService.InputParams(
|
||||||
|
sample_rate=16000,
|
||||||
|
),
|
||||||
)
|
)
|
||||||
|
|
||||||
@transport.event_handler("on_first_participant_joined")
|
@transport.event_handler("on_first_participant_joined")
|
||||||
|
|||||||
@@ -124,7 +124,9 @@ async def main():
|
|||||||
api_key=os.getenv("CARTESIA_API_KEY"),
|
api_key=os.getenv("CARTESIA_API_KEY"),
|
||||||
voice_id=os.getenv("CARTESIA_VOICE_ID", "4d2fd738-3b3d-4368-957a-bb4805275bd9"),
|
voice_id=os.getenv("CARTESIA_VOICE_ID", "4d2fd738-3b3d-4368-957a-bb4805275bd9"),
|
||||||
# British Narration Lady: 4d2fd738-3b3d-4368-957a-bb4805275bd9
|
# British Narration Lady: 4d2fd738-3b3d-4368-957a-bb4805275bd9
|
||||||
sample_rate=44100,
|
params=CartesiaTTSService.InputParams(
|
||||||
|
sample_rate=44100,
|
||||||
|
),
|
||||||
)
|
)
|
||||||
|
|
||||||
llm = OpenAILLMService(
|
llm = OpenAILLMService(
|
||||||
|
|||||||
@@ -10,7 +10,8 @@ import base64
|
|||||||
import asyncio
|
import asyncio
|
||||||
import time
|
import time
|
||||||
|
|
||||||
from typing import AsyncGenerator, Mapping
|
from typing import AsyncGenerator, Optional
|
||||||
|
from pydantic.main import BaseModel
|
||||||
|
|
||||||
from pipecat.frames.frames import (
|
from pipecat.frames.frames import (
|
||||||
CancelFrame,
|
CancelFrame,
|
||||||
@@ -61,6 +62,14 @@ def language_to_cartesia_language(language: Language) -> str | None:
|
|||||||
|
|
||||||
|
|
||||||
class CartesiaTTSService(TTSService):
|
class CartesiaTTSService(TTSService):
|
||||||
|
class InputParams(BaseModel):
|
||||||
|
model_id: Optional[str] = "sonic-english"
|
||||||
|
encoding: Optional[str] = "pcm_s16le"
|
||||||
|
sample_rate: Optional[int] = 16000
|
||||||
|
container: Optional[str] = "raw"
|
||||||
|
language: Optional[str] = "en"
|
||||||
|
speed: Optional[str] = None
|
||||||
|
emotion: Optional[list[str]] = []
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
@@ -69,10 +78,7 @@ class CartesiaTTSService(TTSService):
|
|||||||
voice_id: str,
|
voice_id: str,
|
||||||
cartesia_version: str = "2024-06-10",
|
cartesia_version: str = "2024-06-10",
|
||||||
url: str = "wss://api.cartesia.ai/tts/websocket",
|
url: str = "wss://api.cartesia.ai/tts/websocket",
|
||||||
model_id: str = "sonic-english",
|
params: InputParams = InputParams(),
|
||||||
encoding: str = "pcm_s16le",
|
|
||||||
sample_rate: int = 16000,
|
|
||||||
language: str = "en",
|
|
||||||
**kwargs):
|
**kwargs):
|
||||||
super().__init__(**kwargs)
|
super().__init__(**kwargs)
|
||||||
|
|
||||||
@@ -92,13 +98,15 @@ class CartesiaTTSService(TTSService):
|
|||||||
self._cartesia_version = cartesia_version
|
self._cartesia_version = cartesia_version
|
||||||
self._url = url
|
self._url = url
|
||||||
self._voice_id = voice_id
|
self._voice_id = voice_id
|
||||||
self._model_id = model_id
|
self._model_id = params.model_id
|
||||||
self._output_format = {
|
self._output_format = {
|
||||||
"container": "raw",
|
"container": params.container,
|
||||||
"encoding": encoding,
|
"encoding": params.encoding,
|
||||||
"sample_rate": sample_rate,
|
"sample_rate": params.sample_rate,
|
||||||
}
|
}
|
||||||
self._language = language
|
self._language = params.language
|
||||||
|
self._speed = params.speed
|
||||||
|
self._emotion = params.emotion
|
||||||
|
|
||||||
self._websocket = None
|
self._websocket = None
|
||||||
self._context_id = None
|
self._context_id = None
|
||||||
@@ -249,15 +257,24 @@ class CartesiaTTSService(TTSService):
|
|||||||
await self.start_ttfb_metrics()
|
await self.start_ttfb_metrics()
|
||||||
self._context_id = str(uuid.uuid4())
|
self._context_id = str(uuid.uuid4())
|
||||||
|
|
||||||
|
voice_config = {
|
||||||
|
"mode": "id",
|
||||||
|
"id": self._voice_id
|
||||||
|
}
|
||||||
|
|
||||||
|
if self._speed or self._emotion:
|
||||||
|
voice_config["__experimental_controls"] = {}
|
||||||
|
if self._speed:
|
||||||
|
voice_config["__experimental_controls"]["speed"] = self._speed
|
||||||
|
if self._emotion:
|
||||||
|
voice_config["__experimental_controls"]["emotion"] = self._emotion
|
||||||
|
|
||||||
msg = {
|
msg = {
|
||||||
"transcript": text + " ",
|
"transcript": text + " ",
|
||||||
"continue": True,
|
"continue": True,
|
||||||
"context_id": self._context_id,
|
"context_id": self._context_id,
|
||||||
"model_id": self._model_id,
|
"model_id": self._model_id,
|
||||||
"voice": {
|
"voice": voice_config,
|
||||||
"mode": "id",
|
|
||||||
"id": self._voice_id
|
|
||||||
},
|
|
||||||
"output_format": self._output_format,
|
"output_format": self._output_format,
|
||||||
"language": self._language,
|
"language": self._language,
|
||||||
"add_timestamps": True,
|
"add_timestamps": True,
|
||||||
|
|||||||
Reference in New Issue
Block a user