-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
302 lines (260 loc) · 9.86 KB
/
index.html
1
2
3
4
5
6
7
8
9
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Virtual Herbal Garden Chatbot</title>
<style>
body {
font-family: 'Open Sans', sans-serif;
margin: 0;
padding: 0;
background-color: #e0f7fa;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
#chatbox {
width: 100%;
max-width: 800px;
margin: 0 auto;
padding: 20px;
border: 1px solid #ddd;
border-radius: 10px;
background-color: #f9f9f9;
position: relative;
}
#chat-container {
background-color: #fff;
border-radius: 12px;
width: 400px;
height: 600px;
box-shadow: 0px 4px 20px rgba(0, 0, 0, 0.1);
display: flex;
flex-direction: column;
overflow: hidden;
}
#header {
background-color: #00695c;
color: white;
padding: 15px;
text-align: center;
font-size: 1.2em;
}
#chat-history {
padding: 20px;
flex: 1;
overflow-y: auto;
background-color: #f1f1f1;
max-height: 400px;
overflow-y: auto;
margin-bottom: 20px;
}
#chat-input {
display: flex;
border-top: 1px solid #ddd;
padding: 10px;
}
#chat-input input {
flex: 1;
padding: 10px;
border-radius: 5px;
border: 1px solid #ddd;
}
#chat-input button {
background-color: #00695c;
color: white;
border: none;
padding: 10px;
margin-left: 10px;
border-radius: 5px;
cursor: pointer;
}
#chat-input button:hover {
background-color: #004d40;
}
#actions {
display: flex;
justify-content: space-between;
padding: 10px 20px;
background-color: #f1f1f1;
border-top: 1px solid #ddd;
}
#actions button {
flex: 1;
margin: 0 5px;
background-color: #00796b;
color: white;
border: none;
padding: 10px;
border-radius: 5px;
cursor: pointer;
}
#actions button:hover {
background-color: #004d40;
}
.chat-bubble {
max-width: 50%;
padding: 10px 15px;
border-radius: 15px;
margin-bottom: auto;
line-height: 1.5;
position: relative;
background-color: #4fd185;
}
/* User messages (right-aligned) */
.user-message .chat-bubble {
background-color: #4fd185;
text-align: right;
margin-left: auto;
margin-right: 0;
}
/* Bot messages (left-aligned) */
.bot-message .chat-bubble {
background-color: #e1e1e1;
text-align: left;
margin-right: auto;
margin-left: 0;
}
/* Button st yling */
button {
margin: 5px;
}
</style>
</head>
<body>
<div id="chat-container">
<div id="header">Virtual Herbal Garden Chatbot</div>
<div id="chat-history"></div>
<div id="chat-input">
<input type="text" id="user-input" placeholder="Ask about medicinal plants..." />
<button id="send-button">Send</button>
</div>
<div id="actions">
<button id="stop-button">Stop</button>
<button id="toggle-audio">Audio Off</button>
</div>
</div>
<script>
let isGenerating = false;
let isAudioOn = false;
let audioPlaying = false;
let abortController = new AbortController(); // Controller to handle aborting fetch requests
// Function to clean the text for speech by removing emojis and special characters (except numbers and letters)
function cleanTextForSpeech(text) {
return text.replace(/[^a-zA-Z0-9\s,.!?]/g, ''); // Remove symbols for speech, but retain punctuation
}
document.getElementById('send-button').addEventListener('click', () => {
const userInput = document.getElementById('user-input').value;
if (userInput) {
sendMessage(userInput);
}
});
document.getElementById('user-input').addEventListener('keypress', (event) => {
if (event.key === 'Enter') {
event.preventDefault(); // Prevent form submission if inside a form
const userInput = document.getElementById('user-input').value;
if (userInput) {
sendMessage(userInput);
}
}
});
document.getElementById('stop-button').addEventListener('click', () => {
if (isGenerating) {
abortController.abort(); // Abort current request
isGenerating = false;
displayBotMessage("Request stopped.", false);
}
});
document.getElementById('toggle-audio').addEventListener('click', () => {
isAudioOn = !isAudioOn;
document.getElementById('toggle-audio').innerText = isAudioOn ? "Audio On" : "Audio Off";
if (!isAudioOn && audioPlaying) {
stopAudio();
}
});
async function sendMessage(message) {
if (isGenerating) return;
if (!message) {
console.error("Message is empty or undefined");
return;
}
isGenerating = true;
displayBotMessage(message, true); // User message
document.getElementById('user-input').value = '';
try {
// Check for internet connection
if (!navigator.onLine) {
displayBotMessage("No internet connection. Please check your Network connection.", false); // Bot message
isGenerating = false;
return;
}
const response = await fetch('/generate', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ userInput: message }),
signal: abortController.signal // Attach abort signal
});
if (response.ok) {
const data = await response.json();
const botResponse = data.response;
// Display the bot response with left-alignment
displayBotMessage(botResponse, false); // Bot message
// Clean the text for speech (remove emojis and special characters)
const cleanedTextForSpeech = cleanTextForSpeech(botResponse);
// Play the audio if toggle is enabled and not already playing
if (isAudioOn && !audioPlaying) {
playText(cleanedTextForSpeech);
}
} else {
console.error("Failed to fetch response from server");
}
} catch (error) {
if (error.name === 'AbortError') {
console.log('Request was aborted');
} else {
console.error('Fetch error:', error);
}
}
isGenerating = false;
}
function displayBotMessage(message, isUserMessage) {
const chatHistoryDiv = document.getElementById('chat-history');
// Create a new div for the message
const messageDiv = document.createElement('div');
messageDiv.classList.add(isUserMessage ? 'user-message' : 'bot-message');
// Create a bubble div
const bubbleDiv = document.createElement('div');
bubbleDiv.classList.add('chat-bubble');
bubbleDiv.innerText = message; // Use innerText to avoid HTML injection issues
messageDiv.appendChild(bubbleDiv);
chatHistoryDiv.appendChild(messageDiv);
chatHistoryDiv.scrollTop = chatHistoryDiv.scrollHeight; // Auto-scroll to the bottom
}
function playText(text) {
const utterance = new SpeechSynthesisUtterance(text);
utterance.rate = 1.2; // Increase speed a little
utterance.pitch = 1; // Standard pitch
utterance.lang = 'en-US','te-IN','hi-IN'; // Set language to US English
utterance.onstart = () => { audioPlaying = true; };
utterance.onend = () => { audioPlaying = false; };
window.speechSynthesis.speak(utterance);
}
function stopAudio() {
window.speechSynthesis.cancel();
audioPlaying = false;
}
// Detect loss of internet connection and display an alert
window.addEventListener('offline', () => {
displayBotMessage("You are offline. Please check your internet connection.", false);
});
// Re-detect when internet connection is back and inform the user
window.addEventListener('online', () => {
displayBotMessage("We are online Again!", false);
});
</script>
</body>
</html>