Spaces:
Running
Running
Update static/application.js
Browse files- static/application.js +60 -60
static/application.js
CHANGED
@@ -85,6 +85,8 @@ document.addEventListener("DOMContentLoaded", () => {
|
|
85 |
}
|
86 |
|
87 |
sendBtn.addEventListener("click", async () => {
|
|
|
|
|
88 |
const question = input.value.trim();
|
89 |
if (!question) return;
|
90 |
|
@@ -150,78 +152,76 @@ document.addEventListener("DOMContentLoaded", () => {
|
|
150 |
message.innerText = "⚠️ Chris had trouble responding.";
|
151 |
}
|
152 |
|
153 |
-
input.value = "";
|
154 |
-
selectedFile = null;
|
155 |
});
|
156 |
input.addEventListener("keyup", async (event) => {
|
157 |
if (event.key === "Enter") {
|
158 |
-
|
159 |
-
|
|
|
|
|
160 |
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
171 |
|
172 |
-
//
|
173 |
-
|
|
|
174 |
|
175 |
-
|
176 |
-
|
|
|
|
|
|
|
|
|
|
|
177 |
|
178 |
-
|
179 |
-
|
180 |
-
formData.append("file", selectedFile);
|
181 |
|
182 |
-
|
183 |
-
|
184 |
-
|
185 |
-
|
|
|
186 |
});
|
187 |
|
188 |
-
|
189 |
-
|
190 |
-
|
191 |
-
|
192 |
-
// Replace "Let me think..." with actual answer
|
193 |
-
const message = thinkingBubble.querySelector(".text");
|
194 |
-
message.innerText = answerText;
|
195 |
-
|
196 |
-
if (audioSrc) {
|
197 |
-
const icon = document.createElement("i");
|
198 |
-
icon.className = "fa-solid fa-volume-high audio-toggle";
|
199 |
-
icon.title = "Click to mute";
|
200 |
-
icon.style.cursor = "pointer";
|
201 |
-
icon.style.fontSize = "18px";
|
202 |
-
icon.style.marginLeft = "10px";
|
203 |
-
|
204 |
-
const audio = new Audio(audioSrc);
|
205 |
-
audio.play();
|
206 |
-
|
207 |
-
icon.addEventListener("click", () => {
|
208 |
-
audio.muted = !audio.muted;
|
209 |
-
icon.classList.toggle("fa-volume-high", !audio.muted);
|
210 |
-
icon.classList.toggle("fa-volume-xmark", audio.muted);
|
211 |
-
icon.title = audio.muted ? "Click to unmute" : "Click to mute";
|
212 |
-
});
|
213 |
-
|
214 |
-
message.style.display = "flex";
|
215 |
-
message.style.justifyContent = "space-between";
|
216 |
-
message.appendChild(icon);
|
217 |
-
}
|
218 |
-
} catch (err) {
|
219 |
-
const message = thinkingBubble.querySelector(".text");
|
220 |
-
message.innerText = "⚠️ Chris had trouble responding.";
|
221 |
}
|
222 |
-
|
223 |
-
|
224 |
-
|
|
|
225 |
}
|
|
|
226 |
});
|
227 |
});
|
|
|
85 |
}
|
86 |
|
87 |
sendBtn.addEventListener("click", async () => {
|
88 |
+
input.value = "";
|
89 |
+
selectedFile = null;
|
90 |
const question = input.value.trim();
|
91 |
if (!question) return;
|
92 |
|
|
|
152 |
message.innerText = "⚠️ Chris had trouble responding.";
|
153 |
}
|
154 |
|
|
|
|
|
155 |
});
|
156 |
input.addEventListener("keyup", async (event) => {
|
157 |
if (event.key === "Enter") {
|
158 |
+
input.value = "";
|
159 |
+
selectedFile = null;
|
160 |
+
const question = input.value.trim();
|
161 |
+
if (!question) return;
|
162 |
|
163 |
+
if (!selectedFile) {
|
164 |
+
alert("Please upload a document or image first.");
|
165 |
+
return;
|
166 |
+
}
|
167 |
|
168 |
+
// Remove file preview bubble
|
169 |
+
if (filePreviewBubble) {
|
170 |
+
filePreviewBubble.remove();
|
171 |
+
filePreviewBubble = null;
|
172 |
+
}
|
173 |
+
|
174 |
+
// User message with file info
|
175 |
+
createMessageBubble(question, "You", null, selectedFile.name);
|
176 |
+
|
177 |
+
// Chris is thinking...
|
178 |
+
const thinkingBubble = createMessageBubble("Wait , Let me think 🤔...", "Chris");
|
179 |
+
|
180 |
+
const formData = new FormData();
|
181 |
+
formData.append("question", question);
|
182 |
+
formData.append("file", selectedFile);
|
183 |
+
|
184 |
+
try {
|
185 |
+
const response = await fetch("/predict", {
|
186 |
+
method: "POST",
|
187 |
+
body: formData
|
188 |
+
});
|
189 |
+
|
190 |
+
const result = await response.json();
|
191 |
+
const answerText = result.answer || "No response.";
|
192 |
+
const audioSrc = result.audio || null;
|
193 |
|
194 |
+
// Replace "Let me think..." with actual answer
|
195 |
+
const message = thinkingBubble.querySelector(".text");
|
196 |
+
message.innerText = answerText;
|
197 |
|
198 |
+
if (audioSrc) {
|
199 |
+
const icon = document.createElement("i");
|
200 |
+
icon.className = "fa-solid fa-volume-high audio-toggle";
|
201 |
+
icon.title = "Click to mute";
|
202 |
+
icon.style.cursor = "pointer";
|
203 |
+
icon.style.fontSize = "18px";
|
204 |
+
icon.style.marginLeft = "10px";
|
205 |
|
206 |
+
const audio = new Audio(audioSrc);
|
207 |
+
audio.play();
|
|
|
208 |
|
209 |
+
icon.addEventListener("click", () => {
|
210 |
+
audio.muted = !audio.muted;
|
211 |
+
icon.classList.toggle("fa-volume-high", !audio.muted);
|
212 |
+
icon.classList.toggle("fa-volume-xmark", audio.muted);
|
213 |
+
icon.title = audio.muted ? "Click to unmute" : "Click to mute";
|
214 |
});
|
215 |
|
216 |
+
message.style.display = "flex";
|
217 |
+
message.style.justifyContent = "space-between";
|
218 |
+
message.appendChild(icon);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
219 |
}
|
220 |
+
} catch (err) {
|
221 |
+
const message = thinkingBubble.querySelector(".text");
|
222 |
+
message.innerText = "⚠️ Chris had trouble responding.";
|
223 |
+
}
|
224 |
}
|
225 |
+
|
226 |
});
|
227 |
});
|