Spaces:
Sleeping
Sleeping
File size: 3,130 Bytes
f146512 ab1673d b4b22ff ab1673d b4b22ff ab1673d f146512 b4b22ff f146512 ab1673d a937a1d f146512 ab1673d f146512 b4b22ff f146512 b4b22ff 2661c30 f146512 ab1673d 2bc19f1 ab1673d 2bc19f1 ab1673d f146512 2bc19f1 b4b22ff 2bc19f1 b4b22ff 2bc19f1 ab1673d 2bc19f1 ab1673d 2bc19f1 ab1673d 2bc19f1 b4b22ff f146512 18bd142 22e7ab2 2bc19f1 ab1673d 2bc19f1 ab1673d 2bc19f1 ab1673d f146512 | 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 | let sessionId = null;
function showMessage(msg, isError = false, persist = false) {
const messageBox = document.getElementById("message-box");
messageBox.style.color = isError ? "#ff4d4d" : "#00ffcc";
messageBox.textContent = msg;
if (!persist) {
setTimeout(() => messageBox.textContent = "", 4000);
}
}
document.getElementById("pdf-upload").addEventListener("change", function () {
const file = this.files[0];
const displayName = document.getElementById("selected-file-name");
if (file) {
displayName.textContent = `Selected: ${file.name}`;
} else {
displayName.textContent = "";
}
});
function uploadPDF() {
const file = document.getElementById("pdf-upload").files[0];
if (!file) {
showMessage("Please select a PDF file first.", true);
return;
}
const formData = new FormData();
formData.append("pdf", file);
showMessage("Uploading PDF... please wait.", false, true);
fetch("/upload", {
method: "POST",
body: formData
})
.then(res => res.json())
.then(data => {
if (data.session_id) {
sessionId = data.session_id;
document.getElementById("chat-section").style.display = "block";
showMessage("PDF uploaded! You can now start chatting.");
} else {
showMessage(data.error || "Upload failed.", true);
}
})
.catch(err => {
console.error(err);
showMessage("An error occurred during upload.", true);
});
}
function sendQuestion() {
const input = document.getElementById("question");
const question = input.value.trim();
if (!question) return;
const formData = new FormData();
formData.append("session_id", sessionId);
formData.append("question", question);
addMessage(question, "user");
input.value = "";
fetch("/query", {
method: "POST",
body: formData
})
.then(res => res.json())
.then(data => {
addMessage(data.answer, "bot", true); // render markdown
})
.catch(err => {
console.error(err);
addMessage("Error fetching response from server.", "bot");
});
}
function addMessage(message, sender, isMarkdown = false) {
const chatBox = document.getElementById("chat-box");
const messageRow = document.createElement("div");
messageRow.classList.add("chat-message", sender === "user" ? "chat-user" : "chat-bot");
const bubble = document.createElement("div");
bubble.classList.add("chat-bubble");
if (isMarkdown) {
bubble.innerHTML = marked.parse(message || "");
} else {
bubble.textContent = message;
}
messageRow.appendChild(bubble);
chatBox.appendChild(messageRow);
chatBox.scrollTop = chatBox.scrollHeight;
}
function clearSession() {
const formData = new FormData();
formData.append("session_id", sessionId);
fetch("/clear", {
method: "POST",
body: formData
})
.then(() => {
sessionId = null;
document.getElementById("chat-box").innerHTML = "";
document.getElementById("chat-section").style.display = "none";
showMessage("Session cleared.");
})
.catch(() => {
showMessage("Failed to clear session.", true);
});
}
|