services(cartesia): fix TTFB
This commit is contained in:
@@ -82,7 +82,6 @@ class CartesiaTTSService(TTSService):
|
|||||||
self._timestamped_words_buffer = []
|
self._timestamped_words_buffer = []
|
||||||
self._receive_task = None
|
self._receive_task = None
|
||||||
self._context_appending_task = None
|
self._context_appending_task = None
|
||||||
self._waiting_for_ttfb = False
|
|
||||||
|
|
||||||
def can_generate_metrics(self) -> bool:
|
def can_generate_metrics(self) -> bool:
|
||||||
return True
|
return True
|
||||||
@@ -127,7 +126,6 @@ class CartesiaTTSService(TTSService):
|
|||||||
self._context_id = None
|
self._context_id = None
|
||||||
self._context_id_start_timestamp = None
|
self._context_id_start_timestamp = None
|
||||||
self._timestamped_words_buffer = []
|
self._timestamped_words_buffer = []
|
||||||
self._waiting_for_ttfb = False
|
|
||||||
await self.stop_all_metrics()
|
await self.stop_all_metrics()
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.exception(f"{self} error closing websocket: {e}")
|
logger.exception(f"{self} error closing websocket: {e}")
|
||||||
@@ -148,6 +146,7 @@ class CartesiaTTSService(TTSService):
|
|||||||
if not msg or msg["context_id"] != self._context_id:
|
if not msg or msg["context_id"] != self._context_id:
|
||||||
continue
|
continue
|
||||||
if msg["type"] == "done":
|
if msg["type"] == "done":
|
||||||
|
await self.stop_ttfb_metrics()
|
||||||
# unset _context_id but not the _context_id_start_timestamp because we are likely still
|
# unset _context_id but not the _context_id_start_timestamp because we are likely still
|
||||||
# playing out audio and need the timestamp to set send context frames
|
# playing out audio and need the timestamp to set send context frames
|
||||||
self._context_id = None
|
self._context_id = None
|
||||||
@@ -158,11 +157,9 @@ class CartesiaTTSService(TTSService):
|
|||||||
list(zip(msg["word_timestamps"]["words"], msg["word_timestamps"]["end"]))
|
list(zip(msg["word_timestamps"]["words"], msg["word_timestamps"]["end"]))
|
||||||
)
|
)
|
||||||
elif msg["type"] == "chunk":
|
elif msg["type"] == "chunk":
|
||||||
|
await self.stop_ttfb_metrics()
|
||||||
if not self._context_id_start_timestamp:
|
if not self._context_id_start_timestamp:
|
||||||
self._context_id_start_timestamp = time.time()
|
self._context_id_start_timestamp = time.time()
|
||||||
if self._waiting_for_ttfb:
|
|
||||||
await self.stop_ttfb_metrics()
|
|
||||||
self._waiting_for_ttfb = False
|
|
||||||
frame = AudioRawFrame(
|
frame = AudioRawFrame(
|
||||||
audio=base64.b64decode(msg["data"]),
|
audio=base64.b64decode(msg["data"]),
|
||||||
sample_rate=self._output_format["sample_rate"],
|
sample_rate=self._output_format["sample_rate"],
|
||||||
@@ -198,11 +195,8 @@ class CartesiaTTSService(TTSService):
|
|||||||
if not self._websocket:
|
if not self._websocket:
|
||||||
await self._connect()
|
await self._connect()
|
||||||
|
|
||||||
if not self._waiting_for_ttfb:
|
|
||||||
await self.start_ttfb_metrics()
|
|
||||||
self._waiting_for_ttfb = True
|
|
||||||
|
|
||||||
if not self._context_id:
|
if not self._context_id:
|
||||||
|
await self.start_ttfb_metrics()
|
||||||
self._context_id = str(uuid.uuid4())
|
self._context_id = str(uuid.uuid4())
|
||||||
|
|
||||||
msg = {
|
msg = {
|
||||||
|
|||||||
Reference in New Issue
Block a user