MedhaCodes commited on
Commit
0944dd7
·
verified ·
1 Parent(s): df50022

Update static/script.js

Browse files
Files changed (1) hide show
  1. 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(); // 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();
@@ -19,11 +21,30 @@ document.getElementById("submit-btn").addEventListener("click", async () => {
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");
@@ -31,7 +52,7 @@ document.getElementById("submit-btn").addEventListener("click", async () => {
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
  });
 
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
  });