2
votes

I am trying to use IBM watson api for text to speech service. The service works if I use curl command but when I try to use the srrvice using Python SDK, It throws me below error.

Traceback (most recent call last): File "/anaconda3/lib/python3.6/site-packages/ibm_cloud_sdk_core/base_service.py", line 234, in send response.status_code, error_message, http_response=response) ibm_cloud_sdk_core.api_exception.ApiException: Error: Forbidden, Code: 403 Method failed with status code 403: Forbidden

Below is the curl command

curl -X GET -u "apikey:myapiKey" --output hello_world.wav "https://api.eu-de.text-to-speech.watson.cloud.ibm.com/text-to-speech/api/v1/synthesize?accept=audio/wav&text=Hallo%20Welt&voice=de-DE_DieterVoice"

Below is the python code

from ibm_watson import TextToSpeechV1
from ibm_cloud_sdk_core.authenticators import IAMAuthenticator


from ibm_watson import ApiException
import json

IBM_API_ENDPOINT = "https://api.eu-de.text-to-speech.watson.cloud.ibm.com/text-to-speech/api/v1/synthesize"


IBM_TTS_API_KEY = "myAPIKey"

authenticator = IAMAuthenticator(IBM_TTS_API_KEY)
text_to_speech = TextToSpeechV1(authenticator=authenticator)
text_to_speech.set_service_url(IBM_API_ENDPOINT)


try:
    with open('IBM.wav', 'wb') as audio_file:
        audio_file.write(text_to_speech.synthesize("Hallo world", voice='de-DE_DieterVoice', accept='audio/wav').get_result().content)

except ApiException as ex:
    print("Method failed with status code " + str(ex.code) + ": " + ex.message)
2

2 Answers

2
votes

Check the Current usage ( of 10000 Thousand char). I had the same issue when I reached the maximum limit of characters per month.

( Thousand char: Using 21091 of 10000)

1
votes

This will because you are are tacking the method and the version at the end of the endpoint. As you are using the Python SDK for IBM-Watson the SDK take care of applying the method when you invoke synthesize. Consequently your endpoint should only be:

IBM_API_ENDPOINT = "https://api.eu-de.text-to-speech.watson.cloud.ibm.com"

Check the API documentation for further details - https://cloud.ibm.com/apidocs/text-to-speech/text-to-speech?code=python#service-endpoint