Apply suggestions from code review
Co-authored-by: Mark Backman <m.backman@gmail.com>
This commit is contained in:
@@ -10,7 +10,6 @@ This module provides integration with Gradium's real-time speech-to-text
|
|||||||
WebSocket API for streaming audio transcription.
|
WebSocket API for streaming audio transcription.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import asyncio
|
|
||||||
import base64
|
import base64
|
||||||
import json
|
import json
|
||||||
from typing import AsyncGenerator
|
from typing import AsyncGenerator
|
||||||
@@ -63,7 +62,6 @@ class GradiumSTTService(WebsocketSTTService):
|
|||||||
|
|
||||||
Args:
|
Args:
|
||||||
api_key: Gradium API key for authentication.
|
api_key: Gradium API key for authentication.
|
||||||
language: Language code for transcription. Defaults to English (Language.EN).
|
|
||||||
api_endpoint_base_url: WebSocket endpoint URL. Defaults to Gradium's streaming endpoint.
|
api_endpoint_base_url: WebSocket endpoint URL. Defaults to Gradium's streaming endpoint.
|
||||||
json_config: Optional JSON configuration string for additional model settings.
|
json_config: Optional JSON configuration string for additional model settings.
|
||||||
**kwargs: Additional arguments passed to parent STTService class.
|
**kwargs: Additional arguments passed to parent STTService class.
|
||||||
@@ -96,7 +94,7 @@ class GradiumSTTService(WebsocketSTTService):
|
|||||||
frame: Start frame to begin processing.
|
frame: Start frame to begin processing.
|
||||||
"""
|
"""
|
||||||
await super().start(frame)
|
await super().start(frame)
|
||||||
self._chunk_size_bytes = int(self._chunk_size_ms * self._sample_rate * 2 / 1000)
|
self._chunk_size_bytes = int(self._chunk_size_ms * self.sample_rate * 2 / 1000)
|
||||||
await self._connect()
|
await self._connect()
|
||||||
|
|
||||||
async def stop(self, frame: EndFrame):
|
async def stop(self, frame: EndFrame):
|
||||||
@@ -191,7 +189,7 @@ class GradiumSTTService(WebsocketSTTService):
|
|||||||
raise Exception(f"unexpected first message type {ready_msg['type']}")
|
raise Exception(f"unexpected first message type {ready_msg['type']}")
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"{self}: unable to connect to Gradium: {e}")
|
await self.push_error(error_msg=f"Unknown error occurred: {e}", exception=e)
|
||||||
raise
|
raise
|
||||||
|
|
||||||
async def _disconnect(self):
|
async def _disconnect(self):
|
||||||
@@ -207,7 +205,7 @@ class GradiumSTTService(WebsocketSTTService):
|
|||||||
logger.debug("Disconnecting from Gradium STT")
|
logger.debug("Disconnecting from Gradium STT")
|
||||||
await self._websocket.close()
|
await self._websocket.close()
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"{self} error closing websocket: {e}")
|
await self.push_error(error_msg=f"Unknown error occurred: {e}", exception=e)
|
||||||
finally:
|
finally:
|
||||||
self._websocket = None
|
self._websocket = None
|
||||||
await self._call_event_handler("on_disconnected")
|
await self._call_event_handler("on_disconnected")
|
||||||
@@ -238,7 +236,7 @@ class GradiumSTTService(WebsocketSTTService):
|
|||||||
elif type_ == "end_of_stream":
|
elif type_ == "end_of_stream":
|
||||||
await self._handle_end_of_stream()
|
await self._handle_end_of_stream()
|
||||||
elif type_ == "error":
|
elif type_ == "error":
|
||||||
logger.error(f"Gradium error: {msg.get('message', 'Unknown error')}")
|
await self.push_error(error_msg=f"Error: {msg}")
|
||||||
|
|
||||||
async def _handle_end_of_stream(self):
|
async def _handle_end_of_stream(self):
|
||||||
"""Handle termination message."""
|
"""Handle termination message."""
|
||||||
|
|||||||
@@ -17,7 +17,6 @@ from pipecat.frames.frames import (
|
|||||||
EndFrame,
|
EndFrame,
|
||||||
ErrorFrame,
|
ErrorFrame,
|
||||||
Frame,
|
Frame,
|
||||||
InterruptionFrame,
|
|
||||||
StartFrame,
|
StartFrame,
|
||||||
TTSAudioRawFrame,
|
TTSAudioRawFrame,
|
||||||
TTSStartedFrame,
|
TTSStartedFrame,
|
||||||
@@ -223,7 +222,7 @@ class GradiumTTSService(InterruptibleWordTTSService):
|
|||||||
|
|
||||||
await self._call_event_handler("on_connected")
|
await self._call_event_handler("on_connected")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"{self} initialization error: {e}")
|
await self.push_error(error_msg=f"Unknown error occurred: {e}", exception=e)
|
||||||
self._websocket = None
|
self._websocket = None
|
||||||
await self._call_event_handler("on_connection_error", f"{e}")
|
await self._call_event_handler("on_connection_error", f"{e}")
|
||||||
|
|
||||||
@@ -234,7 +233,7 @@ class GradiumTTSService(InterruptibleWordTTSService):
|
|||||||
if self._websocket:
|
if self._websocket:
|
||||||
await self._websocket.close()
|
await self._websocket.close()
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"{self} error closing websocket: {e}")
|
await self.push_error(error_msg=f"Unknown error occurred: {e}", exception=e)
|
||||||
finally:
|
finally:
|
||||||
self._websocket = None
|
self._websocket = None
|
||||||
self._request_id = None
|
self._request_id = None
|
||||||
@@ -253,7 +252,6 @@ class GradiumTTSService(InterruptibleWordTTSService):
|
|||||||
try:
|
try:
|
||||||
msg = {"type": "end_of_stream"}
|
msg = {"type": "end_of_stream"}
|
||||||
await self._websocket.send(json.dumps(msg))
|
await self._websocket.send(json.dumps(msg))
|
||||||
logger.trace(f"{self}: flushing audio")
|
|
||||||
except ConnectionClosedOK:
|
except ConnectionClosedOK:
|
||||||
logger.debug(f"{self}: connection closed normally during flush")
|
logger.debug(f"{self}: connection closed normally during flush")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
@@ -287,10 +285,9 @@ class GradiumTTSService(InterruptibleWordTTSService):
|
|||||||
await self.stop_all_metrics()
|
await self.stop_all_metrics()
|
||||||
|
|
||||||
elif msg["type"] == "error":
|
elif msg["type"] == "error":
|
||||||
logger.error(f"{self} error: {msg}")
|
|
||||||
await self.push_frame(TTSStoppedFrame())
|
await self.push_frame(TTSStoppedFrame())
|
||||||
await self.stop_all_metrics()
|
await self.stop_all_metrics()
|
||||||
await self.push_error(ErrorFrame(f"{self} error: {msg['message']}"))
|
await self.push_error(error_msg=f"Error: {msg['message']}"))
|
||||||
|
|
||||||
async def push_frame(self, frame: Frame, direction: FrameDirection = FrameDirection.DOWNSTREAM):
|
async def push_frame(self, frame: Frame, direction: FrameDirection = FrameDirection.DOWNSTREAM):
|
||||||
"""Push frame and handle end-of-turn conditions.
|
"""Push frame and handle end-of-turn conditions.
|
||||||
@@ -328,11 +325,11 @@ class GradiumTTSService(InterruptibleWordTTSService):
|
|||||||
await self._get_websocket().send(json.dumps(msg))
|
await self._get_websocket().send(json.dumps(msg))
|
||||||
await self.start_tts_usage_metrics(text)
|
await self.start_tts_usage_metrics(text)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"{self} error sending message: {e}")
|
yield ErrorFrame(error=f"Unknown error occurred: {e}")
|
||||||
yield TTSStoppedFrame()
|
yield TTSStoppedFrame()
|
||||||
await self._disconnect()
|
await self._disconnect()
|
||||||
await self._connect()
|
await self._connect()
|
||||||
return
|
return
|
||||||
yield None
|
yield None
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"{self} exception: {e}")
|
yield ErrorFrame(error=f"Unknown error occurred: {e}")
|
||||||
|
|||||||
Reference in New Issue
Block a user