krishna195 commited on
Commit
67c5c5d
·
verified ·
1 Parent(s): 024f95f

Update index.html

Browse files
Files changed (1) hide show
  1. index.html +109 -61
index.html CHANGED
@@ -9,61 +9,7 @@
9
  <!-- Import the pipeline from transformers.js -->
10
  <script type="module">
11
  import { pipeline } from 'https://cdn.jsdelivr.net/npm/@xenova/[email protected]';
12
- window.pipeline = pipeline;
13
- </script>
14
 
15
- <!-- Bootstrap CSS -->
16
- <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">
17
-
18
- <style>
19
- /* Chat message styling */
20
- .user-message {
21
- text-align: right;
22
- color: #004085;
23
- margin: 10px 0;
24
- padding: 10px;
25
- border-radius: 15px;
26
- background-color: #d1ecf1;
27
- max-width: 75%;
28
- margin-left: auto;
29
- font-family: Arial, sans-serif;
30
- }
31
- .bot-message {
32
- text-align: left;
33
- color: #155724;
34
- margin: 10px 0;
35
- padding: 10px;
36
- border-radius: 15px;
37
- background-color: #d4edda;
38
- max-width: 75%;
39
- margin-right: auto;
40
- font-family: Arial, sans-serif;
41
- }
42
- /* Scrollbar styling */
43
- #chat-container::-webkit-scrollbar {
44
- width: 6px;
45
- }
46
- #chat-container::-webkit-scrollbar-thumb {
47
- background: #888;
48
- border-radius: 10px;
49
- }
50
- </style>
51
- </head>
52
-
53
- <body style="font-family: Arial, sans-serif;">
54
- <div class="container mt-5" style="max-width: 800px; margin: 0 auto;">
55
- <h1 class="text-center mb-4" style="font-weight: bold; color: #333;">Conversational Voice Chatbot</h1>
56
-
57
- <div id="chat-container" class="p-3" style="height: 70vh; overflow-y: auto;"></div>
58
-
59
- <div class="input-group mt-3">
60
- <input id="user-input" type="text" class="form-control" placeholder="Type your message..." />
61
- <button id="send-button" class="btn btn-primary" onclick="handleUserInput()">Send</button>
62
- <button id="record-button" class="btn btn-secondary" onclick="toggleRecording()">🎙️</button>
63
- </div>
64
- </div>
65
-
66
- <script>
67
  let transcriber, generator;
68
  let isRecording = false;
69
  let mediaRecorder;
@@ -71,16 +17,20 @@
71
 
72
  async function initializeModels() {
73
  try {
74
- console.log("Loading transcriber model...");
 
75
  transcriber = await pipeline('automatic-speech-recognition', 'Xenova/whisper-tiny.en');
76
- console.log("Loading generator model...");
 
77
  generator = await pipeline('text2text-generation', 'Xenova/LaMini-Flan-T5-783M');
 
78
  } catch (error) {
79
  console.error("Error loading models:", error);
80
- displayMessage("Error loading models.", 'bot-message');
81
  }
82
  }
83
 
 
84
  function displayMessage(message, className) {
85
  const messageElement = document.createElement('div');
86
  messageElement.className = className;
@@ -89,43 +39,63 @@
89
  document.getElementById('chat-container').scrollTop = document.getElementById('chat-container').scrollHeight;
90
  }
91
 
 
92
  async function handleUserInput() {
93
  const userInput = document.getElementById('user-input').value.trim();
94
  if (!userInput) return;
 
95
  displayMessage("You: " + userInput, 'user-message');
96
  document.getElementById('user-input').value = "";
 
97
  await generateResponse(userInput);
98
  }
99
 
 
100
  async function toggleRecording() {
101
  isRecording = !isRecording;
102
- document.getElementById('record-button').innerText = isRecording ? "⏹️" : "🎙️";
103
- isRecording ? startRecording() : stopRecording();
 
 
 
 
 
 
104
  }
105
 
 
106
  async function startRecording() {
107
  try {
108
  const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
109
  mediaRecorder = new MediaRecorder(stream);
110
  audioChunks = [];
 
111
  mediaRecorder.ondataavailable = event => audioChunks.push(event.data);
112
  mediaRecorder.start();
 
113
  } catch (error) {
114
  console.error("Error starting recording:", error);
115
  displayMessage("Error starting recording.", 'bot-message');
116
  }
117
  }
