Spaces:
Running
Running
Update static/application.js
Browse files- static/application.js +11 -71
static/application.js
CHANGED
@@ -42,7 +42,7 @@ document.addEventListener("DOMContentLoaded", () => {
|
|
42 |
|
43 |
const message = document.createElement("div");
|
44 |
message.className = "text";
|
45 |
-
message.style.whiteSpace = "pre-wrap";
|
46 |
|
47 |
if (sender === "You") {
|
48 |
let fileLine = fileName ? `📎 Selected file: ${fileName}\n` : "";
|
@@ -84,9 +84,7 @@ document.addEventListener("DOMContentLoaded", () => {
|
|
84 |
return bubble;
|
85 |
}
|
86 |
|
87 |
-
|
88 |
-
input.value = "";
|
89 |
-
selectedFile = null;
|
90 |
const question = input.value.trim();
|
91 |
if (!question) return;
|
92 |
|
@@ -152,76 +150,18 @@ document.addEventListener("DOMContentLoaded", () => {
|
|
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 |
-
|
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 |
-
|
210 |
-
|
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 |
-
|
217 |
-
|
218 |
-
|
219 |
-
|
220 |
-
} catch (err) {
|
221 |
-
const message = thinkingBubble.querySelector(".text");
|
222 |
-
message.innerText = "⚠️ Chris had trouble responding.";
|
223 |
-
}
|
224 |
}
|
225 |
-
|
226 |
});
|
227 |
-
});
|
|
|
42 |
|
43 |
const message = document.createElement("div");
|
44 |
message.className = "text";
|
45 |
+
message.style.whiteSpace = "pre-wrap";
|
46 |
|
47 |
if (sender === "You") {
|
48 |
let fileLine = fileName ? `📎 Selected file: ${fileName}\n` : "";
|
|
|
84 |
return bubble;
|
85 |
}
|
86 |
|
87 |
+
async function sendMessage() {
|
|
|
|
|
88 |
const question = input.value.trim();
|
89 |
if (!question) return;
|
90 |
|
|
|
150 |
message.innerText = "⚠️ Chris had trouble responding.";
|
151 |
}
|
152 |
|
153 |
+
// Clear inputs AFTER processing
|
|
|
|
|
154 |
input.value = "";
|
155 |
selectedFile = null;
|
156 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
157 |
|
158 |
+
// Click event for send button
|
159 |
+
sendBtn.addEventListener("click", sendMessage);
|
|
|
|
|
|
|
|
|
160 |
|
161 |
+
// Enter key event for input field
|
162 |
+
input.addEventListener("keyup", (event) => {
|
163 |
+
if (event.key === "Enter") {
|
164 |
+
sendMessage();
|
|
|
|
|
|
|
|
|
165 |
}
|
|
|
166 |
});
|
167 |
+
});
|