Spaces:
Running
Running
Update static/appS.js
Browse files- static/appS.js +357 -82
static/appS.js
CHANGED
@@ -1,82 +1,357 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
document.addEventListener('DOMContentLoaded', () => {
|
4 |
-
var GotitB = document.querySelector(".explainChoix button")
|
5 |
-
var explain = document.querySelector(".explainChoix")
|
6 |
-
var SummarizeInput = document.querySelector(".SummarizeInput")
|
7 |
-
var CaptionInput = document.querySelector(".CaptionInput")
|
8 |
-
GotitB.addEventListener("click",()=>{
|
9 |
-
explain.style.opacity="0"
|
10 |
-
})
|
11 |
-
|
12 |
-
document.querySelectorAll('.select-options input[type="radio"]').forEach(radio => {
|
13 |
-
radio.addEventListener('change', (e) => {
|
14 |
-
if (e.target.checked) {
|
15 |
-
const selectedValue = e.target.value;
|
16 |
-
if(selectedValue=="Summarize"){
|
17 |
-
SummarizeInput.classList.add("active")
|
18 |
-
SummarizeInput.classList.remove("innactive")
|
19 |
-
CaptionInput.classList.remove("active")
|
20 |
-
CaptionInput.classList.add("innactive")
|
21 |
-
}
|
22 |
-
else{
|
23 |
-
SummarizeInput.classList.add("innactive")
|
24 |
-
SummarizeInput.classList.remove("active")
|
25 |
-
CaptionInput.classList.remove("innactive")
|
26 |
-
CaptionInput.classList.add("active")
|
27 |
-
}
|
28 |
-
}
|
29 |
-
});
|
30 |
-
});
|
31 |
-
const fileUpload = document.getElementById('file-upload');
|
32 |
-
const imageUpload = document.getElementById('image-upload');
|
33 |
-
|
34 |
-
// Get the icon buttons
|
35 |
-
const fileBtn = document.getElementById('file-btn');
|
36 |
-
const imageBtn = document.getElementById('image-btn');
|
37 |
-
|
38 |
-
// Set up file input for documents (PDF, DOCX, PPTX, XLSX)
|
39 |
-
fileBtn.addEventListener('click', () => {
|
40 |
-
fileUpload.click();
|
41 |
-
});
|
42 |
-
|
43 |
-
fileUpload.addEventListener('change', (e) => {
|
44 |
-
if (e.target.files.length > 0) {
|
45 |
-
const file = e.target.files[0];
|
46 |
-
const validDocTypes = [
|
47 |
-
'application/pdf',
|
48 |
-
'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
|
49 |
-
'application/vnd.openxmlformats-officedocument.presentationml.presentation',
|
50 |
-
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
|
51 |
-
];
|
52 |
-
|
53 |
-
if (validDocTypes.includes(file.type)) {
|
54 |
-
console.log('Valid document selected:', file.name);
|
55 |
-
// Handle the document file here
|
56 |
-
} else {
|
57 |
-
alert('Please select a valid document (PDF, DOCX, PPTX, or XLSX)');
|
58 |
-
fileUpload.value = ''; // Reset the input
|
59 |
-
}
|
60 |
-
}
|
61 |
-
});
|
62 |
-
|
63 |
-
// Set up file input for images
|
64 |
-
imageBtn.addEventListener('click', () => {
|
65 |
-
imageUpload.click();
|
66 |
-
});
|
67 |
-
|
68 |
-
imageUpload.addEventListener('change', (e) => {
|
69 |
-
if (e.target.files.length > 0) {
|
70 |
-
const file = e.target.files[0];
|
71 |
-
const validImageTypes = ['image/jpeg', 'image/png', 'image/gif', 'image/webp'];
|
72 |
-
|
73 |
-
if (validImageTypes.includes(file.type)) {
|
74 |
-
console.log('Valid image selected:', file.name);
|
75 |
-
// Handle the image file here
|
76 |
-
} else {
|
77 |
-
alert('Please select a valid image (JPEG, PNG, GIF, or WEBP)');
|
78 |
-
imageUpload.value = ''; // Reset the input
|
79 |
-
}
|
80 |
-
}
|
81 |
-
});
|
82 |
-
})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
/*
|
3 |
+
document.addEventListener('DOMContentLoaded', () => {
|
4 |
+
var GotitB = document.querySelector(".explainChoix button")
|
5 |
+
var explain = document.querySelector(".explainChoix")
|
6 |
+
var SummarizeInput = document.querySelector(".SummarizeInput")
|
7 |
+
var CaptionInput = document.querySelector(".CaptionInput")
|
8 |
+
GotitB.addEventListener("click",()=>{
|
9 |
+
explain.style.opacity="0"
|
10 |
+
})
|
11 |
+
|
12 |
+
document.querySelectorAll('.select-options input[type="radio"]').forEach(radio => {
|
13 |
+
radio.addEventListener('change', (e) => {
|
14 |
+
if (e.target.checked) {
|
15 |
+
const selectedValue = e.target.value;
|
16 |
+
if(selectedValue=="Summarize"){
|
17 |
+
SummarizeInput.classList.add("active")
|
18 |
+
SummarizeInput.classList.remove("innactive")
|
19 |
+
CaptionInput.classList.remove("active")
|
20 |
+
CaptionInput.classList.add("innactive")
|
21 |
+
}
|
22 |
+
else{
|
23 |
+
SummarizeInput.classList.add("innactive")
|
24 |
+
SummarizeInput.classList.remove("active")
|
25 |
+
CaptionInput.classList.remove("innactive")
|
26 |
+
CaptionInput.classList.add("active")
|
27 |
+
}
|
28 |
+
}
|
29 |
+
});
|
30 |
+
});
|
31 |
+
const fileUpload = document.getElementById('file-upload');
|
32 |
+
const imageUpload = document.getElementById('image-upload');
|
33 |
+
|
34 |
+
// Get the icon buttons
|
35 |
+
const fileBtn = document.getElementById('file-btn');
|
36 |
+
const imageBtn = document.getElementById('image-btn');
|
37 |
+
|
38 |
+
// Set up file input for documents (PDF, DOCX, PPTX, XLSX)
|
39 |
+
fileBtn.addEventListener('click', () => {
|
40 |
+
fileUpload.click();
|
41 |
+
});
|
42 |
+
|
43 |
+
fileUpload.addEventListener('change', (e) => {
|
44 |
+
if (e.target.files.length > 0) {
|
45 |
+
const file = e.target.files[0];
|
46 |
+
const validDocTypes = [
|
47 |
+
'application/pdf',
|
48 |
+
'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
|
49 |
+
'application/vnd.openxmlformats-officedocument.presentationml.presentation',
|
50 |
+
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
|
51 |
+
];
|
52 |
+
|
53 |
+
if (validDocTypes.includes(file.type)) {
|
54 |
+
console.log('Valid document selected:', file.name);
|
55 |
+
// Handle the document file here
|
56 |
+
} else {
|
57 |
+
alert('Please select a valid document (PDF, DOCX, PPTX, or XLSX)');
|
58 |
+
fileUpload.value = ''; // Reset the input
|
59 |
+
}
|
60 |
+
}
|
61 |
+
});
|
62 |
+
|
63 |
+
// Set up file input for images
|
64 |
+
imageBtn.addEventListener('click', () => {
|
65 |
+
imageUpload.click();
|
66 |
+
});
|
67 |
+
|
68 |
+
imageUpload.addEventListener('change', (e) => {
|
69 |
+
if (e.target.files.length > 0) {
|
70 |
+
const file = e.target.files[0];
|
71 |
+
const validImageTypes = ['image/jpeg', 'image/png', 'image/gif', 'image/webp'];
|
72 |
+
|
73 |
+
if (validImageTypes.includes(file.type)) {
|
74 |
+
console.log('Valid image selected:', file.name);
|
75 |
+
// Handle the image file here
|
76 |
+
} else {
|
77 |
+
alert('Please select a valid image (JPEG, PNG, GIF, or WEBP)');
|
78 |
+
imageUpload.value = ''; // Reset the input
|
79 |
+
}
|
80 |
+
}
|
81 |
+
});
|
82 |
+
});*/
|
83 |
+
document.addEventListener('DOMContentLoaded', () => {
|
84 |
+
// UI Elements
|
85 |
+
const GotitB = document.querySelector(".explainChoix button");
|
86 |
+
const explain = document.querySelector(".explainChoix");
|
87 |
+
const SummarizeInput = document.querySelector(".SummarizeInput");
|
88 |
+
const CaptionInput = document.querySelector(".CaptionInput");
|
89 |
+
const fileUpload = document.getElementById('file-upload');
|
90 |
+
const imageUpload = document.getElementById('image-upload');
|
91 |
+
const fileBtn = document.getElementById('file-btn');
|
92 |
+
const imageBtn = document.getElementById('image-btn');
|
93 |
+
const convo = document.querySelector('.convo');
|
94 |
+
|
95 |
+
// Hide explanation after click
|
96 |
+
GotitB.addEventListener("click", () => {
|
97 |
+
explain.style.opacity = "0";
|
98 |
+
});
|
99 |
+
|
100 |
+
// Handle mode switching
|
101 |
+
document.querySelectorAll('.select-options input[type="radio"]').forEach(radio => {
|
102 |
+
radio.addEventListener('change', (e) => {
|
103 |
+
if (e.target.checked) {
|
104 |
+
const selectedValue = e.target.value;
|
105 |
+
if (selectedValue === "Summarize") {
|
106 |
+
SummarizeInput.classList.add("active");
|
107 |
+
SummarizeInput.classList.remove("innactive");
|
108 |
+
CaptionInput.classList.remove("active");
|
109 |
+
CaptionInput.classList.add("innactive");
|
110 |
+
} else {
|
111 |
+
SummarizeInput.classList.add("innactive");
|
112 |
+
SummarizeInput.classList.remove("active");
|
113 |
+
CaptionInput.classList.remove("innactive");
|
114 |
+
CaptionInput.classList.add("active");
|
115 |
+
}
|
116 |
+
}
|
117 |
+
});
|
118 |
+
});
|
119 |
+
|
120 |
+
// File upload handlers
|
121 |
+
fileBtn.addEventListener('click', () => fileUpload.click());
|
122 |
+
imageBtn.addEventListener('click', () => imageUpload.click());
|
123 |
+
|
124 |
+
// Send button handlers
|
125 |
+
document.querySelectorAll('.sendingQA').forEach(button => {
|
126 |
+
button.addEventListener('click', async () => {
|
127 |
+
const isSummarizeMode = document.querySelector('input[name="option"]:checked').value === 'Summarize';
|
128 |
+
|
129 |
+
if (isSummarizeMode) {
|
130 |
+
await handleSummarize();
|
131 |
+
} else {
|
132 |
+
await handleCaption();
|
133 |
+
}
|
134 |
+
});
|
135 |
+
});
|
136 |
+
|
137 |
+
// Drag and drop functionality
|
138 |
+
convo.addEventListener('dragover', (e) => {
|
139 |
+
e.preventDefault();
|
140 |
+
convo.classList.add('drag-over');
|
141 |
+
});
|
142 |
+
|
143 |
+
convo.addEventListener('dragleave', () => {
|
144 |
+
convo.classList.remove('drag-over');
|
145 |
+
});
|
146 |
+
|
147 |
+
convo.addEventListener('drop', async (e) => {
|
148 |
+
e.preventDefault();
|
149 |
+
convo.classList.remove('drag-over');
|
150 |
+
|
151 |
+
if (e.dataTransfer.files.length) {
|
152 |
+
const file = e.dataTransfer.files[0];
|
153 |
+
const isSummarizeMode = document.querySelector('input[name="option"]:checked').value === 'Summarize';
|
154 |
+
|
155 |
+
if (isSummarizeMode) {
|
156 |
+
fileUpload.files = e.dataTransfer.files;
|
157 |
+
await handleSummarize();
|
158 |
+
} else {
|
159 |
+
imageUpload.files = e.dataTransfer.files;
|
160 |
+
await handleCaption();
|
161 |
+
}
|
162 |
+
}
|
163 |
+
});
|
164 |
+
|
165 |
+
// Handle document summarization
|
166 |
+
async function handleSummarize() {
|
167 |
+
const file = fileUpload.files[0];
|
168 |
+
if (!file) {
|
169 |
+
displayError('Please upload a document first');
|
170 |
+
return;
|
171 |
+
}
|
172 |
+
|
173 |
+
const length = document.querySelector('input[name="optionS"]:checked').value;
|
174 |
+
|
175 |
+
try {
|
176 |
+
// Show loading state
|
177 |
+
convo.innerHTML = '';
|
178 |
+
displayFileInfo(file.name, 'document');
|
179 |
+
displayThinkingMessage();
|
180 |
+
|
181 |
+
// Call API
|
182 |
+
const result = await summarizeDocument(file, length);
|
183 |
+
|
184 |
+
// Display results
|
185 |
+
displaySummaryResult(file.name, result.summary, result.audioUrl, result.pdfUrl);
|
186 |
+
} catch (error) {
|
187 |
+
displayError(error.message || 'Failed to summarize document');
|
188 |
+
}
|
189 |
+
}
|
190 |
+
|
191 |
+
// Handle image captioning
|
192 |
+
async function handleCaption() {
|
193 |
+
const file = imageUpload.files[0];
|
194 |
+
if (!file) {
|
195 |
+
displayError('Please upload an image first');
|
196 |
+
return;
|
197 |
+
}
|
198 |
+
|
199 |
+
try {
|
200 |
+
// Show loading state
|
201 |
+
convo.innerHTML = '';
|
202 |
+
displayFileInfo(file.name, 'image');
|
203 |
+
displayThinkingMessage();
|
204 |
+
|
205 |
+
// Call API
|
206 |
+
const caption = await captionImage(file);
|
207 |
+
|
208 |
+
// Display results
|
209 |
+
displayCaptionResult(file.name, caption);
|
210 |
+
} catch (error) {
|
211 |
+
displayError(error.message || 'Failed to generate caption');
|
212 |
+
}
|
213 |
+
}
|
214 |
+
|
215 |
+
// API Functions
|
216 |
+
async function summarizeDocument(file, length) {
|
217 |
+
const formData = new FormData();
|
218 |
+
formData.append('file', file);
|
219 |
+
formData.append('length', length);
|
220 |
+
|
221 |
+
const response = await fetch('/summarize', {
|
222 |
+
method: 'POST',
|
223 |
+
body: formData
|
224 |
+
});
|
225 |
+
|
226 |
+
if (!response.ok) {
|
227 |
+
const error = await response.json();
|
228 |
+
throw new Error(error.detail || 'Summarization failed');
|
229 |
+
}
|
230 |
+
|
231 |
+
return await response.json();
|
232 |
+
}
|
233 |
+
|
234 |
+
async function captionImage(file) {
|
235 |
+
const formData = new FormData();
|
236 |
+
formData.append('file', file);
|
237 |
+
|
238 |
+
const response = await fetch('/caption', {
|
239 |
+
method: 'POST',
|
240 |
+
body: formData
|
241 |
+
});
|
242 |
+
|
243 |
+
if (!response.ok) {
|
244 |
+
const error = await response.json();
|
245 |
+
throw new Error(error.detail || 'Captioning failed');
|
246 |
+
}
|
247 |
+
|
248 |
+
const result = await response.json();
|
249 |
+
return result.caption;
|
250 |
+
}
|
251 |
+
|
252 |
+
// UI Helper Functions
|
253 |
+
function displayFileInfo(filename, type) {
|
254 |
+
const bubble = document.createElement('div');
|
255 |
+
bubble.className = 'bubble right';
|
256 |
+
bubble.innerHTML = `
|
257 |
+
<div class="label">You</div>
|
258 |
+
<div class="text">${type === 'document' ? '📄' : '🖼️'} ${filename}</div>
|
259 |
+
`;
|
260 |
+
convo.appendChild(bubble);
|
261 |
+
}
|
262 |
+
|
263 |
+
function displayThinkingMessage() {
|
264 |
+
const bubble = document.createElement('div');
|
265 |
+
bubble.className = 'bubble left';
|
266 |
+
bubble.innerHTML = `
|
267 |
+
<div class="label">Aidan</div>
|
268 |
+
<div class="text">
|
269 |
+
<div style="display:flex;align-items:center;gap:8px">
|
270 |
+
<span>Processing your ${document.querySelector('input[name="option"]:checked').value.toLowerCase()}...</span>
|
271 |
+
<div class="loader"></div>
|
272 |
+
</div>
|
273 |
+
</div>
|
274 |
+
`;
|
275 |
+
convo.appendChild(bubble);
|
276 |
+
convo.scrollTop = convo.scrollHeight;
|
277 |
+
}
|
278 |
+
|
279 |
+
function displaySummaryResult(filename, summary, audioUrl, pdfUrl) {
|
280 |
+
// Remove thinking message
|
281 |
+
convo.removeChild(convo.lastChild);
|
282 |
+
|
283 |
+
const bubble = document.createElement('div');
|
284 |
+
bubble.className = 'bubble left';
|
285 |
+
bubble.innerHTML = `
|
286 |
+
<div class="label">Aidan</div>
|
287 |
+
<div class="text">
|
288 |
+
<strong>Summary:</strong><br><br>
|
289 |
+
${summary.replace(/\n/g, '<br>')}
|
290 |
+
${audioUrl ? `<br><br><audio controls src="${audioUrl}" style="width: 100%;"></audio>` : ''}
|
291 |
+
${pdfUrl ? `<br><a href="${pdfUrl}" download="${filename.split('.')[0]}_summary.pdf" class="download-link">📥 Download PDF Summary</a>` : ''}
|
292 |
+
</div>
|
293 |
+
`;
|
294 |
+
convo.appendChild(bubble);
|
295 |
+
convo.scrollTop = convo.scrollHeight;
|
296 |
+
}
|
297 |
+
|
298 |
+
function displayCaptionResult(filename, caption) {
|
299 |
+
// Remove thinking message
|
300 |
+
convo.removeChild(convo.lastChild);
|
301 |
+
|
302 |
+
const bubble = document.createElement('div');
|
303 |
+
bubble.className = 'bubble left';
|
304 |
+
bubble.innerHTML = `
|
305 |
+
<div class="label">Aidan</div>
|
306 |
+
<div class="text">
|
307 |
+
<strong>Caption:</strong><br><br>
|
308 |
+
${caption}
|
309 |
+
</div>
|
310 |
+
`;
|
311 |
+
convo.appendChild(bubble);
|
312 |
+
convo.scrollTop = convo.scrollHeight;
|
313 |
+
}
|
314 |
+
|
315 |
+
function displayError(message) {
|
316 |
+
const bubble = document.createElement('div');
|
317 |
+
bubble.className = 'bubble left';
|
318 |
+
bubble.innerHTML = `
|
319 |
+
<div class="label">Aidan</div>
|
320 |
+
<div class="text" style="color: #ef4444;">
|
321 |
+
⚠️ ${message}
|
322 |
+
</div>
|
323 |
+
`;
|
324 |
+
convo.appendChild(bubble);
|
325 |
+
convo.scrollTop = convo.scrollHeight;
|
326 |
+
}
|
327 |
+
|
328 |
+
// Add loader CSS
|
329 |
+
const style = document.createElement('style');
|
330 |
+
style.textContent = `
|
331 |
+
.loader {
|
332 |
+
border: 2px solid #f3f3f3;
|
333 |
+
border-top: 2px solid #3b82f6;
|
334 |
+
border-radius: 50%;
|
335 |
+
width: 16px;
|
336 |
+
height: 16px;
|
337 |
+
animation: spin 1s linear infinite;
|
338 |
+
}
|
339 |
+
@keyframes spin {
|
340 |
+
0% { transform: rotate(0deg); }
|
341 |
+
100% { transform: rotate(360deg); }
|
342 |
+
}
|
343 |
+
.download-link {
|
344 |
+
display: inline-block;
|
345 |
+
margin-top: 10px;
|
346 |
+
padding: 5px 10px;
|
347 |
+
background: #3b82f6;
|
348 |
+
color: white;
|
349 |
+
border-radius: 5px;
|
350 |
+
text-decoration: none;
|
351 |
+
}
|
352 |
+
.download-link:hover {
|
353 |
+
background: #2563eb;
|
354 |
+
}
|
355 |
+
`;
|
356 |
+
document.head.appendChild(style);
|
357 |
+
});
|