File size: 2,012 Bytes
6e314c7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>PDF-Based Chatbot</title>
    <style>
        body { font-family: Arial, sans-serif; margin: 20px; text-align: center; }
        #chat-container { max-width: 600px; margin: auto; }
        input, button { padding: 10px; margin: 10px; }
        #response { margin-top: 20px; white-space: pre-wrap; }
    </style>
</head>
<body>

    <h2>Chat with Your PDF</h2>

    <!-- PDF Upload -->
    <input type="file" id="pdfInput">
    <button onclick="uploadPDF()">Upload PDF</button>

    <hr>

    <!-- Chat Section -->
    <div id="chat-container">
        <input type="text" id="userMessage" placeholder="Ask a question">
        <button onclick="sendMessage()">Send</button>
        <p id="response"></p>
    </div>

    <script>
        function uploadPDF() {
            let fileInput = document.getElementById('pdfInput');
            if (!fileInput.files.length) {
                alert("Please select a PDF file.");
                return;
            }

            let filePath = fileInput.files[0].name;

            fetch(`/upload_pdf/?file_path=${filePath}`, { method: "POST" })
                .then(response => response.json())
                .then(data => alert(data.message))
                .catch(error => console.error("Error uploading PDF:", error));
        }

        function sendMessage() {
            let userMessage = document.getElementById('userMessage').value;
            if (!userMessage.trim()) {
                alert("Please enter a message.");
                return;
            }

            fetch(`/chat/?msg=${encodeURIComponent(userMessage)}`)
                .then(response => response.json())
                .then(data => document.getElementById('response').innerText = "AI: " + data.response)
                .catch(error => console.error("Error fetching response:", error));
        }
    </script>

</body>
</html>