krishna195 commited on
Commit
5e20069
·
verified ·
1 Parent(s): 67c5c5d

Update index.html

Browse files
Files changed (1) hide show
  1. index.html +38 -118
index.html CHANGED
@@ -4,33 +4,50 @@
4
  <head>
5
  <meta charset="UTF-8">
6
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
7
- <title>Conversational Chatbot with Voice Input</title>
8
 
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;
16
- let audioChunks = [];
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,7 +56,7 @@
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;
@@ -50,96 +67,28 @@
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);
102
- displayMessage("Error in transcription.", 'bot-message');
103
- }
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 {
@@ -153,7 +102,6 @@
153
  margin-left: auto;
154
  font-family: Arial, sans-serif;
155
  }
156
-
157
  .bot-message {
158
  text-align: left;
159
  color: #155724;
@@ -165,43 +113,15 @@
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>
 
4
  <head>
5
  <meta charset="UTF-8">
6
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
7
+ <title>Text Generation Chatbot</title>
8
 
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
+ </head>
18
 
19
+ <body style="font-family: Arial, sans-serif;">
20
+ <div class="container mt-5" style="max-width: 800px; margin: 0 auto;">
21
+ <h1 class="text-center mb-4" style="font-weight: bold; color: #333;">AI Chatbot</h1>
 
22
 
23
+ <!-- Chatbot Interface -->
24
+ <div id="chat-container" class="p-3" style="height: 70vh; overflow-y: auto;">
25
+ <!-- Chat messages will appear here -->
26
+ </div>
27
+
28
+ <!-- Input and Button Section -->
29
+ <div class="input-group mt-3">
30
+ <input id="user-input" type="text" class="form-control" placeholder="Type your message..." />
31
+ <button id="send-button" class="btn btn-primary" onclick="handleUserInput()">Send</button>
32
+ </div>
33
+ </div>
34
 
35
+ <script>
36
+ let generator;
37
 
38
+ // Initialize the text-to-text model
39
+ async function initializeModel() {
40
+ try {
41
+ console.log("Loading text generation model...");
42
  generator = await pipeline('text2text-generation', 'Xenova/LaMini-Flan-T5-783M');
43
+ console.log("Model loaded successfully!");
44
  } catch (error) {
45
+ console.error("Error loading model:", error);
46
+ displayMessage("Error loading model.", 'bot-message');
47
  }
48
  }
49
 
50
+ // Display message in chat
51
  function displayMessage(message, className) {
52
  const messageElement = document.createElement('div');
53
  messageElement.className = className;
 
56
  document.getElementById('chat-container').scrollTop = document.getElementById('chat-container').scrollHeight;
57
  }
58
 
59
+ // Handle user input
60
  async function handleUserInput() {
61
  const userInput = document.getElementById('user-input').value.trim();
62
  if (!userInput) return;
 
67
  await generateResponse(userInput);
68
  }
69
 
70
+ // Generate response
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
71
  async function generateResponse(inputText) {
72
  if (!generator) {
73
+ displayMessage("Model is still loading. Please wait...", 'bot-message');
 
74
  return;
75
  }
76
 
77
  try {
78
+ console.log("Generating response...");
 
79
  const result = await generator(inputText, { max_new_tokens: 100 });
80
+ const generatedText = result[0]?.generated_text || "Sorry, I couldn't generate a response.";
 
 
81
  displayMessage("Bot: " + generatedText, 'bot-message');
 
82
  } catch (error) {
83
  console.error("Error in response generation:", error);
84
  displayMessage("Error in response generation.", 'bot-message');
85
  }
86
  }
87
 
88
+ // Load model on page load
89
+ window.addEventListener("DOMContentLoaded", initializeModel);
 
 
 
 
 
 
90
  </script>
91
 
 
 
 
92
  <style>
93
  /* Chat message styling */
94
  .user-message {
 
102
  margin-left: auto;
103
  font-family: Arial, sans-serif;
104
  }
 
105
  .bot-message {
106
  text-align: left;
107
  color: #155724;
 
113
  margin-right: auto;
114
  font-family: Arial, sans-serif;
115
  }
 
116
  /* Scrollbar styling */
 
 
 
 
 
 
 
 
117
  #chat-container::-webkit-scrollbar {
118
  width: 6px;
119
  }
 
120
  #chat-container::-webkit-scrollbar-thumb {
121
  background: #888;
122
  border-radius: 10px;
123
  }
124
  </style>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
125
  </body>
126
 
127
  </html>