Spaces:
Sleeping
Sleeping
Update index.html
Browse files- 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 {
|
9 |
-
|
10 |
-
|
11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
</style>
|
13 |
</head>
|
14 |
<body>
|
|
|
|
|
15 |
|
16 |
-
|
|
|
17 |
|
18 |
-
|
19 |
-
<input type="file" id="pdfInput">
|
20 |
-
<button onclick="uploadPDF()">Upload PDF</button>
|
21 |
|
22 |
-
|
|
|
23 |
|
24 |
-
|
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
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
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 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
|
|
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>
|