| |
| function updateTopLabel(text) { |
| document.getElementById("topLabel").innerText = text; |
| } |
|
|
| |
| function updateHistory(userQuestion, botAnswer) { |
| const historyBox = document.getElementById("history"); |
| const timestamp = new Date().toLocaleTimeString(); |
| historyBox.value += `\n\n[${timestamp}] You: ${userQuestion}\n[${timestamp}] Bot: ${botAnswer}`; |
| historyBox.scrollTop = historyBox.scrollHeight; |
| } |
|
|
| |
| document.getElementById("generateBtn").addEventListener("click", async () => { |
| const questionInput = document.getElementById("question"); |
| const answerOutput = document.getElementById("answer"); |
| const question = questionInput.value.trim(); |
|
|
| if (!question) { |
| answerOutput.value = "Please enter your legal question."; |
| return; |
| } |
|
|
| answerOutput.value = "Generating..."; |
| updateTopLabel("Generating answer..."); |
|
|
| try { |
| const res = await fetch("/answer", { |
| method: "POST", |
| headers: { "Content-Type": "application/json" }, |
| body: JSON.stringify({ question }) |
| }); |
|
|
| if (!res.ok) { |
| const errorData = await res.json(); |
| throw new Error(errorData.answer || "Failed to generate answer."); |
| } |
|
|
| const data = await res.json(); |
| const answer = data.answer || "No answer received."; |
|
|
| answerOutput.value = answer; |
| updateTopLabel(question); |
| updateHistory(question, answer); |
|
|
| } catch (err) { |
| answerOutput.value = `Error: ${err.message || err}`; |
| updateTopLabel("Error generating answer."); |
| } |
| }); |
|
|
| |
| function resetApp() { |
| document.getElementById("question").value = ""; |
| document.getElementById("answer").value = ""; |
| document.getElementById("topLabel").innerText = "Dictate your legal question!"; |
| document.getElementById("history").value = ""; |
|
|
| |
| if (window.speechSynthesis && window.speechSynthesis.speaking) { |
| window.speechSynthesis.cancel(); |
| } |
| if (window.recognition && (window.recognition.recognizing || window.recognition.continuous)) { |
| window.recognition.stop(); |
| } |
| } |
|
|
| |
| function readAloud() { |
| const text = document.getElementById("answer").value; |
| if (!text.trim()) { |
| alert("No answer to read aloud."); |
| return; |
| } |
| const synth = window.speechSynthesis; |
| |
| synth.cancel(); |
| const utterance = new SpeechSynthesisUtterance(text); |
| synth.speak(utterance); |
| } |
|
|
| |
| function saveQA() { |
| const question = document.getElementById("question").value.trim(); |
| const answer = document.getElementById("answer").value.trim(); |
|
|
| if (!question || !answer) { |
| alert("Nothing to save."); |
| return; |
| } |
|
|
| |
| const confirmSave = confirm("Are you sure you want to save this Q&A?"); |
|
|
| if (confirmSave) { |
| 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 = "Legal_Assistant_QnA.txt"; |
| document.body.appendChild(link); |
| link.click(); |
| document.body.removeChild(link); |
| URL.revokeObjectURL(link.href); |
| alert("Q&A saved successfully!"); |
| } else { |
| alert("Save operation cancelled."); |
| } |
| } |
|
|
| |
| async function uploadMP3() { |
| const fileInput = document.createElement('input'); |
| fileInput.type = 'file'; |
| fileInput.accept = '.mp3'; |
| fileInput.onchange = async (e) => { |
| const file = e.target.files[0]; |
| if (!file) { |
| document.getElementById("answer").value = "No file selected."; |
| return; |
| } |
|
|
| document.getElementById("answer").value = "Uploading and transcribing MP3..."; |
| updateTopLabel("Processing MP3..."); |
|
|
| const formData = new FormData(); |
| formData.append('file', file); |
|
|
| try { |
| const res = await fetch("/upload-mp3", { |
| method: "POST", |
| body: formData |
| }); |
|
|
| if (!res.ok) { |
| const errorData = await res.json(); |
| throw new Error(errorData.message || "Failed to upload and transcribe MP3."); |
| } |
|
|
| const data = await res.json(); |
| const transcription = data.transcription || "Could not transcribe audio."; |
|
|
| updateTopLabel(transcription); |
| document.getElementById("question").value = transcription; |
| document.getElementById("answer").value = "Transcription complete. You can now summarize or generate an answer."; |
|
|
| |
| const summarizeConfirm = confirm("MP3 transcribed. Do you want to summarize it?"); |
| if (summarizeConfirm) { |
| const summaryRes = await fetch("/summarize", { |
| method: "POST", |
| headers: { "Content-Type": "application/json" }, |
| body: JSON.stringify({ text: transcription }) |
| }); |
| const summaryData = await summaryRes.json(); |
| if (summaryRes.ok) { |
| document.getElementById("question").value = summaryData.summary; |
| document.getElementById("answer").value = "Summary pasted in question box. You can now generate an answer."; |
| updateTopLabel("Summary ready."); |
| } else { |
| document.getElementById("answer").value = `Error summarizing: ${summaryData.summary}`; |
| } |
| } else { |
| const generateConfirm = confirm("Do you want to generate an answer from the full transcription?"); |
| if (generateConfirm) { |
| |
| document.getElementById("generateBtn").click(); |
| } |
| } |
|
|
| } catch (err) { |
| document.getElementById("answer").value = `Error: ${err.message || err}`; |
| updateTopLabel("Error processing MP3."); |
| } |
| }; |
| fileInput.click(); |
| } |
|
|
| |
| let recognition; |
|
|
| function handleDictate() { |
| if (!('webkitSpeechRecognition' in window)) { |
| alert("Speech recognition not supported in this browser. Use Chrome."); |
| return; |
| } |
|
|
| |
| if (recognition && (recognition.recognizing || recognition.continuous)) { |
| recognition.stop(); |
| document.getElementById("topLabel").innerText = "Dictate your legal question!"; |
| return; |
| } |
|
|
| recognition = new webkitSpeechRecognition(); |
| recognition.lang = "en-US"; |
| recognition.interimResults = false; |
| recognition.maxAlternatives = 1; |
| |
|
|
| document.getElementById("topLabel").innerText = "Listening... ๐ (Click Dictate again to stop)"; |
| document.getElementById("question").value = ""; |
|
|
| recognition.onresult = function (event) { |
| const transcript = event.results[0][0].transcript; |
| document.getElementById("question").value = transcript; |
| updateTopLabel(transcript); |
| |
| document.getElementById("generateBtn").click(); |
| }; |
|
|
| recognition.onerror = function (event) { |
| console.error("Speech recognition error:", event.error); |
| document.getElementById("topLabel").innerText = "Could not recognize speech or an error occurred."; |
| }; |
|
|
| recognition.onend = function () { |
| console.log("Speech recognition ended."); |
| |
| if (!recognition.recognizing) { |
| document.getElementById("topLabel").innerText = "Dictate your legal question!"; |
| } |
| }; |
|
|
| recognition.start(); |
| } |
|
|
|
|
| |
| document.addEventListener("DOMContentLoaded", () => { |
| |
| |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| }); |