Spaces:
Sleeping
Sleeping
update
Browse files- templates/chat.html +246 -325
templates/chat.html
CHANGED
@@ -9,10 +9,12 @@
|
|
9 |
|
10 |
|
11 |
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"
|
12 |
-
|
|
|
13 |
|
14 |
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.5.0/css/all.css"
|
15 |
-
integrity="sha384-B4dIYHKNBt8Bc12p+WXckhzcICo0wtJAoU8YZTY5qE0Id1GSseTk6S+L3BlXeVIU"
|
|
|
16 |
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
|
17 |
<script>
|
18 |
window.va = window.va || function () { (window.vaq = window.vaq || []).push(arguments); };
|
@@ -21,7 +23,7 @@
|
|
21 |
<link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='style.css') }}" />
|
22 |
|
23 |
<style>
|
24 |
-
|
25 |
</style>
|
26 |
|
27 |
</head>
|
@@ -37,13 +39,31 @@
|
|
37 |
<img src="/static/images/juitlogo.png" class="rounded-circle user_img">
|
38 |
<span class="online_icon"></span>
|
39 |
</div>
|
40 |
-
|
41 |
<div class="user_info">
|
42 |
<span>JUIT AI Assist</span>
|
43 |
<p>Explore College and Beyond: Ask Anything! 🎓</p>
|
44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
<!-- Add this inside the card-header (after user_info div) -->
|
46 |
-
|
47 |
|
48 |
</div>
|
49 |
</div>
|
@@ -55,13 +75,12 @@
|
|
55 |
<div class="card-footer" style="position: relative;">
|
56 |
<form id="messageArea" class="input-group" style="position: relative;">
|
57 |
<!-- Predictive Text Suggestions (ADD THIS HERE) -->
|
58 |
-
<div class="predictive-text"
|
59 |
-
style="position: absolute; display: none; z-index: 1000; width: 100%;"></div>
|
60 |
|
61 |
|
62 |
-
|
63 |
-
<input type="text" id="text" name="msg" placeholder="Type your message..."
|
64 |
-
|
65 |
<div class="input-group-append">
|
66 |
<button type="submit" id="send" class="input-group-text send_btn">
|
67 |
<i class="fas fa-location-arrow"></i>
|
@@ -69,19 +88,18 @@
|
|
69 |
</div>
|
70 |
</form>
|
71 |
<button id="clearChat" class="btn btn-danger mt-2">Clear Chat</button>
|
72 |
-
|
73 |
<div class="card-footer text-center">
|
|
|
74 |
<div class="d-flex justify-content-center align-items-center">
|
75 |
-
<span class="mr-2">Developed by:</span>
|
76 |
<span class="mr-2">Ramandeep Singh Makkar</span>
|
77 |
-
<a href="https://www.linkedin.com/in/ramandeep-singh-makkar/" target="_blank"
|
78 |
-
class="mr-2">
|
79 |
<i class="fab fa-linkedin"></i>
|
80 |
</a>
|
81 |
<a href="mailto:[email protected]" class="mr-3">
|
82 |
<i class="fas fa-envelope"></i>
|
83 |
</a>
|
84 |
-
|
85 |
<span class="mr-2">Aditya Singh</span>
|
86 |
<a href="https://www.linkedin.com/in/aditsg26/" target="_blank" class="mr-2">
|
87 |
<i class="fab fa-linkedin"></i>
|
@@ -91,339 +109,242 @@
|
|
91 |
</a>
|
92 |
</div>
|
93 |
</div>
|
94 |
-
|
|
|
95 |
</div>
|
96 |
</div>
|
97 |
</div>
|
|
|
98 |
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
// Handle predictive text
|
105 |
-
$("#text").on("input", function() {
|
106 |
-
var input = $(this).val().toLowerCase();
|
107 |
-
var predictions = getPredictiveText(input);
|
108 |
-
showPredictiveText(predictions);
|
109 |
-
});
|
110 |
-
|
111 |
-
$(document).on("click", ".predictive-text p", function() {
|
112 |
-
var text = $(this).text();
|
113 |
-
$("#text").val(text);
|
114 |
-
$(".predictive-text").hide();
|
115 |
-
});
|
116 |
-
|
117 |
-
function getPredictiveText(input) {
|
118 |
-
var predictions = [];
|
119 |
-
var patterns = {
|
120 |
-
"greeting": ["Hi", "Hey", "How are you", "what's up", "Is anyone there?", "Hello", "Good day"],
|
121 |
-
"name": ["what is your name", "name", "what's your name", "who are you", "what should I call you"]
|
122 |
-
};
|
123 |
-
|
124 |
-
for (var tag in patterns) {
|
125 |
-
patterns[tag].forEach(function(pattern) {
|
126 |
-
if (pattern.toLowerCase().startsWith(input) && !predictions.includes(pattern)) {
|
127 |
-
predictions.push(pattern);
|
128 |
-
}
|
129 |
-
});
|
130 |
-
}
|
131 |
-
return predictions;
|
132 |
-
}
|
133 |
-
|
134 |
-
function showPredictiveText(predictions) {
|
135 |
-
if (predictions.length > 0) {
|
136 |
-
var html = "";
|
137 |
-
predictions.forEach(function(prediction) {
|
138 |
-
html += "<p>" + prediction + "</p>";
|
139 |
-
});
|
140 |
-
$(".predictive-text").html(html).show();
|
141 |
-
} else {
|
142 |
-
$(".predictive-text").hide();
|
143 |
-
}
|
144 |
-
}
|
145 |
-
|
146 |
-
// Handle message submission and user input history
|
147 |
-
$("#messageArea").on("submit", function(event) {
|
148 |
-
$(".predictive-text").hide(); // Hide predictive suggestions
|
149 |
-
|
150 |
-
const date = new Date();
|
151 |
-
const hour = date.getHours().toString().padStart(2, '0');
|
152 |
-
const minute = date.getMinutes().toString().padStart(2, '0');
|
153 |
-
const str_time = hour + ":" + minute;
|
154 |
-
|
155 |
-
var rawText = $("#text").val();
|
156 |
-
chatHistory.push(rawText); // Store the user's message in history
|
157 |
-
currentIndex = chatHistory.length - 1; // Update the current index
|
158 |
-
|
159 |
-
var userHtml = `
|
160 |
-
<div class="d-flex justify-content-end mb-4">
|
161 |
-
<div class="msg_container_send">${rawText}
|
162 |
-
<span class="msg_time_send">${str_time}</span>
|
163 |
-
</div>
|
164 |
-
<div class="img_cont_msg">
|
165 |
-
<img src="/static/images/user.png" class="rounded-circle user_img_msg">
|
166 |
-
</div>
|
167 |
-
</div>`;
|
168 |
-
|
169 |
-
$("#text").val(""); // Clear the input field
|
170 |
-
$("#messageFormeight").append(userHtml); // Add the user message to the chat
|
171 |
-
|
172 |
-
$(document).ready(function () {
|
173 |
-
let chatHistory = JSON.parse(localStorage.getItem("chatHistory")) || []; // Load stored history
|
174 |
-
let currentIndex = chatHistory.length; // Set index to last message
|
175 |
-
|
176 |
-
$("#messageArea").on("submit", function (event) {
|
177 |
-
event.preventDefault(); // Prevent form submission
|
178 |
-
|
179 |
-
let rawText = $("#text").val().trim();
|
180 |
-
if (rawText === "") return; // Ignore empty input
|
181 |
-
|
182 |
-
const time = new Date().toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' });
|
183 |
-
|
184 |
-
// Append user message to chat
|
185 |
-
let userHtml = `
|
186 |
-
<div class="d-flex justify-content-end mb-4">
|
187 |
-
<div class="msg_container_send">${rawText}<span class="msg_time_send">${time}</span></div>
|
188 |
-
<div class="img_cont_msg"><img src="https://i.ibb.co/d5b84Xw/Untitled-design.png" class="rounded-circle user_img_msg"></div>
|
189 |
-
</div>`;
|
190 |
-
$("#messageFormeight").append(userHtml);
|
191 |
-
|
192 |
-
// Update chat history
|
193 |
-
chatHistory.push(rawText);
|
194 |
-
localStorage.setItem("chatHistory", JSON.stringify(chatHistory)); // Save in localStorage
|
195 |
-
currentIndex = chatHistory.length; // Reset index to end
|
196 |
-
|
197 |
-
// Clear input field & scroll down
|
198 |
-
$("#text").val("");
|
199 |
-
$("#messageFormeight").animate({ scrollTop: $("#messageFormeight")[0].scrollHeight }, 500);
|
200 |
-
|
201 |
-
// Send message to server
|
202 |
-
$.ajax({
|
203 |
-
data: { msg: rawText },
|
204 |
-
type: "POST",
|
205 |
-
url: "/get",
|
206 |
-
}).done(function (response) {
|
207 |
-
// --- Typewriter effect for bot response with HTML support START ---
|
208 |
-
let botContainer = $(`
|
209 |
-
<div class="d-flex justify-content-start mb-4">
|
210 |
-
<div class="img_cont_msg">
|
211 |
-
<img src="/static/juitlogo.png" class="rounded-circle user_img_msg">
|
212 |
-
</div>
|
213 |
-
<div class="msg_container"></div>
|
214 |
-
</div>`);
|
215 |
-
$("#messageFormeight").append(botContainer);
|
216 |
-
|
217 |
-
let msgDiv = botContainer.find(".msg_container");
|
218 |
-
let i = 0;
|
219 |
-
|
220 |
-
function typeWriterInner() {
|
221 |
-
if (i < response.length) {
|
222 |
-
// Check if current character starts an HTML tag
|
223 |
-
if (response.charAt(i) === '<') {
|
224 |
-
let endTag = response.indexOf('>', i);
|
225 |
-
if (endTag !== -1) {
|
226 |
-
// Append the entire tag at once (so that HTML renders)
|
227 |
-
let tag = response.substring(i, endTag + 1);
|
228 |
-
msgDiv.append(tag);
|
229 |
-
i = endTag + 1;
|
230 |
-
} else {
|
231 |
-
// Fallback: append the single character if no closing '>' is found
|
232 |
-
msgDiv.append(response.charAt(i));
|
233 |
-
i++;
|
234 |
-
}
|
235 |
-
} else {
|
236 |
-
msgDiv.append(response.charAt(i));
|
237 |
-
i++;
|
238 |
-
}
|
239 |
-
setTimeout(typeWriterInner, 30); // Adjust delay as desired
|
240 |
-
} else {
|
241 |
-
msgDiv.append(`<span class="msg_time">${time}</span>`);
|
242 |
-
$("#messageFormeight").animate({ scrollTop: $("#messageFormeight")[0].scrollHeight }, 500);
|
243 |
-
}
|
244 |
-
}
|
245 |
-
typeWriterInner();
|
246 |
-
// --- Typewriter effect for bot response with HTML support END ---
|
247 |
-
});
|
248 |
|
249 |
-
|
250 |
-
|
251 |
-
|
252 |
-
|
253 |
-
|
254 |
-
|
255 |
-
localStorage.removeItem("chatHistory"); // Clears from storage
|
256 |
-
currentIndex = 0;
|
257 |
-
});
|
258 |
-
});
|
259 |
-
|
260 |
-
// Send the message to the server via AJAX
|
261 |
-
$.ajax({
|
262 |
-
data: { msg: rawText },
|
263 |
-
type: "POST",
|
264 |
-
url: "/get",
|
265 |
-
}).done(function(data) {
|
266 |
-
// --- Typewriter effect for bot response with HTML support START ---
|
267 |
-
var lines = data.split("**");
|
268 |
-
var fullText = lines.join(' ');
|
269 |
-
|
270 |
-
// Create the container for the bot message
|
271 |
-
var botContainer = $('<div class="d-flex justify-content-start mb-4"></div>');
|
272 |
-
var imgDiv = $('<div class="img_cont_msg"><img src="/static/juitlogo.png" class="rounded-circle user_img_msg"></div>');
|
273 |
-
var msgDiv = $('<div class="msg_container"></div>');
|
274 |
-
botContainer.append(imgDiv).append(msgDiv);
|
275 |
-
$("#messageFormeight").append(botContainer);
|
276 |
-
|
277 |
-
var i = 0;
|
278 |
-
function typeWriter() {
|
279 |
-
if (i < fullText.length) {
|
280 |
-
if (fullText.charAt(i) === '<') {
|
281 |
-
var endTag = fullText.indexOf('>', i);
|
282 |
-
if (endTag !== -1) {
|
283 |
-
var tag = fullText.substring(i, endTag + 1);
|
284 |
-
msgDiv.append(tag);
|
285 |
-
i = endTag + 1;
|
286 |
-
} else {
|
287 |
-
msgDiv.append(fullText.charAt(i));
|
288 |
-
i++;
|
289 |
-
}
|
290 |
-
} else {
|
291 |
-
msgDiv.append(fullText.charAt(i));
|
292 |
-
i++;
|
293 |
-
}
|
294 |
-
setTimeout(typeWriter, 30); // Adjust delay as needed
|
295 |
-
} else {
|
296 |
-
msgDiv.append('<span class="msg_time">' + str_time + '</span>');
|
297 |
-
$("#messageFormeight").animate({ scrollTop: $("#messageFormeight")[0].scrollHeight }, 500);
|
298 |
-
}
|
299 |
-
}
|
300 |
-
typeWriter();
|
301 |
-
// --- Typewriter effect for bot response with HTML support END ---
|
302 |
-
});
|
303 |
|
304 |
-
|
305 |
-
|
306 |
-
|
|
|
307 |
});
|
308 |
-
|
309 |
-
|
310 |
-
|
311 |
-
|
312 |
-
|
313 |
-
|
314 |
-
|
315 |
-
|
316 |
-
|
317 |
-
|
318 |
-
|
319 |
-
|
320 |
-
|
321 |
-
|
322 |
-
|
323 |
-
|
324 |
-
|
325 |
-
updateButton(); // Update button text on load
|
326 |
-
|
327 |
-
// Toggle theme on button click
|
328 |
-
themeToggle.addEventListener("click", () => {
|
329 |
-
if (body.classList.contains("light-mode")) {
|
330 |
-
// Switch to dark mode
|
331 |
-
body.classList.remove("light-mode");
|
332 |
-
body.classList.add("dark-mode");
|
333 |
-
localStorage.setItem("theme", "dark");
|
334 |
-
} else {
|
335 |
-
// Switch to light mode
|
336 |
-
body.classList.remove("dark-mode");
|
337 |
-
body.classList.add("light-mode");
|
338 |
-
localStorage.setItem("theme", "light");
|
339 |
-
}
|
340 |
-
updateButton();
|
341 |
-
});
|
342 |
-
|
343 |
-
function updateButton() {
|
344 |
-
themeToggle.textContent = body.classList.contains("light-mode")
|
345 |
-
? "Switch to Dark Mode"
|
346 |
-
: "Switch to Light Mode";
|
347 |
}
|
348 |
-
|
349 |
-
|
|
|
350 |
function showPredictiveText(predictions) {
|
351 |
-
if (predictions.length > 0
|
352 |
-
|
|
|
|
|
|
|
353 |
$(".predictive-text").html(html).show();
|
354 |
} else {
|
355 |
$(".predictive-text").hide();
|
356 |
}
|
357 |
}
|
358 |
-
|
359 |
-
$("#messageFormeight").animate({ scrollTop: $("#messageFormeight")[0].scrollHeight }, 500);
|
360 |
-
}
|
361 |
-
|
362 |
-
$(document).ready(function() {
|
363 |
-
$("#clearChat").click(function() {
|
364 |
-
$("#messageFormeight").html(""); // Clears the chat area
|
365 |
-
localStorage.removeItem("chatHistory"); // Clears stored history
|
366 |
-
});
|
367 |
-
});
|
368 |
-
|
369 |
-
|
370 |
-
</script>
|
371 |
|
|
|
|
|
|
|
372 |
|
373 |
-
|
374 |
-
|
375 |
-
const
|
376 |
-
const
|
377 |
-
|
378 |
-
|
379 |
-
|
380 |
-
|
381 |
-
|
382 |
-
|
383 |
-
|
384 |
-
|
385 |
-
|
386 |
-
|
387 |
-
|
388 |
-
|
389 |
-
|
390 |
-
|
391 |
-
|
392 |
-
|
393 |
-
|
394 |
-
|
395 |
-
|
396 |
-
|
397 |
-
|
398 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
399 |
}
|
400 |
-
|
|
|
|
|
|
|
|
|
|
|
401 |
});
|
402 |
-
|
403 |
-
|
404 |
-
? "Switch to Dark Mode"
|
405 |
-
: "Switch to Light Mode";
|
406 |
-
}
|
407 |
});
|
408 |
-
|
409 |
-
|
410 |
-
|
411 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
412 |
} else {
|
413 |
-
|
|
|
|
|
|
|
414 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
415 |
}
|
416 |
-
|
417 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
418 |
}
|
419 |
-
|
420 |
-
|
421 |
-
|
422 |
-
|
423 |
-
|
|
|
|
|
|
|
|
|
424 |
});
|
425 |
-
|
426 |
-
|
|
|
|
|
|
|
427 |
</body>
|
428 |
|
429 |
</html>
|
|
|
9 |
|
10 |
|
11 |
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"
|
12 |
+
integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO"
|
13 |
+
crossorigin="anonymous">
|
14 |
|
15 |
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.5.0/css/all.css"
|
16 |
+
integrity="sha384-B4dIYHKNBt8Bc12p+WXckhzcICo0wtJAoU8YZTY5qE0Id1GSseTk6S+L3BlXeVIU"
|
17 |
+
crossorigin="anonymous">
|
18 |
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
|
19 |
<script>
|
20 |
window.va = window.va || function () { (window.vaq = window.vaq || []).push(arguments); };
|
|
|
23 |
<link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='style.css') }}" />
|
24 |
|
25 |
<style>
|
26 |
+
|
27 |
</style>
|
28 |
|
29 |
</head>
|
|
|
39 |
<img src="/static/images/juitlogo.png" class="rounded-circle user_img">
|
40 |
<span class="online_icon"></span>
|
41 |
</div>
|
42 |
+
|
43 |
<div class="user_info">
|
44 |
<span>JUIT AI Assist</span>
|
45 |
<p>Explore College and Beyond: Ask Anything! 🎓</p>
|
46 |
+
<div class="predefined-text">
|
47 |
+
Created by
|
48 |
+
Ramandeep Singh Makkar
|
49 |
+
(<a href="https://www.linkedin.com/in/ramandeep-singh-makkar/" target="_blank">
|
50 |
+
<i class="fab fa-linkedin"></i> LinkedIn
|
51 |
+
</a>)
|
52 |
+
(<a href="mailto:[email protected]">
|
53 |
+
<i class="fas fa-envelope"></i> Gmail
|
54 |
+
</a>)
|
55 |
+
and
|
56 |
+
Aditya Singh
|
57 |
+
(<a href="https://www.linkedin.com/in/aditsg26/" target="_blank">
|
58 |
+
<i class="fab fa-linkedin"></i> LinkedIn
|
59 |
+
</a>)
|
60 |
+
(<a href="mailto:[email protected]">
|
61 |
+
<i class="fas fa-envelope"></i> Gmail
|
62 |
+
</a>)
|
63 |
+
</div>
|
64 |
+
|
65 |
<!-- Add this inside the card-header (after user_info div) -->
|
66 |
+
<button id="themeToggle" class="btn btn-sm btn-light ml-auto">Light Mode</button>
|
67 |
|
68 |
</div>
|
69 |
</div>
|
|
|
75 |
<div class="card-footer" style="position: relative;">
|
76 |
<form id="messageArea" class="input-group" style="position: relative;">
|
77 |
<!-- Predictive Text Suggestions (ADD THIS HERE) -->
|
78 |
+
<div class="predictive-text" style="position: absolute; display: none; z-index: 1000; width: 100%;"></div>
|
|
|
79 |
|
80 |
|
81 |
+
|
82 |
+
<input type="text" id="text" name="msg" placeholder="Type your message..." autocomplete="off"
|
83 |
+
class="form-control type_msg" required />
|
84 |
<div class="input-group-append">
|
85 |
<button type="submit" id="send" class="input-group-text send_btn">
|
86 |
<i class="fas fa-location-arrow"></i>
|
|
|
88 |
</div>
|
89 |
</form>
|
90 |
<button id="clearChat" class="btn btn-danger mt-2">Clear Chat</button>
|
91 |
+
|
92 |
<div class="card-footer text-center">
|
93 |
+
<p class="mb-1">Created by</p>
|
94 |
<div class="d-flex justify-content-center align-items-center">
|
|
|
95 |
<span class="mr-2">Ramandeep Singh Makkar</span>
|
96 |
+
<a href="https://www.linkedin.com/in/ramandeep-singh-makkar/" target="_blank" class="mr-2">
|
|
|
97 |
<i class="fab fa-linkedin"></i>
|
98 |
</a>
|
99 |
<a href="mailto:[email protected]" class="mr-3">
|
100 |
<i class="fas fa-envelope"></i>
|
101 |
</a>
|
102 |
+
|
103 |
<span class="mr-2">Aditya Singh</span>
|
104 |
<a href="https://www.linkedin.com/in/aditsg26/" target="_blank" class="mr-2">
|
105 |
<i class="fab fa-linkedin"></i>
|
|
|
109 |
</a>
|
110 |
</div>
|
111 |
</div>
|
112 |
+
|
113 |
+
|
114 |
</div>
|
115 |
</div>
|
116 |
</div>
|
117 |
+
</div>
|
118 |
|
119 |
+
<script>
|
120 |
+
$(document).ready(function() {
|
121 |
+
let chatHistory = []; // Stores the user's input history
|
122 |
+
let currentIndex = -1; // Tracks the current position in the history
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
123 |
|
124 |
+
// Handle predictive text
|
125 |
+
$("#text").on("input", function() {
|
126 |
+
var input = $(this).val().toLowerCase();
|
127 |
+
var predictions = getPredictiveText(input);
|
128 |
+
showPredictiveText(predictions);
|
129 |
+
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
130 |
|
131 |
+
$(document).on("click", ".predictive-text p", function() {
|
132 |
+
var text = $(this).text();
|
133 |
+
$("#text").val(text);
|
134 |
+
$(".predictive-text").hide();
|
135 |
});
|
136 |
+
|
137 |
+
function getPredictiveText(input) {
|
138 |
+
var predictions = [];
|
139 |
+
var patterns = {
|
140 |
+
"greeting": ["Hi", "Hey", "How are you", "what's up", "Is anyone there?", "Hello", "Good day"],
|
141 |
+
"name": ["what is your name", "name", "what's your name", "who are you", "what should I call you"]
|
142 |
+
|
143 |
+
|
144 |
+
|
145 |
+
};
|
146 |
+
|
147 |
+
for (var tag in patterns) {
|
148 |
+
patterns[tag].forEach(function(pattern) {
|
149 |
+
if (pattern.toLowerCase().startsWith(input) && !predictions.includes(pattern)) {
|
150 |
+
predictions.push(pattern);
|
151 |
+
}
|
152 |
+
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
153 |
}
|
154 |
+
return predictions;
|
155 |
+
}
|
156 |
+
|
157 |
function showPredictiveText(predictions) {
|
158 |
+
if (predictions.length > 0) {
|
159 |
+
var html = "";
|
160 |
+
predictions.forEach(function(prediction) {
|
161 |
+
html += "<p>" + prediction + "</p>";
|
162 |
+
});
|
163 |
$(".predictive-text").html(html).show();
|
164 |
} else {
|
165 |
$(".predictive-text").hide();
|
166 |
}
|
167 |
}
|
168 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
169 |
|
170 |
+
// Handle message submission and user input history
|
171 |
+
$("#messageArea").on("submit", function(event) {
|
172 |
+
$(".predictive-text").hide(); // Hide predictive suggestions
|
173 |
|
174 |
+
const date = new Date();
|
175 |
+
const hour = date.getHours().toString().padStart(2, '0');
|
176 |
+
const minute = date.getMinutes().toString().padStart(2, '0');
|
177 |
+
const str_time = hour + ":" + minute;
|
178 |
+
|
179 |
+
var rawText = $("#text").val();
|
180 |
+
chatHistory.push(rawText); // Store the user's message in history
|
181 |
+
currentIndex = chatHistory.length - 1; // Update the current index
|
182 |
+
|
183 |
+
var userHtml = `
|
184 |
+
<div class="d-flex justify-content-end mb-4">
|
185 |
+
<div class="msg_container_send">${rawText}
|
186 |
+
<span class="msg_time_send">${str_time}</span>
|
187 |
+
</div>
|
188 |
+
<div class="img_cont_msg">
|
189 |
+
<img src="/static/images/user.png" class="rounded-circle user_img_msg">
|
190 |
+
</div>
|
191 |
+
</div>`;
|
192 |
+
|
193 |
+
$("#text").val(""); // Clear the input field
|
194 |
+
$("#messageFormeight").append(userHtml); // Add the user message to the chat
|
195 |
+
|
196 |
+
$(document).ready(function () {
|
197 |
+
let chatHistory = JSON.parse(localStorage.getItem("chatHistory")) || []; // Load stored history
|
198 |
+
let currentIndex = chatHistory.length; // Set index to last message
|
199 |
+
|
200 |
+
$("#messageArea").on("submit", function (event) {
|
201 |
+
event.preventDefault(); // Prevent form submission
|
202 |
+
|
203 |
+
let rawText = $("#text").val().trim();
|
204 |
+
if (rawText === "") return; // Ignore empty input
|
205 |
+
|
206 |
+
const time = new Date().toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' });
|
207 |
+
|
208 |
+
// Append user message to chat
|
209 |
+
let userHtml = `
|
210 |
+
<div class="d-flex justify-content-end mb-4">
|
211 |
+
<div class="msg_container_send">${rawText}<span class="msg_time_send">${time}</span></div>
|
212 |
+
<div class="img_cont_msg"><img src="https://i.ibb.co/d5b84Xw/Untitled-design.png" class="rounded-circle user_img_msg"></div>
|
213 |
+
</div>`;
|
214 |
+
$("#messageFormeight").append(userHtml);
|
215 |
+
|
216 |
+
// Update chat history
|
217 |
+
chatHistory.push(rawText);
|
218 |
+
localStorage.setItem("chatHistory", JSON.stringify(chatHistory)); // Save in localStorage
|
219 |
+
currentIndex = chatHistory.length; // Reset index to end
|
220 |
+
|
221 |
+
// Clear input field & scroll down
|
222 |
+
$("#text").val("");
|
223 |
+
$("#messageFormeight").animate({ scrollTop: $("#messageFormeight")[0].scrollHeight }, 500);
|
224 |
+
|
225 |
+
// Send message to server
|
226 |
+
$.ajax({
|
227 |
+
data: { msg: rawText },
|
228 |
+
type: "POST",
|
229 |
+
url: "/get",
|
230 |
+
}).done(function (response) {
|
231 |
+
let botHtml = `
|
232 |
+
<div class="d-flex justify-content-start mb-4">
|
233 |
+
<div class="img_cont_msg">
|
234 |
+
<img src="/static/images/juitlogo.png" class="rounded-circle user_img_msg">
|
235 |
+
</div>
|
236 |
+
<div class="msg_container">${response}<span class="msg_time">${time}</span></div>
|
237 |
+
</div>`;
|
238 |
+
|
239 |
+
$("#messageFormeight").append($.parseHTML(botHtml));
|
240 |
+
$("#messageFormeight").animate({ scrollTop: $("#messageFormeight")[0].scrollHeight }, 500);
|
241 |
+
});
|
242 |
+
});
|
243 |
+
|
244 |
+
// Clear chat history
|
245 |
+
$("#clearChat").click(function () {
|
246 |
+
$("#messageFormeight").html(""); // Clears UI
|
247 |
+
chatHistory = []; // Clears array
|
248 |
+
localStorage.removeItem("chatHistory"); // Clears from storage
|
249 |
+
currentIndex = 0;
|
250 |
+
});
|
251 |
+
});
|
252 |
+
|
253 |
+
|
254 |
+
|
255 |
+
|
256 |
+
|
257 |
+
// Send the message to the server via AJAX
|
258 |
+
$.ajax({
|
259 |
+
data: {
|
260 |
+
msg: rawText,
|
261 |
+
},
|
262 |
+
type: "POST",
|
263 |
+
url: "/get",
|
264 |
+
}).done(function(data) {
|
265 |
+
var lines = data.split("**");
|
266 |
+
var botHtml = '<div class="d-flex justify-content-start mb-4"><div class="img_cont_msg"><img src="/static/images/juitlogo.png" class="rounded-circle user_img_msg"></div><div class="msg_container">';
|
267 |
+
|
268 |
+
for (var i = 0; i < lines.length; i++) {
|
269 |
+
botHtml += '<div class="msg_container">' + lines[i] + '</div>';
|
270 |
}
|
271 |
+
botHtml += '<span class="msg_time">' + str_time + '</span></div></div>';
|
272 |
+
$("#messageFormeight").append($.parseHTML(botHtml));
|
273 |
+
|
274 |
+
// Smooth scroll to the bottom of the chat
|
275 |
+
var container = $("#messageFormeight");
|
276 |
+
container.animate({ scrollTop: container.prop("scrollHeight") }, 500);
|
277 |
});
|
278 |
+
|
279 |
+
event.preventDefault(); // Prevent form submission
|
|
|
|
|
|
|
280 |
});
|
281 |
+
});
|
282 |
+
</script>
|
283 |
+
|
284 |
+
<script>
|
285 |
+
document.addEventListener("DOMContentLoaded", () => {
|
286 |
+
const themeToggle = document.getElementById("themeToggle");
|
287 |
+
const body = document.body;
|
288 |
+
|
289 |
+
// Check local storage for a saved theme, default to dark
|
290 |
+
const savedTheme = localStorage.getItem("theme") || "dark";
|
291 |
+
if (savedTheme === "light") {
|
292 |
+
body.classList.add("light-mode");
|
293 |
+
body.classList.remove("dark-mode");
|
294 |
+
} else {
|
295 |
+
body.classList.add("dark-mode");
|
296 |
+
body.classList.remove("light-mode");
|
297 |
+
}
|
298 |
+
|
299 |
+
updateButton(); // Update button text on load
|
300 |
+
|
301 |
+
// Toggle theme on button click
|
302 |
+
themeToggle.addEventListener("click", () => {
|
303 |
+
if (body.classList.contains("light-mode")) {
|
304 |
+
// Switch to dark mode
|
305 |
+
body.classList.remove("light-mode");
|
306 |
+
body.classList.add("dark-mode");
|
307 |
+
localStorage.setItem("theme", "dark");
|
308 |
} else {
|
309 |
+
// Switch to light mode
|
310 |
+
body.classList.remove("dark-mode");
|
311 |
+
body.classList.add("light-mode");
|
312 |
+
localStorage.setItem("theme", "light");
|
313 |
}
|
314 |
+
updateButton();
|
315 |
+
});
|
316 |
+
|
317 |
+
function updateButton() {
|
318 |
+
themeToggle.textContent = body.classList.contains("light-mode")
|
319 |
+
? "Switch to Dark Mode"
|
320 |
+
: "Switch to Light Mode";
|
321 |
}
|
322 |
+
});
|
323 |
+
|
324 |
+
function showPredictiveText(predictions) {
|
325 |
+
if (predictions.length > 0 && $("#text").val().trim() !== "") {
|
326 |
+
|
327 |
+
let html = predictions.map(p => `<p>${p}</p>`).join('');
|
328 |
+
$(".predictive-text").html(html).show();
|
329 |
+
} else {
|
330 |
+
$(".predictive-text").hide();
|
331 |
+
|
332 |
}
|
333 |
+
}
|
334 |
+
function scrollToBottom() {
|
335 |
+
$("#messageFormeight").animate({ scrollTop: $("#messageFormeight")[0].scrollHeight }, 500);
|
336 |
+
}
|
337 |
+
|
338 |
+
$(document).ready(function() {
|
339 |
+
$("#clearChat").click(function() {
|
340 |
+
$("#messageFormeight").html(""); // Clears the chat area
|
341 |
+
localStorage.removeItem("chatHistory"); // Clears stored history
|
342 |
});
|
343 |
+
});
|
344 |
+
|
345 |
+
|
346 |
+
</script>
|
347 |
+
|
348 |
</body>
|
349 |
|
350 |
</html>
|