source: main/trunk/model-interfaces-dev/atea/js/asr/asr-controller.js@ 35250

Last change on this file since 35250 was 35250, checked in by davidb, 3 years ago

Implement support for new proxy error responses
Revert to sending audio in batches, with improved fetch usage

File size: 4.7 KB
Line 
1// The controller for asr.xsl
2// @ts-nocheck
3
4/**
5 * The name of the file input that audio files can be uploaded to.
6 */
7const FILE_UPLOAD_INPUT_NAME = "#fileUpload";
8
9/**
10 * The name of the container that holds the progress display for the audio upload.
11 */
12const FILE_UPLOAD_PROGRESS_CONTAINER_NAME = "#prgFileUploadContainer";
13
14/**
15 * The name of the button that triggers the audio upload
16 */
17const FILE_UPLOAD_BUTTON_NAME = "#btnFileUpload";
18
19/**
20 * @callback loadScriptCallback
21 * @param {Event} ev The event
22 * @returns {void}
23 */
24
25/**
26 * Loads a remote script.
27 * Found at https://stackoverflow.com/questions/950087/how-do-i-include-a-javascript-file-in-another-javascript-file
28 *
29 * @param {string} url The URL from which to load the script.
30 * @param {loadScriptCallback} callback Callback performed when the script has been loaded.
31 */
32function loadScript(url, callback = () => {})
33{
34 // Append the script to the body element
35 var body = document.body;
36 var script = document.createElement('script');
37 script.type = 'text/javascript';
38 script.src = url;
39
40 script.onload = callback;
41
42 body.appendChild(script);
43}
44
45loadScript("./interfaces/atea/js/asr/TranscribeService.js");
46
47async function doAudioUpload()
48{
49 const files = $(FILE_UPLOAD_INPUT_NAME)[0].files;
50 const transcribeService = new TranscribeService();
51
52 $(FILE_UPLOAD_PROGRESS_CONTAINER_NAME).removeClass("asr-hidden");
53
54 // Remove old transcriptions
55 const transcriptionsList = document.querySelector("#transcriptionsList");
56 if (transcriptionsList == null)
57 {
58 console.error("Cannot find transcriptions list");
59 return;
60 }
61
62 while (transcriptionsList.lastChild) {
63 transcriptionsList.removeChild(transcriptionsList.lastChild);
64 }
65
66 try
67 {
68 const transcriptions = await transcribeService.transcribeFiles(files);
69 for (const t of transcriptions)
70 {
71 if (!t.success) {
72 insertError(t.file_name, t.log);
73 }
74 else {
75 insertTranscription(t);
76 }
77 }
78 }
79 catch (e)
80 {
81 insertError("all", e.statusMessage);
82 }
83
84 $(FILE_UPLOAD_PROGRESS_CONTAINER_NAME).addClass("asr-hidden");
85}
86
87// Adapted from https://developer.mozilla.org/en-US/docs/Web/HTML/Element/template
88const transcriptionTemplate = document.querySelector('#transcriptionTemplate');
89const metadataTemplate = document.querySelector("#metadataTemplate");
90
91/**
92 * Inserts a transcription object into the DOM.
93 *
94 * @param {TranscriptionModel} transcription The transcription to insert.
95 */
96function insertTranscription(transcription)
97{
98 // Get the transcription list and template
99 let list = document.querySelector("#transcriptionsList");
100
101 // Insert a new transcription row
102 let clone = transcriptionTemplate.content.firstElementChild.cloneNode(true);
103 let spans = clone.querySelectorAll("span");
104 spans[0].textContent = transcription.file_name;
105 spans[1].textContent = transcription.transcription;
106
107 // Get the metadata list and template
108 let metadataList = clone.querySelector("#metadataList");
109
110 if (metadataList == null || metadataTemplate == null)
111 {
112 console.error("Could not find metadata list and/or template.");
113 return;
114 }
115
116 // Insert metadata rows
117 for (let metadata of transcription.metadata)
118 {
119 let metadataClone = metadataTemplate.content.firstElementChild.cloneNode(true);
120 let metadataSpans = metadataClone.querySelectorAll("td");
121
122 metadataSpans[0].textContent = metadata.char;
123 metadataSpans[1].textContent = metadata.start_time;
124
125 metadataList.appendChild(metadataClone);
126 }
127
128 list.appendChild(clone);
129}
130
131const errorTemplate = document.querySelector("#errorTemplate");
132
133/**
134 * Inserts a transcription error into the DOM.
135 *
136 * @param {String} fileName
137 * @param {String | undefined} statusMessage
138 */
139function insertError(fileName, statusMessage)
140{
141 // Get the transcription list and template
142 let list = document.querySelector("#transcriptionsList");
143
144 if (list == null || errorTemplate == null)
145 {
146 console.error("Could not find transcription list and/or error template");
147 return;
148 }
149
150 // Insert a new transcription row
151 let clone = errorTemplate.content.firstElementChild.cloneNode(true);
152 let spans = clone.querySelectorAll("span");
153 spans[0].textContent = statusMessage;
154 spans[1].textContent = fileName;
155
156 list.appendChild(clone);
157}
158
159$(FILE_UPLOAD_INPUT_NAME).on("input", toggleUploadButtonState);
160
161function toggleUploadButtonState(e)
162{
163 if (e.target.files.length <= 0) {
164 $(FILE_UPLOAD_BUTTON_NAME).prop("disabled", true);
165 }
166 else {
167 $(FILE_UPLOAD_BUTTON_NAME).prop("disabled", false);
168 }
169}
Note: See TracBrowser for help on using the repository browser.