Send raw result

This commit is contained in:
Matej Marinko
2025-07-09 09:59:04 +02:00
parent 7becce9e8c
commit 98e24131bd

View File

@@ -121,7 +121,7 @@ class SonioxSTTService(STTService):
self._auto_finalize_delay_ms = auto_finalize_delay_ms self._auto_finalize_delay_ms = auto_finalize_delay_ms
self._websocket = None self._websocket = None
self._final_transcription_buffer = "" self._final_transcription_buffer = []
self._last_tokens_received: Optional[float] = None self._last_tokens_received: Optional[float] = None
self._receive_task = None self._receive_task = None
@@ -250,20 +250,22 @@ class SonioxSTTService(STTService):
return return
# Transcription frame will be only sent after we get the "endpoint" event. # Transcription frame will be only sent after we get the "endpoint" event.
self._final_transcription_buffer = "" self._final_transcription_buffer = []
async def send_endpoint_transcript(): async def send_endpoint_transcript():
if self._final_transcription_buffer: if self._final_transcription_buffer:
text = "".join(map(lambda token: token["text"], self._final_transcription_buffer))
await self.push_frame( await self.push_frame(
TranscriptionFrame( TranscriptionFrame(
self._final_transcription_buffer, text=text,
self._user_id, user_id=self._user_id,
time_now_iso8601(), timestamp=time_now_iso8601(),
result=self._final_transcription_buffer,
) )
) )
await self._handle_transcription(self._final_transcription_buffer, is_final=True) await self._handle_transcription(text, is_final=True)
await self.stop_processing_metrics() await self.stop_processing_metrics()
self._final_transcription_buffer = "" self._final_transcription_buffer = []
try: try:
async for message in self._websocket: async for message in self._websocket:
@@ -280,7 +282,7 @@ class SonioxSTTService(STTService):
self._last_tokens_received = time.time() self._last_tokens_received = time.time()
# We will only send the final tokens after we get the "endpoint" event. # We will only send the final tokens after we get the "endpoint" event.
non_final_transcription = "" non_final_transcription = []
for token in tokens: for token in tokens:
if token["is_final"]: if token["is_final"]:
@@ -289,18 +291,26 @@ class SonioxSTTService(STTService):
# the rest will be sent as interim tokens (even final tokens). # the rest will be sent as interim tokens (even final tokens).
await send_endpoint_transcript() await send_endpoint_transcript()
else: else:
self._final_transcription_buffer += token["text"] self._final_transcription_buffer.append(token)
else: else:
non_final_transcription += token["text"] non_final_transcription.append(token)
if self._final_transcription_buffer or non_final_transcription: if self._final_transcription_buffer or non_final_transcription:
final_text = "".join(
map(lambda token: token["text"], self._final_transcription_buffer)
)
non_final_text = "".join(
map(lambda token: token["text"], non_final_transcription)
)
await self.push_frame( await self.push_frame(
InterimTranscriptionFrame( InterimTranscriptionFrame(
# Even final tokens are sent as interim tokens as we want to send # Even final tokens are sent as interim tokens as we want to send
# nicely formatted messages - therefore waiting for the endpoint. # nicely formatted messages - therefore waiting for the endpoint.
self._final_transcription_buffer + non_final_transcription, text=final_text + non_final_text,
self._user_id, user_id=self._user_id,
time_now_iso8601(), timestamp=time_now_iso8601(),
result=self._final_transcription_buffer + non_final_transcription,
) )
) )