|
| 1 | +<!DOCTYPE html> |
| 2 | +<html lang="en"> |
| 3 | +<head> |
| 4 | + <meta charset="UTF-8"> |
| 5 | + <title>Watson Speech to Text client example - custom model</title> |
| 6 | +</head> |
| 7 | +<body> |
| 8 | + |
| 9 | +<section> |
| 10 | + <h2>Transcribe from Microphone</h2> |
| 11 | + <p>Model: <select id="model"></select> <small><i>Choose the model to be used for the speech recognition.</i></small></p> |
| 12 | + <button id="button">Start Microphone Transcription</button> |
| 13 | + <button id="stop">Stop</button> |
| 14 | + |
| 15 | + <h2>Output:</h2> |
| 16 | + <div id="output">--</div> |
| 17 | +</section> |
| 18 | + |
| 19 | +<script src="watson-speech.js"></script> |
| 20 | +<!-- window.fetch pollyfill for IE/Edge & Older Chrome/FireFox --> |
| 21 | +<script src="bower_components/fetch/fetch.js"></script> |
| 22 | + |
| 23 | +<h2>Code for this demo:</h2> |
| 24 | + |
| 25 | +<pre><code><script style="display: block;"> |
| 26 | +function getTtsToken() { |
| 27 | + return fetch('/api/speech-to-text/token') |
| 28 | + .then(function(response) { |
| 29 | + return response.text(); |
| 30 | + }); |
| 31 | +} |
| 32 | + |
| 33 | +// fetch the models and populate the dropdown |
| 34 | +getTtsToken() |
| 35 | + .then(function (token) { |
| 36 | + return WatsonSpeech.SpeechToText.getModels({token: token}); |
| 37 | + }).then(function (models) { |
| 38 | + |
| 39 | + var dropdown = document.querySelector('#model'); |
| 40 | + models.forEach(function (m) { |
| 41 | + var o = document.createElement('option'); |
| 42 | + o.value = m.name; |
| 43 | + o.textContent = m.name; |
| 44 | + if (m.name == 'en-US_BroadbandModel') { |
| 45 | + o.selected = true; |
| 46 | + } |
| 47 | + dropdown.appendChild(o); |
| 48 | + }); |
| 49 | + |
| 50 | + }).catch(console.error.bind(console)); |
| 51 | + |
| 52 | +// recognize the text using the chosen model |
| 53 | +document.querySelector('#button').onclick = function () { |
| 54 | + getTtsToken().then(function (token) { |
| 55 | + |
| 56 | + var stream = WatsonSpeech.SpeechToText.recognizeMicrophone({ |
| 57 | + token: token, |
| 58 | + model: document.querySelector('#model').value, |
| 59 | + outputElement: '#output' // CSS selector or DOM Element |
| 60 | + }); |
| 61 | + |
| 62 | + stream.on('error', function(err) { |
| 63 | + console.log(err); |
| 64 | + }); |
| 65 | + |
| 66 | + document.querySelector('#stop').onclick = stream.stop.bind(stream); |
| 67 | + |
| 68 | + }).catch(function(error) { |
| 69 | + console.log(error); |
| 70 | + }); |
| 71 | +}; |
| 72 | + |
| 73 | +</script></code></pre> |
| 74 | + |
| 75 | +</body> |
| 76 | +</html> |
0 commit comments