Spaces:
Sleeping
Sleeping
Update static/script.js
Browse files- static/script.js +28 -27
static/script.js
CHANGED
|
@@ -1,36 +1,37 @@
|
|
| 1 |
-
document.getElementById("
|
| 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 |
});
|
|
|
|
| 1 |
+
document.getElementById("submit-btn").addEventListener("click", async () => {
|
| 2 |
+
const context = document.getElementById("context").value.trim();
|
| 3 |
+
const questions = document.getElementById("question").value.trim(); // fixed ID
|
| 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", { // fixed URL
|
| 10 |
+
method: "POST",
|
| 11 |
+
headers: { "Content-Type": "application/json" },
|
| 12 |
+
body: JSON.stringify({ context, question: questions }) // key must match backend
|
| 13 |
+
});
|
| 14 |
|
| 15 |
+
const data = await res.json();
|
| 16 |
|
| 17 |
+
if (data.error) {
|
| 18 |
+
output.textContent = "⚠️ " + data.error;
|
| 19 |
+
return;
|
| 20 |
+
}
|
| 21 |
|
| 22 |
+
let text = "";
|
| 23 |
+
data.results.forEach((r, i) => {
|
| 24 |
+
text += `Q${i + 1}: ${r.question}\nAnswer: ${r.answer}\nConfidence: ${r.score}\n${"-".repeat(50)}\n`;
|
| 25 |
+
});
|
| 26 |
+
output.textContent = text;
|
| 27 |
|
| 28 |
+
// Add to history
|
| 29 |
+
const li = document.createElement("li");
|
| 30 |
+
li.textContent = questions.replace(/\n/g, " | ");
|
| 31 |
+
history.prepend(li);
|
| 32 |
});
|
| 33 |
|
| 34 |
+
// Fixed dark mode toggle
|
| 35 |
+
document.getElementById("dark-toggle").addEventListener("click", () => {
|
| 36 |
+
document.body.classList.toggle("dark-mode");
|
| 37 |
});
|