Spaces:
Running
Running
File size: 6,218 Bytes
5a540b1 b18639b 4915e32 5a540b1 4915e32 5a540b1 4915e32 5a540b1 4915e32 5a540b1 4915e32 60d069b 4915e32 3d74941 4915e32 5a540b1 4915e32 5a540b1 4915e32 5a540b1 4915e32 5a540b1 4915e32 b18639b b3f0d65 b18639b 2597823 b18639b 2597823 b18639b 2597823 b18639b 2597823 b18639b 5a540b1 b18639b 2597823 b18639b 2597823 5a540b1 b18639b 2597823 b18639b |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 |
/*const inputField = document.querySelector(".qt input");
const icons = document.querySelectorAll(".icons i");
const convo = document.querySelector(".convo");
const send = document.querySelector(".sendingQA");
// Create hidden file inputs dynamically
const imageInput = document.createElement("input");
imageInput.type = "file";
imageInput.accept = "image/*";
imageInput.style.display = "none";
const docInput = document.createElement("input");
docInput.type = "file";
docInput.accept = ".pdf,.docx,.pptx,.xlsx";
docInput.style.display = "none";
document.body.appendChild(imageInput);
document.body.appendChild(docInput);
let selectedFile = null;
let selectedType = null;
// Icon click handlers
icons[0].addEventListener("click", () => imageInput.click()); // image icon
icons[1].addEventListener("click", () => docInput.click()); // file icon
send.addEventListener("click", handleSubmit); // send button
imageInput.addEventListener("change", () => {
selectedFile = imageInput.files[0];
selectedType = "Image";
showSelectedFile(selectedFile.name, "๐ผ๏ธ");
});
docInput.addEventListener("change", () => {
selectedFile = docInput.files[0];
selectedType = "Document";
showSelectedFile(selectedFile.name, "๐");
});
function showSelectedFile(fileName, icon) {
const fileNotice = document.createElement("div");
fileNotice.style.padding = "0.8rem";
fileNotice.style.backgroundColor = "#f3f3f3";
fileNotice.style.borderRadius = "10px";
fileNotice.style.margin = "1rem";
fileNotice.innerHTML = `${icon} <strong>Selected file:</strong> ${fileName}`;
convo.appendChild(fileNotice);
}
async function handleSubmit() {
const question = inputField.value.trim();
if (!question && !selectedFile) {
alert("Please type a question or select a file.");
return;
}
const formData = new FormData();
formData.append("question", question);
formData.append("file", selectedFile);
// Show question in convo
const userBlock = document.createElement("div");
userBlock.style.padding = "1rem";
userBlock.style.background = "#e0e7ff";
userBlock.style.borderRadius = "10px";
userBlock.style.margin = "1rem";
userBlock.innerHTML = `
<strong>Question:</strong> ${question}<br>
${selectedFile ? `<strong>${selectedType}:</strong> ${selectedFile.name}` : ""}
`;
convo.appendChild(userBlock);
// Show loading message
const loadingBlock = document.createElement("div");
loadingBlock.style.padding = "1rem";
loadingBlock.style.background = "#eee";
loadingBlock.style.borderRadius = "10px";
loadingBlock.style.margin = "1rem";
loadingBlock.innerText = "Processing your request...";
convo.appendChild(loadingBlock);
try {
const response = await fetch("/predict", {
method: "POST",
body: formData,
});
const result = await response.json();
loadingBlock.remove(); // remove loading message
const answerBlock = document.createElement("div");
answerBlock.style.padding = "1rem";
answerBlock.style.background = "#e1f7e1";
answerBlock.style.borderRadius = "10px";
answerBlock.style.margin = "1rem";
answerBlock.innerHTML = `
<strong>Answer:</strong> ${result.answer || result.error || "No response"}
`;
convo.appendChild(answerBlock);
} catch (error) {
loadingBlock.remove();
const errorBlock = document.createElement("div");
errorBlock.style.color = "red";
errorBlock.style.margin = "1rem";
errorBlock.innerText = `Request failed: ${error.message}`;
convo.appendChild(errorBlock);
}
// Reset input field and file
inputField.value = "";
selectedFile = null;
selectedType = null;
}
*/
// frontend.js
async function handleSubmit() {
const question = inputField.value.trim();
if (!question && !selectedFile) {
alert("Please type a question or select a file.");
return;
}
const formData = new FormData();
formData.append("question", question);
formData.append("file", selectedFile);
// Show user question
const userBlock = document.createElement("div");
userBlock.className = "user-block";
userBlock.innerHTML = `
<strong>Question:</strong> ${question}<br>
${selectedFile ? `<strong>${selectedType}:</strong> ${selectedFile.name}` : ""}
`;
convo.appendChild(userBlock);
// Loading message
const loadingBlock = document.createElement("div");
loadingBlock.className = "loading-block";
loadingBlock.innerText = "Processing your request...";
convo.appendChild(loadingBlock);
try {
const response = await fetch("/predict", {
method: "POST",
body: formData,
});
const result = await response.json();
loadingBlock.remove();
// Answer block with optional audio
const answerBlock = document.createElement("div");
answerBlock.className = "answer-block";
answerBlock.innerHTML = `
<strong>Answer:</strong> ${result.answer || result.error || "No response"}
${result.audio ? `
<div style="margin-top: 0.5rem;">
<i class="fa-solid fa-volume-high audio-toggle" title="Click to mute" style="cursor:pointer; margin-right: 8px;"></i>
<audio src="${result.audio}" autoplay></audio>
</div>
` : ""}
`;
convo.appendChild(answerBlock);
// Handle audio toggle
const audioIcon = answerBlock.querySelector(".audio-toggle");
const audioElement = answerBlock.querySelector("audio");
if (audioIcon && audioElement) {
audioIcon.addEventListener("click", () => {
if (audioElement.muted) {
audioElement.muted = false;
audioIcon.classList.replace("fa-volume-xmark", "fa-volume-high");
audioIcon.title = "Click to mute";
} else {
audioElement.muted = true;
audioIcon.classList.replace("fa-volume-high", "fa-volume-xmark");
audioIcon.title = "Click to unmute";
}
});
}
} catch (error) {
loadingBlock.remove();
const errorBlock = document.createElement("div");
errorBlock.className = "error-block";
errorBlock.innerText = `Request failed: ${error.message}`;
convo.appendChild(errorBlock);
}
inputField.value = "";
selectedFile = null;
selectedType = null;
}
|