Spaces:
Sleeping
Sleeping
File size: 13,897 Bytes
7a1c9d6 |
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 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 |
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>RAG Chat Application</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
background-color: #f5f5f5;
}
.container {
max-width: 1200px;
margin: 0 auto;
padding: 20px;
}
header {
background-color: #282c34;
color: white;
padding: 20px;
text-align: center;
}
header h1 {
margin: 0;
}
.upload-container {
background-color: white;
border-radius: 8px;
box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
padding: 30px;
margin: 30px 0;
text-align: center;
}
.upload-container.hidden {
display: none;
}
.chat-container {
display: none;
background-color: white;
border-radius: 8px;
box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
height: 70vh;
display: flex;
flex-direction: column;
}
.chat-container.visible {
display: flex;
}
.file-info {
background-color: #f1f1f1;
padding: 10px 20px;
border-bottom: 1px solid #e0e0e0;
text-align: left;
}
.chat-messages {
flex-grow: 1;
overflow-y: auto;
padding: 20px;
}
.welcome-message {
text-align: center;
color: #555;
margin: 30px 0;
}
.message {
max-width: 70%;
margin-bottom: 15px;
padding: 10px 15px;
border-radius: 8px;
font-size: 14px;
line-height: 1.5;
position: relative;
}
.user-message {
background-color: #2196F3;
color: white;
border-bottom-right-radius: 0;
margin-left: auto;
}
.ai-message {
background-color: #f1f1f1;
color: #333;
border-bottom-left-radius: 0;
}
.message.error {
background-color: #ffebee;
color: #d32f2f;
}
.message-sender {
font-weight: bold;
margin-bottom: 5px;
font-size: 12px;
}
.chat-input-form {
display: flex;
padding: 15px;
border-top: 1px solid #e0e0e0;
}
.chat-input {
flex-grow: 1;
padding: 12px 15px;
border: 1px solid #e0e0e0;
border-radius: 4px;
font-size: 14px;
}
.send-button {
background-color: #2196F3;
color: white;
border: none;
border-radius: 4px;
padding: 0 20px;
margin-left: 10px;
cursor: pointer;
font-size: 14px;
}
.send-button:disabled {
background-color: #ccc;
}
.file-input {
display: none;
}
.file-label {
display: inline-block;
padding: 12px 20px;
background-color: #4CAF50;
color: white;
border-radius: 4px;
cursor: pointer;
margin-bottom: 20px;
}
.upload-button {
padding: 12px 30px;
background-color: #2196F3;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
font-size: 16px;
}
.upload-button:disabled {
background-color: #ccc;
}
.error-message {
color: #d32f2f;
margin: 10px 0;
}
.loading {
display: flex;
justify-content: center;
align-items: center;
gap: 8px;
margin-bottom: 15px;
}
.loading .dot {
width: 8px;
height: 8px;
background-color: #2196F3;
border-radius: 50%;
animation: bounce 1.4s infinite ease-in-out both;
}
.loading .dot:nth-child(1) {
animation-delay: -0.32s;
}
.loading .dot:nth-child(2) {
animation-delay: -0.16s;
}
@keyframes bounce {
0%, 80%, 100% {
transform: scale(0);
}
40% {
transform: scale(1);
}
}
</style>
</head>
<body>
<header>
<h1>RAG Chat Application</h1>
</header>
<div class="container">
<div id="upload-container" class="upload-container">
<h2>Upload a Document</h2>
<p>Upload a text or PDF file to start chatting</p>
<div>
<input type="file" id="file-input" class="file-input" accept=".txt,.pdf">
<label for="file-input" class="file-label" id="file-label">Choose a file</label>
</div>
<div id="error-message" class="error-message"></div>
<button id="upload-button" class="upload-button" disabled>Upload</button>
</div>
<div id="chat-container" class="chat-container">
<div class="file-info">
<p>Using file: <strong id="filename-display"></strong></p>
</div>
<div id="chat-messages" class="chat-messages">
<div class="welcome-message">
<h3>Welcome to the RAG Chat!</h3>
<p>Ask questions about the document you uploaded.</p>
</div>
</div>
<form id="chat-form" class="chat-input-form">
<input type="text" id="chat-input" class="chat-input" placeholder="Ask a question about your document...">
<button type="submit" id="send-button" class="send-button">Send</button>
</form>
</div>
</div>
<script>
// Generate a session ID
const sessionId = Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15);
// DOM elements
const uploadContainer = document.getElementById('upload-container');
const chatContainer = document.getElementById('chat-container');
const fileInput = document.getElementById('file-input');
const fileLabel = document.getElementById('file-label');
const uploadButton = document.getElementById('upload-button');
const errorMessage = document.getElementById('error-message');
const filenameDisplay = document.getElementById('filename-display');
const chatMessages = document.getElementById('chat-messages');
const chatForm = document.getElementById('chat-form');
const chatInput = document.getElementById('chat-input');
const sendButton = document.getElementById('send-button');
// Event listeners
fileInput.addEventListener('change', handleFileChange);
uploadButton.addEventListener('click', handleUpload);
chatForm.addEventListener('submit', handleChatSubmit);
// Handle file selection
function handleFileChange(e) {
const file = e.target.files[0];
if (!file) {
fileLabel.textContent = 'Choose a file';
uploadButton.disabled = true;
return;
}
// Check file type
if (file.type !== 'text/plain' && file.type !== 'application/pdf') {
errorMessage.textContent = 'Please select a text or PDF file';
fileLabel.textContent = 'Choose a file';
uploadButton.disabled = true;
return;
}
// Check file size (2MB max)
if (file.size > 2 * 1024 * 1024) {
errorMessage.textContent = 'File size must be less than 2MB';
fileLabel.textContent = 'Choose a file';
uploadButton.disabled = true;
return;
}
fileLabel.textContent = file.name;
errorMessage.textContent = '';
uploadButton.disabled = false;
}
// Handle file upload
async function handleUpload() {
const file = fileInput.files[0];
if (!file) {
errorMessage.textContent = 'Please select a file';
return;
}
// Disable upload button and show loading state
uploadButton.disabled = true;
uploadButton.textContent = 'Uploading...';
errorMessage.textContent = '';
const formData = new FormData();
formData.append('file', file);
formData.append('session_id', sessionId);
try {
const response = await fetch('/upload', {
method: 'POST',
body: formData
});
const data = await response.json();
if (data.status === 'success') {
// Show chat container and hide upload container
uploadContainer.style.display = 'none';
chatContainer.style.display = 'flex';
filenameDisplay.textContent = file.name;
} else {
errorMessage.textContent = data.message || 'Failed to upload file';
}
} catch (error) {
console.error('Error uploading file:', error);
errorMessage.textContent = 'Failed to upload file';
} finally {
uploadButton.textContent = 'Upload';
uploadButton.disabled = false;
}
}
// Handle chat submission
async function handleChatSubmit(e) {
e.preventDefault();
const message = chatInput.value.trim();
if (!message) {
return;
}
// Disable input and button
chatInput.disabled = true;
sendButton.disabled = true;
// Add user message to chat
addMessage(message, 'user');
// Clear input
chatInput.value = '';
// Add loading indicator
const loadingElement = document.createElement('div');
loadingElement.className = 'loading';
loadingElement.innerHTML = `
<div class="dot"></div>
<div class="dot"></div>
<div class="dot"></div>
`;
chatMessages.appendChild(loadingElement);
chatMessages.scrollTop = chatMessages.scrollHeight;
try {
const response = await fetch('/query', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
session_id: sessionId,
query: message
})
});
const data = await response.json();
// Remove loading indicator
chatMessages.removeChild(loadingElement);
// Add AI response to chat
addMessage(data.response, 'ai');
} catch (error) {
console.error('Error getting response:', error);
// Remove loading indicator
chatMessages.removeChild(loadingElement);
// Add error message
addMessage('Sorry, there was an error processing your request.', 'ai', true);
} finally {
chatInput.disabled = false;
sendButton.disabled = false;
chatInput.focus();
}
}
// Add message to chat
function addMessage(text, sender, isError = false) {
// Remove welcome message if it exists
const welcomeMessage = document.querySelector('.welcome-message');
if (welcomeMessage) {
chatMessages.removeChild(welcomeMessage);
}
const messageElement = document.createElement('div');
messageElement.className = `message ${sender}-message${isError ? ' error' : ''}`;
const senderElement = document.createElement('div');
senderElement.className = 'message-sender';
senderElement.textContent = sender === 'user' ? 'You' : 'AI';
const textElement = document.createElement('div');
textElement.className = 'message-text';
textElement.textContent = text;
messageElement.appendChild(senderElement);
messageElement.appendChild(textElement);
chatMessages.appendChild(messageElement);
chatMessages.scrollTop = chatMessages.scrollHeight;
}
</script>
</body>
</html> |