Adieee5's picture
Update
d43574d verified
raw
history blame
14.3 kB
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>JUIT AI Assist</title>
<link rel="shortcut icon" href="/static/juitlogo.png" type="image/x-icon">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"
integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO"
crossorigin="anonymous">
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.5.0/css/all.css"
integrity="sha384-B4dIYHKNBt8Bc12p+WXckhzcICo0wtJAoU8YZTY5qE0Id1GSseTk6S+L3BlXeVIU"
crossorigin="anonymous">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
window.va = window.va || function () { (window.vaq = window.vaq || []).push(arguments); };
</script>
<script defer src="/_vercel/insights/script.js"></script>
<link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='style.css') }}" />
<style>
</style>
</head>
<body>
<div class="container-fluid h-100">
<div class="row justify-content-center h-100">
<div class="col-md-8 col-xl-6 chat">
<div class="card">
<div class="card-header msg_head">
<div class="d-flex bd-highlight">
<div class="img_cont">
<img src="/static/juitlogo.png" class="rounded-circle user_img">
<span class="online_icon"></span>
</div>
<div class="user_info">
<span>JUIT AI Assist</span>
<p>Explore College and Beyond: Ask Anything! 🎓</p>
<div class="predefined-text">
Created by
Ramandeep Singh Makkar
(<a href="https://www.linkedin.com/in/ramandeep-singh-makkar/" target="_blank">
<i class="fab fa-linkedin"></i> LinkedIn
</a>)
(<a href="mailto:[email protected]">
<i class="fas fa-envelope"></i> Gmail
</a>)
and
Aditya Singh
(<a href="https://www.linkedin.com/in/aditsg26/" target="_blank">
<i class="fab fa-linkedin"></i> LinkedIn
</a>)
(<a href="mailto:[email protected]">
<i class="fas fa-envelope"></i> Gmail
</a>)
</div>
<!-- Add this inside the card-header (after user_info div) -->
<button id="themeToggle" class="btn btn-sm btn-light ml-auto">Light Mode</button>
</div>
</div>
</div>
<div id="messageFormeight" class="card-body msg_card_body">
<!-- Chat messages will be displayed here -->
</div>
<div class="card-footer" style="position: relative;">
<form id="messageArea" class="input-group" style="position: relative;">
<!-- Predictive Text Suggestions (ADD THIS HERE) -->
<div class="predictive-text" style="position: absolute; background: white; border: 1px solid #ccc; display: none; z-index: 1000; width: 100%;"></div>
<input type="text" id="text" name="msg" placeholder="Type your message..." autocomplete="off"
class="form-control type_msg" required />
<div class="input-group-append">
<button type="submit" id="send" class="input-group-text send_btn">
<i class="fas fa-location-arrow"></i>
</button>
</div>
</form>
<button id="clearChat" class="btn btn-danger mt-2">Clear Chat</button>
</div>
</div>
</div>
</div>
<script>
$(document).ready(function() {
let chatHistory = []; // Stores the user's input history
let currentIndex = -1; // Tracks the current position in the history
// Handle predictive text
$("#text").on("input", function() {
var input = $(this).val().toLowerCase();
var predictions = getPredictiveText(input);
showPredictiveText(predictions);
});
$(document).on("click", ".predictive-text p", function() {
var text = $(this).text();
$("#text").val(text);
$(".predictive-text").hide();
});
function getPredictiveText(input) {
var predictions = [];
var patterns = {
"greeting": ["Hi", "Hey", "How are you", "what's up", "Is anyone there?", "Hello", "Good day"],
"name": ["what is your name", "name", "what's your name", "who are you", "what should I call you"]
};
for (var tag in patterns) {
patterns[tag].forEach(function(pattern) {
if (pattern.toLowerCase().startsWith(input) && !predictions.includes(pattern)) {
predictions.push(pattern);
}
});
}
return predictions;
}
function showPredictiveText(predictions) {
if (predictions.length > 0) {
var html = "";
predictions.forEach(function(prediction) {
html += "<p>" + prediction + "</p>";
});
$(".predictive-text").html(html).show();
} else {
$(".predictive-text").hide();
}
}
// Handle message submission and user input history
$("#messageArea").on("submit", function(event) {
$(".predictive-text").hide(); // Hide predictive suggestions
const date = new Date();
const hour = date.getHours().toString().padStart(2, '0');
const minute = date.getMinutes().toString().padStart(2, '0');
const str_time = hour + ":" + minute;
var rawText = $("#text").val();
chatHistory.push(rawText); // Store the user's message in history
currentIndex = chatHistory.length - 1; // Update the current index
var userHtml = `
<div class="d-flex justify-content-end mb-4">
<div class="msg_cotainer_send">${rawText}
<span class="msg_time_send">${str_time}</span>
</div>
<div class="img_cont_msg">
<img src="/static/userlogo.png" class="rounded-circle user_img_msg">
</div>
</div>`;
$("#text").val(""); // Clear the input field
$("#messageFormeight").append(userHtml); // Add the user message to the chat
$(document).ready(function () {
let chatHistory = JSON.parse(localStorage.getItem("chatHistory")) || []; // Load stored history
let currentIndex = chatHistory.length; // Set index to last message
$("#messageArea").on("submit", function (event) {
event.preventDefault(); // Prevent form submission
let rawText = $("#text").val().trim();
if (rawText === "") return; // Ignore empty input
const time = new Date().toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' });
// Append user message to chat
let userHtml = `
<div class="d-flex justify-content-end mb-4">
<div class="msg_cotainer_send">${rawText}<span class="msg_time_send">${time}</span></div>
<div class="img_cont_msg"><img src="https://i.ibb.co/d5b84Xw/Untitled-design.png" class="rounded-circle user_img_msg"></div>
</div>`;
$("#messageFormeight").append(userHtml);
// Update chat history
chatHistory.push(rawText);
localStorage.setItem("chatHistory", JSON.stringify(chatHistory)); // Save in localStorage
currentIndex = chatHistory.length; // Reset index to end
// Clear input field & scroll down
$("#text").val("");
$("#messageFormeight").animate({ scrollTop: $("#messageFormeight")[0].scrollHeight }, 500);
// Send message to server
$.ajax({
data: { msg: rawText },
type: "POST",
url: "/get",
}).done(function (response) {
let botHtml = `
<div class="d-flex justify-content-start mb-4">
<div class="img_cont_msg">
<img src="/static/juitlogo.png" class="rounded-circle user_img_msg">
</div>
<div class="msg_cotainer">${response}<span class="msg_time">${time}</span></div>
</div>`;
$("#messageFormeight").append($.parseHTML(botHtml));
$("#messageFormeight").animate({ scrollTop: $("#messageFormeight")[0].scrollHeight }, 500);
});
});
// Clear chat history
$("#clearChat").click(function () {
$("#messageFormeight").html(""); // Clears UI
chatHistory = []; // Clears array
localStorage.removeItem("chatHistory"); // Clears from storage
currentIndex = 0;
});
});
// Send the message to the server via AJAX
$.ajax({
data: {
msg: rawText,
},
type: "POST",
url: "/get",
}).done(function(data) {
var lines = data.split("**");
var botHtml = '<div class="d-flex justify-content-start mb-4"><div class="img_cont_msg"><img src="/static/juitlogo.png" class="rounded-circle user_img_msg"></div><div class="msg_cotainer">';
for (var i = 0; i < lines.length; i++) {
botHtml += '<div class="msg_cotainer">' + lines[i] + '</div>';
}
botHtml += '<span class="msg_time">' + str_time + '</span></div></div>';
$("#messageFormeight").append($.parseHTML(botHtml));
// Smooth scroll to the bottom of the chat
var container = $("#messageFormeight");
container.animate({ scrollTop: container.prop("scrollHeight") }, 500);
});
event.preventDefault(); // Prevent form submission
});
});
</script>
<script>
document.addEventListener("DOMContentLoaded", () => {
const themeToggle = document.getElementById("themeToggle");
if (!themeToggle) return; // Prevent errors if button is missing
const body = document.body;
// Check local storage for a saved theme, default to dark
const savedTheme = localStorage.getItem("theme") || "dark";
body.classList.toggle("light-mode", savedTheme === "light");
updateButton(); // Update button text on load
// Toggle theme on button click
themeToggle.addEventListener("click", () => {
body.classList.toggle("light-mode");
const newTheme = body.classList.contains("light-mode") ? "light" : "dark";
localStorage.setItem("theme", newTheme);
updateButton();
});
function updateButton() {
themeToggle.textContent = body.classList.contains("light-mode") ? "Switch to Dark Mode" : "Switch to Light Mode";
}
});
function showPredictiveText(predictions) {
if (predictions.length > 0 && $("#text").val().trim() !== "") {
let html = predictions.map(p => `<p>${p}</p>`).join('');
$(".predictive-text").html(html).show();
} else {
$(".predictive-text").hide();
}
}
function scrollToBottom() {
$("#messageFormeight").animate({ scrollTop: $("#messageFormeight")[0].scrollHeight }, 500);
}
$(document).ready(function() {
$("#clearChat").click(function() {
$("#messageFormeight").html(""); // Clears the chat area
localStorage.removeItem("chatHistory"); // Clears stored history
});
});
</script>
</body>
</html>