4

I'm using python on the IBM Bluemix platform. How can I do a call to the text-to-speech Watson service? I have the string inside my python code and I need to pass this text to be read.

Vitor Hugo
  • 41
  • 3

2 Answers2

3

Assuming that you already have a Bluemix account and added text to speech Watson API to your Bluemix workspace, you have the credentials to access the API (restful).

If you were requesting using the CURL linux app, it would be something like this

curl -u "xxxxx729-b03f-4403-8adf-c5418ee4ea05":"xxxxxiWtmVoG" "https://stream.watsonplatform.net/text-to-speech/api/v1/synthesize?text=Hello+World" -H "accept: audio/flac" > sound.flac

Using Python, it can be

import requests

headers = {'accept': 'audio/flac'}

r = requests.get('https://stream.watsonplatform.net/text-to-speech/api/v1/synthesize?text=Hello+World', auth=('xxxxx729-b03f-4403-8adf-c5418ee4ea05', 'xxxxxiWtmVoG'), headers=headers)

with open('/home/leo/sound.flac', 'wb') as fd:
    for chunk in r.iter_content(1024):
        fd.write(chunk)

See http://docs.python-requests.org/en/master/user/quickstart/ for details on the requests package.

See https://www.ibm.com/smarterplanet/us/en/ibmwatson/developercloud/text-to-speech/api/v1/ for text2Speech documentation.

Leo
  • 751
  • 4
  • 29
1

Best thing is to use Watson Developer Cloud Python SDK

Dudi
  • 2,340
  • 1
  • 24
  • 39