|
|
|
document.getElementById("generateBtn").addEventListener("click", async () => {
|
|
const question = document.getElementById("questionInput").value.trim();
|
|
const responseBox = document.getElementById("responseOutput");
|
|
const historyBox = document.getElementById("history");
|
|
|
|
if (!question) {
|
|
responseBox.value = "Please enter your legal question.";
|
|
return;
|
|
}
|
|
|
|
responseBox.value = "Generating...";
|
|
|
|
try {
|
|
const res = await fetch("/answer", {
|
|
method: "POST",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify({ question })
|
|
});
|
|
|
|
const data = await res.json();
|
|
const answer = data.answer || "No answer received.";
|
|
|
|
responseBox.value = answer;
|
|
|
|
|
|
document.getElementById("topLabel").innerText = question;
|
|
|
|
|
|
historyBox.value += `\n\nYou: ${question}\nBot: ${answer}`;
|
|
} catch (err) {
|
|
responseBox.value = `Error: ${err}`;
|
|
}
|
|
});
|
|
|
|
|
|
function resetApp() {
|
|
document.getElementById("questionInput").value = "";
|
|
document.getElementById("responseOutput").value = "";
|
|
document.getElementById("topLabel").innerText = "Dictate your legal question!";
|
|
}
|
|
|
|
|
|
function readAloud() {
|
|
const text = document.getElementById("responseOutput").value;
|
|
if (!text.trim()) return;
|
|
const synth = window.speechSynthesis;
|
|
const utterance = new SpeechSynthesisUtterance(text);
|
|
synth.speak(utterance);
|
|
}
|
|
|
|
|
|
function saveQA() {
|
|
const question = document.getElementById("questionInput").value.trim();
|
|
const answer = document.getElementById("responseOutput").value.trim();
|
|
|
|
if (!question || !answer) {
|
|
alert("Nothing to save.");
|
|
return;
|
|
}
|
|
|
|
const blob = new Blob([`Question:\n${question}\n\nAnswer:\n${answer}`], { type: "text/plain" });
|
|
const link = document.createElement("a");
|
|
link.href = URL.createObjectURL(blob);
|
|
link.download = "QnA.txt";
|
|
document.body.appendChild(link);
|
|
link.click();
|
|
document.body.removeChild(link);
|
|
}
|
|
|
|
|
|
function uploadMP3() {
|
|
alert("π MP3 upload is only supported in the desktop assistant.");
|
|
}
|
|
|
|
|
|
function handleDictate() {
|
|
if (!('webkitSpeechRecognition' in window)) {
|
|
alert("Speech recognition not supported in this browser. Use Chrome.");
|
|
return;
|
|
}
|
|
|
|
const recognition = new webkitSpeechRecognition();
|
|
recognition.lang = "en-US";
|
|
recognition.interimResults = false;
|
|
recognition.maxAlternatives = 1;
|
|
|
|
document.getElementById("topLabel").innerText = "Listening... π";
|
|
|
|
recognition.onresult = function (event) {
|
|
const transcript = event.results[0][0].transcript;
|
|
document.getElementById("questionInput").value = transcript;
|
|
document.getElementById("topLabel").innerText = transcript;
|
|
};
|
|
|
|
recognition.onerror = function (event) {
|
|
console.error("Speech recognition error:", event.error);
|
|
document.getElementById("topLabel").innerText = "Could not recognize speech.";
|
|
};
|
|
|
|
recognition.onend = function () {
|
|
console.log("Speech recognition ended.");
|
|
};
|
|
|
|
recognition.start();
|
|
}
|
|
|