Spaces:
Running
Running
Update script.js
Browse files
script.js
CHANGED
|
@@ -1,105 +1,122 @@
|
|
| 1 |
-
let transcriber;
|
| 2 |
-
let
|
| 3 |
let mediaRecorder;
|
| 4 |
let audioChunks = [];
|
| 5 |
|
| 6 |
-
//
|
| 7 |
-
async function
|
| 8 |
-
document.getElementById("modelStatus").innerText = "β³ Loading Whisper model...";
|
| 9 |
try {
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
);
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
]);
|
| 18 |
-
|
| 19 |
-
document.getElementById("modelStatus").innerText = "β
Model Loaded!";
|
| 20 |
-
document.getElementById("recordButton").disabled = false;
|
| 21 |
-
document.getElementById("recordButton").innerText = "π€ Start Recording";
|
| 22 |
-
document.getElementById("testModel").disabled = false;
|
| 23 |
} catch (error) {
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
console.error("Error loading model:", error);
|
| 27 |
}
|
| 28 |
}
|
| 29 |
|
| 30 |
-
//
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
}
|
| 42 |
}
|
| 43 |
|
| 44 |
-
//
|
| 45 |
async function startRecording() {
|
| 46 |
-
|
| 47 |
-
|
|
|
|
|
|
|
| 48 |
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
|
|
|
|
|
|
|
|
|
| 53 |
mediaRecorder.onstop = async () => {
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
reader.readAsDataURL(audioBlob);
|
| 73 |
};
|
| 74 |
-
|
| 75 |
-
mediaRecorder.start();
|
| 76 |
-
audioChunks = [];
|
| 77 |
-
recording = true;
|
| 78 |
-
document.getElementById("recordButton").innerText = "βΉ Stop Recording";
|
| 79 |
-
document.getElementById("status").innerText = "ποΈ Recording...";
|
| 80 |
}
|
| 81 |
|
| 82 |
-
//
|
| 83 |
-
function
|
| 84 |
-
|
| 85 |
-
mediaRecorder.stop();
|
| 86 |
-
recording = false;
|
| 87 |
-
document.getElementById("recordButton").innerText = "π€ Start Recording";
|
| 88 |
-
document.getElementById("status").innerText = "β³ Processing audio...";
|
| 89 |
-
}
|
| 90 |
-
}
|
| 91 |
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
}
|
| 99 |
-
}
|
| 100 |
-
|
| 101 |
-
document.getElementById("testModel").addEventListener("click", testModel);
|
| 102 |
|
| 103 |
-
//
|
| 104 |
-
|
|
|
|
|
|
|
|
|
|
| 105 |
|
|
|
|
|
|
|
|
|
| 1 |
+
let transcriber, generator;
|
| 2 |
+
let isRecording = false;
|
| 3 |
let mediaRecorder;
|
| 4 |
let audioChunks = [];
|
| 5 |
|
| 6 |
+
// Initialize models
|
| 7 |
+
async function initializeModels() {
|
|
|
|
| 8 |
try {
|
| 9 |
+
console.log("Loading transcriber model...");
|
| 10 |
+
transcriber = await pipeline('automatic-speech-recognition', 'Xenova/whisper-tiny.en');
|
| 11 |
+
console.log("Transcriber model loaded successfully:", transcriber);
|
| 12 |
+
|
| 13 |
+
console.log("Loading generator model...");
|
| 14 |
+
generator = await pipeline('text2text-generation', 'Xenova/LaMini-Flan-T5-783M');
|
| 15 |
+
console.log("Generator model loaded successfully:", generator);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
} catch (error) {
|
| 17 |
+
console.error("Error loading models:", error);
|
| 18 |
+
displayMessage("Error loading models.", 'bot-message');
|
|
|
|
| 19 |
}
|
| 20 |
}
|
| 21 |
|
| 22 |
+
// Display message in the chat container
|
| 23 |
+
function displayMessage(message, className) {
|
| 24 |
+
const messageElement = document.createElement('div');
|
| 25 |
+
messageElement.className = className;
|
| 26 |
+
messageElement.innerText = message;
|
| 27 |
+
document.getElementById('chat-container').appendChild(messageElement);
|
| 28 |
+
document.getElementById('chat-container').scrollTop = document.getElementById('chat-container').scrollHeight;
|
| 29 |
+
}
|
| 30 |
+
|
| 31 |
+
// Handle user text input
|
| 32 |
+
async function handleUserInput() {
|
| 33 |
+
const userInput = document.getElementById('user-input').value.trim();
|
| 34 |
+
if (!userInput) return;
|
| 35 |
+
|
| 36 |
+
displayMessage("You: " + userInput, 'user-message');
|
| 37 |
+
document.getElementById('user-input').value = "";
|
| 38 |
+
|
| 39 |
+
await generateResponse(userInput);
|
| 40 |
+
}
|
| 41 |
+
|
| 42 |
+
// Toggle recording
|
| 43 |
+
async function toggleRecording() {
|
| 44 |
+
isRecording = !isRecording;
|
| 45 |
+
|
| 46 |
+
if (isRecording) {
|
| 47 |
+
document.getElementById('record-button').innerText = "βΉοΈ"; // Change to Stop icon
|
| 48 |
+
startRecording();
|
| 49 |
+
} else {
|
| 50 |
+
document.getElementById('record-button').innerText = "ποΈ"; // Change to Mic icon
|
| 51 |
+
stopRecording();
|
| 52 |
}
|
| 53 |
}
|
| 54 |
|
| 55 |
+
// Start audio recording
|
| 56 |
async function startRecording() {
|
| 57 |
+
try {
|
| 58 |
+
const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
|
| 59 |
+
mediaRecorder = new MediaRecorder(stream);
|
| 60 |
+
audioChunks = [];
|
| 61 |
|
| 62 |
+
mediaRecorder.ondataavailable = event => audioChunks.push(event.data);
|
| 63 |
+
mediaRecorder.start();
|
| 64 |
+
console.log("Recording started.");
|
| 65 |
+
} catch (error) {
|
| 66 |
+
console.error("Error starting recording:", error);
|
| 67 |
+
displayMessage("Error starting recording.", 'bot-message');
|
| 68 |
+
}
|
| 69 |
+
}
|
| 70 |
|
| 71 |
+
// Stop recording and transcribe
|
| 72 |
+
async function stopRecording() {
|
| 73 |
+
mediaRecorder.stop();
|
| 74 |
mediaRecorder.onstop = async () => {
|
| 75 |
+
const audioBlob = new Blob(audioChunks);
|
| 76 |
+
const url = URL.createObjectURL(audioBlob);
|
| 77 |
+
|
| 78 |
+
try {
|
| 79 |
+
console.log("Transcribing audio...");
|
| 80 |
+
const result = await transcriber(url);
|
| 81 |
+
const transcribedText = result?.text || "Error in transcription.";
|
| 82 |
+
console.log("Transcription result:", transcribedText);
|
| 83 |
+
|
| 84 |
+
// Display transcribed text
|
| 85 |
+
displayMessage("You (voice): " + transcribedText, 'user-message');
|
| 86 |
+
|
| 87 |
+
// Generate response based on transcribed text
|
| 88 |
+
await generateResponse(transcribedText);
|
| 89 |
+
} catch (error) {
|
| 90 |
+
console.error("Error in transcription:", error);
|
| 91 |
+
displayMessage("Error in transcription.", 'bot-message');
|
| 92 |
+
}
|
|
|
|
| 93 |
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
}
|
| 95 |
|
| 96 |
+
// Generate a response using text generation model
|
| 97 |
+
async function generateResponse(inputText) {
|
| 98 |
+
console.log("Generating response for input:", inputText);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 99 |
|
| 100 |
+
try {
|
| 101 |
+
console.log("Input text before sending to generator:", inputText);
|
| 102 |
+
|
| 103 |
+
const result = await generator(inputText, { max_new_tokens: 100 });
|
| 104 |
+
const generatedText = result;
|
| 105 |
+
console.log("Generated response text:", generatedText);
|
| 106 |
+
|
| 107 |
+
displayMessage("Bot: " + generatedText, 'bot-message');
|
| 108 |
+
speakText(generatedText);
|
| 109 |
+
} catch (error) {
|
| 110 |
+
console.error("Error in response generation:", error);
|
| 111 |
+
displayMessage("Error in response generation: " + error.message, 'bot-message');
|
| 112 |
}
|
| 113 |
+
}
|
|
|
|
|
|
|
| 114 |
|
| 115 |
+
// Speak the generated text
|
| 116 |
+
function speakText(text) {
|
| 117 |
+
const utterance = new SpeechSynthesisUtterance(text);
|
| 118 |
+
window.speechSynthesis.speak(utterance);
|
| 119 |
+
}
|
| 120 |
|
| 121 |
+
// Initialize models when the page loads
|
| 122 |
+
window.addEventListener("DOMContentLoaded", initializeModels);
|