Spaces:
Running
Running
Update index.html
Browse files- index.html +106 -62
index.html
CHANGED
|
@@ -1,63 +1,107 @@
|
|
| 1 |
-
<!DOCTYPE html>
|
| 2 |
-
<html lang="en">
|
| 3 |
-
<head>
|
| 4 |
-
<meta charset="UTF-8">
|
| 5 |
-
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
| 6 |
-
<title>Voice
|
| 7 |
-
<script src="https://cdn.jsdelivr.net/npm/onnxruntime-web/dist/ort.
|
| 8 |
-
<script src="https://cdn.
|
| 9 |
-
<script>
|
| 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 |
</html>
|
|
|
|
| 1 |
+
<!DOCTYPE html>
|
| 2 |
+
<html lang="en">
|
| 3 |
+
<head>
|
| 4 |
+
<meta charset="UTF-8">
|
| 5 |
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
| 6 |
+
<title>Voice Chat Bot</title>
|
| 7 |
+
<script src="https://cdn.jsdelivr.net/npm/onnxruntime-web/dist/ort.js"></script>
|
| 8 |
+
<script src="https://cdn.jsdelivr.net/npm/@ricky0123/[email protected]/dist/bundle.min.js"></script>
|
| 9 |
+
<script src="https://cdn.jsdelivr.net/npm/@xenova/[email protected]"></script>
|
| 10 |
+
<style>
|
| 11 |
+
body { font-family: Arial, sans-serif; max-width: 800px; margin: 0 auto; padding: 20px; }
|
| 12 |
+
button { font-size: 18px; padding: 10px 20px; margin: 10px 0; }
|
| 13 |
+
#conversation { border: 1px solid #ccc; padding: 10px; height: 300px; overflow-y: scroll; margin-bottom: 10px; }
|
| 14 |
+
</style>
|
| 15 |
+
</head>
|
| 16 |
+
<body>
|
| 17 |
+
<h1>Voice Chat Bot</h1>
|
| 18 |
+
<div id="conversation"></div>
|
| 19 |
+
<button id="startButton">Start Listening</button>
|
| 20 |
+
<button id="stopButton" disabled>Stop Listening</button>
|
| 21 |
+
|
| 22 |
+
<script type="module">
|
| 23 |
+
import { pipeline } from 'https://cdn.jsdelivr.net/npm/@xenova/[email protected]';
|
| 24 |
+
|
| 25 |
+
const conversationDiv = document.getElementById('conversation');
|
| 26 |
+
const startButton = document.getElementById('startButton');
|
| 27 |
+
const stopButton = document.getElementById('stopButton');
|
| 28 |
+
|
| 29 |
+
let myvad;
|
| 30 |
+
let sttPipeline;
|
| 31 |
+
let ttsPipeline;
|
| 32 |
+
|
| 33 |
+
async function initializePipelines() {
|
| 34 |
+
sttPipeline = await pipeline('automatic-speech-recognition', 'Xenova/whisper-tiny.en');
|
| 35 |
+
ttsPipeline = await pipeline('text-to-speech', 'Xenova/speecht5_tts');
|
| 36 |
+
}
|
| 37 |
+
|
| 38 |
+
async function processSpeech(audio) {
|
| 39 |
+
try {
|
| 40 |
+
const transcription = await sttPipeline(audio);
|
| 41 |
+
addMessage('User', transcription.text);
|
| 42 |
+
|
| 43 |
+
// Placeholder for LLM response
|
| 44 |
+
const botResponse = `I heard you say: "${transcription.text}". This is a placeholder response.`;
|
| 45 |
+
addMessage('Bot', botResponse);
|
| 46 |
+
|
| 47 |
+
const speechOutput = await ttsPipeline(botResponse);
|
| 48 |
+
playAudio(speechOutput.audio);
|
| 49 |
+
} catch (error) {
|
| 50 |
+
console.error('Error processing speech:', error);
|
| 51 |
+
}
|
| 52 |
+
}
|
| 53 |
+
|
| 54 |
+
function addMessage(sender, message) {
|
| 55 |
+
const messageElement = document.createElement('p');
|
| 56 |
+
messageElement.innerHTML = `<strong>${sender}:</strong> ${message}`;
|
| 57 |
+
conversationDiv.appendChild(messageElement);
|
| 58 |
+
conversationDiv.scrollTop = conversationDiv.scrollHeight;
|
| 59 |
+
}
|
| 60 |
+
|
| 61 |
+
function playAudio(audioArray) {
|
| 62 |
+
const audioContext = new (window.AudioContext || window.webkitAudioContext)();
|
| 63 |
+
const audioBuffer = audioContext.createBuffer(1, audioArray.length, 16000);
|
| 64 |
+
const channelData = audioBuffer.getChannelData(0);
|
| 65 |
+
channelData.set(audioArray);
|
| 66 |
+
|
| 67 |
+
const source = audioContext.createBufferSource();
|
| 68 |
+
source.buffer = audioBuffer;
|
| 69 |
+
source.connect(audioContext.destination);
|
| 70 |
+
source.start();
|
| 71 |
+
}
|
| 72 |
+
|
| 73 |
+
async function startListening() {
|
| 74 |
+
try {
|
| 75 |
+
myvad = await vad.MicVAD.new({
|
| 76 |
+
onSpeechEnd: (audio) => {
|
| 77 |
+
processSpeech(audio);
|
| 78 |
+
}
|
| 79 |
+
});
|
| 80 |
+
await myvad.start();
|
| 81 |
+
startButton.disabled = true;
|
| 82 |
+
stopButton.disabled = false;
|
| 83 |
+
addMessage('System', 'Listening...');
|
| 84 |
+
} catch (error) {
|
| 85 |
+
console.error('Error starting VAD:', error);
|
| 86 |
+
}
|
| 87 |
+
}
|
| 88 |
+
|
| 89 |
+
function stopListening() {
|
| 90 |
+
if (myvad) {
|
| 91 |
+
myvad.stop();
|
| 92 |
+
startButton.disabled = false;
|
| 93 |
+
stopButton.disabled = true;
|
| 94 |
+
addMessage('System', 'Stopped listening.');
|
| 95 |
+
}
|
| 96 |
+
}
|
| 97 |
+
|
| 98 |
+
startButton.addEventListener('click', startListening);
|
| 99 |
+
stopButton.addEventListener('click', stopListening);
|
| 100 |
+
|
| 101 |
+
// Initialize pipelines when the page loads
|
| 102 |
+
initializePipelines().then(() => {
|
| 103 |
+
addMessage('System', 'Voice Chat Bot initialized. Click "Start Listening" to begin.');
|
| 104 |
+
});
|
| 105 |
+
</script>
|
| 106 |
+
</body>
|
| 107 |
</html>
|