NanobotzAI commited on
Commit
24a5e73
·
verified ·
1 Parent(s): d1b294b

Update index.html

Browse files
Files changed (1) hide show
  1. index.html +80 -41
index.html CHANGED
@@ -4,59 +4,98 @@
4
  <meta charset="UTF-8">
5
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
  <title>PDF-Based Chatbot</title>
 
7
  <style>
8
- body { font-family: Arial, sans-serif; margin: 20px; text-align: center; }
9
- #chat-container { max-width: 600px; margin: auto; }
10
- input, button { padding: 10px; margin: 10px; }
11
- #response { margin-top: 20px; white-space: pre-wrap; }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
  </style>
13
  </head>
14
  <body>
 
 
15
 
16
- <h2>Chat with Your PDF</h2>
 
17
 
18
- <!-- PDF Upload -->
19
- <input type="file" id="pdfInput">
20
- <button onclick="uploadPDF()">Upload PDF</button>
21
 
22
- <hr>
 
23
 
24
- <!-- Chat Section -->
25
- <div id="chat-container">
26
- <input type="text" id="userMessage" placeholder="Ask a question">
27
- <button onclick="sendMessage()">Send</button>
28
- <p id="response"></p>
29
  </div>
30
 
31
  <script>
32
- function uploadPDF() {
33
- let fileInput = document.getElementById('pdfInput');
34
- if (!fileInput.files.length) {
35
- alert("Please select a PDF file.");
36
- return;
37
- }
38
-
39
- let filePath = fileInput.files[0].name;
40
-
41
- fetch(`/upload_pdf/?file_path=${filePath}`, { method: "POST" })
42
- .then(response => response.json())
43
- .then(data => alert(data.message))
44
- .catch(error => console.error("Error uploading PDF:", error));
45
- }
46
 
47
- function sendMessage() {
48
- let userMessage = document.getElementById('userMessage').value;
49
- if (!userMessage.trim()) {
50
- alert("Please enter a message.");
51
- return;
52
- }
53
-
54
- fetch(`/chat/?msg=${encodeURIComponent(userMessage)}`)
55
- .then(response => response.json())
56
- .then(data => document.getElementById('response').innerText = "AI: " + data.response)
57
- .catch(error => console.error("Error fetching response:", error));
58
- }
59
- </script>
 
60
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
61
  </body>
62
  </html>
 
4
  <meta charset="UTF-8">
5
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
  <title>PDF-Based Chatbot</title>
7
+ <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
8
  <style>
9
+ body {
10
+ font-family: Arial, sans-serif;
11
+ }
12
+ .container {
13
+ max-width: 600px;
14
+ margin: 0 auto;
15
+ padding: 20px;
16
+ }
17
+ #chat {
18
+ max-height: 400px;
19
+ overflow-y: auto;
20
+ border: 1px solid #ddd;
21
+ padding: 10px;
22
+ margin-bottom: 20px;
23
+ }
24
+ input[type="file"] {
25
+ margin-bottom: 20px;
26
+ }
27
  </style>
28
  </head>
29
  <body>
30
+ <div class="container">
31
+ <h1>Chat with Your PDF</h1>
32
 
33
+ <input type="file" id="pdfUpload" accept=".pdf" />
34
+ <button id="uploadButton">Upload PDF</button>
35
 
36
+ <div id="chat"></div>
 
 
37
 
38
+ <textarea id="userInput" placeholder="Ask a question" rows="3"></textarea>
39
+ <button id="sendButton">Send</button>
40
 
41
+ <div id="response"></div>
 
 
 
 
42
  </div>
43
 
44
  <script>
45
+ $(document).ready(function() {
46
+ // Handle PDF upload
47
+ $('#uploadButton').click(function() {
48
+ var formData = new FormData();
49
+ formData.append('pdf', $('#pdfUpload')[0].files[0]);
 
 
 
 
 
 
 
 
 
50
 
51
+ $.ajax({
52
+ url: '/upload_pdf',
53
+ type: 'POST',
54
+ data: formData,
55
+ contentType: false,
56
+ processData: false,
57
+ success: function(response) {
58
+ alert(response.message);
59
+ },
60
+ error: function(err) {
61
+ alert('Error uploading PDF');
62
+ }
63
+ });
64
+ });
65
 
66
+ // Handle sending user message
67
+ $('#sendButton').click(function() {
68
+ var message = $('#userInput').val();
69
+ if (message.trim() === "") return;
70
+
71
+ var history = $('#chat').data('history') || [];
72
+ history.push([message, '']);
73
+
74
+ // Display user message in chat
75
+ $('#chat').append(`<div><b>User:</b> ${message}</div>`);
76
+
77
+ $.ajax({
78
+ url: '/ask_question',
79
+ type: 'POST',
80
+ contentType: 'application/json',
81
+ data: JSON.stringify({ message: message, history: history }),
82
+ success: function(response) {
83
+ // Display assistant's response in chat
84
+ $('#chat').append(`<div><b>Assistant:</b> ${response.response}</div>`);
85
+ $('#chat').scrollTop($('#chat')[0].scrollHeight);
86
+
87
+ // Update history
88
+ history.push([message, response.response]);
89
+ $('#chat').data('history', history);
90
+ },
91
+ error: function(err) {
92
+ alert('Error getting response');
93
+ }
94
+ });
95
+
96
+ $('#userInput').val('');
97
+ });
98
+ });
99
+ </script>
100
  </body>
101
  </html>