From 264807d7eb3287bbca56328496a477e042a9b2ca Mon Sep 17 00:00:00 2001 From: gabe-l-hart Date: Tue, 6 Jul 2021 11:13:39 -0600 Subject: [PATCH 1/3] fix: robustify the STT streaming results handling (#768) * 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. * Remove buggy extra call to on_hypothesis Co-authored-by: Mamoon Raja Co-authored-by: Angelo Paparazzi --- ibm_watson/websocket/recognize_listener.py | 27 ++++++++++++---------- 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/ibm_watson/websocket/recognize_listener.py b/ibm_watson/websocket/recognize_listener.py index 3931a252..21679760 100644 --- a/ibm_watson/websocket/recognize_listener.py +++ b/ibm_watson/websocket/recognize_listener.py @@ -194,18 +194,21 @@ 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) - - self.callback.on_hypothesis(hypothesis) + # 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) + + # 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 09a379a6a66e8c98ae65b8fe5df28ea08de2c3d2 Mon Sep 17 00:00:00 2001 From: semantic-release-bot Date: Tue, 6 Jul 2021 17:25:52 +0000 Subject: [PATCH 2/3] =?UTF-8?q?Bump=20version:=205.2.1=20=E2=86=92=205.2.2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .bumpversion.cfg | 2 +- ibm_watson/version.py | 2 +- setup.py | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.bumpversion.cfg b/.bumpversion.cfg index ad22cb85..2ab67022 100644 --- a/.bumpversion.cfg +++ b/.bumpversion.cfg @@ -1,5 +1,5 @@ [bumpversion] -current_version = 5.2.1 +current_version = 5.2.2 commit = True [bumpversion:file:ibm_watson/version.py] diff --git a/ibm_watson/version.py b/ibm_watson/version.py index 4dc2ef26..7478af2c 100644 --- a/ibm_watson/version.py +++ b/ibm_watson/version.py @@ -1 +1 @@ -__version__ = '5.2.1' +__version__ = '5.2.2' diff --git a/setup.py b/setup.py index d8d42ea5..f3d67142 100644 --- a/setup.py +++ b/setup.py @@ -16,7 +16,7 @@ from setuptools import setup from os import path -__version__ = '5.2.1' +__version__ = '5.2.2' # read contents of README file this_directory = path.abspath(path.dirname(__file__)) From 1eca9a163aceff8ff8e8dde5dbe85461324f72bf Mon Sep 17 00:00:00 2001 From: semantic-release-bot Date: Tue, 6 Jul 2021 17:25:52 +0000 Subject: [PATCH 3/3] chore(release): 5.2.2 release notes ## [5.2.2](https://github.com/watson-developer-cloud/python-sdk/compare/v5.2.1...v5.2.2) (2021-07-06) ### Bug Fixes * robustify the STT streaming results handling ([#768](https://github.com/watson-developer-cloud/python-sdk/issues/768)) ([264807d](https://github.com/watson-developer-cloud/python-sdk/commit/264807d7eb3287bbca56328496a477e042a9b2ca)) --- CHANGELOG.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2f86b1ef..248fa0ce 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,10 @@ +## [5.2.2](https://github.com/watson-developer-cloud/python-sdk/compare/v5.2.1...v5.2.2) (2021-07-06) + + +### Bug Fixes + +* robustify the STT streaming results handling ([#768](https://github.com/watson-developer-cloud/python-sdk/issues/768)) ([264807d](https://github.com/watson-developer-cloud/python-sdk/commit/264807d7eb3287bbca56328496a477e042a9b2ca)) + ## [5.2.1](https://github.com/watson-developer-cloud/python-sdk/compare/v5.2.0...v5.2.1) (2021-06-28)