Spaces:
Running
Running
Update static/application.js
Browse files- static/application.js +78 -61
static/application.js
CHANGED
@@ -1,41 +1,45 @@
|
|
1 |
document.addEventListener("DOMContentLoaded", () => {
|
2 |
const convo = document.querySelector(".convo");
|
3 |
-
const input = document.querySelector("
|
4 |
const sendBtn = document.querySelector(".sendingQA");
|
5 |
const fileBtn = document.querySelector(".fa-file");
|
6 |
const imageBtn = document.querySelector(".fa-image");
|
7 |
-
|
8 |
let selectedFile = null;
|
9 |
let filePreviewBubble = null;
|
10 |
|
11 |
-
// Hidden
|
12 |
const fileInput = document.createElement("input");
|
13 |
fileInput.type = "file";
|
14 |
fileInput.style.display = "none";
|
15 |
document.body.appendChild(fileInput);
|
16 |
|
17 |
-
// π Handle document icon click
|
18 |
fileBtn.addEventListener("click", () => {
|
19 |
-
console.log("clicked")
|
20 |
fileInput.value = "";
|
21 |
fileInput.accept = ".pdf,.docx,.pptx,.xlsx";
|
22 |
fileInput.click();
|
23 |
});
|
24 |
|
25 |
-
// πΌοΈ Handle image icon click
|
26 |
imageBtn.addEventListener("click", () => {
|
27 |
fileInput.value = "";
|
28 |
fileInput.accept = "image/*";
|
29 |
fileInput.click();
|
30 |
});
|
31 |
|
32 |
-
// When file is selected via input
|
33 |
fileInput.addEventListener("change", () => {
|
34 |
const file = fileInput.files[0];
|
35 |
-
if (file)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
});
|
37 |
|
38 |
-
//
|
39 |
convo.addEventListener("dragover", (e) => {
|
40 |
e.preventDefault();
|
41 |
convo.classList.add("drag-over");
|
@@ -50,19 +54,26 @@ document.addEventListener("DOMContentLoaded", () => {
|
|
50 |
convo.classList.remove("drag-over");
|
51 |
|
52 |
const file = e.dataTransfer.files[0];
|
53 |
-
if (file)
|
54 |
-
|
55 |
|
56 |
-
|
57 |
-
selectedFile = file;
|
58 |
-
if (filePreviewBubble) filePreviewBubble.remove();
|
59 |
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
66 |
|
67 |
function createMessageBubble(text, sender = "You", audioSrc = null, fileName = null) {
|
68 |
const bubble = document.createElement("div");
|
@@ -80,12 +91,12 @@ document.addEventListener("DOMContentLoaded", () => {
|
|
80 |
const fileLine = fileName ? `π Selected file: ${fileName}\n` : "";
|
81 |
message.innerText = `${fileLine}β ${text}`;
|
82 |
} else {
|
83 |
-
const msgSpan = document.createElement("span");
|
84 |
-
msgSpan.innerText = text;
|
85 |
-
|
86 |
message.style.display = "flex";
|
87 |
message.style.justifyContent = "space-between";
|
88 |
message.style.alignItems = "center";
|
|
|
|
|
|
|
89 |
message.appendChild(msgSpan);
|
90 |
|
91 |
if (audioSrc) {
|
@@ -93,27 +104,31 @@ document.addEventListener("DOMContentLoaded", () => {
|
|
93 |
icon.className = "fa-solid fa-volume-high audio-toggle";
|
94 |
icon.title = "Click to mute";
|
95 |
icon.style.cursor = "pointer";
|
96 |
-
icon.style.fontSize = "
|
97 |
-
icon.style.marginLeft = "40px";
|
98 |
|
99 |
-
|
100 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
101 |
|
102 |
-
let isMuted = false;
|
103 |
-
icon.addEventListener("click", () => {
|
104 |
-
if (isMuted) {
|
105 |
-
currentAudio = new Audio(audioSrc);
|
106 |
-
currentAudio.play();
|
107 |
-
isMuted = false;
|
108 |
-
icon.classList.replace("fa-volume-xmark", "fa-volume-high");
|
109 |
-
icon.title = "Click to mute";
|
110 |
-
} else {
|
111 |
-
currentAudio.pause();
|
112 |
-
isMuted = true;
|
113 |
-
icon.classList.replace("fa-volume-high", "fa-volume-xmark");
|
114 |
-
icon.title = "Click to unmute";
|
115 |
-
}
|
116 |
-
});
|
117 |
|
118 |
message.appendChild(icon);
|
119 |
}
|
@@ -126,8 +141,8 @@ document.addEventListener("DOMContentLoaded", () => {
|
|
126 |
return bubble;
|
127 |
}
|
128 |
|
129 |
-
// π§ Send question
|
130 |
sendBtn.addEventListener("click", async () => {
|
|
|
131 |
const question = input.value.trim();
|
132 |
if (!question) return;
|
133 |
|
@@ -142,11 +157,16 @@ document.addEventListener("DOMContentLoaded", () => {
|
|
142 |
}
|
143 |
|
144 |
createMessageBubble(question, "You", null, selectedFile.name);
|
145 |
-
const thinkingBubble = createMessageBubble("
|
|
|
|
|
|
|
|
|
|
|
146 |
|
147 |
const formData = new FormData();
|
148 |
formData.append("question", question);
|
149 |
-
formData.append("file",
|
150 |
|
151 |
try {
|
152 |
const response = await fetch("/predict", {
|
@@ -159,11 +179,7 @@ document.addEventListener("DOMContentLoaded", () => {
|
|
159 |
const audioSrc = result.audio || null;
|
160 |
|
161 |
const message = thinkingBubble.querySelector(".text");
|
162 |
-
message.
|
163 |
-
|
164 |
-
const msgSpan = document.createElement("span");
|
165 |
-
msgSpan.innerText = answerText;
|
166 |
-
message.appendChild(msgSpan);
|
167 |
|
168 |
if (audioSrc) {
|
169 |
const icon = document.createElement("i");
|
@@ -173,38 +189,39 @@ document.addEventListener("DOMContentLoaded", () => {
|
|
173 |
icon.style.fontSize = "18px";
|
174 |
icon.style.marginLeft = "10px";
|
175 |
|
176 |
-
|
177 |
-
|
178 |
|
179 |
-
let isMuted = false;
|
180 |
icon.addEventListener("click", () => {
|
181 |
-
if (
|
182 |
-
|
183 |
-
|
184 |
-
|
185 |
icon.classList.replace("fa-volume-xmark", "fa-volume-high");
|
186 |
icon.title = "Click to mute";
|
187 |
} else {
|
188 |
-
|
189 |
-
isMuted = true;
|
190 |
icon.classList.replace("fa-volume-high", "fa-volume-xmark");
|
191 |
icon.title = "Click to unmute";
|
192 |
}
|
193 |
});
|
194 |
|
|
|
|
|
195 |
message.appendChild(icon);
|
196 |
}
|
|
|
197 |
} catch (err) {
|
198 |
const message = thinkingBubble.querySelector(".text");
|
199 |
message.innerText = "β οΈ Chris had trouble responding.";
|
200 |
}
|
201 |
|
202 |
-
|
203 |
-
selectedFile = null;
|
204 |
});
|
205 |
});
|
206 |
|
207 |
|
|
|
208 |
/*
|
209 |
document.addEventListener("DOMContentLoaded", () => {
|
210 |
const convo = document.querySelector(".convo");
|
|
|
1 |
document.addEventListener("DOMContentLoaded", () => {
|
2 |
const convo = document.querySelector(".convo");
|
3 |
+
const input = document.querySelector(".qt input");
|
4 |
const sendBtn = document.querySelector(".sendingQA");
|
5 |
const fileBtn = document.querySelector(".fa-file");
|
6 |
const imageBtn = document.querySelector(".fa-image");
|
|
|
7 |
let selectedFile = null;
|
8 |
let filePreviewBubble = null;
|
9 |
|
10 |
+
// Hidden input
|
11 |
const fileInput = document.createElement("input");
|
12 |
fileInput.type = "file";
|
13 |
fileInput.style.display = "none";
|
14 |
document.body.appendChild(fileInput);
|
15 |
|
|
|
16 |
fileBtn.addEventListener("click", () => {
|
|
|
17 |
fileInput.value = "";
|
18 |
fileInput.accept = ".pdf,.docx,.pptx,.xlsx";
|
19 |
fileInput.click();
|
20 |
});
|
21 |
|
|
|
22 |
imageBtn.addEventListener("click", () => {
|
23 |
fileInput.value = "";
|
24 |
fileInput.accept = "image/*";
|
25 |
fileInput.click();
|
26 |
});
|
27 |
|
|
|
28 |
fileInput.addEventListener("change", () => {
|
29 |
const file = fileInput.files[0];
|
30 |
+
if (file) {
|
31 |
+
selectedFile = file;
|
32 |
+
if (filePreviewBubble) filePreviewBubble.remove();
|
33 |
+
|
34 |
+
filePreviewBubble = document.createElement("div");
|
35 |
+
filePreviewBubble.className = "file-preview-bubble bubble right";
|
36 |
+
filePreviewBubble.textContent = `π Selected: ${file.name}`;
|
37 |
+
convo.appendChild(filePreviewBubble);
|
38 |
+
convo.scrollTop = convo.scrollHeight;
|
39 |
+
}
|
40 |
});
|
41 |
|
42 |
+
// Drag & drop
|
43 |
convo.addEventListener("dragover", (e) => {
|
44 |
e.preventDefault();
|
45 |
convo.classList.add("drag-over");
|
|
|
54 |
convo.classList.remove("drag-over");
|
55 |
|
56 |
const file = e.dataTransfer.files[0];
|
57 |
+
if (file) {
|
58 |
+
selectedFile = file;
|
59 |
|
60 |
+
if (filePreviewBubble) filePreviewBubble.remove();
|
|
|
|
|
61 |
|
62 |
+
filePreviewBubble = document.createElement("div");
|
63 |
+
filePreviewBubble.className = "file-preview-bubble bubble right";
|
64 |
+
filePreviewBubble.textContent = `π Selected: ${file.name}`;
|
65 |
+
convo.appendChild(filePreviewBubble);
|
66 |
+
convo.scrollTop = convo.scrollHeight;
|
67 |
+
}
|
68 |
+
});
|
69 |
+
|
70 |
+
// Reset chat
|
71 |
+
resetBtn.addEventListener("click", () => {
|
72 |
+
convo.innerHTML = "";
|
73 |
+
selectedFile = null;
|
74 |
+
filePreviewBubble = null;
|
75 |
+
input.value = "";
|
76 |
+
});
|
77 |
|
78 |
function createMessageBubble(text, sender = "You", audioSrc = null, fileName = null) {
|
79 |
const bubble = document.createElement("div");
|
|
|
91 |
const fileLine = fileName ? `π Selected file: ${fileName}\n` : "";
|
92 |
message.innerText = `${fileLine}β ${text}`;
|
93 |
} else {
|
|
|
|
|
|
|
94 |
message.style.display = "flex";
|
95 |
message.style.justifyContent = "space-between";
|
96 |
message.style.alignItems = "center";
|
97 |
+
|
98 |
+
const msgSpan = document.createElement("span");
|
99 |
+
msgSpan.innerText = text;
|
100 |
message.appendChild(msgSpan);
|
101 |
|
102 |
if (audioSrc) {
|
|
|
104 |
icon.className = "fa-solid fa-volume-high audio-toggle";
|
105 |
icon.title = "Click to mute";
|
106 |
icon.style.cursor = "pointer";
|
107 |
+
icon.style.fontSize = "18px";
|
|
|
108 |
|
109 |
+
const audio = new Audio(audioSrc);
|
110 |
+
audio.play();
|
111 |
+
|
112 |
+
let isMuted = false;
|
113 |
+
let audio = new Audio(audioSrc);
|
114 |
+
audio.play();
|
115 |
+
|
116 |
+
icon.addEventListener("click", () => {
|
117 |
+
if (isMuted) {
|
118 |
+
// Recreate audio to force replay
|
119 |
+
audio = new Audio(audioSrc);
|
120 |
+
audio.play();
|
121 |
+
isMuted = false;
|
122 |
+
icon.classList.replace("fa-volume-xmark", "fa-volume-high");
|
123 |
+
icon.title = "Click to mute";
|
124 |
+
} else {
|
125 |
+
audio.pause();
|
126 |
+
isMuted = true;
|
127 |
+
icon.classList.replace("fa-volume-high", "fa-volume-xmark");
|
128 |
+
icon.title = "Click to unmute";
|
129 |
+
}
|
130 |
+
});
|
131 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
132 |
|
133 |
message.appendChild(icon);
|
134 |
}
|
|
|
141 |
return bubble;
|
142 |
}
|
143 |
|
|
|
144 |
sendBtn.addEventListener("click", async () => {
|
145 |
+
input.value = "";
|
146 |
const question = input.value.trim();
|
147 |
if (!question) return;
|
148 |
|
|
|
157 |
}
|
158 |
|
159 |
createMessageBubble(question, "You", null, selectedFile.name);
|
160 |
+
const thinkingBubble = createMessageBubble("π§ Let me think...", "Chris");
|
161 |
+
|
162 |
+
// Rewrap file if needed (for reuse in FormData)
|
163 |
+
const wrappedFile = new File([selectedFile], selectedFile.name, {
|
164 |
+
type: selectedFile.type,
|
165 |
+
});
|
166 |
|
167 |
const formData = new FormData();
|
168 |
formData.append("question", question);
|
169 |
+
formData.append("file", wrappedFile);
|
170 |
|
171 |
try {
|
172 |
const response = await fetch("/predict", {
|
|
|
179 |
const audioSrc = result.audio || null;
|
180 |
|
181 |
const message = thinkingBubble.querySelector(".text");
|
182 |
+
message.innerText = answerText;
|
|
|
|
|
|
|
|
|
183 |
|
184 |
if (audioSrc) {
|
185 |
const icon = document.createElement("i");
|
|
|
189 |
icon.style.fontSize = "18px";
|
190 |
icon.style.marginLeft = "10px";
|
191 |
|
192 |
+
const audio = new Audio(audioSrc);
|
193 |
+
audio.play();
|
194 |
|
|
|
195 |
icon.addEventListener("click", () => {
|
196 |
+
if (audio.muted) {
|
197 |
+
audio.currentTime = 0;
|
198 |
+
audio.muted = false;
|
199 |
+
audio.play();
|
200 |
icon.classList.replace("fa-volume-xmark", "fa-volume-high");
|
201 |
icon.title = "Click to mute";
|
202 |
} else {
|
203 |
+
audio.muted = true;
|
|
|
204 |
icon.classList.replace("fa-volume-high", "fa-volume-xmark");
|
205 |
icon.title = "Click to unmute";
|
206 |
}
|
207 |
});
|
208 |
|
209 |
+
message.style.display = "flex";
|
210 |
+
message.style.justifyContent = "space-between";
|
211 |
message.appendChild(icon);
|
212 |
}
|
213 |
+
|
214 |
} catch (err) {
|
215 |
const message = thinkingBubble.querySelector(".text");
|
216 |
message.innerText = "β οΈ Chris had trouble responding.";
|
217 |
}
|
218 |
|
219 |
+
// selectedFile is NOT cleared, so multiple questions work β
|
|
|
220 |
});
|
221 |
});
|
222 |
|
223 |
|
224 |
+
|
225 |
/*
|
226 |
document.addEventListener("DOMContentLoaded", () => {
|
227 |
const convo = document.querySelector(".convo");
|