Spaces:
Running
Running
Update static/application.js
Browse files- static/application.js +71 -0
static/application.js
CHANGED
@@ -153,4 +153,75 @@ document.addEventListener("DOMContentLoaded", () => {
|
|
153 |
input.value = "";
|
154 |
selectedFile = null;
|
155 |
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
156 |
});
|
|
|
153 |
input.value = "";
|
154 |
selectedFile = null;
|
155 |
});
|
156 |
+
input.addEventListener("keyup", async (event) => {
|
157 |
+
if (event.key === "Enter") {
|
158 |
+
const question = input.value.trim();
|
159 |
+
if (!question) return;
|
160 |
+
|
161 |
+
if (!selectedFile) {
|
162 |
+
alert("Please upload a document or image first.");
|
163 |
+
return;
|
164 |
+
}
|
165 |
+
|
166 |
+
// Remove file preview bubble
|
167 |
+
if (filePreviewBubble) {
|
168 |
+
filePreviewBubble.remove();
|
169 |
+
filePreviewBubble = null;
|
170 |
+
}
|
171 |
+
|
172 |
+
// User message with file info
|
173 |
+
createMessageBubble(question, "You", null, selectedFile.name);
|
174 |
+
|
175 |
+
// Chris is thinking...
|
176 |
+
const thinkingBubble = createMessageBubble("Wait , Let me think 🤔...", "Chris");
|
177 |
+
|
178 |
+
const formData = new FormData();
|
179 |
+
formData.append("question", question);
|
180 |
+
formData.append("file", selectedFile);
|
181 |
+
|
182 |
+
try {
|
183 |
+
const response = await fetch("/predict", {
|
184 |
+
method: "POST",
|
185 |
+
body: formData
|
186 |
+
});
|
187 |
+
|
188 |
+
const result = await response.json();
|
189 |
+
const answerText = result.answer || "No response.";
|
190 |
+
const audioSrc = result.audio || null;
|
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 |
+
input.value = "";
|
224 |
+
selectedFile = null;
|
225 |
+
}
|
226 |
+
});
|
227 |
});
|