File size: 3,268 Bytes
ce4fe6e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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
107
// 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();
}