Spaces:
Running
Running
File size: 6,356 Bytes
b18639b 4915e32 e0e61b1 4915e32 e0e61b1 4915e32 60d069b 4915e32 3d74941 4915e32 3d74941 4915e32 3d74941 4915e32 3d74941 4915e32 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 199 200 201 |
/*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
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) {
convo.innerHTML = `
<div style="padding: 0.8rem; background-color: #f3f3f3; border-radius: 10px; margin: 1rem;">
${icon} <strong>Selected file:</strong> ${fileName}
</div>
`;
}
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);
convo.innerHTML = `<div style="padding: 1rem; background: #eee; border-radius: 8px;">Processing your request...</div>`;
try {
const response = await fetch("/predict", {
method: "POST",
body: formData,
});
const result = await response.json();
if (result.answer) {
convo.innerHTML = `
<div style="padding: 1rem; background: #e1f7e1; border-radius: 10px; margin: 1rem;">
<strong>Question:</strong> ${question}<br>
${selectedFile ? `<strong>${selectedType}:</strong> ${selectedFile.name}<br>` : ""}
<strong>Answer:</strong> ${result.answer}
</div>
`;
} else {
convo.innerHTML = `<div style="color: red;">Error: ${result.error}</div>`;
}
} catch (error) {
convo.innerHTML = `<div style="color: red;">Request failed: ${error.message}</div>`;
}
// Reset input field
inputField.value = "";
}
*/
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;
}
|