首页 > 解决方案 > IBM 文本到语音 Python 解码错误

问题描述

我刚刚尝试了 IBM 使用 Python 进行文本到语音的基本示例:

!pip install ibm_watson

from ibm_watson import TextToSpeechV1

from ibm_cloud_sdk_core.authenticators import IAMAuthenticator

apikey = 'my API KEY'
url = 'my SERVICE URL'

authenticator = IAMAuthenticator(apikey)
tts = TextToSpeechV1(authenticator=authenticator)
tts.set_service_url(url)

with open('./speech.mp3', 'wb') as audio_file:
    res = tts.synthesize('Hello World!', accept='audio/mp3', voice='en-US_AllisonV3Voice').get_result()
    audio_file.write(res.content)

但我收到一条错误消息:DecodeError: It is required to pass in a value for the "algorithms" argument when calling decode().*

DecodeError                               Traceback (most recent call last)
<ipython-input-5-53b9d398591b> in <module>
      1 with open('./speech.mp3', 'wb') as audio_file:
----> 2     res = tts.synthesize('Hello World!', accept='audio/mp3', voice='en-US_AllisonV3Voice').get_result()
      3     audio_file.write(res.content)

c:\users\chris\appdata\local\programs\python\python39\lib\site-packages\ibm_watson\text_to_speech_v1.py in synthesize(self, text, accept, voice, customization_id, **kwargs)
    275 
    276         url = '/v1/synthesize'
--> 277         request = self.prepare_request(method='POST',
    278                                        url=url,
    279                                        headers=headers,

c:\users\chris\appdata\local\programs\python\python39\lib\site-packages\ibm_cloud_sdk_core\base_service.py in prepare_request(self, method, url, headers, params, data, files, **kwargs)
    295         request['data'] = data
    296 
--> 297         self.authenticator.authenticate(request)
    298 
    299         # Next, we need to process the 'files' argument to try to fill in

c:\users\chris\appdata\local\programs\python\python39\lib\site-packages\ibm_cloud_sdk_core\authenticators\iam_authenticator.py in authenticate(self, req)
    104         """
    105         headers = req.get('headers')
--> 106         bearer_token = self.token_manager.get_token()
    107         headers['Authorization'] = 'Bearer {0}'.format(bearer_token)
    108 

c:\users\chris\appdata\local\programs\python\python39\lib\site-packages\ibm_cloud_sdk_core\jwt_token_manager.py in get_token(self)
     77         """
     78         if self._is_token_expired():
---> 79             self.paced_request_token()
     80 
     81         if self._token_needs_refresh():

c:\users\chris\appdata\local\programs\python\python39\lib\site-packages\ibm_cloud_sdk_core\jwt_token_manager.py in paced_request_token(self)
    122             if not request_active:
    123                 token_response = self.request_token()
--> 124                 self._save_token_info(token_response)
    125                 self.request_time = 0
    126                 return


c:\users\chris\appdata\local\programs\python\python39\lib\site-packages\ibm_cloud_sdk_core\jwt_token_manager.py in _save_token_info(self, token_response)
    189 
    190         # The time of expiration is found by decoding the JWT access token
--> 191         decoded_response = jwt.decode(access_token, verify=False)
    192         # exp is the time of expire and iat is the time of token retrieval
    193         exp = decoded_response.get('exp')

c:\users\chris\appdata\local\programs\python\python39\lib\site-packages\jwt\api_jwt.py in decode(self, jwt, key, algorithms, options, **kwargs)
    111         **kwargs,
    112     ) -> Dict[str, Any]:
--> 113         decoded = self.decode_complete(jwt, key, algorithms, options, **kwargs)
    114         return decoded["payload"]
    115 

c:\users\chris\appdata\local\programs\python\python39\lib\site-packages\jwt\api_jwt.py in decode_complete(self, jwt, key, algorithms, options, **kwargs)
     77 
     78         if options["verify_signature"] and not algorithms:
---> 79             raise DecodeError(
     80                 'It is required that you pass in a value for the "algorithms" argument when calling decode().'
     81             )

DecodeError: It is required that you pass in a value for the "algorithms" argument when calling decode().

标签: python

解决方案


问题可能出在最新版本的 PyJWT 包 (2.0.0) 上。利用

pip install PyJWT==1.7.1

降级到以前的版本,您的项目现在可以工作了。(这对我有用)


推荐阅读