Using Go for IBM Watson services

IBM Bluemix is the cloud platform in which you deploy applications that use IBM Watson Developer Cloud services Application Programming Interfaces (APIs).

Satish Manohar Talim
3 min readMay 9, 2016

When you register, you receive a free 30-day trial for all Watson services. After 30 days, free use of Watson beta and experimental services continues. All other Watson services, such as the generally available “Personality Insights” and “Tradeoff Analytics” services, offer a specified number of free API calls on a monthly or daily basis. Pricing varies for each service after you exceed the specified number of free calls.

Once you have registered for Bluemix, you are ready to begin creating your first Watson services application!

Getting service credentials in Bluemix

The above video shows how to get started programming with the Watson Developer Cloud services by obtaining service credentials on Bluemix.net.

For this article, we shall select the “Text to Speech” service.

Note: Service credentials (“username” and “password”) are different from your Bluemix account username and password.

IBM Watson Developer Cloud services support two typical programming models for HTTP communications:

  • Relaying requests via a proxy requires the client and service to exchange all data via a server-side proxy application that resides in Bluemix.
  • Direct interaction with a service lets the client communicate directly with the service.

We shall be using the “Direct interaction with a service” model in our program “gowatson.go”.

gowatson.go

We shall write a Go program called “gowatson.go” which will convert written text to natural-sounding speech.

I have created a folder “gowatson” which will hold the Go source code “gowatson.go”.

C:\go_projects\go\src\github.com\SatishTalim\gowatson

We shall be running our program at the command prompt in the folder “gowatson” as “go run gowatson.go”.

Here’s the complete Go code for “gowatson.go”.

gowatson.go

Let’s understand our code:

The endpoint URL is:

https://stream.watsonplatform.net/text-to-speech/api/v1

To synthesize text to speech, you call the service’s “synthesize” method to pass input text to the service, which returns spoken audio in response. The GET “synthesize” method accepts the text to be synthesized via its required “text” query parameter. Use this version of the method for simple text that is easily accommodated on the URL.

The “synthesize” method has the following parameters:

  • An optional parameter “accept” that has one of these values — “audio/ogg;codecs=opus” (the default), “audio/wav” or “audio/flac”.
  • An optional parameter “voice” in which the text is to be spoken in the audio. The various values are available here.

The HTTP GET version of the method accepts input text specified by the “text” query parameter. You specify the input as plain text or as SSML, both of which must be URL-encoded. Speech Synthesis Markup Language (SSML) is an XML-based markup language that provides annotations of text for speech synthesis applications.

SetBasicAuth” sets the request’s Authorization header to use HTTP Basic Authentication with the provided username and password.

After you run the program, a file “talk.wav” should be downloaded in the folder from which you ran the program.

Have fun!

--

--

Satish Manohar Talim

Senior Technical Evangelist at GoProducts Engineering India LLP, Co-Organizer GopherConIndia. Director JoshSoftware and Maybole Technologies. #golang hobbyist