Spaces:
Sleeping
Sleeping
File size: 5,499 Bytes
4304dd8 |
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 |
// JavaScript code will be added here
let accessToken = "";
function showSection(sectionId) {
document
.querySelectorAll(".container > div")
.forEach((div) => (div.style.display = "none"));
document.getElementById(sectionId).style.display = "block";
}
document
.getElementById("loginLink")
.addEventListener("click", () => showSection("loginForm"));
document
.getElementById("registerLink")
.addEventListener("click", () => showSection("registerForm"));
document
.getElementById("uploadLink")
.addEventListener("click", () => showSection("uploadForm"));
document
.getElementById("chatLink")
.addEventListener("click", () => showSection("chatSection"));
document
.getElementById("historyLink")
.addEventListener("click", () => showSection("historySection"));
async function register() {
const username = document.getElementById("registerUsername").value;
const password = document.getElementById("registerPassword").value;
try {
const response = await fetch("/register", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ username, password }),
});
const data = await response.json();
alert(data.msg);
if (response.ok) {
showSection("loginForm");
}
} catch (error) {
console.error("Error:", error);
alert("An error occurred during registration");
}
}
async function login() {
const username = document.getElementById("loginUsername").value;
const password = document.getElementById("loginPassword").value;
try {
const response = await fetch("/login", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ username, password }),
});
const data = await response.json();
if (response.ok) {
accessToken = data.access_token;
alert("Login successful");
showSection("uploadForm");
} else {
alert(data.msg);
}
} catch (error) {
console.error("Error:", error);
alert("An error occurred during login");
}
}
async function uploadFile() {
const fileInput = document.getElementById("fileUpload");
const file = fileInput.files[0];
if (!file) {
alert("Please select a file");
return;
}
const formData = new FormData();
formData.append("file", file);
try {
const response = await fetch("/upload", {
method: "POST",
headers: {
Authorization: `Bearer ${accessToken}`,
},
body: formData,
});
const data = await response.json();
alert(data.msg);
if (response.ok) {
showSection("chatSection");
}
} catch (error) {
console.error("Error:", error);
alert("An error occurred during file upload");
}
}
async function sendMessage() {
const chatInput = document.getElementById("chatInput");
const query = chatInput.value;
if (!query) return;
try {
const response = await fetch("/chat", {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${accessToken}`,
},
body: JSON.stringify({ query }),
});
const data = await response.json();
if (response.ok) {
displayMessage(query, "user");
displayMessage(data.response, "assistant");
chatInput.value = "";
} else {
alert(data.msg);
}
} catch (error) {
console.error("Error:", error);
alert("An error occurred during chat");
}
}
function displayMessage(message, sender) {
const chatHistory = document.getElementById("chatHistory");
const messageElement = document.createElement("div");
messageElement.classList.add("chat-message", `${sender}-message`);
messageElement.textContent = message;
chatHistory.appendChild(messageElement);
chatHistory.scrollTop = chatHistory.scrollHeight;
}
async function loadChatHistory() {
try {
const response = await fetch("/history", {
headers: {
Authorization: `Bearer ${accessToken}`,
},
});
const data = await response.json();
if (response.ok) {
const historyList = document.getElementById("chatHistoryList");
historyList.innerHTML = "";
data.history.forEach((entry) => {
const historyItem = document.createElement("div");
/*historyItem.innerHTML = `
<p><strong>User:</strong> ${entry.message}</p>
<p><strong>Assistant:</strong> ${entry.response}</p>
<p><small>${new Date(
entry.timestamp
).toLocaleString()}</small></p>
<hr>
`; */
historyItem.innerHTML = `<div class="chat-history-interaction">
<div><p class="user-msg">${entry.message}</p></div>
<div><p class="chatbot-msg">${
entry.response
}</p></div>
<div><p class="timestamp-msg">${new Date(
entry.timestamp
).toLocaleString()}</p></div>
</div>`;
historyList.appendChild(historyItem);
});
} else {
alert(data.msg);
}
} catch (error) {
console.error("Error:", error);
alert("An error occurred while loading chat history");
}
}
document
.getElementById("historyLink")
.addEventListener("click", loadChatHistory);
|