Update script.js
Browse files
script.js
CHANGED
@@ -1,106 +1,233 @@
|
|
1 |
-
//
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
}
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
}
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
const
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
// Function to update the top label
|
2 |
+
function updateTopLabel(text) {
|
3 |
+
document.getElementById("topLabel").innerText = text;
|
4 |
+
}
|
5 |
+
|
6 |
+
// Function to update the history textarea
|
7 |
+
function updateHistory(userQuestion, botAnswer) {
|
8 |
+
const historyBox = document.getElementById("history");
|
9 |
+
const timestamp = new Date().toLocaleTimeString();
|
10 |
+
historyBox.value += `\n\n[${timestamp}] You: ${userQuestion}\n[${timestamp}] Bot: ${botAnswer}`;
|
11 |
+
historyBox.scrollTop = historyBox.scrollHeight; // Scroll to bottom
|
12 |
+
}
|
13 |
+
|
14 |
+
// Handle Generate button click
|
15 |
+
document.getElementById("generateBtn").addEventListener("click", async () => {
|
16 |
+
const questionInput = document.getElementById("question"); // Changed from questionInput
|
17 |
+
const answerOutput = document.getElementById("answer"); // Changed from responseOutput
|
18 |
+
const question = questionInput.value.trim();
|
19 |
+
|
20 |
+
if (!question) {
|
21 |
+
answerOutput.value = "Please enter your legal question.";
|
22 |
+
return;
|
23 |
+
}
|
24 |
+
|
25 |
+
answerOutput.value = "Generating...";
|
26 |
+
updateTopLabel("Generating answer...");
|
27 |
+
|
28 |
+
try {
|
29 |
+
const res = await fetch("/answer", {
|
30 |
+
method: "POST",
|
31 |
+
headers: { "Content-Type": "application/json" },
|
32 |
+
body: JSON.stringify({ question })
|
33 |
+
});
|
34 |
+
|
35 |
+
if (!res.ok) {
|
36 |
+
const errorData = await res.json();
|
37 |
+
throw new Error(errorData.answer || "Failed to generate answer.");
|
38 |
+
}
|
39 |
+
|
40 |
+
const data = await res.json();
|
41 |
+
const answer = data.answer || "No answer received.";
|
42 |
+
|
43 |
+
answerOutput.value = answer;
|
44 |
+
updateTopLabel(question); // Update top label with the question
|
45 |
+
updateHistory(question, answer); // Update history with Q&A
|
46 |
+
|
47 |
+
} catch (err) {
|
48 |
+
answerOutput.value = `Error: ${err.message || err}`;
|
49 |
+
updateTopLabel("Error generating answer.");
|
50 |
+
}
|
51 |
+
});
|
52 |
+
|
53 |
+
// Handle Reset button
|
54 |
+
function resetApp() {
|
55 |
+
document.getElementById("question").value = "";
|
56 |
+
document.getElementById("answer").value = "";
|
57 |
+
document.getElementById("topLabel").innerText = "Dictate your legal question!";
|
58 |
+
document.getElementById("history").value = ""; // Clear history on reset
|
59 |
+
}
|
60 |
+
|
61 |
+
// Handle Read Aloud (using Web Speech API)
|
62 |
+
function readAloud() {
|
63 |
+
const text = document.getElementById("answer").value;
|
64 |
+
if (!text.trim()) {
|
65 |
+
alert("No answer to read aloud.");
|
66 |
+
return;
|
67 |
+
}
|
68 |
+
const synth = window.speechSynthesis;
|
69 |
+
// Stop any ongoing speech before starting a new one
|
70 |
+
synth.cancel();
|
71 |
+
const utterance = new SpeechSynthesisUtterance(text);
|
72 |
+
synth.speak(utterance);
|
73 |
+
}
|
74 |
+
|
75 |
+
// Handle Save/Print
|
76 |
+
function saveQA() {
|
77 |
+
const question = document.getElementById("question").value.trim();
|
78 |
+
const answer = document.getElementById("answer").value.trim();
|
79 |
+
|
80 |
+
if (!question || !answer) {
|
81 |
+
alert("Nothing to save.");
|
82 |
+
return;
|
83 |
+
}
|
84 |
+
|
85 |
+
const blob = new Blob([`Question:\n${question}\n\nAnswer:\n${answer}`], { type: "text/plain" });
|
86 |
+
const link = document.createElement("a");
|
87 |
+
link.href = URL.createObjectURL(blob);
|
88 |
+
link.download = "Legal_Assistant_QnA.txt";
|
89 |
+
document.body.appendChild(link);
|
90 |
+
link.click();
|
91 |
+
document.body.removeChild(link);
|
92 |
+
URL.revokeObjectURL(link.href); // Clean up the URL object
|
93 |
+
}
|
94 |
+
|
95 |
+
// Handle Upload MP3
|
96 |
+
async function uploadMP3() {
|
97 |
+
const fileInput = document.createElement('input');
|
98 |
+
fileInput.type = 'file';
|
99 |
+
fileInput.accept = '.mp3';
|
100 |
+
fileInput.onchange = async (e) => {
|
101 |
+
const file = e.target.files[0];
|
102 |
+
if (!file) {
|
103 |
+
document.getElementById("answer").value = "No file selected.";
|
104 |
+
return;
|
105 |
+
}
|
106 |
+
|
107 |
+
document.getElementById("answer").value = "Uploading and transcribing MP3...";
|
108 |
+
updateTopLabel("Processing MP3...");
|
109 |
+
|
110 |
+
const formData = new FormData();
|
111 |
+
formData.append('file', file);
|
112 |
+
|
113 |
+
try {
|
114 |
+
const res = await fetch("/upload-mp3", {
|
115 |
+
method: "POST",
|
116 |
+
body: formData
|
117 |
+
});
|
118 |
+
|
119 |
+
if (!res.ok) {
|
120 |
+
const errorData = await res.json();
|
121 |
+
throw new Error(errorData.message || "Failed to upload and transcribe MP3.");
|
122 |
+
}
|
123 |
+
|
124 |
+
const data = await res.json();
|
125 |
+
const transcription = data.transcription || "Could not transcribe audio.";
|
126 |
+
|
127 |
+
updateTopLabel(transcription);
|
128 |
+
document.getElementById("question").value = transcription; // Put transcription in question box
|
129 |
+
document.getElementById("answer").value = "Transcription complete. You can now summarize or generate an answer.";
|
130 |
+
|
131 |
+
// Optional: Prompt user to summarize or generate an answer
|
132 |
+
const summarizeConfirm = confirm("MP3 transcribed. Do you want to summarize it?");
|
133 |
+
if (summarizeConfirm) {
|
134 |
+
const summaryRes = await fetch("/summarize", {
|
135 |
+
method: "POST",
|
136 |
+
headers: { "Content-Type": "application/json" },
|
137 |
+
body: JSON.stringify({ text: transcription })
|
138 |
+
});
|
139 |
+
const summaryData = await summaryRes.json();
|
140 |
+
if (summaryRes.ok) {
|
141 |
+
document.getElementById("question").value = summaryData.summary;
|
142 |
+
document.getElementById("answer").value = "Summary pasted in question box. You can now generate an answer.";
|
143 |
+
updateTopLabel("Summary ready.");
|
144 |
+
} else {
|
145 |
+
document.getElementById("answer").value = `Error summarizing: ${summaryData.summary}`;
|
146 |
+
}
|
147 |
+
} else {
|
148 |
+
const generateConfirm = confirm("Do you want to generate an answer from the full transcription?");
|
149 |
+
if (generateConfirm) {
|
150 |
+
// Trigger generateAnswer function with the transcription
|
151 |
+
document.getElementById("generateBtn").click(); // Simulate click on generate button
|
152 |
+
}
|
153 |
+
}
|
154 |
+
|
155 |
+
} catch (err) {
|
156 |
+
document.getElementById("answer").value = `Error: ${err.message || err}`;
|
157 |
+
updateTopLabel("Error processing MP3.");
|
158 |
+
}
|
159 |
+
};
|
160 |
+
fileInput.click(); // Open file dialog
|
161 |
+
}
|
162 |
+
|
163 |
+
// Handle Dictate (Web Speech API)
|
164 |
+
let recognition; // Keep recognition object in scope to stop it
|
165 |
+
|
166 |
+
function handleDictate() {
|
167 |
+
if (!('webkitSpeechRecognition' in window)) {
|
168 |
+
alert("Speech recognition not supported in this browser. Use Chrome.");
|
169 |
+
return;
|
170 |
+
}
|
171 |
+
|
172 |
+
// If already listening, stop it
|
173 |
+
if (recognition && (recognition.recognizing || recognition.continuous)) {
|
174 |
+
recognition.stop();
|
175 |
+
document.getElementById("topLabel").innerText = "Dictate your legal question!";
|
176 |
+
// If you want to automatically trigger answer generation after stopping dictation:
|
177 |
+
// document.getElementById("generateBtn").click();
|
178 |
+
return;
|
179 |
+
}
|
180 |
+
|
181 |
+
recognition = new webkitSpeechRecognition();
|
182 |
+
recognition.lang = "en-US";
|
183 |
+
recognition.interimResults = false;
|
184 |
+
recognition.maxAlternatives = 1;
|
185 |
+
// recognition.continuous = true; // For continuous listening
|
186 |
+
|
187 |
+
document.getElementById("topLabel").innerText = "Listening... 🎙 (Click Dictate again to stop)";
|
188 |
+
document.getElementById("question").value = ""; // Clear previous question
|
189 |
+
|
190 |
+
recognition.onresult = function (event) {
|
191 |
+
const transcript = event.results[0][0].transcript;
|
192 |
+
document.getElementById("question").value = transcript;
|
193 |
+
updateTopLabel(transcript);
|
194 |
+
// Automatically generate answer after dictation
|
195 |
+
document.getElementById("generateBtn").click();
|
196 |
+
};
|
197 |
+
|
198 |
+
recognition.onerror = function (event) {
|
199 |
+
console.error("Speech recognition error:", event.error);
|
200 |
+
document.getElementById("topLabel").innerText = "Could not recognize speech or an error occurred.";
|
201 |
+
};
|
202 |
+
|
203 |
+
recognition.onend = function () {
|
204 |
+
console.log("Speech recognition ended.");
|
205 |
+
// Only revert label if not actively listening or if an error occurred during listening
|
206 |
+
if (!recognition.recognizing) {
|
207 |
+
document.getElementById("topLabel").innerText = "Dictate your legal question!";
|
208 |
+
}
|
209 |
+
};
|
210 |
+
|
211 |
+
recognition.start();
|
212 |
+
}
|
213 |
+
|
214 |
+
|
215 |
+
// Event listeners (ensure they are properly mapped to the new HTML IDs)
|
216 |
+
document.addEventListener("DOMContentLoaded", () => {
|
217 |
+
// The existing event listener for "generateBtn" is already set up.
|
218 |
+
// Ensure the button IDs in HTML match what's used in JS.
|
219 |
+
// current IDs:
|
220 |
+
// Dictate: handleDictate()
|
221 |
+
// Generate Response: generateAnswer() -> now mapped to generateBtn click event
|
222 |
+
// Read Aloud: readAloud()
|
223 |
+
// Upload MP3: uploadMP3()
|
224 |
+
// Save/Print: saveQA()
|
225 |
+
// Reset: resetApp()
|
226 |
+
|
227 |
+
// Add event listeners for other buttons if they don't have inline onclick
|
228 |
+
// For example, if you replace onclick="generateAnswer()" with id="generateBtn"
|
229 |
+
// then the document.getElementById("generateBtn").addEventListener("click", ...) handles it.
|
230 |
+
|
231 |
+
// If your HTML buttons use onclick, make sure the function names match these JS functions.
|
232 |
+
// Example: <button onclick="handleDictate()">
|
233 |
+
});
|