student2222333051 commited on
Commit
642cccf
·
verified ·
1 Parent(s): 042899c

Update script.js

Browse files
Files changed (1) hide show
  1. script.js +78 -16
script.js CHANGED
@@ -1,26 +1,88 @@
 
1
  async function summarizeText() {
 
2
  const text = document.getElementById("inputText").value;
3
-
4
- const response = await fetch("/summarize/text", {
5
- method: "POST",
6
- headers: {"Content-Type": "application/json"},
7
- body: JSON.stringify({ text })
8
- });
9
-
10
- const data = await response.json();
11
- document.getElementById("summaryBox").innerText = data.summary;
 
 
 
 
 
 
 
 
 
 
 
 
 
12
  }
13
 
14
  async function summarizePDF() {
15
- const file = document.getElementById("pdfFile").files[0];
 
 
 
 
 
 
16
  const formData = new FormData();
17
  formData.append("file", file);
18
 
19
- const response = await fetch("/summarize/pdf", {
20
- method: "POST",
21
- body: formData
22
- });
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23
 
24
- const data = await response.json();
25
- document.getElementById("summaryBox").innerText = data.summary;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26
  }
 
1
+ // script.js
2
  async function summarizeText() {
3
+ clearMessages();
4
  const text = document.getElementById("inputText").value;
5
+ const btn = document.getElementById("summTextBtn");
6
+ btn.disabled = true;
7
+ btn.innerText = "Summarizing...";
8
+ try {
9
+ const response = await fetch("/summarize/text", {
10
+ method: "POST",
11
+ headers: {"Content-Type": "application/json"},
12
+ body: JSON.stringify({ text })
13
+ });
14
+ if (!response.ok) {
15
+ const err = await response.json();
16
+ showError(err.detail || "Error during summarization.");
17
+ return;
18
+ }
19
+ const data = await response.json();
20
+ document.getElementById("summaryBox").innerText = data.summary;
21
+ } catch (e) {
22
+ showError(e.message || "Network error.");
23
+ } finally {
24
+ btn.disabled = false;
25
+ btn.innerText = "Summarize Text";
26
+ }
27
  }
28
 
29
  async function summarizePDF() {
30
+ clearMessages();
31
+ const fileInput = document.getElementById("pdfFile");
32
+ if (!fileInput.files.length) {
33
+ showError("Please select a PDF file first.");
34
+ return;
35
+ }
36
+ const file = fileInput.files[0];
37
  const formData = new FormData();
38
  formData.append("file", file);
39
 
40
+ const btn = document.getElementById("summPdfBtn");
41
+ btn.disabled = true;
42
+ btn.innerText = "Summarizing...";
43
+
44
+ try {
45
+ const response = await fetch("/summarize/pdf", {
46
+ method: "POST",
47
+ body: formData
48
+ });
49
+ if (!response.ok) {
50
+ const err = await response.json();
51
+ showError(err.detail || "Error during PDF summarization.");
52
+ return;
53
+ }
54
+ const data = await response.json();
55
+ document.getElementById("summaryBox").innerText = data.summary;
56
+ } catch (e) {
57
+ showError(e.message || "Network error.");
58
+ } finally {
59
+ btn.disabled = false;
60
+ btn.innerText = "Summarize PDF";
61
+ }
62
+ }
63
 
64
+ function showError(msg) {
65
+ const errBox = document.getElementById("errorBox");
66
+ errBox.hidden = false;
67
+ errBox.innerText = msg;
68
+ }
69
+
70
+ function clearMessages() {
71
+ document.getElementById("summaryBox").innerText = "";
72
+ const errBox = document.getElementById("errorBox");
73
+ errBox.hidden = true;
74
+ errBox.innerText = "";
75
+ }
76
+
77
+ function copySummary() {
78
+ const text = document.getElementById("summaryBox").innerText;
79
+ if (!text) {
80
+ alert("There's no summary to copy.");
81
+ return;
82
+ }
83
+ navigator.clipboard.writeText(text).then(() => {
84
+ alert("Summary copied to clipboard.");
85
+ }).catch(() => {
86
+ alert("Failed to copy summary.");
87
+ });
88
  }