Spaces:
Running
on
Zero
Running
on
Zero
<html> | |
<head> | |
<title>Whisper, Vicuna, & TTS Demo</title> | |
</head> | |
<body> | |
<div id="gradio-container"></div> | |
<script src="/js/index.js"></script> | |
<script> | |
window.addEventListener('DOMContentLoaded', (event) => { | |
let recorder; | |
let chunks =[]; | |
let audioComponent; | |
let recognition; // Speech recognition object | |
window.addEventListener('DOMContentLoaded', (event) => { | |
audioComponent = document.getElementById("mic_audio"); | |
// Initialize speech recognition | |
recognition = new webkitSpeechRecognition() || new SpeechRecognition(); // Cross-browser compatibility | |
recognition.continuous = true; // Keep listening | |
recognition.interimResults = false; // Don't return partial results | |
recognition.onresult = event => { | |
const transcript = event.results.transcript.toLowerCase(); // Get the recognized text | |
console.log("Recognized:", transcript); // For debugging | |
if (transcript.includes("start recording")) { // Your "start" command | |
startRecording(); | |
} else if (transcript.includes("stop recording")) { // Your "stop" command | |
stopRecording(); | |
} | |
}; | |
recognition.onerror = event => { | |
console.error("Speech recognition error:", event.error); | |
}; | |
recognition.start(); // Start listening for voice commands immediately | |
}); | |
function startRecording() { | |
navigator.mediaDevices.getUserMedia({ audio: true }) | |
.then(stream => { | |
recorder = new MediaRecorder(stream); | |
recorder.ondataavailable = e => { | |
if (e.data.size > 0) { | |
chunks.push(e.data); | |
} | |
}; | |
recorder.start(); | |
console.log("Recording started."); | |
}) | |
.catch(err => console.error('Error accessing microphone:', err)); | |
} | |
function stopRecording() { | |
if (recorder && recorder.state === "recording") { | |
recorder.stop(); | |
recorder.onstop = async () => { | |
const blob = new Blob(chunks, { type: 'audio/wav' }); // Or appropriate type | |
const file = new File([blob], "recording.wav", { type: 'audio/wav' }); // Create a File object | |
const dataTransfer = new DataTransfer(); | |
dataTransfer.items.add(file); | |
audioComponent.files = dataTransfer.files; // Set the files property | |
audioComponent.dispatchEvent(new Event('change')); // Trigger change to send data | |
chunks =; | |
console.log("Recording stopped and data sent."); | |
}; | |
} | |
} | |
}); | |
</script> | |
</body> | |
</html> |