assistant / script.js
fizzarif7's picture
Upload 8 files
ce4fe6e verified
raw
history blame
3.27 kB
// Handle Generate button click
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;
// Update top label
document.getElementById("topLabel").innerText = question;
// Update history
historyBox.value += `\n\nYou: ${question}\nBot: ${answer}`;
} catch (err) {
responseBox.value = `Error: ${err}`;
}
});
// Handle Reset button
function resetApp() {
document.getElementById("questionInput").value = "";
document.getElementById("responseOutput").value = "";
document.getElementById("topLabel").innerText = "Dictate your legal question!";
}
// Handle Read Aloud
function readAloud() {
const text = document.getElementById("responseOutput").value;
if (!text.trim()) return;
const synth = window.speechSynthesis;
const utterance = new SpeechSynthesisUtterance(text);
synth.speak(utterance);
}
// Handle Save
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);
}
// Handle Upload MP3 (Disabled in web)
function uploadMP3() {
alert("πŸ“ MP3 upload is only supported in the desktop assistant.");
}
// Handle Dictate (Web Speech API)
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();
}