Spaces:
Running
Running
Update static/application.js
Browse files- static/application.js +25 -190
static/application.js
CHANGED
@@ -1,112 +1,3 @@
|
|
1 |
-
document.addEventListener("DOMContentLoaded", () => {
|
2 |
-
const convo = document.querySelector(".convo");
|
3 |
-
const input = document.querySelector("#user-input");
|
4 |
-
const sendBtn = document.querySelector(".sendingQA");
|
5 |
-
const fileBtn = document.querySelector("#file-btn");
|
6 |
-
const imageBtn = document.querySelector("#image-btn");
|
7 |
-
const fileInput = document.querySelector("#file-upload");
|
8 |
-
const imageInput = document.querySelector("#image-upload");
|
9 |
-
|
10 |
-
// --- File Upload Logic (Supports Re-selection) ---
|
11 |
-
fileBtn.addEventListener("click", () => {
|
12 |
-
fileInput.value = ""; // Reset to allow same-file reselection
|
13 |
-
fileInput.click();
|
14 |
-
});
|
15 |
-
|
16 |
-
imageBtn.addEventListener("click", () => {
|
17 |
-
imageInput.value = ""; // Reset to allow same-file reselection
|
18 |
-
imageInput.click();
|
19 |
-
});
|
20 |
-
|
21 |
-
// Handle file/image selection
|
22 |
-
const handleFileUpload = (file, type) => {
|
23 |
-
if (!file) return;
|
24 |
-
createFilePreview(file.name, type);
|
25 |
-
};
|
26 |
-
|
27 |
-
fileInput.addEventListener("change", () => handleFileUpload(fileInput.files[0], "file"));
|
28 |
-
imageInput.addEventListener("change", () => handleFileUpload(imageInput.files[0], "image"));
|
29 |
-
|
30 |
-
// --- Drag & Drop Support ---
|
31 |
-
convo.addEventListener("dragover", (e) => {
|
32 |
-
e.preventDefault();
|
33 |
-
convo.classList.add("drag-active");
|
34 |
-
});
|
35 |
-
|
36 |
-
convo.addEventListener("dragleave", () => {
|
37 |
-
convo.classList.remove("drag-active");
|
38 |
-
});
|
39 |
-
|
40 |
-
convo.addEventListener("drop", (e) => {
|
41 |
-
e.preventDefault();
|
42 |
-
convo.classList.remove("drag-active");
|
43 |
-
|
44 |
-
const files = e.dataTransfer.files;
|
45 |
-
if (files.length > 0) {
|
46 |
-
const file = files[0];
|
47 |
-
const type = file.type.startsWith("image") ? "image" : "file";
|
48 |
-
createFilePreview(file.name, type);
|
49 |
-
}
|
50 |
-
});
|
51 |
-
|
52 |
-
// --- Chat Bubbles & Audio Toggle ---
|
53 |
-
function createMessageBubble(message, isUser = true, audioSrc = null) {
|
54 |
-
const bubble = document.createElement("div");
|
55 |
-
bubble.className = `bubble ${isUser ? "right" : "left"}`;
|
56 |
-
bubble.innerHTML = `<span>${message}</span>`;
|
57 |
-
|
58 |
-
if (audioSrc && !isUser) {
|
59 |
-
const audio = document.createElement("audio");
|
60 |
-
audio.src = audioSrc;
|
61 |
-
audio.autoplay = true;
|
62 |
-
audio.currentTime = 0; // Restarts audio when unmuted
|
63 |
-
|
64 |
-
const audioIcon = document.createElement("i");
|
65 |
-
audioIcon.className = "fa-solid fa-volume-high audio-toggle";
|
66 |
-
audioIcon.title = "Click to mute";
|
67 |
-
|
68 |
-
bubble.appendChild(audio);
|
69 |
-
bubble.appendChild(audioIcon);
|
70 |
-
|
71 |
-
audioIcon.addEventListener("click", () => {
|
72 |
-
audio.muted = !audio.muted;
|
73 |
-
if (!audio.muted) audio.currentTime = 0; // Restart on unmute
|
74 |
-
audioIcon.classList.toggle("fa-volume-high");
|
75 |
-
audioIcon.classList.toggle("fa-volume-xmark");
|
76 |
-
audioIcon.title = audio.muted ? "Click to unmute" : "Click to mute";
|
77 |
-
});
|
78 |
-
}
|
79 |
-
|
80 |
-
convo.appendChild(bubble);
|
81 |
-
convo.scrollTop = convo.scrollHeight;
|
82 |
-
}
|
83 |
-
|
84 |
-
// --- Send Message ---
|
85 |
-
function sendMessage() {
|
86 |
-
const message = input.value.trim();
|
87 |
-
if (!message && !fileInput.files.length && !imageInput.files.length) return;
|
88 |
-
|
89 |
-
if (message) createMessageBubble(message, true);
|
90 |
-
if (fileInput.files.length) handleFileUpload(fileInput.files[0], "file");
|
91 |
-
if (imageInput.files.length) handleFileUpload(imageInput.files[0], "image");
|
92 |
-
|
93 |
-
// Simulate bot response
|
94 |
-
setTimeout(() => {
|
95 |
-
createMessageBubble("Here's your answer!", false, "/static/audio/response.mp3");
|
96 |
-
}, 1000);
|
97 |
-
|
98 |
-
// Reset inputs
|
99 |
-
input.value = "";
|
100 |
-
fileInput.value = "";
|
101 |
-
imageInput.value = "";
|
102 |
-
}
|
103 |
-
|
104 |
-
// Event listeners
|
105 |
-
sendBtn.addEventListener("click", sendMessage);
|
106 |
-
input.addEventListener("keydown", (e) => e.key === "Enter" && sendMessage());
|
107 |
-
});
|
108 |
-
|
109 |
-
/*
|
110 |
document.addEventListener("DOMContentLoaded", () => {
|
111 |
const convo = document.querySelector(".convo");
|
112 |
const input = document.querySelector(".qt input");
|
@@ -117,27 +8,16 @@ document.addEventListener("DOMContentLoaded", () => {
|
|
117 |
let selectedFile = null;
|
118 |
let filePreviewBubble = null;
|
119 |
|
120 |
-
//
|
121 |
const fileInput = document.createElement("input");
|
122 |
fileInput.type = "file";
|
|
|
123 |
fileInput.style.display = "none";
|
124 |
document.body.appendChild(fileInput);
|
125 |
|
126 |
-
|
127 |
-
|
128 |
-
fileInput.value = ""; // ✅ allow selecting same file again
|
129 |
-
fileInput.accept = ".pdf,.docx,.pptx,.xlsx";
|
130 |
-
fileInput.click();
|
131 |
-
});
|
132 |
-
|
133 |
-
// Choose image
|
134 |
-
imageBtn.addEventListener("click", () => {
|
135 |
-
fileInput.value = "";
|
136 |
-
fileInput.accept = "image/*";
|
137 |
-
fileInput.click();
|
138 |
-
});
|
139 |
|
140 |
-
// Handle file selection
|
141 |
fileInput.addEventListener("change", () => {
|
142 |
const file = fileInput.files[0];
|
143 |
if (file) {
|
@@ -152,34 +32,6 @@ document.addEventListener("DOMContentLoaded", () => {
|
|
152 |
}
|
153 |
});
|
154 |
|
155 |
-
// Drag & drop support
|
156 |
-
convo.addEventListener("dragover", (e) => {
|
157 |
-
e.preventDefault();
|
158 |
-
convo.classList.add("drag-over");
|
159 |
-
});
|
160 |
-
|
161 |
-
convo.addEventListener("dragleave", () => {
|
162 |
-
convo.classList.remove("drag-over");
|
163 |
-
});
|
164 |
-
|
165 |
-
convo.addEventListener("drop", (e) => {
|
166 |
-
e.preventDefault();
|
167 |
-
convo.classList.remove("drag-over");
|
168 |
-
|
169 |
-
const file = e.dataTransfer.files[0];
|
170 |
-
if (file) {
|
171 |
-
selectedFile = file;
|
172 |
-
|
173 |
-
if (filePreviewBubble) filePreviewBubble.remove();
|
174 |
-
|
175 |
-
filePreviewBubble = document.createElement("div");
|
176 |
-
filePreviewBubble.className = "file-preview-bubble bubble right";
|
177 |
-
filePreviewBubble.textContent = `📎 Selected: ${file.name}`;
|
178 |
-
convo.appendChild(filePreviewBubble);
|
179 |
-
convo.scrollTop = convo.scrollHeight;
|
180 |
-
}
|
181 |
-
});
|
182 |
-
|
183 |
function createMessageBubble(text, sender = "You", audioSrc = null, fileName = null) {
|
184 |
const bubble = document.createElement("div");
|
185 |
bubble.className = `bubble ${sender === "You" ? "right" : "left"}`;
|
@@ -190,10 +42,10 @@ document.addEventListener("DOMContentLoaded", () => {
|
|
190 |
|
191 |
const message = document.createElement("div");
|
192 |
message.className = "text";
|
193 |
-
message.style.whiteSpace = "pre-wrap";
|
194 |
|
195 |
if (sender === "You") {
|
196 |
-
|
197 |
message.innerText = `${fileLine}❓ ${text}`;
|
198 |
} else {
|
199 |
message.style.display = "flex";
|
@@ -211,23 +63,14 @@ document.addEventListener("DOMContentLoaded", () => {
|
|
211 |
icon.style.cursor = "pointer";
|
212 |
icon.style.fontSize = "18px";
|
213 |
|
214 |
-
|
215 |
-
|
216 |
|
217 |
-
let isMuted = false;
|
218 |
icon.addEventListener("click", () => {
|
219 |
-
|
220 |
-
|
221 |
-
|
222 |
-
|
223 |
-
icon.classList.replace("fa-volume-xmark", "fa-volume-high");
|
224 |
-
icon.title = "Click to mute";
|
225 |
-
} else {
|
226 |
-
currentAudio.pause();
|
227 |
-
isMuted = true;
|
228 |
-
icon.classList.replace("fa-volume-high", "fa-volume-xmark");
|
229 |
-
icon.title = "Click to unmute";
|
230 |
-
}
|
231 |
});
|
232 |
|
233 |
message.appendChild(icon);
|
@@ -250,21 +93,21 @@ document.addEventListener("DOMContentLoaded", () => {
|
|
250 |
return;
|
251 |
}
|
252 |
|
|
|
253 |
if (filePreviewBubble) {
|
254 |
filePreviewBubble.remove();
|
255 |
filePreviewBubble = null;
|
256 |
}
|
257 |
|
|
|
258 |
createMessageBubble(question, "You", null, selectedFile.name);
|
259 |
-
const thinkingBubble = createMessageBubble("🧠 Let me think...", "Chris");
|
260 |
|
261 |
-
|
262 |
-
|
263 |
-
});
|
264 |
|
265 |
const formData = new FormData();
|
266 |
formData.append("question", question);
|
267 |
-
formData.append("file",
|
268 |
|
269 |
try {
|
270 |
const response = await fetch("/predict", {
|
@@ -276,6 +119,7 @@ document.addEventListener("DOMContentLoaded", () => {
|
|
276 |
const answerText = result.answer || "No response.";
|
277 |
const audioSrc = result.audio || null;
|
278 |
|
|
|
279 |
const message = thinkingBubble.querySelector(".text");
|
280 |
message.innerText = answerText;
|
281 |
|
@@ -287,35 +131,26 @@ document.addEventListener("DOMContentLoaded", () => {
|
|
287 |
icon.style.fontSize = "18px";
|
288 |
icon.style.marginLeft = "10px";
|
289 |
|
290 |
-
|
291 |
-
|
292 |
|
293 |
-
let isMuted = false;
|
294 |
icon.addEventListener("click", () => {
|
295 |
-
|
296 |
-
|
297 |
-
|
298 |
-
|
299 |
-
icon.classList.replace("fa-volume-xmark", "fa-volume-high");
|
300 |
-
icon.title = "Click to mute";
|
301 |
-
} else {
|
302 |
-
currentAudio.pause();
|
303 |
-
isMuted = true;
|
304 |
-
icon.classList.replace("fa-volume-high", "fa-volume-xmark");
|
305 |
-
icon.title = "Click to unmute";
|
306 |
-
}
|
307 |
});
|
308 |
|
309 |
message.style.display = "flex";
|
310 |
message.style.justifyContent = "space-between";
|
311 |
message.appendChild(icon);
|
312 |
}
|
313 |
-
|
314 |
} catch (err) {
|
315 |
const message = thinkingBubble.querySelector(".text");
|
316 |
message.innerText = "⚠️ Chris had trouble responding.";
|
317 |
}
|
318 |
|
319 |
input.value = "";
|
|
|
320 |
});
|
321 |
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
document.addEventListener("DOMContentLoaded", () => {
|
2 |
const convo = document.querySelector(".convo");
|
3 |
const input = document.querySelector(".qt input");
|
|
|
8 |
let selectedFile = null;
|
9 |
let filePreviewBubble = null;
|
10 |
|
11 |
+
// Hidden file input
|
12 |
const fileInput = document.createElement("input");
|
13 |
fileInput.type = "file";
|
14 |
+
fileInput.accept = ".pdf,.docx,.pptx,.xlsx,image/*";
|
15 |
fileInput.style.display = "none";
|
16 |
document.body.appendChild(fileInput);
|
17 |
|
18 |
+
fileBtn.addEventListener("click", () => fileInput.click());
|
19 |
+
imageBtn.addEventListener("click", () => fileInput.click());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
|
|
|
21 |
fileInput.addEventListener("change", () => {
|
22 |
const file = fileInput.files[0];
|
23 |
if (file) {
|
|
|
32 |
}
|
33 |
});
|
34 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
35 |
function createMessageBubble(text, sender = "You", audioSrc = null, fileName = null) {
|
36 |
const bubble = document.createElement("div");
|
37 |
bubble.className = `bubble ${sender === "You" ? "right" : "left"}`;
|
|
|
42 |
|
43 |
const message = document.createElement("div");
|
44 |
message.className = "text";
|
45 |
+
message.style.whiteSpace = "pre-wrap"; // preserve line breaks
|
46 |
|
47 |
if (sender === "You") {
|
48 |
+
let fileLine = fileName ? `📎 Selected file: ${fileName}\n` : "";
|
49 |
message.innerText = `${fileLine}❓ ${text}`;
|
50 |
} else {
|
51 |
message.style.display = "flex";
|
|
|
63 |
icon.style.cursor = "pointer";
|
64 |
icon.style.fontSize = "18px";
|
65 |
|
66 |
+
const audio = new Audio(audioSrc);
|
67 |
+
audio.play();
|
68 |
|
|
|
69 |
icon.addEventListener("click", () => {
|
70 |
+
audio.muted = !audio.muted;
|
71 |
+
icon.classList.toggle("fa-volume-high", !audio.muted);
|
72 |
+
icon.classList.toggle("fa-volume-xmark", audio.muted);
|
73 |
+
icon.title = audio.muted ? "Click to unmute" : "Click to mute";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
74 |
});
|
75 |
|
76 |
message.appendChild(icon);
|
|
|
93 |
return;
|
94 |
}
|
95 |
|
96 |
+
// Remove file preview bubble
|
97 |
if (filePreviewBubble) {
|
98 |
filePreviewBubble.remove();
|
99 |
filePreviewBubble = null;
|
100 |
}
|
101 |
|
102 |
+
// User message with file info
|
103 |
createMessageBubble(question, "You", null, selectedFile.name);
|
|
|
104 |
|
105 |
+
// Chris is thinking...
|
106 |
+
const thinkingBubble = createMessageBubble("Wait , Let me think 🤔...", "Chris");
|
|
|
107 |
|
108 |
const formData = new FormData();
|
109 |
formData.append("question", question);
|
110 |
+
formData.append("file", selectedFile);
|
111 |
|
112 |
try {
|
113 |
const response = await fetch("/predict", {
|
|
|
119 |
const answerText = result.answer || "No response.";
|
120 |
const audioSrc = result.audio || null;
|
121 |
|
122 |
+
// Replace "Let me think..." with actual answer
|
123 |
const message = thinkingBubble.querySelector(".text");
|
124 |
message.innerText = answerText;
|
125 |
|
|
|
131 |
icon.style.fontSize = "18px";
|
132 |
icon.style.marginLeft = "10px";
|
133 |
|
134 |
+
const audio = new Audio(audioSrc);
|
135 |
+
audio.play();
|
136 |
|
|
|
137 |
icon.addEventListener("click", () => {
|
138 |
+
audio.muted = !audio.muted;
|
139 |
+
icon.classList.toggle("fa-volume-high", !audio.muted);
|
140 |
+
icon.classList.toggle("fa-volume-xmark", audio.muted);
|
141 |
+
icon.title = audio.muted ? "Click to unmute" : "Click to mute";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
142 |
});
|
143 |
|
144 |
message.style.display = "flex";
|
145 |
message.style.justifyContent = "space-between";
|
146 |
message.appendChild(icon);
|
147 |
}
|
|
|
148 |
} catch (err) {
|
149 |
const message = thinkingBubble.querySelector(".text");
|
150 |
message.innerText = "⚠️ Chris had trouble responding.";
|
151 |
}
|
152 |
|
153 |
input.value = "";
|
154 |
+
selectedFile = null;
|
155 |
});
|
156 |
});
|