SmartDoc / static /scripts /text-summarization.js
chabane
Update the UI and main
180dc07
raw
history blame
2.35 kB
document.addEventListener("DOMContentLoaded", function () {
const fileInput = document.getElementById("document-upload");
const fileName = document.getElementById("file-name");
const summarizeBtn = document.getElementById("summarize-btn");
const loadingContainer = document.getElementById("loading-container");
const resultsSection = document.getElementById("results-section");
const result_summary = document.getElementById("summary-content");
const newSummaryBtn = document.getElementById("new-summary-btn");
const downloadBtn = document.getElementById("download-btn");
fileInput.addEventListener("change", function () {
if (this.files && this.files[0]) {
fileName.textContent = this.files[0].name;
} else {
fileName.textContent = "No file chosen";
}
});
summarizeBtn.addEventListener("click", async function () {
try {
if (fileInput.files.length === 0) {
showNotification("warning", "Please select a file first");
return;
}
loadingContainer.style.display = "block";
summarizeBtn.disabled = true;
const formData = new FormData();
formData.append("file", fileInput.files[0]);
const response = await fetch("/summerize", {
method: "POST",
body: formData,
});
const result = await response.json();
if (result.error) {
showNotification("error", result.error);
return;
}
result_summary.innerText = result.summary;
resultsSection.style.display = "block";
resultsSection.scrollIntoView({ behavior: "smooth" });
} catch (error) {
showNotification("error", error);
} finally {
loadingContainer.style.display = "none";
summarizeBtn.disabled = false;
}
});
newSummaryBtn.addEventListener("click", function () {
fileInput.value = "";
fileName.textContent = "No file chosen";
resultsSection.style.display = "none";
document.querySelector(".upload").scrollIntoView({ behavior: "smooth" });
});
downloadBtn.addEventListener("click", function () {
const summaryText = document.getElementById("summary-content").innerText;
const blob = new Blob([summaryText], { type: "text/plain" });
const url = URL.createObjectURL(blob);
const a = document.createElement("a");
a.href = url;
a.download = "document-summary.txt";
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(url);
});
});