Spaces:
Sleeping
Sleeping
Update index.html
Browse files- index.html +467 -48
index.html
CHANGED
@@ -3,50 +3,381 @@
|
|
3 |
<head>
|
4 |
<meta charset="UTF-8">
|
5 |
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
6 |
-
<title>
|
7 |
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
|
|
|
8 |
<style>
|
9 |
-
|
10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
}
|
12 |
-
|
13 |
-
|
14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
padding: 20px;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
#chat {
|
18 |
-
|
19 |
overflow-y: auto;
|
20 |
-
|
21 |
-
|
22 |
-
|
|
|
23 |
}
|
24 |
-
|
25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
</style>
|
28 |
</head>
|
29 |
<body>
|
30 |
-
<div class="container">
|
31 |
-
<
|
|
|
|
|
32 |
|
33 |
-
<
|
34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
35 |
|
36 |
-
<div id="chat"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
|
38 |
-
|
39 |
-
<
|
|
|
|
|
|
|
|
|
|
|
40 |
|
41 |
-
<div
|
|
|
|
|
|
|
|
|
|
|
42 |
</div>
|
43 |
|
44 |
<script>
|
45 |
$(document).ready(function() {
|
46 |
-
//
|
47 |
-
|
48 |
-
|
49 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
50 |
|
51 |
$.ajax({
|
52 |
url: '/upload_pdf',
|
@@ -55,47 +386,135 @@
|
|
55 |
contentType: false,
|
56 |
processData: false,
|
57 |
success: function(response) {
|
58 |
-
|
|
|
|
|
|
|
|
|
59 |
},
|
60 |
-
error: function(
|
61 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
-
|
72 |
-
|
|
|
|
|
73 |
|
74 |
-
|
75 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
76 |
|
77 |
$.ajax({
|
78 |
url: '/ask_question',
|
79 |
type: 'POST',
|
80 |
contentType: 'application/json',
|
81 |
-
data: JSON.stringify({
|
|
|
|
|
|
|
|
|
82 |
success: function(response) {
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
90 |
},
|
91 |
-
|
92 |
-
|
|
|
|
|
|
|
|
|
93 |
}
|
94 |
});
|
|
|
95 |
|
96 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
97 |
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
98 |
});
|
99 |
</script>
|
100 |
</body>
|
101 |
-
</html>
|
|
|
3 |
<head>
|
4 |
<meta charset="UTF-8">
|
5 |
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
6 |
+
<title>Document Assistant</title>
|
7 |
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
|
8 |
+
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css" integrity="sha512-9usAa10IRO0HhonpyAIVpjrylPvoDwiPUiKdWk5t3PyolY1cOd4DSE0Ga+ri4AuTroPR5aQvXU9xC6qOPnzFeg==" crossorigin="anonymous" referrerpolicy="no-referrer" />
|
9 |
<style>
|
10 |
+
:root {
|
11 |
+
--primary-color: #36393f; /* Dark background */
|
12 |
+
--secondary-color: #40444b; /* Slightly lighter dark */
|
13 |
+
--text-color: #dcddde; /* Light gray text */
|
14 |
+
--accent-color: #7289da; /* Blurple */
|
15 |
+
--user-message-bg: #7289da; /* User message bubble */
|
16 |
+
--assistant-message-bg: #4f545c; /* Assistant message bubble */
|
17 |
+
--input-bg: #40444b;
|
18 |
+
--border-color: #2f3136;
|
19 |
+
--success-color: #43b581;
|
20 |
+
--error-color: #f04747;
|
21 |
+
--font-family: 'Inter', 'Helvetica Neue', Helvetica, Arial, sans-serif;
|
22 |
+
--border-radius: 8px;
|
23 |
}
|
24 |
+
|
25 |
+
body {
|
26 |
+
font-family: var(--font-family);
|
27 |
+
background-color: var(--primary-color);
|
28 |
+
color: var(--text-color);
|
29 |
+
margin: 0;
|
30 |
+
display: flex;
|
31 |
+
justify-content: center;
|
32 |
+
align-items: center;
|
33 |
+
min-height: 100vh;
|
34 |
padding: 20px;
|
35 |
+
box-sizing: border-box;
|
36 |
+
}
|
37 |
+
|
38 |
+
.chat-container {
|
39 |
+
background-color: var(--secondary-color);
|
40 |
+
border-radius: var(--border-radius);
|
41 |
+
box-shadow: 0 10px 25px rgba(0, 0, 0, 0.2);
|
42 |
+
width: 100%;
|
43 |
+
max-width: 700px;
|
44 |
+
height: 85vh; /* Limit height */
|
45 |
+
display: flex;
|
46 |
+
flex-direction: column;
|
47 |
+
overflow: hidden; /* Contain children */
|
48 |
+
}
|
49 |
+
|
50 |
+
header {
|
51 |
+
background-color: var(--primary-color);
|
52 |
+
padding: 15px 20px;
|
53 |
+
border-bottom: 1px solid var(--border-color);
|
54 |
+
text-align: center;
|
55 |
+
}
|
56 |
+
|
57 |
+
header h1 {
|
58 |
+
margin: 0;
|
59 |
+
font-size: 1.4em;
|
60 |
+
color: #fff;
|
61 |
+
}
|
62 |
+
|
63 |
+
.upload-section {
|
64 |
+
padding: 15px 20px;
|
65 |
+
border-bottom: 1px solid var(--border-color);
|
66 |
+
display: flex;
|
67 |
+
align-items: center;
|
68 |
+
gap: 10px;
|
69 |
+
background-color: var(--secondary-color); /* Match container bg */
|
70 |
}
|
71 |
+
|
72 |
+
/* Hide default file input */
|
73 |
+
#pdfUpload {
|
74 |
+
display: none;
|
75 |
+
}
|
76 |
+
|
77 |
+
/* Style the label like a button */
|
78 |
+
.upload-label {
|
79 |
+
background-color: var(--accent-color);
|
80 |
+
color: white;
|
81 |
+
padding: 8px 15px;
|
82 |
+
border-radius: 5px;
|
83 |
+
cursor: pointer;
|
84 |
+
transition: background-color 0.2s ease;
|
85 |
+
font-size: 0.9em;
|
86 |
+
white-space: nowrap;
|
87 |
+
}
|
88 |
+
|
89 |
+
.upload-label:hover {
|
90 |
+
background-color: #677bc4; /* Slightly darker accent */
|
91 |
+
}
|
92 |
+
|
93 |
+
#uploadStatus {
|
94 |
+
font-size: 0.85em;
|
95 |
+
color: var(--text-color);
|
96 |
+
flex-grow: 1; /* Take remaining space */
|
97 |
+
overflow: hidden;
|
98 |
+
text-overflow: ellipsis;
|
99 |
+
white-space: nowrap;
|
100 |
+
}
|
101 |
+
|
102 |
#chat {
|
103 |
+
flex-grow: 1; /* Take available space */
|
104 |
overflow-y: auto;
|
105 |
+
padding: 20px;
|
106 |
+
display: flex;
|
107 |
+
flex-direction: column;
|
108 |
+
gap: 15px; /* Space between messages */
|
109 |
}
|
110 |
+
|
111 |
+
/* Scrollbar styling (optional, WebKit browsers) */
|
112 |
+
#chat::-webkit-scrollbar {
|
113 |
+
width: 8px;
|
114 |
+
}
|
115 |
+
#chat::-webkit-scrollbar-track {
|
116 |
+
background: var(--secondary-color);
|
117 |
+
}
|
118 |
+
#chat::-webkit-scrollbar-thumb {
|
119 |
+
background-color: var(--input-bg);
|
120 |
+
border-radius: 4px;
|
121 |
+
}
|
122 |
+
#chat::-webkit-scrollbar-thumb:hover {
|
123 |
+
background-color: #5c616a;
|
124 |
+
}
|
125 |
+
|
126 |
+
|
127 |
+
.message {
|
128 |
+
display: flex;
|
129 |
+
max-width: 75%;
|
130 |
+
opacity: 0; /* Start hidden for animation */
|
131 |
+
animation: fadeIn 0.3s ease forwards;
|
132 |
+
}
|
133 |
+
|
134 |
+
@keyframes fadeIn {
|
135 |
+
to { opacity: 1; }
|
136 |
+
}
|
137 |
+
|
138 |
+
.message-content {
|
139 |
+
padding: 10px 15px;
|
140 |
+
border-radius: var(--border-radius);
|
141 |
+
line-height: 1.4;
|
142 |
+
word-wrap: break-word;
|
143 |
+
font-size: 0.95em;
|
144 |
+
}
|
145 |
+
|
146 |
+
.message.user {
|
147 |
+
margin-left: auto; /* Align right */
|
148 |
+
flex-direction: row-reverse; /* Keep content left within flex */
|
149 |
+
}
|
150 |
+
.message.user .message-content {
|
151 |
+
background-color: var(--user-message-bg);
|
152 |
+
color: white;
|
153 |
+
border-bottom-right-radius: 2px; /* Subtle shape difference */
|
154 |
+
}
|
155 |
+
|
156 |
+
.message.assistant {
|
157 |
+
margin-right: auto; /* Align left */
|
158 |
+
}
|
159 |
+
.message.assistant .message-content {
|
160 |
+
background-color: var(--assistant-message-bg);
|
161 |
+
color: var(--text-color);
|
162 |
+
border-bottom-left-radius: 2px; /* Subtle shape difference */
|
163 |
+
}
|
164 |
+
|
165 |
+
.message.system { /* For status messages like errors */
|
166 |
+
font-style: italic;
|
167 |
+
color: var(--error-color);
|
168 |
+
text-align: center;
|
169 |
+
width: 100%;
|
170 |
+
max-width: 100%;
|
171 |
+
font-size: 0.9em;
|
172 |
+
margin: 5px 0;
|
173 |
+
}
|
174 |
+
.message.system .message-content {
|
175 |
+
background: none;
|
176 |
+
padding: 0;
|
177 |
+
}
|
178 |
+
|
179 |
+
|
180 |
+
.input-area {
|
181 |
+
display: flex;
|
182 |
+
padding: 15px 20px;
|
183 |
+
border-top: 1px solid var(--border-color);
|
184 |
+
background-color: var(--primary-color);
|
185 |
}
|
186 |
+
|
187 |
+
#userInput {
|
188 |
+
flex-grow: 1;
|
189 |
+
background-color: var(--input-bg);
|
190 |
+
color: var(--text-color);
|
191 |
+
border: none;
|
192 |
+
border-radius: var(--border-radius);
|
193 |
+
padding: 10px 15px;
|
194 |
+
resize: none; /* Prevent manual resizing */
|
195 |
+
font-family: var(--font-family);
|
196 |
+
font-size: 0.95em;
|
197 |
+
margin-right: 10px;
|
198 |
+
height: 40px; /* Initial height */
|
199 |
+
max-height: 100px; /* Limit growth */
|
200 |
+
box-sizing: border-box;
|
201 |
+
overflow-y: auto; /* Allow scrolling if text exceeds max height */
|
202 |
+
}
|
203 |
+
|
204 |
+
#userInput:focus {
|
205 |
+
outline: none;
|
206 |
+
box-shadow: 0 0 0 2px var(--accent-color);
|
207 |
+
}
|
208 |
+
|
209 |
+
#sendButton {
|
210 |
+
background-color: var(--accent-color);
|
211 |
+
color: white;
|
212 |
+
border: none;
|
213 |
+
border-radius: var(--border-radius);
|
214 |
+
padding: 0 15px;
|
215 |
+
cursor: pointer;
|
216 |
+
font-size: 1.2em; /* Icon size */
|
217 |
+
transition: background-color 0.2s ease, opacity 0.2s ease;
|
218 |
+
display: flex;
|
219 |
+
align-items: center;
|
220 |
+
justify-content: center;
|
221 |
+
height: 40px;
|
222 |
+
width: 50px; /* Fixed width for the button */
|
223 |
+
}
|
224 |
+
|
225 |
+
#sendButton:hover {
|
226 |
+
background-color: #677bc4;
|
227 |
+
}
|
228 |
+
|
229 |
+
#sendButton:disabled {
|
230 |
+
opacity: 0.5;
|
231 |
+
cursor: not-allowed;
|
232 |
+
}
|
233 |
+
|
234 |
+
.typing-indicator {
|
235 |
+
display: flex;
|
236 |
+
align-items: center;
|
237 |
+
padding: 5px 0 0 0; /* Add slight top padding */
|
238 |
+
opacity: 0;
|
239 |
+
transition: opacity 0.3s ease;
|
240 |
+
height: 0; /* Start hidden */
|
241 |
+
overflow: hidden;
|
242 |
+
}
|
243 |
+
.typing-indicator.visible {
|
244 |
+
opacity: 1;
|
245 |
+
height: 20px; /* Make visible */
|
246 |
+
}
|
247 |
+
|
248 |
+
.typing-indicator span {
|
249 |
+
font-size: 0.85em;
|
250 |
+
color: var(--text-color);
|
251 |
+
margin-right: 5px;
|
252 |
+
}
|
253 |
+
.typing-indicator .dot {
|
254 |
+
display: inline-block;
|
255 |
+
width: 6px;
|
256 |
+
height: 6px;
|
257 |
+
background-color: var(--text-color);
|
258 |
+
border-radius: 50%;
|
259 |
+
margin: 0 2px;
|
260 |
+
animation: typing 1s infinite ease-in-out;
|
261 |
+
}
|
262 |
+
.typing-indicator .dot:nth-child(2) { animation-delay: 0.2s; }
|
263 |
+
.typing-indicator .dot:nth-child(3) { animation-delay: 0.4s; }
|
264 |
+
|
265 |
+
@keyframes typing {
|
266 |
+
0%, 100% { transform: translateY(0); }
|
267 |
+
50% { transform: translateY(-4px); }
|
268 |
+
}
|
269 |
+
|
270 |
</style>
|
271 |
</head>
|
272 |
<body>
|
273 |
+
<div class="chat-container">
|
274 |
+
<header>
|
275 |
+
<h1>Document Assistant</h1>
|
276 |
+
</header>
|
277 |
|
278 |
+
<div class="upload-section">
|
279 |
+
<label for="pdfUpload" class="upload-label">
|
280 |
+
<i class="fas fa-file-pdf"></i> Choose PDF
|
281 |
+
</label>
|
282 |
+
<input type="file" id="pdfUpload" accept=".pdf" />
|
283 |
+
<span id="uploadStatus">No PDF uploaded yet.</span>
|
284 |
+
<button id="uploadButton" style="display: none;">Upload</button> <!-- Hidden, triggered by JS -->
|
285 |
+
</div>
|
286 |
|
287 |
+
<div id="chat">
|
288 |
+
<!-- Chat messages will appear here -->
|
289 |
+
<div class="message assistant">
|
290 |
+
<div class="message-content">
|
291 |
+
Please upload a PDF document using the button above to begin.
|
292 |
+
</div>
|
293 |
+
</div>
|
294 |
+
</div>
|
295 |
|
296 |
+
<!-- Typing Indicator -->
|
297 |
+
<div class="typing-indicator" id="typingIndicator">
|
298 |
+
<span>Assistant is typing</span>
|
299 |
+
<div class="dot"></div>
|
300 |
+
<div class="dot"></div>
|
301 |
+
<div class="dot"></div>
|
302 |
+
</div>
|
303 |
|
304 |
+
<div class="input-area">
|
305 |
+
<textarea id="userInput" placeholder="Ask a question about the PDF..." rows="1"></textarea>
|
306 |
+
<button id="sendButton" title="Send Message" disabled> <!-- Start disabled -->
|
307 |
+
<i class="fas fa-paper-plane"></i>
|
308 |
+
</button>
|
309 |
+
</div>
|
310 |
</div>
|
311 |
|
312 |
<script>
|
313 |
$(document).ready(function() {
|
314 |
+
let chatHistory = []; // Use a JS array for history
|
315 |
+
let pdfUploaded = false;
|
316 |
+
|
317 |
+
// Function to add a message to the chat interface
|
318 |
+
function addMessage(sender, text, type = 'normal') {
|
319 |
+
// Sanitize text to prevent basic HTML injection
|
320 |
+
const sanitizedText = $('<div>').text(text).html();
|
321 |
+
let messageClass = sender; // 'user' or 'assistant' or 'system'
|
322 |
+
let content = `<div class="message-content">${sanitizedText.replace(/\n/g, '<br>')}</div>`; // Replace newlines with <br>
|
323 |
+
|
324 |
+
// Use system class for errors or status messages
|
325 |
+
if (type === 'error') {
|
326 |
+
messageClass = 'system';
|
327 |
+
content = `<div class="message-content">${sanitizedText}</div>`; // No extra formatting needed
|
328 |
+
} else if (type === 'info') {
|
329 |
+
messageClass = 'system'; // Or style differently if needed
|
330 |
+
content = `<div class="message-content" style="color: var(--success-color);">${sanitizedText}</div>`;
|
331 |
+
}
|
332 |
+
|
333 |
+
const messageElement = $(`<div class="message ${messageClass}">${content}</div>`);
|
334 |
+
$('#chat').append(messageElement);
|
335 |
+
// Scroll to bottom smoothly
|
336 |
+
$('#chat').animate({ scrollTop: $('#chat')[0].scrollHeight }, 300);
|
337 |
+
}
|
338 |
+
|
339 |
+
// Function to show/hide typing indicator
|
340 |
+
function showTypingIndicator(show) {
|
341 |
+
if (show) {
|
342 |
+
$('#typingIndicator').addClass('visible');
|
343 |
+
} else {
|
344 |
+
$('#typingIndicator').removeClass('visible');
|
345 |
+
}
|
346 |
+
// Adjust scroll after potential layout shift
|
347 |
+
setTimeout(() => {$('#chat').scrollTop($('#chat')[0].scrollHeight);}, 50);
|
348 |
+
}
|
349 |
+
|
350 |
+
// --- Upload Handling ---
|
351 |
+
// Trigger hidden file input when label is clicked
|
352 |
+
$('.upload-label').on('click', function() {
|
353 |
+
$('#pdfUpload').click();
|
354 |
+
});
|
355 |
+
|
356 |
+
// Handle file selection
|
357 |
+
$('#pdfUpload').on('change', function() {
|
358 |
+
const file = this.files[0];
|
359 |
+
if (file) {
|
360 |
+
if (file.type === "application/pdf") {
|
361 |
+
$('#uploadStatus').text(`Selected: ${file.name}`).css('color', 'var(--text-color)');
|
362 |
+
// Automatically trigger the hidden upload button's logic
|
363 |
+
uploadFile(file);
|
364 |
+
} else {
|
365 |
+
$('#uploadStatus').text('Error: Please select a PDF file.').css('color', 'var(--error-color)');
|
366 |
+
this.value = ''; // Reset file input
|
367 |
+
}
|
368 |
+
} else {
|
369 |
+
// Optional: Handle case where selection is cancelled
|
370 |
+
// $('#uploadStatus').text('No file selected.');
|
371 |
+
}
|
372 |
+
});
|
373 |
+
|
374 |
+
// Actual upload logic (separated function)
|
375 |
+
function uploadFile(file) {
|
376 |
+
const formData = new FormData();
|
377 |
+
formData.append('pdf', file);
|
378 |
+
|
379 |
+
$('#uploadStatus').text(`Uploading: ${file.name}...`).css('color', 'var(--text-color)');
|
380 |
+
$('.upload-label').prop('disabled', true).css('opacity', 0.6); // Disable button visually
|
381 |
|
382 |
$.ajax({
|
383 |
url: '/upload_pdf',
|
|
|
386 |
contentType: false,
|
387 |
processData: false,
|
388 |
success: function(response) {
|
389 |
+
$('#uploadStatus').html(`<i class="fas fa-check-circle" style="color: var(--success-color);"></i> ${response.message || 'PDF processed successfully!'}`).css('color', 'var(--success-color)');
|
390 |
+
// addMessage('system', response.message || 'PDF processed successfully!', 'info'); // Add message to chat
|
391 |
+
pdfUploaded = true;
|
392 |
+
$('#sendButton').prop('disabled', false); // Enable send button
|
393 |
+
$('#userInput').prop('disabled', false).attr('placeholder', 'Ask a question about the PDF...'); // Enable input
|
394 |
},
|
395 |
+
error: function(jqXHR, textStatus, errorThrown) {
|
396 |
+
let errorMsg = 'Error uploading PDF.';
|
397 |
+
if (jqXHR.responseJSON && jqXHR.responseJSON.error) {
|
398 |
+
errorMsg = jqXHR.responseJSON.error;
|
399 |
+
} else if (jqXHR.responseText) {
|
400 |
+
// Try to get text if JSON parsing fails
|
401 |
+
try {
|
402 |
+
const errObj = JSON.parse(jqXHR.responseText);
|
403 |
+
if (errObj.error) errorMsg = errObj.error;
|
404 |
+
} catch (e) { /* Ignore parsing error */ }
|
405 |
+
}
|
406 |
+
$('#uploadStatus').text(`Error: ${errorMsg}`).css('color', 'var(--error-color)');
|
407 |
+
// addMessage('system', `Upload failed: ${errorMsg}`, 'error');
|
408 |
+
pdfUploaded = false;
|
409 |
+
$('#sendButton').prop('disabled', true);
|
410 |
+
$('#userInput').prop('disabled', true).attr('placeholder', 'Upload a PDF first');
|
411 |
+
},
|
412 |
+
complete: function() {
|
413 |
+
$('.upload-label').prop('disabled', false).css('opacity', 1); // Re-enable button
|
414 |
}
|
415 |
});
|
416 |
+
}
|
417 |
|
|
|
|
|
|
|
|
|
418 |
|
419 |
+
// --- Chat Message Handling ---
|
420 |
+
function sendMessage() {
|
421 |
+
const message = $('#userInput').val().trim();
|
422 |
+
if (message === "" || !pdfUploaded) return; // Don't send empty or if no PDF
|
423 |
|
424 |
+
addMessage('user', message);
|
425 |
+
chatHistory.push(["user", message]); // Add user message to history
|
426 |
+
|
427 |
+
$('#userInput').val('').prop('disabled', true); // Clear and disable input
|
428 |
+
$('#sendButton').prop('disabled', true); // Disable send button
|
429 |
+
showTypingIndicator(true); // Show typing indicator
|
430 |
+
|
431 |
+
// Prepare history for API (send pairs [user, assistant])
|
432 |
+
const historyForApi = [];
|
433 |
+
for(let i = 0; i < chatHistory.length; i += 2) {
|
434 |
+
if (chatHistory[i] && chatHistory[i+1]) {
|
435 |
+
historyForApi.push([ chatHistory[i][1], chatHistory[i+1][1] ]);
|
436 |
+
}
|
437 |
+
}
|
438 |
+
// If the last message was user, don't include it in history pairs yet
|
439 |
+
// The backend structure expects pairs, the current user query is sent separately
|
440 |
|
441 |
$.ajax({
|
442 |
url: '/ask_question',
|
443 |
type: 'POST',
|
444 |
contentType: 'application/json',
|
445 |
+
data: JSON.stringify({
|
446 |
+
message: message,
|
447 |
+
// Send history structure expected by backend (pairs of user/assistant)
|
448 |
+
history: historyForApi
|
449 |
+
}),
|
450 |
success: function(response) {
|
451 |
+
if (response && response.response) {
|
452 |
+
addMessage('assistant', response.response);
|
453 |
+
chatHistory.push(["assistant", response.response]); // Add assistant response
|
454 |
+
} else {
|
455 |
+
addMessage('assistant', "Received an empty response.", 'error');
|
456 |
+
chatHistory.push(["assistant", ""]); // Add empty response pair
|
457 |
+
}
|
458 |
+
},
|
459 |
+
error: function(jqXHR, textStatus, errorThrown) {
|
460 |
+
let errorMsg = 'Error getting response from assistant.';
|
461 |
+
if (jqXHR.responseJSON && jqXHR.responseJSON.error) {
|
462 |
+
errorMsg = jqXHR.responseJSON.error;
|
463 |
+
} else if (jqXHR.responseJSON && jqXHR.responseJSON.response){
|
464 |
+
// Sometimes errors might be in 'response' field
|
465 |
+
errorMsg = jqXHR.responseJSON.response;
|
466 |
+
}
|
467 |
+
addMessage('system', errorMsg, 'error');
|
468 |
+
// Add a placeholder in history to maintain pairing if needed, or adjust logic
|
469 |
+
chatHistory.push(["assistant", ""]); // Indicate error turn
|
470 |
},
|
471 |
+
complete: function() {
|
472 |
+
$('#userInput').prop('disabled', false); // Re-enable input
|
473 |
+
$('#sendButton').prop('disabled', !$('#userInput').val().trim()); // Re-enable send if input has text
|
474 |
+
showTypingIndicator(false); // Hide typing indicator
|
475 |
+
$('#userInput').focus(); // Focus back on input
|
476 |
+
adjustTextareaHeight(); // Adjust height in case of past multiline input
|
477 |
}
|
478 |
});
|
479 |
+
}
|
480 |
|
481 |
+
// Send message on button click
|
482 |
+
$('#sendButton').click(sendMessage);
|
483 |
+
|
484 |
+
// Send message on Enter key press in textarea (Shift+Enter for newline)
|
485 |
+
$('#userInput').keypress(function(e) {
|
486 |
+
if (e.which === 13 && !e.shiftKey) {
|
487 |
+
e.preventDefault(); // Prevent newline
|
488 |
+
sendMessage();
|
489 |
+
}
|
490 |
+
});
|
491 |
+
|
492 |
+
// Enable/disable send button based on input
|
493 |
+
$('#userInput').on('input keyup', function() {
|
494 |
+
adjustTextareaHeight();
|
495 |
+
const isEmpty = $(this).val().trim() === '';
|
496 |
+
$('#sendButton').prop('disabled', isEmpty || !pdfUploaded);
|
497 |
});
|
498 |
+
|
499 |
+
// Adjust textarea height dynamically
|
500 |
+
function adjustTextareaHeight() {
|
501 |
+
const textarea = $('#userInput')[0];
|
502 |
+
textarea.style.height = 'auto'; // Reset height
|
503 |
+
textarea.style.height = (textarea.scrollHeight) + 'px'; // Set to scroll height
|
504 |
+
// Check against max-height defined in CSS
|
505 |
+
const maxHeight = parseInt($(textarea).css('max-height'));
|
506 |
+
if (textarea.scrollHeight > maxHeight) {
|
507 |
+
textarea.style.overflowY = 'auto'; // Ensure scrollbar appears if needed
|
508 |
+
} else {
|
509 |
+
textarea.style.overflowY = 'hidden'; // Hide scrollbar if not needed
|
510 |
+
}
|
511 |
+
}
|
512 |
+
|
513 |
+
// Initial state
|
514 |
+
$('#userInput').prop('disabled', true).attr('placeholder', 'Upload a PDF first');
|
515 |
+
$('#sendButton').prop('disabled', true);
|
516 |
+
adjustTextareaHeight(); // Initial height adjustment
|
517 |
});
|
518 |
</script>
|
519 |
</body>
|
520 |
+
</html>
|