mercredi 8 avril 2015

IBM Watson QA + Speech Recognition + Speech Synthesis = A Conversation With Your Computer

" Back in November I released a demo application here on my blog showing the IBM Watson QA Service for cognitive/natural language computing connected to the Web Speech API in Google Chrome to have real conversational interaction with a web application.  It’s a nice demo, but it always drove me nuts that it only worked in Chrome.  Last month the IBM Watson team released 5 new services, and guess what… Speech Recognition and Speech Synthesis are included!
These two services enable you to quickly add Text-To-Speech or Speech-To-Text capability to any application.  What’s a better way to show them off than by updating my existing app to leverage the new speech services?

So here it is: watsonhealthqa.mybluemix.net!

By leveraging the Watson services it can now run in any browser that supports getUserMedia (for speech recognition) and HTML5 <Audio> (for speech playback).

(Full source code available at the bottom of this post)

You can check out a video of it in action below:





If your browser doesn’t support the getUserMedia API or HTML5 <Audio>, then your mileage may vary.  You can check where these features are supported with these links: <Audio>getUserMedia
Warning: This is targeting desktop browsers – HTML5 Audio is a mess on mobile devices due to limited codec support and immature APIs.

So how does this all work?

Just like the QA service, the new Text To Speech and Speech To Text services are now available in IBM Bluemix, so you can create a new application that leverages any of these services, or you can add them to any existing application.
I simply added the Text To Speech and Speech To Text services to my existing Healthcare QA application that runs on Bluemix:

IBM Bluemix Dashboard


These services are available via a REST API. Once you’ve added them to your application, you can consume them easily within any of your applications.
I updated the code from my previous example in 2 ways: 1) take advantage of the Watson Node.js Wrapper that makes interacting with Watson a lot easier and 2) to take advantage of these new services services.

Watson Node.js Wrapper

Using the Watson Node.js Wrapper, you can now easily instantiate Watson services in a single line of code.  For example:

1.var watson = require('watson-developer-cloud');
2.var question_and_answer_healthcare = watson.question_and_answer(QA_CREDENTIALS);
3.var speechToText = watson.speech_to_text(STT_CREDENTIALS);
The credentials come from your environment configuration, then you just create instances of whichever services that you want to consume.

QA Service

The code for consuming a service is now much simpler than the previous version.  When we want to submit a question to the Watson QA service, you can now simply call the “ask” method on the QA service instance.
Below is my server-side code from app.js that accepts a POST submission from the browser, delegates the question to Watson, and takes the result and renders HTML using a Jade template. See the Getting Started Guide for the Watson QA Service to learn more about the wrappers for Node or Java.


01.// Handle the form POST containing the question
02.app.post('/ask', function(req, res){
03. 
04.// delegate to Watson
05.question_and_answer_healthcare.ask({ text: req.body.questionText}, function (err, response) {
06.if (err)
07.console.log('error:', err);
08.else {
09.var response = extend({ 'answers': response[0] },req.body);
10. 
11.// render the template to HTML and send it to the browser
12.return res.render('response', response);
13.}
14.});
15.});
Compare this to the previous version, and you’ll quickly see that it is much simpler.


Speech Synthesis

At this point, we already have a functional service that can take natural language text, submit it to Watson,  and return a search result as text.  The next logical step for me was to add speech synthesis using the Watson Text To Speech Service (TTS).  Again, the Watson Node Wrapper and Watson’s REST services make this task very simple.  On the client side you just need to set the src of an <audio> instance to the URL for the TTS service:
1
<audio controls="" autoplay="" src="/synthesize?text=The text that should generate the audio goes here"></audio>
On the server you just need to synthesize the audio from the data in the URL query string.  Here’s an example how to invoke the text to speech service directly from the Watson TTS sample app:
01.var textToSpeech = new watson.text_to_speech(credentials);
02. 
03.// handle get requests
04.app.get('/synthesize', function(req, res) {
05. 
06.// make the request to Watson to synthesize the audio file from the query text
07.var transcript = textToSpeech.synthesize(req.query);
08. 
09.// set content-disposition header if downloading the
10.// file instead of playing directly in the browser
11.transcript.on('response', function(response) {
12.console.log(response.headers);
13.if (req.query.download) {
14.response.headers['content-disposition'] = 'attachment; filename=transcript.ogg';
15.}
16.});
17. 
18.// pipe results back to the browser as they come in from Watson
19.transcript.pipe(res);
20.});
The Watson TTS service supports .ogg and .wav file formats.  I modified this sample slightly to return .ogg files for Chrome and Firefox, and .wav files for other browsers.  On the client side, these are played using the HTML5 <audio> tag. You can see my modifications in the git repository.

Speech Recognition

Now that we’re able to process natural language and generate speech, that last part of the solution is to recognize spoken input and turn it into text.  The Watson Speech To Text (STT) service handles this for us.  Just like the TTS service, the Speech To Text service also has a sample app, complete with source code to help you get started.
This service uses the browser’s getUserMedia (streaming) API with socket.io on Node to stream the data back to the server with minimal latency. The best part is that you don’t have to setup any of this on your own. Just leverage the code from the sample app. Note: the getUserMedia API isn’t supported everywhere, so be advised.

On the client side you just need to create an instance of the SpeechRecognizer class in JavaScript and handle the result:


01.var recognizer = new SpeechRecognizer({
02.ws: '',
03.model: 'WatsonModel'
04.});
05. 
06.recognizer.onresult = function(data) {
07. 
08.//get the transcript from the service result data
09.var result = data.results[data.results.length-1];
10.var transcript = result.alternatives[0].transcript;
11. 
12.// do something with the transcript
13.search( transcript, result.final );
14.}
On the server, you need to create an instance of the Watson Speech To Text service, and setup handlers for the post request to receive the audio stream.


01.// create an instance of the speech to text service
02.var speechToText = watson.speech_to_text(STT_CREDENTIALS);
03. 
04.// Handle audio stream processing for speech recognition
05.app.post('/', function(req, res) {
06.var audio;
07. 
08.if(req.body.url && req.body.url.indexOf('audio/') === 0) {
09.// sample audio stream
10.audio = fs.createReadStream(__dirname + '/../public/' + req.body.url);
11.} else {
12.// malformed url
13.return res.status(500).json({ error: 'Malformed URL' });
14.}
15. 
16.// use Watson to generate a text transcript from the audio stream
17.speechToText.recognize({audio: audio, content_type: 'audio/l16; rate=44100'}, function(err, transcript) {
18.if (err)
19.return res.status(500).json({ error: err });
20.else
21.return res.json(transcript);
22.});
23.});

Source Code

You can interact with a live instance of this application at watsonhealthqa.mybluemix.net, and complete client and server side code is available at github.com/triceam/IBMWatson-QA-Speech.
Just setup your Bluemix app, clone the sample code, run NPM install and deploy your app to Bluemix using the Cloud Foundry CLI.


Release on the 04.04.2015
Written by Andrew Trice. More on Andrew here : http://java.dzone.com/users/triceam

Link to the article here : http://java.dzone.com/articles/ibm-watson-qa-speech 

Aucun commentaire:

Enregistrer un commentaire