Apply Ruff formater

This commit is contained in:
Jin Kim
2024-09-26 19:52:25 +09:00
parent 49f2123893
commit d05717a1bd

View File

@@ -71,15 +71,16 @@ class CartesiaTTSService(AsyncWordTTSService):
emotion: Optional[List[str]] = [] emotion: Optional[List[str]] = []
def __init__( def __init__(
self, self,
*, *,
api_key: str, api_key: str,
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", model_id: str = "sonic-english",
params: InputParams = InputParams(), params: InputParams = InputParams(),
**kwargs): **kwargs,
):
# Aggregating sentences still gives cleaner-sounding results and fewer # Aggregating sentences still gives cleaner-sounding results and fewer
# artifacts than streaming one word at a time. On average, waiting for a # artifacts than streaming one word at a time. On average, waiting for a
# full sentence should only "cost" us 15ms or so with GPT-4o or a Llama # full sentence should only "cost" us 15ms or so with GPT-4o or a Llama
@@ -91,7 +92,10 @@ class CartesiaTTSService(AsyncWordTTSService):
# can use those to generate text frames ourselves aligned with the # can use those to generate text frames ourselves aligned with the
# playout timing of the audio! # playout timing of the audio!
super().__init__( super().__init__(
aggregate_sentences=True, push_text_frames=False, sample_rate=params.sample_rate, **kwargs aggregate_sentences=True,
push_text_frames=False,
sample_rate=params.sample_rate,
**kwargs,
) )
self._api_key = api_key self._api_key = api_key
@@ -137,11 +141,10 @@ class CartesiaTTSService(AsyncWordTTSService):
logger.debug(f"Switching TTS language to: [{language}]") logger.debug(f"Switching TTS language to: [{language}]")
self._language = language_to_cartesia_language(language) self._language = language_to_cartesia_language(language)
def _build_msg(self, text: str = "", continue_transcript: bool = True, add_timestamps: bool = True): def _build_msg(
voice_config = { self, text: str = "", continue_transcript: bool = True, add_timestamps: bool = True
"mode": "id", ):
"id": self._voice_id voice_config = {"mode": "id", "id": self._voice_id}
}
if self._speed or self._emotion: if self._speed or self._emotion:
voice_config["__experimental_controls"] = {} voice_config["__experimental_controls"] = {}
@@ -236,8 +239,7 @@ class CartesiaTTSService(AsyncWordTTSService):
await self.add_word_timestamps([("LLMFullResponseEndFrame", 0)]) await self.add_word_timestamps([("LLMFullResponseEndFrame", 0)])
elif msg["type"] == "timestamps": elif msg["type"] == "timestamps":
await self.add_word_timestamps( await self.add_word_timestamps(
list(zip(msg["word_timestamps"]["words"], list(zip(msg["word_timestamps"]["words"], msg["word_timestamps"]["start"]))
msg["word_timestamps"]["start"]))
) )
elif msg["type"] == "chunk": elif msg["type"] == "chunk":
await self.stop_ttfb_metrics() await self.stop_ttfb_metrics()
@@ -254,8 +256,7 @@ class CartesiaTTSService(AsyncWordTTSService):
await self.stop_all_metrics() await self.stop_all_metrics()
await self.push_error(ErrorFrame(f'{self} error: {msg["error"]}')) await self.push_error(ErrorFrame(f'{self} error: {msg["error"]}'))
else: else:
logger.error( logger.error(f"Cartesia error, unknown message type: {msg}")
f"Cartesia error, unknown message type: {msg}")
except asyncio.CancelledError: except asyncio.CancelledError:
pass pass
except Exception as e: except Exception as e:
@@ -379,7 +380,7 @@ class CartesiaHttpTTSService(TTSService):
output_format=self._output_format, output_format=self._output_format,
language=self._language, language=self._language,
stream=False, stream=False,
_experimental_voice_controls=voice_controls _experimental_voice_controls=voice_controls,
) )
await self.stop_ttfb_metrics() await self.stop_ttfb_metrics()