118
 
 
119
  async function stopRecording() {
120
  mediaRecorder.stop();
121
  mediaRecorder.onstop = async () => {
122
  const audioBlob = new Blob(audioChunks);
123
  const url = URL.createObjectURL(audioBlob);
 
124
  try {
125
  console.log("Transcribing audio...");
126
  const result = await transcriber(url);
127
  const transcribedText = result?.text || "Error in transcription.";
 
 
 
128
  displayMessage("You (voice): " + transcribedText, 'user-message');
 
 
129
  await generateResponse(transcribedText);
130
  } catch (error) {
131
  console.error("Error in transcription:", error);
@@ -134,26 +104,104 @@
134
  };
135
  }
136
 
 
137
  async function generateResponse(inputText) {
 
 
 
 
 
 
138
  try {
139
  console.log("Generating response for input:", inputText);
 
140
  const result = await generator(inputText, { max_new_tokens: 100 });
141
- const generatedText = result;
 
 
142
  displayMessage("Bot: " + generatedText, 'bot-message');
143
  speakText(generatedText);
144
  } catch (error) {
145
  console.error("Error in response generation:", error);
146
- displayMessage("Error in response generation: " + error.message, 'bot-message');
147
  }
148
  }
149
 
 
150
  function speakText(text) {
151
  const utterance = new SpeechSynthesisUtterance(text);
152
  window.speechSynthesis.speak(utterance);
153
  }
154
 
 
155
  window.addEventListener("DOMContentLoaded", initializeModels);
156
  </script>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
157
  </body>
158
 
159
  </html>
 
9
  <!-- Import the pipeline from transformers.js -->
10
  <script type="module">
11
  import { pipeline } from 'https://cdn.jsdelivr.net/npm/@xenova/[email protected]';
 
 
12
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
  let transcriber, generator;
14
  let isRecording = false;
15
  let mediaRecorder;
 
17
 
18
  async function initializeModels() {
19
  try {
20
+ console.log("Loading models...");
21
+
22
  transcriber = await pipeline('automatic-speech-recognition', 'Xenova/whisper-tiny.en');
23
+ console.log("Transcriber model loaded successfully.");
24
+
25
  generator = await pipeline('text2text-generation', 'Xenova/LaMini-Flan-T5-783M');
26
+ console.log("Generator model loaded successfully.");
27
  } catch (error) {
28
  console.error("Error loading models:", error);
29
+ displayMessage("Error loading models. Try refreshing the page.", 'bot-message');
30
  }
31
  }
32
 
33
+ // Display message in the chat container
34
  function displayMessage(message, className) {
35
  const messageElement = document.createElement('div');
36
  messageElement.className = className;
 
39
  document.getElementById('chat-container').scrollTop = document.getElementById('chat-container').scrollHeight;
40
  }
41
 
42
+ // Handle user text input
43
  async function handleUserInput() {
44
  const userInput = document.getElementById('user-input').value.trim();
45
  if (!userInput) return;
46
+
47
  displayMessage("You: " + userInput, 'user-message');
48
  document.getElementById('user-input').value = "";
49
+
50
  await generateResponse(userInput);
51
  }
52
 
53
+ // Toggle recording
54
  async function toggleRecording() {
55
  isRecording = !isRecording;
56
+
57
+ if (isRecording) {
58
+ document.getElementById('record-button').innerText = "⏹️"; // Stop icon
59
+ startRecording();
60
+ } else {
61
+ document.getElementById('record-button').innerText = "🎙️"; // Mic icon
62
+ stopRecording();
63
+ }
64
  }
65
 
66
+ // Start audio recording
67
  async function startRecording() {
68
  try {
69
  const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
70
  mediaRecorder = new MediaRecorder(stream);
71
  audioChunks = [];
72
+
73
  mediaRecorder.ondataavailable = event => audioChunks.push(event.data);
74
  mediaRecorder.start();
75
+ console.log("Recording started.");
76
  } catch (error) {
77
  console.error("Error starting recording:", error);
78
  displayMessage("Error starting recording.", 'bot-message');
79
  }
80
  }
81
 
82
+ // Stop recording and transcribe
83
  async function stopRecording() {
84
  mediaRecorder.stop();
85
  mediaRecorder.onstop = async () => {
86
  const audioBlob = new Blob(audioChunks);
87
  const url = URL.createObjectURL(audioBlob);
88
+
89
  try {
90
  console.log("Transcribing audio...");
91
  const result = await transcriber(url);
92
  const transcribedText = result?.text || "Error in transcription.";
93
+ console.log("Transcription result:", transcribedText);
94
+
95
+ // Display transcribed text
96
  displayMessage("You (voice): " + transcribedText, 'user-message');
97
+
98
+ // Generate response based on transcribed text
99
  await generateResponse(transcribedText);
100
  } catch (error) {
101
  console.error("Error in transcription:", error);
 
104
  };
105
  }
106
 
107
+ // Generate a response using text generation model
108
  async function generateResponse(inputText) {
109
+ if (!generator) {
110
+ console.error("Generator model is not loaded yet.");
111
+ displayMessage("Error: Model still loading. Please wait.", 'bot-message');
112
+ return;
113
+ }
114
+
115
  try {
116
  console.log("Generating response for input:", inputText);
117
+
118
  const result = await generator(inputText, { max_new_tokens: 100 });
119
+ const generatedText = result[0]?.generated_text || "I couldn't generate a response.";
120
+ console.log("Generated response text:", generatedText);
121
+
122
  displayMessage("Bot: " + generatedText, 'bot-message');
123
  speakText(generatedText);
124
  } catch (error) {
125
  console.error("Error in response generation:", error);
126
+ displayMessage("Error in response generation.", 'bot-message');
127
  }
128
  }
129
 
130
+ // Speak the generated text
131
  function speakText(text) {
132
  const utterance = new SpeechSynthesisUtterance(text);
133
  window.speechSynthesis.speak(utterance);
134
  }
135
 
136
+ // Initialize models when the page loads
137
  window.addEventListener("DOMContentLoaded", initializeModels);
138
  </script>
139
+
140
+ <!-- Bootstrap CSS -->
141
+ <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">
142
+
143
+ <style>
144
+ /* Chat message styling */
145
+ .user-message {
146
+ text-align: right;
147
+ color: #004085;
148
+ margin: 10px 0;
149
+ padding: 10px;
150
+ border-radius: 15px;
151
+ background-color: #d1ecf1;
152
+ max-width: 75%;
153
+ margin-left: auto;
154
+ font-family: Arial, sans-serif;
155
+ }
156
+
157
+ .bot-message {
158
+ text-align: left;
159
+ color: #155724;
160
+ margin: 10px 0;
161
+ padding: 10px;
162
+ border-radius: 15px;
163
+ background-color: #d4edda;
164
+ max-width: 75%;
165
+ margin-right: auto;
166
+ font-family: Arial, sans-serif;
167
+ }
168
+
169
+ /* Scrollbar styling */
170
+ #chat-container {
171
+ height: 70vh;
172
+ overflow-y: auto;
173
+ padding: 10px;
174
+ border: 1px solid #ccc;
175
+ border-radius: 5px;
176
+ }
177
+
178
+ #chat-container::-webkit-scrollbar {
179
+ width: 6px;
180
+ }
181
+
182
+ #chat-container::-webkit-scrollbar-thumb {
183
+ background: #888;
184
+ border-radius: 10px;
185
+ }
186
+ </style>
187
+ </head>
188
+
189
+ <body style="font-family: Arial, sans-serif;">
190
+ <div class="container mt-5" style="max-width: 800px; margin: 0 auto;">
191
+ <h1 class="text-center mb-4" style="font-weight: bold; color: #333;">Conversational Voice Chatbot</h1>
192
+
193
+ <!-- Chatbot Interface -->
194
+ <div id="chat-container" class="p-3">
195
+ <!-- Chat messages will appear here -->
196
+ </div>
197
+
198
+ <!-- Input and Button Section -->
199
+ <div class="input-group mt-3">
200
+ <input id="user-input" type="text" class="form-control" placeholder="Type your message..." />
201
+ <button id="send-button" class="btn btn-primary" onclick="handleUserInput()">Send</button>
202
+ <button id="record-button" class="btn btn-secondary" onclick="toggleRecording()">🎙️</button>
203
+ </div>
204
+ </div>
205
  </body>
206
 
207
  </html>