Spaces:
Sleeping
Sleeping
File size: 5,168 Bytes
6de3e11 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>OdiaGenAI Speech Recognition</title>
<script type="text/javascript" src="/static/record.js"></script>
<link href="/static/index.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<div id="header">
<h1>OdiaGenAI Speech Recognition</h1>
</div>
<div id="content">
<div>
<a id="upload" onclick="uploadAudioFile()" class="file">select audio file</a>
<a id="play_btn" onclick="uploadRecordAudio()" class="file">predict audio file</a>
<audio controls autoplay></audio>
<img id="record_btn" onclick="record()" src="/static/record.png" alt="record"/>
</div>
<div id="result">
<label for="result_p"></label><textarea id="result_p"></textarea>
</div>
<!-- <div id="llm_result">
<a id="llm_predict" onclick="uploadAudioFile()" class="file">generate text</a>
<label for="result_llm"></label><textarea id="result_llm"></textarea>
</div> -->
</div>
<script>
let is_recording = false;
let is_playing = false;
let host = location.origin;
let recorder;
let audio = document.querySelector('audio');
let textarea = document.getElementById('result_p')
function record() {
if (is_recording) {
is_recording = false;
stopRecording()
document.getElementById('record_btn').src = '/static/record.png'
startPlay();
stopPlay();
} else {
is_recording = true;
startRecording()
document.getElementById('record_btn').src = '/static/recording.gif'
}
}
function play() {
if (is_playing) {
is_playing = false;
stopPlay()
document.getElementById('play_btn').innerText = 'play audio'
} else {
is_playing = true;
startPlay()
document.getElementById('play_btn').innerText = 'Stop play'
}
}
function startRecording() {
HZRecorder.get(function (rec) {
recorder = rec;
recorder.start();
});
}
function stopRecording() {
recorder.stop();
}
function startPlay() {
recorder.play(audio);
}
function stopPlay() {
audio.pause();
}
function cancelAudio() {
recorder.stop();
recorder.clear();
}
function uploadRecordAudio() {
recorder.upload(location.origin + "/recognition", function (state, e) {
switch (state) {
case 'uploading':
const percentComplete = Math.round(e.loaded * 100 / e.total) + '%';
console.log(percentComplete);
break;
case 'ok':
console.log(e.target.responseText)
document.getElementById('result_p').innerHTML = e.target.responseText
break;
case 'error':
alert("upload failed");
break;
case 'cancel':
alert("upload canceled");
break;
}
});
}
//
function uploadAudioFile() {
const input = document.createElement("input");
input.type = "file";
input.accept = "audio/*,video/*";
input.click();
input.onchange = function () {
const file = input.files[0];
console.log(file)
audio.src = window.URL.createObjectURL(file);
stopPlay();
upload_file(host + "/recognition", file, function (state, e) {
switch (state) {
case 'uploading':
const percentComplete = Math.round(e.loaded * 100 / e.total) + '%';
console.log(percentComplete);
break;
case 'ok':
console.log(e.target.responseText)
textarea.innerText = e.target.responseText
break;
case 'error':
alert("upload failed");
break;
case 'cancel':
alert("upload canceled");
break;
}
});
}
}
//
upload_file = function (url, file, callback) {
const fd = new FormData();
//
fd.append("audio", file);
const xhr = new XMLHttpRequest();
xhr.timeout = 60000
if (callback) {
xhr.upload.addEventListener("progress", function (e) {
callback('uploading', e);
}, false);
xhr.addEventListener("load", function (e) {
callback('ok', e);
}, false);
xhr.addEventListener("error", function (e) {
callback('error', e);
}, false);
xhr.addEventListener("abort", function (e) {
callback('cancel', e);
}, false);
}
xhr.open("POST", url);
xhr.send(fd);
}
</script>
</body>
</html> |