Overview

Skill Level: Any Skill Level

Low - Medium

Ingredients

  • Python 2.7 (not tested with 3)
  • Python watson_developer_cloud
  • Bluemix
  • Watson TextToSpeech

Step-by-step

  1. Install Python.

    Mac OS X 10.8 comes with Python 2.7 pre-installed by Apple. Remember that if you choose to install a newer Python version from python.org, you will have two different but functional Python installations on your computer, so it will be important that your paths and usages are consistent with what you want to do. The latest Mac and Window Versions of Python can be found here. https://www.python.org/downloads/release/python-2711/

  2. Install Python-Watson Developer Cloud

    Install the Python-Watson Developer cloud API’s, to do this, go to this link and download and unzip the package.

    https://pypi.python.org/pypi/watson-developer-cloud

    Unzip the package and change to that directory via your command-line to install.

    Once you have moved to that directory / folder, it is time to install Watson Developer Cloud, so all your awesome Python programs can use it and leverage it. For Mac / Linux users you may need to allow yourself install privaleges Sudo. You will see below mine is already installed, however you should get a message saying it is all installed OK.

    So now you have access to to Watson via Python – lets see how we use it!

  3. Including the files.

    Using your fav. programming editor (I use sublime2 . you can download it for free and pay a small fee if you just love it!), the first thing we need to do is include the imports at the start of our small program. You will see that to simplify my life I have imported codecs to stop all the Unicode aggrivation that one can face!

    from __future__ import unicode_literals
    import requests
    #import json
    from os.path import join, dirname
    from watson_developer_cloud import TextToSpeechV1
    import codecs
    import sys
    import os
    reload(sys)

    So now we are going to initialize some global variables to that our program will use.

  4. Set up your Username and Password Credentials

    The most important lines to note here are the username ad Password. These are NOT your bluemix credentials but your TextToSpeech API credentials. You can get that from entering Bluemix and clicking and registering for the TextToSpeech API. You should see something like this. Just replace the ***** with your credentials.

    #—————————————————————————————#
    #—————————initialized global variables —————————#
    #—————————————————————————————#
    sys.setdefaultencoding(‘utf8’)
    url = ‘https://stream.watsonplatform.net/text-to-speech/api/v1/voices/en-US_MichaelVoice’
    username=’******’,
    password=’******’
    accept = ”
    say_this = ‘the quick brown fox jumped over the lazy dog at IBM headquarters’

  5. Calling the TextToSpeech API in Python

    The Watson Text to Speech API come with about a dozen different sounding voices and is tailorable. To convert the Text to different voices you have to use the voice Keyword in the API. Don’t worry if you dont know the VoiceKey words, the small program I have written lists them out for you and then you can use them.

    However, to use them it is pretty simple,

    x = text_to_speech.voices() <- this line of code extracts the entire API to a literal named x
    all_voices_in_dictionary = x[‘voices’] <- this line of code extracts all the voices from the API and places them in a Python Dictionary

    The Watson TextToSpeech API contains the following information for you to use; Name, Language, Gender and the URL are all important if you need to call them. In addition there is a description to aid your understanding of what you are going to get. Please note, it is expected that the language of the text and the voice chosen are the same, if you get them mixed up – you will get strange results.

    ********** Voice Number 3**************
    the API key name is name and its value is en-US_AllisonVoice
    the API key name is language and its value is en-US
    the API key name is customizable and its value is True
    the API key name is gender and its value is female
    the API key name is url and its value is https://stream.watsonplatform.net/text-to-speech/api/v1/voices/en-US_AllisonVoice
    the API key name is description and its value is Allison: American English female voice.

    You can follow the code here to list the API.

  6. Call the API and write a simple .wav file

    For each of the voices we will create a wave file based on the input text. This is easy enough to do in Python.

    first we allocate a file and then we write to it from the output from the Watson TextToSpeech API.

  7. Get a Copy of all the code here – try it for yourself.

    I have placed the code above in GitHub for you to download. https://github.com/saviosaldanha/Watson-TextToSpeech-Python

    When you run your small program, you will find that you will traverse the API headers and will see about a dozen voice types to use, the keys look like this;

    ********** Voice Number 3**************
    the API key name is name and its value is en-US_AllisonVoice
    the API key name is language and its value is en-US
    the API key name is customizable and its value is True
    the API key name is gender and its value is female
    the API key name is url and its value is https://stream.watsonplatform.net/text-to-speech/api/v1/voices/en-US_AllisonVoice
    the API key name is description and its value is Allison: American English female voice.

2 comments on"Creating Alerts & notifications using Python and Watson TextToSpeech"

  1. Great example! Thanks to this I’m discover IBM Bluemix!

  2. SavioSaldanha April 14, 2016

    Thank-you!! Have a look at my other recipe’s I try to keep them simple.

Join The Discussion