From 211da4ec18cc0071c73abba487fb5ea515df114f Mon Sep 17 00:00:00 2001 From: steven1046 Date: Thu, 8 Nov 2018 10:36:36 -0600 Subject: [PATCH 1/2] Change fileupload to just file name; Add a polling function and transcript check --- src/main/webapp/js/main.js | 65 ++++++++++++++++++++++++++++++++++---- 1 file changed, 59 insertions(+), 6 deletions(-) diff --git a/src/main/webapp/js/main.js b/src/main/webapp/js/main.js index 7af55a7..71057b3 100644 --- a/src/main/webapp/js/main.js +++ b/src/main/webapp/js/main.js @@ -13,7 +13,7 @@ $(document).ready(function() { $('#audio_text').text("Uploading audio file..."); var formData = new FormData(); - formData.append('data', file); + formData.append('filename', file.name); $.ajax({ url: upload_url, type: "POST", @@ -25,7 +25,8 @@ $(document).ready(function() { $('#audio').removeClass('hidden'); if(response.id !== null){ // ID from the job comes back. Need to keep polling to determine when the job is done/failed. - deferred.resolve(response.id); + // deferred.resolve(response.id); + deferred.resolve(result.id); } else { deferred.reject(); } @@ -41,11 +42,56 @@ $(document).ready(function() { return deferred; }; - var pollForTranscript = function() { + var pollForTranscript = function(id, num_tries) { + var deferred = $.Deferred(); + var done = false; + if(num_tries > 0) { + num_tries--; + $.ajax({ + url: "rest/speech/check/" + id, + type: "GET", + data: formData, + async: false, + processData: false, + success: function(response){ + // Insert the uploaded audio file in the audio portion. + deferred.resolve(id); + }, + error: function(jqXHR){ + // deferred.reject(jqXHR); + if(num_tries == 0){ + deferred.reject(); + } + else { + return pollForTranscript(id, num_tries-1); + } + } + }); + } + return deferred; + }; + var retrieveTranscript = function(id) { + var deferred = $.deferred(); + $.ajax({ + url: "rest/speech/" + id + "/transcript", + type: "GET", + async: true, + success: function(response){ + if(response.transcript !== null){ + deferred.resolve(response.transcript); + } else { + deferred.reject(); + } + }, + error: function(jqXHR){ + deferred.reject(jqXHR); + } + }); + return deferred; }; - $('#file_browse').on('change', function(){ + $('#file_browse').on('change', function(){ var audio = document.getElementById('audio'); var reader = new FileReader(); reader.onload = function(event) { @@ -53,11 +99,18 @@ $(document).ready(function() { audio.controls = true; }; reader.readAsDataURL(this.files[0]); - uploadFile(this.files[0]).then(function(jobID){ + uploadFile(this.files[0]).then(function(id){ // Poll for updates using the token + pollForTranscript(id).then(function(id){ + // Retrieve the transcript + retrieveTranscript(id).then(function(){ + }).fail(function(){ - + }); + }).fail(function(){ + + }); // Show pills for each word in the transcript of the audio file. }).fail(function(error){ // Display error that no transcription was found. From 70f662d1914ef79eac4bae729dc0176dc5e316dc Mon Sep 17 00:00:00 2001 From: steven1046 Date: Thu, 8 Nov 2018 10:41:55 -0600 Subject: [PATCH 2/2] Show audio element after choosing a file just for visual feedback on demo --- src/main/webapp/css/main.css | 4 ++++ src/main/webapp/js/main.js | 3 ++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/main/webapp/css/main.css b/src/main/webapp/css/main.css index ecd7f96..2f44bc9 100644 --- a/src/main/webapp/css/main.css +++ b/src/main/webapp/css/main.css @@ -44,6 +44,10 @@ p { margin-bottom: 50px; } +#audio { + width: 100%; +} + /* Transcript */ #transcript_section { diff --git a/src/main/webapp/js/main.js b/src/main/webapp/js/main.js index 71057b3..c26db0d 100644 --- a/src/main/webapp/js/main.js +++ b/src/main/webapp/js/main.js @@ -11,6 +11,7 @@ $(document).ready(function() { $('#audio_status').removeClass('hidden'); $('#audio_text').removeClass('hidden'); $('#audio_text').text("Uploading audio file..."); + $('#audio').removeClass('hidden'); var formData = new FormData(); formData.append('filename', file.name); @@ -22,7 +23,7 @@ $(document).ready(function() { processData: false, success: function(response){ // Insert the uploaded audio file in the audio portion. - $('#audio').removeClass('hidden'); + // $('#audio').removeClass('hidden'); if(response.id !== null){ // ID from the job comes back. Need to keep polling to determine when the job is done/failed. // deferred.resolve(response.id);