MedhaCodes commited on
Commit
9fbc238
·
verified ·
1 Parent(s): fb8d240

Update static/script.js

Browse files
Files changed (1) hide show
  1. static/script.js +28 -27
static/script.js CHANGED
@@ -1,36 +1,37 @@
1
- document.getElementById("get-answer").addEventListener("click", async () => {
2
- const context = document.getElementById("context").value.trim();
3
- const questions = document.getElementById("questions").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("/get_answer", {
10
- method: "POST",
11
- headers: {"Content-Type": "application/json"},
12
- body: JSON.stringify({ context, questions })
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
- document.getElementById("toggle-dark").addEventListener("click", () => {
35
- document.body.classList.toggle("dark-mode");
 
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
  });