From 51654cb2e8a64195263ad81db42d8d31896e5e00 Mon Sep 17 00:00:00 2001 From: gabe-l-hart Date: Tue, 22 Dec 2020 09:50:44 -0700 Subject: [PATCH 1/2] fix: robustify the STT streaming results handling In the STT recognize_listener, the clause that handles 'results' or 'speaker_labels' was prone to index errors when the 'results' object returned by the service was empty. This can happen, so this change makes that logic safe to empty (or otherwise malformed) results. --- ibm_watson/websocket/recognize_listener.py | 27 +++++++++++++--------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/ibm_watson/websocket/recognize_listener.py b/ibm_watson/websocket/recognize_listener.py index 3931a252..947b0340 100644 --- a/ibm_watson/websocket/recognize_listener.py +++ b/ibm_watson/websocket/recognize_listener.py @@ -194,18 +194,23 @@ def on_data(self, ws, message, message_type, fin): # if in streaming elif 'results' in json_object or 'speaker_labels' in json_object: - hypothesis = '' - if 'results' in json_object: - hypothesis = json_object['results'][0]['alternatives'][0][ - 'transcript'] - b_final = (json_object['results'][0]['final'] is True) - transcripts = self.extract_transcripts( - json_object['results'][0]['alternatives']) - - if b_final: - self.callback.on_transcription(transcripts) - + + # If results are present, extract the hypothesis and, if finalized, the full + # set of transcriptions and send them to the appropriate callbacks. + results = json_object.get('results') + if results: + b_final = (results[0].get('final') is True) + alternatives = results[0].get('alternatives') + if alternatives: + hypothesis = alternatives[0].get('transcript') + transcripts = self.extract_transcripts(alternatives) + if b_final: + self.callback.on_transcription(transcripts) + if hypothesis: + self.callback.on_hypothesis(hypothesis) self.callback.on_hypothesis(hypothesis) + + # Always call the on_data callback if 'results' or 'speaker_labels' are present self.callback.on_data(json_object) def on_error(self, ws, error): From e6f6457a680774099d9605581d104b9506073f56 Mon Sep 17 00:00:00 2001 From: Gabe Goodhart Date: Tue, 22 Jun 2021 15:42:40 -0600 Subject: [PATCH 2/2] Remove buggy extra call to on_hypothesis --- ibm_watson/websocket/recognize_listener.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/ibm_watson/websocket/recognize_listener.py b/ibm_watson/websocket/recognize_listener.py index 947b0340..21679760 100644 --- a/ibm_watson/websocket/recognize_listener.py +++ b/ibm_watson/websocket/recognize_listener.py @@ -194,7 +194,6 @@ def on_data(self, ws, message, message_type, fin): # if in streaming elif 'results' in json_object or 'speaker_labels' in json_object: - # If results are present, extract the hypothesis and, if finalized, the full # set of transcriptions and send them to the appropriate callbacks. results = json_object.get('results') @@ -208,7 +207,6 @@ def on_data(self, ws, message, message_type, fin): self.callback.on_transcription(transcripts) if hypothesis: self.callback.on_hypothesis(hypothesis) - self.callback.on_hypothesis(hypothesis) # Always call the on_data callback if 'results' or 'speaker_labels' are present self.callback.on_data(json_object)