Spaces:
Sleeping
Sleeping
Update static/test.js
Browse files- static/test.js +138 -0
static/test.js
CHANGED
|
@@ -235,3 +235,141 @@ document.querySelectorAll('.card').forEach(card => {
|
|
| 235 |
showSection(sectionId);
|
| 236 |
});
|
| 237 |
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 235 |
showSection(sectionId);
|
| 236 |
});
|
| 237 |
});
|
| 238 |
+
|
| 239 |
+
// Document Summarization Functionality
|
| 240 |
+
const summarizeDropArea = document.getElementById('summarize-drop-area');
|
| 241 |
+
const summarizeFileInput = document.getElementById('summarize-file-input');
|
| 242 |
+
const summarizePreview = document.getElementById('summarize-preview');
|
| 243 |
+
const summarizeBtn = document.getElementById('summarize-btn');
|
| 244 |
+
const summarizeResult = document.getElementById('summarize-result');
|
| 245 |
+
const summarizeError = document.getElementById('summarize-error');
|
| 246 |
+
|
| 247 |
+
// Set up drag and drop for summarization
|
| 248 |
+
setupFileUpload(summarizeDropArea, summarizeFileInput, summarizePreview);
|
| 249 |
+
|
| 250 |
+
// Summarize button click handler
|
| 251 |
+
summarizeBtn.addEventListener('click', async () => {
|
| 252 |
+
const file = summarizeFileInput.files[0];
|
| 253 |
+
|
| 254 |
+
if (!file) {
|
| 255 |
+
showError(summarizeError, "Please select a file first");
|
| 256 |
+
return;
|
| 257 |
+
}
|
| 258 |
+
|
| 259 |
+
try {
|
| 260 |
+
// Show loading state
|
| 261 |
+
summarizeBtn.disabled = true;
|
| 262 |
+
summarizeBtn.textContent = "Processing...";
|
| 263 |
+
hideError(summarizeError);
|
| 264 |
+
summarizeResult.style.display = 'none';
|
| 265 |
+
|
| 266 |
+
const formData = new FormData();
|
| 267 |
+
formData.append('file', file);
|
| 268 |
+
|
| 269 |
+
const response = await fetch('/summarize', {
|
| 270 |
+
method: 'POST',
|
| 271 |
+
body: formData
|
| 272 |
+
});
|
| 273 |
+
|
| 274 |
+
if (!response.ok) {
|
| 275 |
+
const error = await response.json();
|
| 276 |
+
throw new Error(error.detail || "Failed to summarize document");
|
| 277 |
+
}
|
| 278 |
+
|
| 279 |
+
const result = await response.json();
|
| 280 |
+
|
| 281 |
+
// Display the result
|
| 282 |
+
summarizeResult.innerHTML = `<h4>Summary:</h4><p>${result.summary}</p>`;
|
| 283 |
+
summarizeResult.style.display = 'block';
|
| 284 |
+
|
| 285 |
+
} catch (error) {
|
| 286 |
+
showError(summarizeError, error.message);
|
| 287 |
+
} finally {
|
| 288 |
+
summarizeBtn.disabled = false;
|
| 289 |
+
summarizeBtn.textContent = "Summarize Document";
|
| 290 |
+
}
|
| 291 |
+
});
|
| 292 |
+
|
| 293 |
+
// Helper functions for file upload
|
| 294 |
+
function setupFileUpload(dropArea, fileInput, previewElement) {
|
| 295 |
+
// Drag and drop functionality
|
| 296 |
+
['dragenter', 'dragover', 'dragleave', 'drop'].forEach(eventName => {
|
| 297 |
+
dropArea.addEventListener(eventName, preventDefaults, false);
|
| 298 |
+
});
|
| 299 |
+
|
| 300 |
+
['dragenter', 'dragover'].forEach(eventName => {
|
| 301 |
+
dropArea.addEventListener(eventName, () => {
|
| 302 |
+
dropArea.classList.add('highlight');
|
| 303 |
+
}, false);
|
| 304 |
+
});
|
| 305 |
+
|
| 306 |
+
['dragleave', 'drop'].forEach(eventName => {
|
| 307 |
+
dropArea.addEventListener(eventName, () => {
|
| 308 |
+
dropArea.classList.remove('highlight');
|
| 309 |
+
}, false);
|
| 310 |
+
});
|
| 311 |
+
|
| 312 |
+
dropArea.addEventListener('drop', handleDrop, false);
|
| 313 |
+
dropArea.addEventListener('click', () => fileInput.click());
|
| 314 |
+
fileInput.addEventListener('change', handleFileSelect);
|
| 315 |
+
|
| 316 |
+
function handleDrop(e) {
|
| 317 |
+
const dt = e.dataTransfer;
|
| 318 |
+
const files = dt.files;
|
| 319 |
+
handleFiles(files);
|
| 320 |
+
}
|
| 321 |
+
|
| 322 |
+
function handleFileSelect(e) {
|
| 323 |
+
const files = e.target.files;
|
| 324 |
+
if (files.length) {
|
| 325 |
+
handleFiles(files);
|
| 326 |
+
}
|
| 327 |
+
}
|
| 328 |
+
|
| 329 |
+
function handleFiles(files) {
|
| 330 |
+
const file = files[0];
|
| 331 |
+
previewElement.innerHTML = `Selected: <strong>${file.name}</strong> (${formatFileSize(file.size)})`;
|
| 332 |
+
fileInput.files = files;
|
| 333 |
+
}
|
| 334 |
+
}
|
| 335 |
+
|
| 336 |
+
function preventDefaults(e) {
|
| 337 |
+
e.preventDefault();
|
| 338 |
+
e.stopPropagation();
|
| 339 |
+
}
|
| 340 |
+
|
| 341 |
+
function formatFileSize(bytes) {
|
| 342 |
+
if (bytes === 0) return '0 Bytes';
|
| 343 |
+
const k = 1024;
|
| 344 |
+
const sizes = ['Bytes', 'KB', 'MB', 'GB'];
|
| 345 |
+
const i = Math.floor(Math.log(bytes) / Math.log(k));
|
| 346 |
+
return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i];
|
| 347 |
+
}
|
| 348 |
+
|
| 349 |
+
function showError(element, message) {
|
| 350 |
+
element.textContent = message;
|
| 351 |
+
element.style.display = 'block';
|
| 352 |
+
}
|
| 353 |
+
|
| 354 |
+
function hideError(element) {
|
| 355 |
+
element.style.display = 'none';
|
| 356 |
+
}
|
| 357 |
+
|
| 358 |
+
// Keep your existing showSection function
|
| 359 |
+
function showSection(sectionId) {
|
| 360 |
+
const sections = document.querySelectorAll('.tool-section');
|
| 361 |
+
sections.forEach(section => {
|
| 362 |
+
section.style.display = 'none';
|
| 363 |
+
});
|
| 364 |
+
|
| 365 |
+
const activeSection = document.getElementById(sectionId);
|
| 366 |
+
if (activeSection) {
|
| 367 |
+
activeSection.style.display = 'block';
|
| 368 |
+
activeSection.scrollIntoView({ behavior: 'smooth' });
|
| 369 |
+
}
|
| 370 |
+
}
|
| 371 |
+
|
| 372 |
+
// Initialize - show first section by default
|
| 373 |
+
document.addEventListener('DOMContentLoaded', () => {
|
| 374 |
+
showSection('document-image-analysis');
|
| 375 |
+
});
|