Spaces:
Running
Running
Update static/application.js
Browse files- static/application.js +117 -1
static/application.js
CHANGED
@@ -1,4 +1,4 @@
|
|
1 |
-
const inputField = document.querySelector(".qt input");
|
2 |
const icons = document.querySelectorAll(".icons i");
|
3 |
const convo = document.querySelector(".convo");
|
4 |
const send = document.querySelector(".sendingQA")
|
@@ -82,3 +82,119 @@ async function handleSubmit() {
|
|
82 |
// Reset input field
|
83 |
inputField.value = "";
|
84 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
/*const inputField = document.querySelector(".qt input");
|
2 |
const icons = document.querySelectorAll(".icons i");
|
3 |
const convo = document.querySelector(".convo");
|
4 |
const send = document.querySelector(".sendingQA")
|
|
|
82 |
// Reset input field
|
83 |
inputField.value = "";
|
84 |
}
|
85 |
+
*/
|
86 |
+
const inputField = document.querySelector(".qt input");
|
87 |
+
const icons = document.querySelectorAll(".icons i");
|
88 |
+
const convo = document.querySelector(".convo");
|
89 |
+
const send = document.querySelector(".sendingQA");
|
90 |
+
|
91 |
+
// Create hidden file inputs dynamically
|
92 |
+
const imageInput = document.createElement("input");
|
93 |
+
imageInput.type = "file";
|
94 |
+
imageInput.accept = "image/*";
|
95 |
+
imageInput.style.display = "none";
|
96 |
+
|
97 |
+
const docInput = document.createElement("input");
|
98 |
+
docInput.type = "file";
|
99 |
+
docInput.accept = ".pdf,.docx,.pptx,.xlsx";
|
100 |
+
docInput.style.display = "none";
|
101 |
+
|
102 |
+
document.body.appendChild(imageInput);
|
103 |
+
document.body.appendChild(docInput);
|
104 |
+
|
105 |
+
let selectedFile = null;
|
106 |
+
let selectedType = null;
|
107 |
+
|
108 |
+
// Icon click handlers
|
109 |
+
icons[0].addEventListener("click", () => imageInput.click()); // image icon
|
110 |
+
icons[1].addEventListener("click", () => docInput.click()); // file icon
|
111 |
+
send.addEventListener("click", handleSubmit); // send button
|
112 |
+
|
113 |
+
imageInput.addEventListener("change", () => {
|
114 |
+
selectedFile = imageInput.files[0];
|
115 |
+
selectedType = "Image";
|
116 |
+
showSelectedFile(selectedFile.name, "🖼️");
|
117 |
+
});
|
118 |
+
|
119 |
+
docInput.addEventListener("change", () => {
|
120 |
+
selectedFile = docInput.files[0];
|
121 |
+
selectedType = "Document";
|
122 |
+
showSelectedFile(selectedFile.name, "📄");
|
123 |
+
});
|
124 |
+
|
125 |
+
function showSelectedFile(fileName, icon) {
|
126 |
+
const fileNotice = document.createElement("div");
|
127 |
+
fileNotice.style.padding = "0.8rem";
|
128 |
+
fileNotice.style.backgroundColor = "#f3f3f3";
|
129 |
+
fileNotice.style.borderRadius = "10px";
|
130 |
+
fileNotice.style.margin = "1rem";
|
131 |
+
fileNotice.innerHTML = `${icon} <strong>Selected file:</strong> ${fileName}`;
|
132 |
+
convo.appendChild(fileNotice);
|
133 |
+
}
|
134 |
+
|
135 |
+
async function handleSubmit() {
|
136 |
+
const question = inputField.value.trim();
|
137 |
+
if (!question && !selectedFile) {
|
138 |
+
alert("Please type a question or select a file.");
|
139 |
+
return;
|
140 |
+
}
|
141 |
+
|
142 |
+
const formData = new FormData();
|
143 |
+
formData.append("question", question);
|
144 |
+
formData.append("file", selectedFile);
|
145 |
+
|
146 |
+
// Show question in convo
|
147 |
+
const userBlock = document.createElement("div");
|
148 |
+
userBlock.style.padding = "1rem";
|
149 |
+
userBlock.style.background = "#e0e7ff";
|
150 |
+
userBlock.style.borderRadius = "10px";
|
151 |
+
userBlock.style.margin = "1rem";
|
152 |
+
userBlock.innerHTML = `
|
153 |
+
<strong>Question:</strong> ${question}<br>
|
154 |
+
${selectedFile ? `<strong>${selectedType}:</strong> ${selectedFile.name}` : ""}
|
155 |
+
`;
|
156 |
+
convo.appendChild(userBlock);
|
157 |
+
|
158 |
+
// Show loading message
|
159 |
+
const loadingBlock = document.createElement("div");
|
160 |
+
loadingBlock.style.padding = "1rem";
|
161 |
+
loadingBlock.style.background = "#eee";
|
162 |
+
loadingBlock.style.borderRadius = "10px";
|
163 |
+
loadingBlock.style.margin = "1rem";
|
164 |
+
loadingBlock.innerText = "Processing your request...";
|
165 |
+
convo.appendChild(loadingBlock);
|
166 |
+
|
167 |
+
try {
|
168 |
+
const response = await fetch("/predict", {
|
169 |
+
method: "POST",
|
170 |
+
body: formData,
|
171 |
+
});
|
172 |
+
|
173 |
+
const result = await response.json();
|
174 |
+
|
175 |
+
loadingBlock.remove(); // remove loading message
|
176 |
+
|
177 |
+
const answerBlock = document.createElement("div");
|
178 |
+
answerBlock.style.padding = "1rem";
|
179 |
+
answerBlock.style.background = "#e1f7e1";
|
180 |
+
answerBlock.style.borderRadius = "10px";
|
181 |
+
answerBlock.style.margin = "1rem";
|
182 |
+
answerBlock.innerHTML = `
|
183 |
+
<strong>Answer:</strong> ${result.answer || result.error || "No response"}
|
184 |
+
`;
|
185 |
+
convo.appendChild(answerBlock);
|
186 |
+
} catch (error) {
|
187 |
+
loadingBlock.remove();
|
188 |
+
|
189 |
+
const errorBlock = document.createElement("div");
|
190 |
+
errorBlock.style.color = "red";
|
191 |
+
errorBlock.style.margin = "1rem";
|
192 |
+
errorBlock.innerText = `Request failed: ${error.message}`;
|
193 |
+
convo.appendChild(errorBlock);
|
194 |
+
}
|
195 |
+
|
196 |
+
// Reset input field and file
|
197 |
+
inputField.value = "";
|
198 |
+
selectedFile = null;
|
199 |
+
selectedType = null;
|
200 |
+
}
|