Skip to content

Commit

Permalink
[Cartesia] Adding base url option to Cartesia TTS plugin (#1457)
Browse files Browse the repository at this point in the history
  • Loading branch information
chongzluong authored Feb 7, 2025
1 parent 0865638 commit 913a4ff
Showing 1 changed file with 14 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,13 @@ class _TTSOptions:
emotion: list[TTSVoiceEmotion | str] | None
api_key: str
language: str
base_url: str

def get_http_url(self, path: str) -> str:
return f"{self.base_url}{path}"

def get_ws_url(self, path: str) -> str:
return f"{self.base_url.replace('http', 'ws', 1)}{path}"


class TTS(tts.TTS):
Expand All @@ -76,6 +83,7 @@ def __init__(
sample_rate: int = 24000,
api_key: str | None = None,
http_session: aiohttp.ClientSession | None = None,
base_url: str = "https://api.cartesia.ai",
) -> None:
"""
Create a new instance of Cartesia TTS.
Expand All @@ -92,6 +100,7 @@ def __init__(
sample_rate (int, optional): The audio sample rate in Hz. Defaults to 24000.
api_key (str, optional): The Cartesia API key. If not provided, it will be read from the CARTESIA_API_KEY environment variable.
http_session (aiohttp.ClientSession | None, optional): An existing aiohttp ClientSession to use. If not provided, a new session will be created.
base_url (str, optional): The base URL for the Cartesia API. Defaults to "https://api.cartesia.ai".
"""

super().__init__(
Expand All @@ -113,6 +122,7 @@ def __init__(
speed=speed,
emotion=emotion,
api_key=api_key,
base_url=base_url,
)
self._session = http_session

Expand Down Expand Up @@ -207,7 +217,7 @@ async def _run(self) -> None:

try:
async with self._session.post(
"https://api.cartesia.ai/tts/bytes",
self._opts.get_http_url("/tts/bytes"),
headers=headers,
json=json,
timeout=aiohttp.ClientTimeout(
Expand Down Expand Up @@ -344,7 +354,9 @@ def _send_last_frame(*, segment_id: str, is_final: bool) -> None:
else:
logger.error("unexpected Cartesia message %s", data)

url = f"wss://api.cartesia.ai/tts/websocket?api_key={self._opts.api_key}&cartesia_version={API_VERSION}"
url = self._opts.get_ws_url(
f"/tts/websocket?api_key={self._opts.api_key}&cartesia_version={API_VERSION}"
)

ws: aiohttp.ClientWebSocketResponse | None = None

Expand Down

0 comments on commit 913a4ff

Please sign in to comment.