From b5f6e5ae59889c5fdbe754e06b5a4e34b85b963e Mon Sep 17 00:00:00 2001 From: JayZed Date: Wed, 30 Oct 2024 16:54:05 -0400 Subject: [PATCH] Handle empty responses from whisper This will replace JSONDecodeError with a more friendly message. --- .../subliminal_patch/providers/whisperai.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/custom_libs/subliminal_patch/providers/whisperai.py b/custom_libs/subliminal_patch/providers/whisperai.py index 0546717a2..4b3fa466c 100644 --- a/custom_libs/subliminal_patch/providers/whisperai.py +++ b/custom_libs/subliminal_patch/providers/whisperai.py @@ -5,6 +5,7 @@ from requests import Session +from requests.exceptions import JSONDecodeError from subliminal_patch.subtitle import Subtitle from subliminal_patch.providers import Provider from subliminal import __short_version__ @@ -269,10 +270,19 @@ def detect_language(self, path) -> Language: params={'encode': 'false'}, files={'audio_file': out}, timeout=(self.response, self.timeout)) + + try: + results = r.json() + except JSONDecodeError: + results = {} + + if len(results) == 0: + logger.info(f"Whisper returned empty response when detecting language") + return None - logger.debug(f"Whisper detected language of {path} as {r.json()['detected_language']}") + logger.debug(f"Whisper detected language of {path} as {results['detected_language']}") - return whisper_get_language(r.json()["language_code"], r.json()["detected_language"]) + return whisper_get_language(results["language_code"], results["detected_language"]) def query(self, language, video): if language not in self.languages: