Spaces:
Running
Running
File size: 6,111 Bytes
2983032 6c4629b 2983032 6c4629b 2983032 6c4629b 2983032 1f9fb90 2983032 1f9fb90 2983032 0028406 2983032 0028406 2983032 6c4629b |
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 |
// Form Divs
const sumText = document.getElementById('sum-text-div');
const sumFile = document.getElementById('sum-file-div')
// Form Data
const sumTextInput = document.getElementById('sum-text-input');
const sumFileInput = document.getElementById('sum-file-input');
// Error Output Section
const sumError = document.getElementById('sum-err');
// Result Section
const extractText = document.getElementById('extracted-text');
const summaryText = document.getElementById('summarized-text');
// Word Counter
const wordsCount = document.getElementById('word-counter');
// Tabs
const original = document.getElementById('sum-original');
const summary = document.getElementById('sum-summary');
const showOriginal = document.getElementById('show-original');
const showSummary = document.getElementById('show-summary');
const MAX_SIZE = 20000;
let cache = {
'text': undefined,
'result': undefined,
}
function _check() {
var xhr = new XMLHttpRequest();
xhr.open('POST', '/predict_grammar', true);
xhr.setRequestHeader('Content-Type', 'application/json');
var data = JSON.stringify({ 'sum_type': 'sum-text', 'text': extractText.value });
xhr.onreadystatechange = function () {
if (xhr.readyState === 4 && xhr.status === 200) {
result = xhr.responseText.split('\\n').join('\n');
summaryText.value = result.slice(1, -1);
cache = {
'text': extractText.value,
'result': summaryText.value
};
}
};
xhr.send(data)
return;
}
function _extractFile() {
const file = sumFileInput.files[0];
if (file.type === 'text/plain') {
const reader = new FileReader();
reader.onload = function() {
sumTextInput.value = reader.result.slice(0, MAX_SIZE);
};
reader.readAsText(file, 'CP1251');
return;
} else if (file.type === 'application/pdf') {
sumTextInput.value = '';
const reader = new FileReader();
reader.onload = function (e) {
const pdfData = e.target.result;
pdfjsLib.getDocument(pdfData).promise.then(function (pdfDocument) {
for (let pageNum = 1; pageNum <= pdfDocument.numPages; pageNum++) {
pdfDocument.getPage(pageNum).then(function (pdfPage) {
pdfPage.getTextContent().then(function (textContent) {
let size = sumTextInput.value.length;
let pageText = [];
for (const textItem of textContent.items) {
pageText.push(textItem.str);
size += textItem.str.length;
if (size > MAX_SIZE) break;
}
sumTextInput.value += pageText.join(' ');
});
});
}
});
};
reader.readAsDataURL(file);
}
return;
}
async function summarize(event) {
event.preventDefault();
let value = sumTextInput.value.trim()
if (value === '') {
sumError.innerText = `You need to input some text`;
sumError.classList.remove('hidden');
return;
}
sumError.classList.add('hidden');
_show_summary();
if (value === cache.text) {
console.log('Result already in cache!');
summaryText.value = cache.result;
return;
}
// Here we can finally summarize data
summaryText.value = 'Please wait...';
extractText.value = sumTextInput.value.trim().slice(0, MAX_SIZE);
_check();
}
function _update_counter() {
let text = sumTextInput.value.trim()
if (text === '') {
sumFile.classList.remove('hidden');
wordsCount.classList.add('hidden');
return;
}
sumFile.classList.add('hidden');
wordsCount.classList.remove('hidden');
wordsCount.innerHTML = `Words: ${text.split(/\s+/).length} | Chars: ${text.length}`
}
function _show_summary() {
showOriginal.classList.remove('bg-gray-100');
showSummary.classList.add('bg-gray-100');
summary.classList.remove('hidden');
original.classList.add('hidden');
summaryText.focus({ preventScroll: true });
}
function _show_original() {
showOriginal.classList.add('bg-gray-100');
showSummary.classList.remove('bg-gray-100');
original.classList.remove('hidden');
summary.classList.add('hidden');
sumTextInput.focus({ preventScroll: true });
}
document.addEventListener('DOMContentLoaded', function () {
const submitButton = document.getElementById('submit');
submitButton.addEventListener('click', summarize);
document.addEventListener('keydown', function(event) {
if (event.ctrlKey && event.key === 'Enter') {
_show_original()
summarize(event);
}
});
document.addEventListener('keydown', function(event) {
if (event.key === 'PageUp' || event.key === 'Home') {
event.preventDefault();
_show_original();
}
else if (event.key === 'PageDown' || event.key == 'End') {
event.preventDefault();
_show_summary();
}
});
sumFileInput.addEventListener('change', async function() {
const allowedTypes = ['application/pdf', 'text/plain'];
const file = sumFileInput.files[0];
if (!file) {
sumError.classList.remove('hidden');
return;
}
if (!allowedTypes.includes(file.type)) {
sumError.innerText = 'Not supported type (Only `.pdf` or `.txt`)';
sumError.classList.remove('hidden');
return;
}
_extractFile();
await (new Promise(resolve => setTimeout(resolve, 1000)));
_update_counter();
sumError.classList.add('hidden');
});
sumTextInput.addEventListener('input', _update_counter);
showSummary.addEventListener('click', _show_summary);
showOriginal.addEventListener('click', _show_original);
});
summaryText.placeholder = `Acceptance: 50.00%`; |