Spaces:
Sleeping
Sleeping
Update static/script.js
Browse files- static/script.js +28 -7
static/script.js
CHANGED
|
@@ -1,15 +1,17 @@
|
|
| 1 |
document.getElementById("submit-btn").addEventListener("click", async () => {
|
| 2 |
const context = document.getElementById("context").value.trim();
|
| 3 |
-
const questions = document.getElementById("question").value.trim();
|
| 4 |
const output = document.getElementById("answer-box");
|
| 5 |
const history = document.getElementById("history-list");
|
| 6 |
|
|
|
|
|
|
|
| 7 |
output.textContent = "⏳ Generating answers...";
|
| 8 |
|
| 9 |
-
const res = await fetch("/predict", {
|
| 10 |
method: "POST",
|
| 11 |
headers: { "Content-Type": "application/json" },
|
| 12 |
-
body: JSON.stringify({ context, question: questions })
|
| 13 |
});
|
| 14 |
|
| 15 |
const data = await res.json();
|
|
@@ -19,11 +21,30 @@ document.getElementById("submit-btn").addEventListener("click", async () => {
|
|
| 19 |
return;
|
| 20 |
}
|
| 21 |
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
| 23 |
data.results.forEach((r, i) => {
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
});
|
| 26 |
-
output.textContent = text;
|
| 27 |
|
| 28 |
// Add to history
|
| 29 |
const li = document.createElement("li");
|
|
@@ -31,7 +52,7 @@ document.getElementById("submit-btn").addEventListener("click", async () => {
|
|
| 31 |
history.prepend(li);
|
| 32 |
});
|
| 33 |
|
| 34 |
-
//
|
| 35 |
document.getElementById("dark-toggle").addEventListener("click", () => {
|
| 36 |
document.body.classList.toggle("dark-mode");
|
| 37 |
});
|
|
|
|
| 1 |
document.getElementById("submit-btn").addEventListener("click", async () => {
|
| 2 |
const context = document.getElementById("context").value.trim();
|
| 3 |
+
const questions = document.getElementById("question").value.trim();
|
| 4 |
const output = document.getElementById("answer-box");
|
| 5 |
const history = document.getElementById("history-list");
|
| 6 |
|
| 7 |
+
// Clear previous answers
|
| 8 |
+
output.innerHTML = "";
|
| 9 |
output.textContent = "⏳ Generating answers...";
|
| 10 |
|
| 11 |
+
const res = await fetch("/predict", {
|
| 12 |
method: "POST",
|
| 13 |
headers: { "Content-Type": "application/json" },
|
| 14 |
+
body: JSON.stringify({ context, question: questions })
|
| 15 |
});
|
| 16 |
|
| 17 |
const data = await res.json();
|
|
|
|
| 21 |
return;
|
| 22 |
}
|
| 23 |
|
| 24 |
+
// Clear loading text
|
| 25 |
+
output.innerHTML = "";
|
| 26 |
+
|
| 27 |
+
// Display each Q&A nicely
|
| 28 |
data.results.forEach((r, i) => {
|
| 29 |
+
const qaDiv = document.createElement("div");
|
| 30 |
+
qaDiv.className = "mb-4 p-3 bg-indigo-50 dark:bg-gray-800 rounded-lg shadow";
|
| 31 |
+
|
| 32 |
+
const qElem = document.createElement("p");
|
| 33 |
+
qElem.innerHTML = `<strong>Q${i + 1}:</strong> ${r.question}`;
|
| 34 |
+
qElem.className = "font-semibold";
|
| 35 |
+
|
| 36 |
+
const aElem = document.createElement("p");
|
| 37 |
+
aElem.innerHTML = `<strong>Answer:</strong> ${r.answer}`;
|
| 38 |
+
|
| 39 |
+
const sElem = document.createElement("p");
|
| 40 |
+
sElem.innerHTML = `<strong>Confidence:</strong> ${r.score}`;
|
| 41 |
+
|
| 42 |
+
qaDiv.appendChild(qElem);
|
| 43 |
+
qaDiv.appendChild(aElem);
|
| 44 |
+
qaDiv.appendChild(sElem);
|
| 45 |
+
|
| 46 |
+
output.appendChild(qaDiv);
|
| 47 |
});
|
|
|
|
| 48 |
|
| 49 |
// Add to history
|
| 50 |
const li = document.createElement("li");
|
|
|
|
| 52 |
history.prepend(li);
|
| 53 |
});
|
| 54 |
|
| 55 |
+
// Dark mode toggle
|
| 56 |
document.getElementById("dark-toggle").addEventListener("click", () => {
|
| 57 |
document.body.classList.toggle("dark-mode");
|
| 58 |
});
|