ikraamkb commited on
Commit
2597823
ยท
verified ยท
1 Parent(s): c3e7024

Update static/application.js

Browse files
Files changed (1) hide show
  1. static/application.js +34 -85
static/application.js CHANGED
@@ -116,66 +116,6 @@ async function handleSubmit() {
116
  }
117
  */
118
  // frontend.js
119
- const inputField = document.querySelector(".qt input");
120
- const icons = document.querySelectorAll(".icons i");
121
- const convo = document.querySelector(".convo");
122
- const send = document.querySelector(".sendingQA");
123
- const audioToggle = document.createElement("input");
124
- audioToggle.type = "checkbox";
125
- audioToggle.checked = true;
126
- audioToggle.id = "audioToggle";
127
- audioToggle.style.marginLeft = "1rem";
128
-
129
- const audioLabel = document.createElement("label");
130
- audioLabel.htmlFor = "audioToggle";
131
- audioLabel.innerText = "๐Ÿ”Š Audio";
132
- audioLabel.style.marginLeft = "0.5rem";
133
-
134
- document.querySelector(".controls").appendChild(audioToggle);
135
- document.querySelector(".controls").appendChild(audioLabel);
136
-
137
- const imageInput = document.createElement("input");
138
- imageInput.type = "file";
139
- imageInput.accept = "image/*";
140
- imageInput.style.display = "none";
141
-
142
- const docInput = document.createElement("input");
143
- docInput.type = "file";
144
- docInput.accept = ".pdf,.docx,.pptx,.xlsx";
145
- docInput.style.display = "none";
146
-
147
- document.body.appendChild(imageInput);
148
- document.body.appendChild(docInput);
149
-
150
- let selectedFile = null;
151
- let selectedType = null;
152
-
153
- icons[0].addEventListener("click", () => imageInput.click());
154
- icons[1].addEventListener("click", () => docInput.click());
155
- send.addEventListener("click", handleSubmit);
156
-
157
- imageInput.addEventListener("change", () => {
158
- selectedFile = imageInput.files[0];
159
- selectedType = "Image";
160
- showSelectedFile(selectedFile.name, "๐Ÿ—พ๏ธ");
161
- });
162
-
163
- docInput.addEventListener("change", () => {
164
- selectedFile = docInput.files[0];
165
- selectedType = "Document";
166
- showSelectedFile(selectedFile.name, "๐Ÿ“„");
167
- });
168
-
169
- function showSelectedFile(fileName, icon) {
170
- const fileNotice = document.createElement("div");
171
- fileNotice.style.padding = "0.8rem";
172
- fileNotice.style.backgroundColor = "#f3f3f3";
173
- fileNotice.style.borderRadius = "10px";
174
- fileNotice.style.margin = "1rem";
175
- fileNotice.innerHTML = `${icon} <strong>Selected file:</strong> ${fileName}`;
176
- convo.appendChild(fileNotice);
177
- }
178
-
179
  async function handleSubmit() {
180
  const question = inputField.value.trim();
181
  if (!question && !selectedFile) {
@@ -186,24 +126,19 @@ async function handleSubmit() {
186
  const formData = new FormData();
187
  formData.append("question", question);
188
  formData.append("file", selectedFile);
189
- formData.append("audio", audioToggle.checked);
190
 
 
191
  const userBlock = document.createElement("div");
192
- userBlock.style.padding = "1rem";
193
- userBlock.style.background = "#e0e7ff";
194
- userBlock.style.borderRadius = "10px";
195
- userBlock.style.margin = "1rem";
196
  userBlock.innerHTML = `
197
  <strong>Question:</strong> ${question}<br>
198
  ${selectedFile ? `<strong>${selectedType}:</strong> ${selectedFile.name}` : ""}
199
  `;
200
  convo.appendChild(userBlock);
201
 
 
202
  const loadingBlock = document.createElement("div");
203
- loadingBlock.style.padding = "1rem";
204
- loadingBlock.style.background = "#eee";
205
- loadingBlock.style.borderRadius = "10px";
206
- loadingBlock.style.margin = "1rem";
207
  loadingBlock.innerText = "Processing your request...";
208
  convo.appendChild(loadingBlock);
209
 
@@ -216,28 +151,42 @@ async function handleSubmit() {
216
  const result = await response.json();
217
  loadingBlock.remove();
218
 
 
219
  const answerBlock = document.createElement("div");
220
- answerBlock.style.padding = "1rem";
221
- answerBlock.style.background = "#e1f7e1";
222
- answerBlock.style.borderRadius = "10px";
223
- answerBlock.style.margin = "1rem";
224
- answerBlock.innerHTML = `<strong>Answer:</strong> ${result.answer}`;
225
-
226
- if (result.audio_url) {
227
- const audio = document.createElement("audio");
228
- audio.controls = true;
229
- audio.src = result.audio_url;
230
- answerBlock.appendChild(document.createElement("br"));
231
- answerBlock.appendChild(audio);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
232
  }
233
 
234
- convo.appendChild(answerBlock);
235
  } catch (error) {
236
  loadingBlock.remove();
237
-
238
  const errorBlock = document.createElement("div");
239
- errorBlock.style.color = "red";
240
- errorBlock.style.margin = "1rem";
241
  errorBlock.innerText = `Request failed: ${error.message}`;
242
  convo.appendChild(errorBlock);
243
  }
 
116
  }
117
  */
118
  // frontend.js
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
119
  async function handleSubmit() {
120
  const question = inputField.value.trim();
121
  if (!question && !selectedFile) {
 
126
  const formData = new FormData();
127
  formData.append("question", question);
128
  formData.append("file", selectedFile);
 
129
 
130
+ // Show user question
131
  const userBlock = document.createElement("div");
132
+ userBlock.className = "user-block";
 
 
 
133
  userBlock.innerHTML = `
134
  <strong>Question:</strong> ${question}<br>
135
  ${selectedFile ? `<strong>${selectedType}:</strong> ${selectedFile.name}` : ""}
136
  `;
137
  convo.appendChild(userBlock);
138
 
139
+ // Loading message
140
  const loadingBlock = document.createElement("div");
141
+ loadingBlock.className = "loading-block";
 
 
 
142
  loadingBlock.innerText = "Processing your request...";
143
  convo.appendChild(loadingBlock);
144
 
 
151
  const result = await response.json();
152
  loadingBlock.remove();
153
 
154
+ // Answer block with optional audio
155
  const answerBlock = document.createElement("div");
156
+ answerBlock.className = "answer-block";
157
+ answerBlock.innerHTML = `
158
+ <strong>Answer:</strong> ${result.answer || result.error || "No response"}
159
+ ${result.audio ? `
160
+ <div style="margin-top: 0.5rem;">
161
+ <i class="fa-solid fa-volume-high audio-toggle" title="Click to mute" style="cursor:pointer; margin-right: 8px;"></i>
162
+ <audio src="${result.audio}" autoplay></audio>
163
+ </div>
164
+ ` : ""}
165
+ `;
166
+ convo.appendChild(answerBlock);
167
+
168
+ // Handle audio toggle
169
+ const audioIcon = answerBlock.querySelector(".audio-toggle");
170
+ const audioElement = answerBlock.querySelector("audio");
171
+
172
+ if (audioIcon && audioElement) {
173
+ audioIcon.addEventListener("click", () => {
174
+ if (audioElement.muted) {
175
+ audioElement.muted = false;
176
+ audioIcon.classList.replace("fa-volume-xmark", "fa-volume-high");
177
+ audioIcon.title = "Click to mute";
178
+ } else {
179
+ audioElement.muted = true;
180
+ audioIcon.classList.replace("fa-volume-high", "fa-volume-xmark");
181
+ audioIcon.title = "Click to unmute";
182
+ }
183
+ });
184
  }
185
 
 
186
  } catch (error) {
187
  loadingBlock.remove();
 
188
  const errorBlock = document.createElement("div");
189
+ errorBlock.className = "error-block";
 
190
  errorBlock.innerText = `Request failed: ${error.message}`;
191
  convo.appendChild(errorBlock);
192
  }