atlury commited on
Commit
eaeaa29
·
verified ·
1 Parent(s): 86e62f7

Update index.html

Browse files
Files changed (1) hide show
  1. index.html +21 -8
index.html CHANGED
@@ -20,7 +20,10 @@
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');
@@ -31,12 +34,22 @@
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
 
@@ -44,10 +57,11 @@
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
 
@@ -83,12 +97,13 @@
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.');
@@ -99,9 +114,7 @@
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>
 
20
  <button id="stopButton" disabled>Stop Listening</button>
21
 
22
  <script type="module">
23
+ import { pipeline, env } from 'https://cdn.jsdelivr.net/npm/@xenova/[email protected]';
24
+
25
+ // Set the local directory for caching models
26
+ env.localModelPath = './models';
27
 
28
  const conversationDiv = document.getElementById('conversation');
29
  const startButton = document.getElementById('startButton');
 
34
  let ttsPipeline;
35
 
36
  async function initializePipelines() {
37
+ try {
38
+ sttPipeline = await pipeline('automatic-speech-recognition', 'Xenova/whisper-tiny.en');
39
+ ttsPipeline = await pipeline('text-to-speech', 'Xenova/speecht5_tts');
40
+ addMessage('System', 'Voice Chat Bot initialized. Click "Start Listening" to begin.');
41
+ } catch (error) {
42
+ console.error('Error initializing pipelines:', error);
43
+ addMessage('System', 'Error initializing Voice Chat Bot. Please check the console for details.');
44
+ }
45
  }
46
 
47
  async function processSpeech(audio) {
48
  try {
49
+ if (!sttPipeline || !ttsPipeline) {
50
+ throw new Error('Pipelines not initialized');
51
+ }
52
+
53
  const transcription = await sttPipeline(audio);
54
  addMessage('User', transcription.text);
55
 
 
57
  const botResponse = `I heard you say: "${transcription.text}". This is a placeholder response.`;
58
  addMessage('Bot', botResponse);
59
 
60
+ const speechOutput = await ttsPipeline(botResponse, { speaker_id: 0 });
61
  playAudio(speechOutput.audio);
62
  } catch (error) {
63
  console.error('Error processing speech:', error);
64
+ addMessage('System', 'Error processing speech. Please try again.');
65
  }
66
  }
67
 
 
97
  addMessage('System', 'Listening...');
98
  } catch (error) {
99
  console.error('Error starting VAD:', error);
100
+ addMessage('System', 'Error starting voice detection. Please check your microphone and try again.');
101
  }
102
  }
103
 
104
  function stopListening() {
105
  if (myvad) {
106
+ myvad.pause();
107
  startButton.disabled = false;
108
  stopButton.disabled = true;
109
  addMessage('System', 'Stopped listening.');
 
114
  stopButton.addEventListener('click', stopListening);
115
 
116
  // Initialize pipelines when the page loads
117
+ initializePipelines();
 
 
118
  </script>
119
  </body>
120
  </html>