Spaces:
Sleeping
Sleeping
Upload script.js
Browse files- static/script.js +44 -0
static/script.js
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
const form = document.getElementById("qa-form");
|
| 2 |
+
const input = document.getElementById("question-input");
|
| 3 |
+
const currentAnswer = document.getElementById("current-answer");
|
| 4 |
+
const historyList = document.getElementById("history-list");
|
| 5 |
+
|
| 6 |
+
form.addEventListener("submit", async (e) => {
|
| 7 |
+
e.preventDefault();
|
| 8 |
+
|
| 9 |
+
const question = input.value.trim();
|
| 10 |
+
if (!question) return;
|
| 11 |
+
|
| 12 |
+
currentAnswer.style.display = "block";
|
| 13 |
+
currentAnswer.innerHTML = "<p>Thinking...</p>";
|
| 14 |
+
|
| 15 |
+
try {
|
| 16 |
+
const response = await fetch("/ask", {
|
| 17 |
+
method: "POST",
|
| 18 |
+
headers: {
|
| 19 |
+
"Content-Type": "application/x-www-form-urlencoded",
|
| 20 |
+
},
|
| 21 |
+
body: new URLSearchParams({ query: question }),
|
| 22 |
+
});
|
| 23 |
+
|
| 24 |
+
const data = await response.json();
|
| 25 |
+
const answer = data.answer;
|
| 26 |
+
|
| 27 |
+
// Create history card
|
| 28 |
+
const historyItem = document.createElement("div");
|
| 29 |
+
historyItem.className = "history-item";
|
| 30 |
+
historyItem.innerHTML = `
|
| 31 |
+
<div class="history-question">${question}</div>
|
| 32 |
+
<div class="history-answer">${answer}</div>
|
| 33 |
+
`;
|
| 34 |
+
|
| 35 |
+
// Add to top of history
|
| 36 |
+
historyList.prepend(historyItem);
|
| 37 |
+
|
| 38 |
+
} catch (err) {
|
| 39 |
+
alert("Something went wrong. Try again.");
|
| 40 |
+
} finally {
|
| 41 |
+
currentAnswer.style.display = "none";
|
| 42 |
+
input.value = "";
|
| 43 |
+
}
|
| 44 |
+
});
|