NvidiaSegmentedSTTService: simplify exception handling

This commit is contained in:
Aleix Conchillo Flaqué
2026-01-19 20:31:58 -08:00
parent 671dc8cd9b
commit 655006aff5

View File

@@ -510,8 +510,6 @@ class NvidiaSegmentedSTTService(SegmentedSTTService):
auth = riva.client.Auth(None, self._use_ssl, self._server, metadata) auth = riva.client.Auth(None, self._use_ssl, self._server, metadata)
self._asr_service = riva.client.ASRService(auth) self._asr_service = riva.client.ASRService(auth)
logger.info(f"Initialized NvidiaSegmentedSTTService with model: {self.model_name}")
def _create_recognition_config(self): def _create_recognition_config(self):
"""Create the NVIDIA Riva ASR recognition configuration.""" """Create the NVIDIA Riva ASR recognition configuration."""
# Create base configuration # Create base configuration
@@ -579,6 +577,7 @@ class NvidiaSegmentedSTTService(SegmentedSTTService):
await super().start(frame) await super().start(frame)
self._initialize_client() self._initialize_client()
self._config = self._create_recognition_config() self._config = self._create_recognition_config()
logger.debug(f"Initialized NvidiaSegmentedSTTService with model: {self.model_name}")
async def set_language(self, language: Language): async def set_language(self, language: Language):
"""Set the language for the STT service. """Set the language for the STT service.
@@ -612,21 +611,12 @@ class NvidiaSegmentedSTTService(SegmentedSTTService):
Frame: TranscriptionFrame containing the transcribed text. Frame: TranscriptionFrame containing the transcribed text.
""" """
try: try:
await self.start_processing_metrics()
await self.start_ttfb_metrics()
# Make sure the client is initialized
if self._asr_service is None:
self._initialize_client()
# Make sure the config is created
if self._config is None:
self._config = self._create_recognition_config()
# Type assertion to satisfy the IDE
assert self._asr_service is not None, "ASR service not initialized" assert self._asr_service is not None, "ASR service not initialized"
assert self._config is not None, "Recognition config not created" assert self._config is not None, "Recognition config not created"
await self.start_processing_metrics()
await self.start_ttfb_metrics()
# Process audio with NVIDIA Riva ASR - explicitly request non-future response # Process audio with NVIDIA Riva ASR - explicitly request non-future response
raw_response = self._asr_service.offline_recognize(audio, self._config, future=False) raw_response = self._asr_service.offline_recognize(audio, self._config, future=False)
@@ -634,7 +624,6 @@ class NvidiaSegmentedSTTService(SegmentedSTTService):
await self.stop_processing_metrics() await self.stop_processing_metrics()
# Process the response - handle different possible return types # Process the response - handle different possible return types
try:
# If it's a future-like object, get the result # If it's a future-like object, get the result
if hasattr(raw_response, "result"): if hasattr(raw_response, "result"):
response = raw_response.result() response = raw_response.result()
@@ -665,12 +654,10 @@ class NvidiaSegmentedSTTService(SegmentedSTTService):
await self._handle_transcription(text, True, self._language_enum) await self._handle_transcription(text, True, self._language_enum)
if not transcription_found: if not transcription_found:
logger.debug("No transcription results found in NVIDIA Riva response") logger.debug(f"{self}: No transcription results found in NVIDIA Riva response")
except AttributeError as ae: except AttributeError as ae:
logger.error(f"Unexpected response structure from NVIDIA Riva: {ae}") logger.error(f"{self}: Unexpected response structure from NVIDIA Riva: {ae}")
yield ErrorFrame(f"Unexpected NVIDIA Riva response format: {str(ae)}") yield ErrorFrame(f"{self}: Unexpected NVIDIA Riva response format: {str(ae)}")
except Exception as e: except Exception as e:
logger.error(f"{self} exception: {e}") logger.error(f"{self} exception: {e}")
yield ErrorFrame(error=f"{self} error: {e}") yield ErrorFrame(error=f"{self} error: {e}")