Spaces:
Sleeping
Sleeping
File size: 17,920 Bytes
cbddeae a6ffce9 9beb60d 997dc45 db10a5f a6ffce9 db10a5f a6ffce9 db10a5f a6ffce9 7ef985b a6ffce9 7ef985b 5601c4a a6ffce9 11c8146 a6ffce9 11c8146 db10a5f 0f53820 db10a5f 0f53820 db10a5f 0f53820 db10a5f 0f53820 db10a5f 0f53820 db10a5f 0f53820 db10a5f 1cda2c9 db10a5f 0f53820 db10a5f 1cda2c9 db10a5f 1cda2c9 db10a5f 1cda2c9 db10a5f 1cda2c9 5601c4a db10a5f 2ed44a9 db10a5f 5601c4a db10a5f 0fe854a db10a5f 72584a9 0fe854a db10a5f 8e60863 db10a5f 2ed44a9 db10a5f 2ed44a9 db10a5f fa1311d |
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 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 |
// Function to show the selected section and hide the others
function showSection(sectionId) {
// Show the selected section (in case it was hidden)
const activeSection = document.getElementById(sectionId);
if (activeSection) {
activeSection.style.display = 'block';
// Scroll to the section smoothly
activeSection.scrollIntoView({
behavior: 'smooth',
block: 'start' // Aligns the section at the top of the viewport
});
}
}
function showCards() {
// Show cards section
document.querySelector('.cards').style.display = 'grid'; // or 'flex' depending on your layout
// Hide all tool sections
const sections = document.querySelectorAll('.tool-section');
sections.forEach(section => {
section.style.display = 'none';
});
// Scroll to top
window.scrollTo({ top: 0, behavior: 'smooth' });
}
document.addEventListener('DOMContentLoaded', () => {
// Set up card click handlers
document.querySelectorAll('.card').forEach(card => {
card.addEventListener('click', function() {
const sectionId = this.getAttribute('onclick').match(/'([^']+)'/)[1];
showSection(sectionId);
});
});
// Hide all tool sections by default
const sections = document.querySelectorAll('.tool-section');
sections.forEach(section => {
section.style.display = 'none';
});
// Show first section by default
showSection('document-image-analysis');
// Helper functions
function preventDefaults(e) {
e.preventDefault();
e.stopPropagation();
}
function highlight(element) {
element.classList.add('highlight');
}
function unhighlight(element) {
element.classList.remove('highlight');
}
function isValidFileType(file, types, extensions) {
return types.includes(file.type) || extensions.some(ext => file.name.endsWith(ext));
}
function getFileIcon(file) {
if (file.type.startsWith('image/')) return 'πΌοΈ';
if (file.type === 'application/pdf') return 'π';
if (file.type.includes('wordprocessingml')) return 'π';
if (file.type.includes('spreadsheetml') || file.type === 'text/csv') return 'π';
return 'π';
}
function formatFileSize(bytes) {
if (bytes === 0) return '0 Bytes';
const k = 1024;
const sizes = ['Bytes', 'KB', 'MB', 'GB'];
const i = Math.floor(Math.log(bytes) / Math.log(k));
return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i];
}
function showError(element, message) {
element.textContent = message;
element.classList.remove('hidden');
}
function hideError(element) {
element.classList.add('hidden');
}
function setLoading(button, isLoading, loadingText = 'Processing...') {
const btnText = button.querySelector('span:not(.spinner)');
const spinner = button.querySelector('.spinner');
button.disabled = isLoading;
btnText.textContent = isLoading ? loadingText : btnText.dataset.defaultText;
spinner.classList.toggle('hidden', !isLoading);
}
// Document Analysis Section Setup
const summarizeDropArea = document.getElementById('upload-area');
const summarizeFileInput = document.getElementById('file-input');
const summarizePreview = document.getElementById('file-preview');
const textInput = document.getElementById('text-input');
const summarizeBtn = document.getElementById('summarize-btn');
const summarizeResult = document.getElementById('result-content');
const summarizeError = document.getElementById('error-message');
const languageSelect = document.getElementById('language-select');
// Store default button text
summarizeBtn.querySelector('span').dataset.defaultText = 'Summarize Document';
// Set up drag and drop for file upload
['dragenter', 'dragover', 'dragleave', 'drop'].forEach(eventName => {
summarizeDropArea.addEventListener(eventName, preventDefaults, false);
});
['dragenter', 'dragover'].forEach(eventName => {
summarizeDropArea.addEventListener(eventName, () => highlight(summarizeDropArea), false);
});
['dragleave', 'drop'].forEach(eventName => {
summarizeDropArea.addEventListener(eventName, () => unhighlight(summarizeDropArea), false);
});
summarizeDropArea.addEventListener('drop', handleDrop, false);
summarizeDropArea.addEventListener('click', (e) => {
if (e.target === summarizeDropArea) {
summarizeFileInput.click();
}
});
summarizeFileInput.addEventListener('change', handleFileSelect);
textInput.addEventListener('input', validateInputs);
function handleDrop(e) {
const dt = e.dataTransfer;
const files = dt.files;
handleFiles(files);
}
function handleFileSelect(e) {
const files = e.target.files;
if (files.length) {
handleFiles(files);
}
}
function handleFiles(files) {
const file = files[0];
const validTypes = [
'application/pdf',
'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
'text/plain',
'image/jpeg',
'image/png'
];
const validExtensions = ['.pdf', '.docx', '.txt'];
if (!isValidFileType(file, validTypes, validExtensions)) {
showError(summarizeError, "Unsupported file type. Please upload PDF, DOCX, TXT, or image files.");
return;
}
summarizePreview.innerHTML = `
<div class="file-info">
<span class="file-icon">${getFileIcon(file)}</span>
<div>
<strong>${file.name}</strong>
<span>${formatFileSize(file.size)}</span>
</div>
</div>
`;
validateInputs();
}
function validateInputs() {
const hasFile = summarizeFileInput.files.length > 0;
const hasText = textInput.value.trim() !== '';
summarizeBtn.disabled = !(hasFile || hasText);
}
// Summarize button click handler
summarizeBtn.addEventListener('click', async () => {
const file = summarizeFileInput.files[0];
const text = textInput.value.trim();
const language = languageSelect.value;
if (!file && !text) {
showError(summarizeError, "Please upload a file or enter text");
return;
}
try {
setLoading(summarizeBtn, true);
hideError(summarizeError);
summarizeResult.textContent = '';
const formData = new FormData();
if (file) formData.append('file', file);
if (text) formData.append('text', text);
formData.append('language', language);
const response = await fetch('/summarize', {
method: 'POST',
body: formData
});
if (!response.ok) {
const error = await response.json();
throw new Error(error.detail || "Failed to summarize content");
}
const result = await response.json();
summarizeResult.textContent = result.summary;
} catch (error) {
showError(summarizeError, error.message);
console.error("Summarization error:", error);
} finally {
setLoading(summarizeBtn, false);
}
});
// Question Answering Section Setup
const qaDropArea = document.getElementById('qa-upload-area');
const qaFileInput = document.getElementById('qa-file-input');
const qaPreview = document.getElementById('qa-file-preview');
const qaQuestionInput = document.getElementById('qa-question');
const qaSubmitBtn = document.getElementById('qa-submit-btn');
const qaResult = document.getElementById('qa-answer');
const qaError = document.getElementById('qa-error-message');
const qaLanguageSelect = document.getElementById('qa-language');
// Store default button text
qaSubmitBtn.querySelector('span').dataset.defaultText = 'Get Answer';
// Setup event listeners
if (qaDropArea && qaFileInput) {
['dragenter', 'dragover', 'dragleave', 'drop'].forEach(eventName => {
qaDropArea.addEventListener(eventName, preventDefaults, false);
});
['dragenter', 'dragover'].forEach(eventName => {
qaDropArea.addEventListener(eventName, () => highlight(qaDropArea), false);
});
['dragleave', 'drop'].forEach(eventName => {
qaDropArea.addEventListener(eventName, () => unhighlight(qaDropArea), false);
});
qaDropArea.addEventListener('drop', handleQADrop, false);
// Question Answering Section
qaDropArea.addEventListener('click', (e) => {
if (e.target === qaDropArea) {
qaFileInput.click();
}
});
qaFileInput.addEventListener('change', handleQAFileSelect);
}
if (qaQuestionInput && qaSubmitBtn) {
qaQuestionInput.addEventListener('input', validateQAInputs);
qaSubmitBtn.addEventListener('click', handleQASubmit);
}
function handleQADrop(e) {
const dt = e.dataTransfer;
const files = dt.files;
handleQAFiles(files);
}
function handleQAFileSelect(e) {
const files = e.target.files;
if (files.length) {
handleQAFiles(files);
}
}
function handleQAFiles(files) {
const file = files[0];
const validTypes = [
'application/pdf',
'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
'text/plain'
];
const validExtensions = ['.pdf', '.docx', '.txt'];
if (!isValidFileType(file, validTypes, validExtensions)) {
showError(qaError, "Unsupported file type. Please upload PDF, DOCX, or TXT files.");
return;
}
qaPreview.innerHTML = `
<div class="file-info">
<span class="file-icon">${getFileIcon(file)}</span>
<div>
<strong>${file.name}</strong>
<span>${formatFileSize(file.size)}</span>
</div>
</div>
`;
validateQAInputs();
}
function validateQAInputs() {
const hasFile = qaFileInput.files.length > 0;
const hasQuestion = qaQuestionInput.value.trim() !== '';
qaSubmitBtn.disabled = !(hasFile && hasQuestion);
}
async function handleQASubmit() {
const file = qaFileInput.files[0];
const question = qaQuestionInput.value.trim();
const language = qaLanguageSelect.value;
if (!file) {
showError(qaError, "Please upload a document first");
return;
}
if (!question) {
showError(qaError, "Please enter your question");
return;
}
try {
setLoading(qaSubmitBtn, true);
hideError(qaError);
const formData = new FormData();
formData.append('file', file);
formData.append('question', question);
formData.append('language', language);
const response = await fetch('/qa', {
method: 'POST',
body: formData
});
if (!response.ok) {
const error = await response.json();
throw new Error(error.detail || "Failed to get answer");
}
const result = await response.json();
displayQAResult(result);
} catch (error) {
showError(qaError, error.message);
console.error("QA Error:", error);
} finally {
setLoading(qaSubmitBtn, false);
}
}
function displayQAResult(result) {
qaResult.textContent = result.answer;
}
// Data Visualization Section Setup
const dataDropArea = document.getElementById('data-upload-area');
const dataFileInput = document.getElementById('data-file-input');
const dataPreview = document.getElementById('data-file-preview');
const visualizeBtn = document.getElementById('visualize-btn');
const visualizationPrompt = document.getElementById('visualization-prompt');
const chartTypeSelect = document.getElementById('chart-type-select');
const visualizationResult = document.getElementById('visualization-results');
const visualizationError = document.getElementById('visualization-error');
const visualizationImage = document.getElementById('generated-visualization');
const pythonCodeOutput = document.getElementById('python-code-output');
const dataPreviewContainer = document.getElementById('data-preview-container');
const copyCodeBtn = document.getElementById('copy-code-btn');
// Store default button text
visualizeBtn.querySelector('span').dataset.defaultText = 'Generate Visualization';
// Setup drag and drop for data visualization
if (dataDropArea && dataFileInput) {
['dragenter', 'dragover', 'dragleave', 'drop'].forEach(eventName => {
dataDropArea.addEventListener(eventName, preventDefaults, false);
});
['dragenter', 'dragover'].forEach(eventName => {
dataDropArea.addEventListener(eventName, () => highlight(dataDropArea), false);
});
['dragleave', 'drop'].forEach(eventName => {
dataDropArea.addEventListener(eventName, () => unhighlight(dataDropArea), false);
});
dataDropArea.addEventListener('drop', handleDataDrop, false);
// Data Visualization Section
dataDropArea.addEventListener('click', (e) => {
if (e.target === dataDropArea) {
dataFileInput.click();
}
});
dataFileInput.addEventListener('change', handleDataFileSelect);
}
if (visualizeBtn) {
visualizeBtn.addEventListener('click', handleVisualizationSubmit);
}
if (copyCodeBtn) {
copyCodeBtn.addEventListener('click', copyPythonCode);
}
function handleDataDrop(e) {
const dt = e.dataTransfer;
const files = dt.files;
handleDataFiles(files);
}
function handleDataFileSelect(e) {
const files = e.target.files;
if (files.length) {
handleDataFiles(files);
}
}
function handleDataFiles(files) {
const file = files[0];
const validTypes = [
'application/vnd.ms-excel',
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
'text/csv'
];
const validExtensions = ['.xlsx', '.xls', '.csv'];
if (!isValidFileType(file, validTypes, validExtensions)) {
showVisualizationError("Unsupported file type. Please upload Excel or CSV files.");
return;
}
dataPreview.innerHTML = `
<div class="file-info">
<span class="file-icon">${getFileIcon(file)}</span>
<div>
<strong>${file.name}</strong>
<span>${formatFileSize(file.size)}</span>
</div>
</div>
`;
}
async function handleVisualizationSubmit() {
const file = dataFileInput.files[0];
const prompt = visualizationPrompt.value.trim();
const chartType = chartTypeSelect.value;
if (!file) {
showVisualizationError("Please upload a file first");
return;
}
try {
setLoading(visualizeBtn, true, 'Generating...');
hideVisualizationError();
visualizationResult.classList.add('hidden');
const formData = new FormData();
formData.append('file', file);
formData.append('request', prompt);
formData.append('chart_type', chartType);
const response = await fetch('/generate-visualization', {
method: 'POST',
body: formData
});
if (!response.ok) {
const error = await response.json();
throw new Error(error.detail || "Failed to generate visualization");
}
const result = await response.json();
displayVisualizationResult(result);
} catch (error) {
showVisualizationError(error.message);
console.error("Visualization error:", error);
} finally {
setLoading(visualizeBtn, false);
}
}
function displayVisualizationResult(result) {
visualizationImage.src = result.image_url;
pythonCodeOutput.textContent = result.python_code;
// Display data preview
dataPreviewContainer.innerHTML = `
<p>Columns: ${result.columns.join(', ')}</p>
<small>Note: Only showing first few rows of data</small>
`;
visualizationResult.classList.remove('hidden');
}
function showVisualizationError(message) {
visualizationError.textContent = message;
visualizationError.classList.remove('hidden');
}
function hideVisualizationError() {
visualizationError.classList.add('hidden');
}
function copyPythonCode() {
const code = pythonCodeOutput.textContent;
navigator.clipboard.writeText(code)
.then(() => {
copyCodeBtn.textContent = 'Copied!';
setTimeout(() => {
copyCodeBtn.textContent = 'Copy Code';
}, 2000);
})
.catch(err => {
console.error('Failed to copy code:', err);
});
}
}); |