Spaces:
Running
Running
File size: 4,418 Bytes
fa396c8 |
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 |
const chatBox = document.getElementById("chatBox");
const status = document.getElementById("status");
const typing = document.getElementById("typing");
const dropZone = document.getElementById("dropZone");
const fileInput = document.getElementById("fileInput");
const docInfo = document.getElementById("docInfo");
const docNameEl = document.getElementById("docName");
const activeDoc = document.getElementById("activeDoc");
const activeDocName = document.getElementById("activeDocName");
const uploadText = document.getElementById("uploadText");
/* =========================
DRAG & DROP + FILE SELECT
========================= */
// Click to browse
dropZone.onclick = () => fileInput.click();
// Drag over
dropZone.addEventListener("dragover", e => {
e.preventDefault();
dropZone.classList.add("dragover");
});
// Drag leave
dropZone.addEventListener("dragleave", () => {
dropZone.classList.remove("dragover");
});
// Drop file
dropZone.addEventListener("drop", e => {
e.preventDefault();
dropZone.classList.remove("dragover");
fileInput.files = e.dataTransfer.files;
if (fileInput.files.length > 0) {
const file = fileInput.files[0];
uploadText.innerText = "File selected β";
// β
SHOW FILE NAME IMMEDIATELY
docNameEl.innerText = file.name;
docInfo.classList.remove("hidden");
}
});
// Browse PDF selection
fileInput.addEventListener("change", () => {
if (fileInput.files.length > 0) {
const file = fileInput.files[0];
uploadText.innerText = "File selected β";
// β
SHOW FILE NAME IMMEDIATELY
docNameEl.innerText = file.name;
docInfo.classList.remove("hidden");
}
});
/* =========================
ASSISTANT FORMATTING
========================= */
function formatAssistant(text) {
let html = text;
html = html.replace(/\*\*(.+?)\*\*/g, "<h2 class='ans-heading'>$1</h2>");
html = html.replace(/β’ (.+)/g, "<li>$1</li>");
html = html.replace(/- (.+)/g, "<li>$1</li>");
html = html.replace(/(<li>.*<\/li>)/gs, "<ul class='ans-list'>$1</ul>");
html = html.replace(/INR\s?[0-9,]+\/-/g, "<span class='highlight'>$&</span>");
return html;
}
function addUser(text) {
const d = document.createElement("div");
d.className = "user-msg";
d.innerText = "You:\n" + text;
chatBox.appendChild(d);
}
function addBot(text) {
const d = document.createElement("div");
d.className = "bot-msg";
const source = activeDocName.innerText || "Uploaded Document";
d.innerHTML = `
<div style="font-size:12px;color:#777;margin-bottom:4px;">
π Answer from: <strong>${source}</strong>
</div>
${formatAssistant(text)}
`;
chatBox.appendChild(d);
}
/* =========================
UPLOAD DOCUMENT
========================= */
async function uploadFile() {
const file = fileInput.files[0];
if (!file) {
status.innerText = "Please select a document.";
status.className = "status error";
return;
}
status.innerText = "Processing document...";
status.className = "status";
const fd = new FormData();
fd.append("file", file);
try {
const res = await fetch("http://localhost:8000/upload", {
method: "POST",
body: fd
});
const data = await res.json();
// Backend-confirmed document name
docNameEl.innerText = data.document_name || file.name;
docInfo.classList.remove("hidden");
activeDocName.innerText = data.document_name || file.name;
activeDoc.classList.remove("hidden");
chatBox.innerHTML = "";
status.innerText = "Document indexed successfully β
";
} catch (err) {
status.innerText = "Indexing failed. Please try again.";
status.className = "status error";
}
}
/* =========================
CHAT
========================= */
async function sendMessage() {
const qInput = document.getElementById("question");
const q = qInput.value.trim();
if (!q) return;
addUser(q);
qInput.value = "";
typing.classList.remove("hidden");
try {
const res = await fetch("http://localhost:8000/chat", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ question: q })
});
const data = await res.json();
typing.classList.add("hidden");
addBot(data.answer);
} catch (err) {
typing.classList.add("hidden");
addBot("β Unable to get response. Please try again.");
}
chatBox.scrollTop = chatBox.scrollHeight;
}
|