Spaces:
Running
Running
File size: 7,901 Bytes
7ab594e 4a46b4a 7ab594e 4a46b4a 7ab594e 4a46b4a 7ab594e 4a46b4a 7ab594e 87e622a 2983032 4a383f5 7ab594e 6c4629b c6714eb 7ab594e 4a46b4a d98a217 4a46b4a 7ab594e 4a46b4a d98a217 6c4629b 7ab594e 87e622a 7ab594e 4a383f5 4a46b4a 4a383f5 4a46b4a 4a383f5 4a46b4a 4a383f5 4a46b4a 4a383f5 4a46b4a 4a383f5 7ab594e 5673d7a 7ab594e 4a46b4a d98a217 4a46b4a 4a383f5 d98a217 7ab594e 87e622a 7ab594e d98a217 7ab594e 4a46b4a 7ab594e d98a217 7ab594e 6c4629b c6714eb 7ab594e 4a46b4a 87e622a 1f9fb90 87e622a 1f9fb90 87e622a 4a46b4a 2983032 4a46b4a 2983032 0028406 2983032 0028406 2983032 4a46b4a 7ab594e 4a46b4a 7ab594e 4a46b4a 87e622a 2983032 |
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 |
// Form Divs
const sumText = document.getElementById('sum-text-div');
const sumFile = document.getElementById('sum-file-div')
const sumVideo = document.getElementById('sum-video-div');
// Form Data
const selectOption = document.getElementById('sum-type');
const sumTextInput = document.getElementById('sum-text-input');
const sumFileInput = document.getElementById('sum-file-input');
const sumVideoInput = document.getElementById('sum-video-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 = 100000;
let cache = {
'text': undefined,
'result': undefined,
}
function _summarize() {
var xhr = new XMLHttpRequest();
xhr.open('POST', '/predict_summarization', true);
xhr.setRequestHeader('Content-Type', 'application/json');
var data = JSON.stringify({ 'sum_type': selectOption.value, '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();
switch (selectOption.value) {
case 'sum-text':
len = sumTextInput.value.trim().length
if (len < 250) {
sumError.innerText = `The text size should be at least 250 characters (${len} < 250)`;
sumError.classList.remove('hidden');
return;
}
break;
case 'sum-video':
regex = /^((((http)s?:\/\/)?((www\.)|(m\.))?youtube.com\/watch\?([^\?]*&)?v=.+)|(((http)s?:\/\/)?youtu.be\/([^\?=]+)(\?[^?]+)?))$/
if (!sumVideoInput.value.match(regex)) {
sumError.innerText = 'Invalid youtube link';
sumError.classList.remove('hidden');
return;
}
break;
}
sumError.classList.add('hidden');
_show_summary();
// Here we can finally summarize data
summaryText.value = 'Please wait...';
switch (selectOption.value) {
case 'sum-text':
extractText.value = sumTextInput.value.trim().slice(0, MAX_SIZE);
break;
case 'sum-video':
extractText.value = sumVideoInput.value.slice(0, MAX_SIZE);
break;
}
if (extractText.value === cache.text) {
console.log('Result already in cache!');
summaryText.value = cache.result;
return;
}
_summarize();
}
function _update_option() {
switch (selectOption.value) {
case 'sum-text':
sumText.classList.remove('hidden');
sumVideo.classList.add('hidden');
sumTextInput.setAttribute('required', '');
sumVideoInput.removeAttribute('required');
break;
case 'sum-video':
sumText.classList.add('hidden');
sumVideo.classList.remove('hidden');
sumTextInput.removeAttribute('required');
sumVideoInput.setAttribute('required', '');
break;
}
sumError.classList.add('hidden');
}
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 () {
selectOption.addEventListener('change', _update_option);
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;
}
// Back to main option
selectOption.options[0].selected = true;
_update_option();
_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 = `London is the capital of Great Britain`; |