forked from oobabooga/text-generation-webui
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Whisper stt overhaul js (oobabooga#6194)
--------- Co-authored-by: RandoInternetPreson <[email protected]>
- Loading branch information
1 parent
33cc62d
commit 4f04c6b
Showing
2 changed files
with
142 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,86 @@ | ||
var recButton = document.getElementsByClassName("record-button")[0].cloneNode(true); | ||
console.log("Whisper STT script loaded"); | ||
|
||
let mediaRecorder; | ||
let audioChunks = []; | ||
let isRecording = false; | ||
|
||
window.startStopRecording = function() { | ||
if (!navigator.mediaDevices || !navigator.mediaDevices.getUserMedia) { | ||
console.error("getUserMedia not supported on your browser!"); | ||
return; | ||
} | ||
|
||
if (isRecording == false) { | ||
//console.log("Start recording function called"); | ||
navigator.mediaDevices.getUserMedia({ audio: true }) | ||
.then(stream => { | ||
//console.log("Got audio stream"); | ||
mediaRecorder = new MediaRecorder(stream); | ||
audioChunks = []; // Reset audio chunks | ||
mediaRecorder.start(); | ||
//console.log("MediaRecorder started"); | ||
recButton.icon; | ||
recordButton.innerHTML = recButton.innerHTML = "Stop"; | ||
isRecording = true; | ||
|
||
mediaRecorder.addEventListener("dataavailable", event => { | ||
//console.log("Data available event, data size: ", event.data.size); | ||
audioChunks.push(event.data); | ||
}); | ||
|
||
mediaRecorder.addEventListener("stop", () => { | ||
//console.log("MediaRecorder stopped"); | ||
if (audioChunks.length > 0) { | ||
const audioBlob = new Blob(audioChunks, { type: "audio/webm" }); | ||
//console.log("Audio blob created, size: ", audioBlob.size); | ||
const reader = new FileReader(); | ||
reader.readAsDataURL(audioBlob); | ||
reader.onloadend = function() { | ||
const base64data = reader.result; | ||
//console.log("Audio converted to base64, length: ", base64data.length); | ||
|
||
const audioBase64Input = document.querySelector("#audio-base64 textarea"); | ||
if (audioBase64Input) { | ||
audioBase64Input.value = base64data; | ||
audioBase64Input.dispatchEvent(new Event("input", { bubbles: true })); | ||
audioBase64Input.dispatchEvent(new Event("change", { bubbles: true })); | ||
//console.log("Updated textarea with base64 data"); | ||
} else { | ||
console.error("Could not find audio-base64 textarea"); | ||
} | ||
}; | ||
} else { | ||
console.error("No audio data recorded for Whisper"); | ||
} | ||
}); | ||
}); | ||
} else { | ||
//console.log("Stopping MediaRecorder"); | ||
recordButton.innerHTML = recButton.innerHTML = "Rec."; | ||
isRecording = false; | ||
mediaRecorder.stop(); | ||
} | ||
}; | ||
|
||
const recordButton = gradioApp().querySelector("#record-button"); | ||
recordButton.addEventListener("click", window.startStopRecording); | ||
|
||
|
||
function gradioApp() { | ||
const elems = document.getElementsByTagName("gradio-app"); | ||
const gradioShadowRoot = elems.length == 0 ? null : elems[0].shadowRoot; | ||
return gradioShadowRoot ? gradioShadowRoot : document; | ||
} | ||
|
||
|
||
// extra rec button next to generate button | ||
var recButton = recordButton.cloneNode(true); | ||
var generate_button = document.getElementById("Generate"); | ||
generate_button.insertAdjacentElement("afterend", recButton); | ||
|
||
recButton.style.setProperty("margin-left", "-10px"); | ||
recButton.innerText = "Rec."; | ||
|
||
recButton.innerHTML = "Rec."; | ||
|
||
recButton.addEventListener("click", function() { | ||
var originalRecordButton = document.getElementsByClassName("record-button")[1]; | ||
originalRecordButton.click(); | ||
|
||
var stopRecordButtons = document.getElementsByClassName("stop-button"); | ||
if (stopRecordButtons.length > 1) generate_button.parentElement.removeChild(stopRecordButtons[0]); | ||
var stopRecordButton = document.getElementsByClassName("stop-button")[0]; | ||
generate_button.insertAdjacentElement("afterend", stopRecordButton); | ||
|
||
//stopRecordButton.style.setProperty("margin-left", "-10px"); | ||
stopRecordButton.style.setProperty("padding-right", "10px"); | ||
recButton.style.display = "none"; | ||
|
||
stopRecordButton.addEventListener("click", function() { | ||
recButton.style.display = "flex"; | ||
}); | ||
}); | ||
recordButton.click(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters