ikraamkb commited on
Commit
5a540b1
·
verified ·
1 Parent(s): 2644d07

Update static/application.js

Browse files
Files changed (1) hide show
  1. static/application.js +76 -31
static/application.js CHANGED
@@ -1,7 +1,9 @@
 
1
  /*const inputField = document.querySelector(".qt input");
2
  const icons = document.querySelectorAll(".icons i");
3
  const convo = document.querySelector(".convo");
4
- const send = document.querySelector(".sendingQA")
 
5
  // Create hidden file inputs dynamically
6
  const imageInput = document.createElement("input");
7
  imageInput.type = "file";
@@ -22,7 +24,7 @@ let selectedType = null;
22
  // Icon click handlers
23
  icons[0].addEventListener("click", () => imageInput.click()); // image icon
24
  icons[1].addEventListener("click", () => docInput.click()); // file icon
25
- send.addEventListener("click", handleSubmit); // send
26
 
27
  imageInput.addEventListener("change", () => {
28
  selectedFile = imageInput.files[0];
@@ -37,11 +39,13 @@ docInput.addEventListener("change", () => {
37
  });
38
 
39
  function showSelectedFile(fileName, icon) {
40
- convo.innerHTML = `
41
- <div style="padding: 0.8rem; background-color: #f3f3f3; border-radius: 10px; margin: 1rem;">
42
- ${icon} <strong>Selected file:</strong> ${fileName}
43
- </div>
44
- `;
 
 
45
  }
46
 
47
  async function handleSubmit() {
@@ -55,7 +59,26 @@ async function handleSubmit() {
55
  formData.append("question", question);
56
  formData.append("file", selectedFile);
57
 
58
- convo.innerHTML = `<div style="padding: 1rem; background: #eee; border-radius: 8px;">Processing your request...</div>`;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59
 
60
  try {
61
  const response = await fetch("/predict", {
@@ -64,23 +87,32 @@ async function handleSubmit() {
64
  });
65
 
66
  const result = await response.json();
67
- if (result.answer) {
68
- convo.innerHTML = `
69
- <div style="padding: 1rem; background: #e1f7e1; border-radius: 10px; margin: 1rem;">
70
- <strong>Question:</strong> ${question}<br>
71
- ${selectedFile ? `<strong>${selectedType}:</strong> ${selectedFile.name}<br>` : ""}
72
- <strong>Answer:</strong> ${result.answer}
73
- </div>
74
- `;
75
- } else {
76
- convo.innerHTML = `<div style="color: red;">Error: ${result.error}</div>`;
77
- }
 
78
  } catch (error) {
79
- convo.innerHTML = `<div style="color: red;">Request failed: ${error.message}</div>`;
 
 
 
 
 
 
80
  }
81
 
82
- // Reset input field
83
  inputField.value = "";
 
 
84
  }
85
  */
86
  const inputField = document.querySelector(".qt input");
@@ -102,13 +134,23 @@ docInput.style.display = "none";
102
  document.body.appendChild(imageInput);
103
  document.body.appendChild(docInput);
104
 
 
 
 
 
 
 
 
 
 
 
105
  let selectedFile = null;
106
  let selectedType = null;
107
 
108
  // Icon click handlers
109
- icons[0].addEventListener("click", () => imageInput.click()); // image icon
110
- icons[1].addEventListener("click", () => docInput.click()); // file icon
111
- send.addEventListener("click", handleSubmit); // send button
112
 
113
  imageInput.addEventListener("change", () => {
114
  selectedFile = imageInput.files[0];
@@ -143,7 +185,6 @@ async function handleSubmit() {
143
  formData.append("question", question);
144
  formData.append("file", selectedFile);
145
 
146
- // Show question in convo
147
  const userBlock = document.createElement("div");
148
  userBlock.style.padding = "1rem";
149
  userBlock.style.background = "#e0e7ff";
@@ -155,7 +196,6 @@ async function handleSubmit() {
155
  `;
156
  convo.appendChild(userBlock);
157
 
158
- // Show loading message
159
  const loadingBlock = document.createElement("div");
160
  loadingBlock.style.padding = "1rem";
161
  loadingBlock.style.background = "#eee";
@@ -172,17 +212,23 @@ async function handleSubmit() {
172
 
173
  const result = await response.json();
174
 
175
- loadingBlock.remove(); // remove loading message
176
 
177
  const answerBlock = document.createElement("div");
178
  answerBlock.style.padding = "1rem";
179
  answerBlock.style.background = "#e1f7e1";
180
  answerBlock.style.borderRadius = "10px";
181
  answerBlock.style.margin = "1rem";
182
- answerBlock.innerHTML = `
183
- <strong>Answer:</strong> ${result.answer || result.error || "No response"}
184
- `;
185
  convo.appendChild(answerBlock);
 
 
 
 
 
 
 
 
186
  } catch (error) {
187
  loadingBlock.remove();
188
 
@@ -193,7 +239,6 @@ async function handleSubmit() {
193
  convo.appendChild(errorBlock);
194
  }
195
 
196
- // Reset input field and file
197
  inputField.value = "";
198
  selectedFile = null;
199
  selectedType = null;
 
1
+
2
  /*const inputField = document.querySelector(".qt input");
3
  const icons = document.querySelectorAll(".icons i");
4
  const convo = document.querySelector(".convo");
5
+ const send = document.querySelector(".sendingQA");
6
+
7
  // Create hidden file inputs dynamically
8
  const imageInput = document.createElement("input");
9
  imageInput.type = "file";
 
24
  // Icon click handlers
25
  icons[0].addEventListener("click", () => imageInput.click()); // image icon
26
  icons[1].addEventListener("click", () => docInput.click()); // file icon
27
+ send.addEventListener("click", handleSubmit); // send button
28
 
29
  imageInput.addEventListener("change", () => {
30
  selectedFile = imageInput.files[0];
 
39
  });
40
 
41
  function showSelectedFile(fileName, icon) {
42
+ const fileNotice = document.createElement("div");
43
+ fileNotice.style.padding = "0.8rem";
44
+ fileNotice.style.backgroundColor = "#f3f3f3";
45
+ fileNotice.style.borderRadius = "10px";
46
+ fileNotice.style.margin = "1rem";
47
+ fileNotice.innerHTML = `${icon} <strong>Selected file:</strong> ${fileName}`;
48
+ convo.appendChild(fileNotice);
49
  }
50
 
51
  async function handleSubmit() {
 
59
  formData.append("question", question);
60
  formData.append("file", selectedFile);
61
 
62
+ // Show question in convo
63
+ const userBlock = document.createElement("div");
64
+ userBlock.style.padding = "1rem";
65
+ userBlock.style.background = "#e0e7ff";
66
+ userBlock.style.borderRadius = "10px";
67
+ userBlock.style.margin = "1rem";
68
+ userBlock.innerHTML = `
69
+ <strong>Question:</strong> ${question}<br>
70
+ ${selectedFile ? `<strong>${selectedType}:</strong> ${selectedFile.name}` : ""}
71
+ `;
72
+ convo.appendChild(userBlock);
73
+
74
+ // Show loading message
75
+ const loadingBlock = document.createElement("div");
76
+ loadingBlock.style.padding = "1rem";
77
+ loadingBlock.style.background = "#eee";
78
+ loadingBlock.style.borderRadius = "10px";
79
+ loadingBlock.style.margin = "1rem";
80
+ loadingBlock.innerText = "Processing your request...";
81
+ convo.appendChild(loadingBlock);
82
 
83
  try {
84
  const response = await fetch("/predict", {
 
87
  });
88
 
89
  const result = await response.json();
90
+
91
+ loadingBlock.remove(); // remove loading message
92
+
93
+ const answerBlock = document.createElement("div");
94
+ answerBlock.style.padding = "1rem";
95
+ answerBlock.style.background = "#e1f7e1";
96
+ answerBlock.style.borderRadius = "10px";
97
+ answerBlock.style.margin = "1rem";
98
+ answerBlock.innerHTML = `
99
+ <strong>Answer:</strong> ${result.answer || result.error || "No response"}
100
+ `;
101
+ convo.appendChild(answerBlock);
102
  } catch (error) {
103
+ loadingBlock.remove();
104
+
105
+ const errorBlock = document.createElement("div");
106
+ errorBlock.style.color = "red";
107
+ errorBlock.style.margin = "1rem";
108
+ errorBlock.innerText = `Request failed: ${error.message}`;
109
+ convo.appendChild(errorBlock);
110
  }
111
 
112
+ // Reset input field and file
113
  inputField.value = "";
114
+ selectedFile = null;
115
+ selectedType = null;
116
  }
117
  */
118
  const inputField = document.querySelector(".qt input");
 
134
  document.body.appendChild(imageInput);
135
  document.body.appendChild(docInput);
136
 
137
+ // Create audio toggle checkbox
138
+ const audioToggle = document.createElement("div");
139
+ audioToggle.innerHTML = `
140
+ <label style="margin-left: 1rem;">
141
+ 🔊 Play Audio Answer:
142
+ <input type="checkbox" id="audioToggle" checked>
143
+ </label>
144
+ `;
145
+ document.querySelector(".qt").appendChild(audioToggle);
146
+
147
  let selectedFile = null;
148
  let selectedType = null;
149
 
150
  // Icon click handlers
151
+ icons[0].addEventListener("click", () => imageInput.click());
152
+ icons[1].addEventListener("click", () => docInput.click());
153
+ send.addEventListener("click", handleSubmit);
154
 
155
  imageInput.addEventListener("change", () => {
156
  selectedFile = imageInput.files[0];
 
185
  formData.append("question", question);
186
  formData.append("file", selectedFile);
187
 
 
188
  const userBlock = document.createElement("div");
189
  userBlock.style.padding = "1rem";
190
  userBlock.style.background = "#e0e7ff";
 
196
  `;
197
  convo.appendChild(userBlock);
198
 
 
199
  const loadingBlock = document.createElement("div");
200
  loadingBlock.style.padding = "1rem";
201
  loadingBlock.style.background = "#eee";
 
212
 
213
  const result = await response.json();
214
 
215
+ loadingBlock.remove();
216
 
217
  const answerBlock = document.createElement("div");
218
  answerBlock.style.padding = "1rem";
219
  answerBlock.style.background = "#e1f7e1";
220
  answerBlock.style.borderRadius = "10px";
221
  answerBlock.style.margin = "1rem";
222
+ answerBlock.innerHTML = `<strong>Answer:</strong> ${result.answer || result.error || "No response"}`;
 
 
223
  convo.appendChild(answerBlock);
224
+
225
+ // Check audio toggle and play if available
226
+ const playAudio = document.getElementById("audioToggle").checked;
227
+ if (playAudio && result.audio_url) {
228
+ const audioPlayer = new Audio(result.audio_url);
229
+ audioPlayer.play();
230
+ }
231
+
232
  } catch (error) {
233
  loadingBlock.remove();
234
 
 
239
  convo.appendChild(errorBlock);
240
  }
241
 
 
242
  inputField.value = "";
243
  selectedFile = null;
244
  selectedType = null;