File size: 2,639 Bytes
642cccf
68001ef
642cccf
3fe73a4
642cccf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
68001ef
 
 
642cccf
 
 
 
 
 
 
68001ef
 
 
642cccf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
68001ef
642cccf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0b09172
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
// script.js
async function summarizeText() {
    clearMessages();
    const text = document.getElementById("inputText").value;
    const btn = document.getElementById("summTextBtn");
    btn.disabled = true;
    btn.innerText = "Summarizing...";
    try {
        const response = await fetch("/summarize/text", {
            method: "POST",
            headers: {"Content-Type": "application/json"},
            body: JSON.stringify({ text })
        });
        if (!response.ok) {
            const err = await response.json();
            showError(err.detail || "Error during summarization.");
            return;
        }
        const data = await response.json();
        document.getElementById("summaryBox").innerText = data.summary;
    } catch (e) {
        showError(e.message || "Network error.");
    } finally {
        btn.disabled = false;
        btn.innerText = "Summarize Text";
    }
}

async function summarizePDF() {
    clearMessages();
    const fileInput = document.getElementById("pdfFile");
    if (!fileInput.files.length) {
        showError("Please select a PDF file first.");
        return;
    }
    const file = fileInput.files[0];
    const formData = new FormData();
    formData.append("file", file);

    const btn = document.getElementById("summPdfBtn");
    btn.disabled = true;
    btn.innerText = "Summarizing...";

    try {
        const response = await fetch("/summarize/pdf", {
            method: "POST",
            body: formData
        });
        if (!response.ok) {
            const err = await response.json();
            showError(err.detail || "Error during PDF summarization.");
            return;
        }
        const data = await response.json();
        document.getElementById("summaryBox").innerText = data.summary;
    } catch (e) {
        showError(e.message || "Network error.");
    } finally {
        btn.disabled = false;
        btn.innerText = "Summarize PDF";
    }
}

function showError(msg) {
    const errBox = document.getElementById("errorBox");
    errBox.hidden = false;
    errBox.innerText = msg;
}

function clearMessages() {
    document.getElementById("summaryBox").innerText = "";
    const errBox = document.getElementById("errorBox");
    errBox.hidden = true;
    errBox.innerText = "";
}

function copySummary() {
    const text = document.getElementById("summaryBox").innerText;
    if (!text) {
        alert("There's no summary to copy.");
        return;
    }
    navigator.clipboard.writeText(text).then(() => {
        alert("Summary copied to clipboard.");
    }).catch(() => {
        alert("Failed to copy summary.");
    });
}