File size: 2,349 Bytes
180dc07
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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);
	});
});