Spaces:
Sleeping
Sleeping
File size: 17,285 Bytes
49adc11 91e1079 49adc11 91e1079 49adc11 91e1079 49adc11 91e1079 49adc11 91e1079 49adc11 91e1079 49adc11 | 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 | <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Google Docs Knowledge Chatbot</title>
<link rel="stylesheet" href="/static/styles.css">
</head>
<body>
<div class="container">
<div class="header">
<h1>π Google Docs Knowledge Bot</h1>
<p class="subtitle">Ask questions about your Google Drive folder</p>
</div>
<div class="index-section">
<h3>π Folder-Based RAG System</h3>
<div class="info-box">
<p><strong>How it works:</strong></p>
<ol>
<li>Share a Google Drive folder with your service account</li>
<li>Add Google Docs to that folder</li>
<li>Click "Index All Documents" below</li>
<li>Ask questions - the bot searches across ALL your docs!</li>
</ol>
</div>
<div class="button-group">
<button onclick="indexAllDocuments()" id="index-all-btn" class="btn btn-primary">
π₯ Index All Documents
</button>
<button onclick="listDocuments()" id="list-btn" class="btn btn-secondary">
π View Documents
</button>
<button onclick="reindexAll()" id="reindex-btn" class="btn btn-tertiary">
π Re-Index
</button>
</div>
<div id="index-status" class="status-message"></div>
<div id="documents-list" class="documents-container"></div>
</div>
<div class="chat-section">
<h3>π¬ Ask Questions</h3>
<div class="chat-container">
<div class="messages" id="messages">
<div class="message bot-message">
<div class="message-content">
π Hello! I can answer questions about all documents in your Google Drive folder.<br><br>
Click "Index All Documents" to get started!
</div>
</div>
</div>
</div>
<div class="input-area">
<input
type="text"
id="question"
placeholder="Ask a question about your documents..."
class="question-input"
onkeypress="handleKeyPress(event)"
/>
<button onclick="sendMessage()" id="send-btn" class="btn btn-secondary">
Send
</button>
</div>
</div>
</div>
<script>
let isIndexed = false;
let conversationHistory = []; // Store last 5 exchanges
async function listDocuments() {
const listBtn = document.getElementById('list-btn');
const docsList = document.getElementById('documents-list');
listBtn.disabled = true;
listBtn.textContent = 'Loading...';
docsList.innerHTML = '<div class="loading">Fetching documents...</div>';
try {
const response = await fetch('/documents');
const docs = await response.json();
if (response.ok) {
if (docs.length === 0) {
docsList.innerHTML = '<div class="info">No documents found in the configured folder.</div>';
} else {
let html = '<div class="docs-header">π Documents in Folder:</div><ul class="docs-list">';
docs.forEach(doc => {
const status = doc.indexed ? 'β
' : 'β³';
html += `<li>${status} <strong>${doc.name}</strong><br><small>Modified: ${new Date(doc.modified).toLocaleString()}</small></li>`;
});
html += '</ul>';
docsList.innerHTML = html;
}
} else {
docsList.innerHTML = `<div class="error">Error: ${docs.detail}</div>`;
}
} catch (error) {
docsList.innerHTML = `<div class="error">Error: ${error.message}</div>`;
} finally {
listBtn.disabled = false;
listBtn.textContent = 'π View Documents';
}
}
async function indexAllDocuments() {
const statusDiv = document.getElementById('index-status');
const indexBtn = document.getElementById('index-all-btn');
indexBtn.disabled = true;
indexBtn.textContent = 'β³ Indexing...';
statusDiv.innerHTML = '<span class="info">π Indexing all documents in your folder... This may take a minute.</span>';
try {
const response = await fetch('/index-all', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
}
});
const data = await response.json();
if (response.ok) {
isIndexed = true;
let statusHtml = `<span class="success">β
${data.message}<br>
π Documents processed: ${data.documents_processed}`;
if (data.total_documents) {
statusHtml += ` / ${data.total_documents}`;
}
statusHtml += `<br>π¦ Total chunks indexed: ${data.chunks_indexed}</span>`;
// Show warnings if any documents failed
if (data.warnings && data.warnings.failed_documents) {
statusHtml += '<div class="warning-box"><strong>β οΈ Warnings:</strong><ul>';
data.warnings.failed_documents.forEach(doc => {
statusHtml += `<li><strong>${doc.name}:</strong> ${doc.error}</li>`;
});
statusHtml += '</ul></div>';
}
statusDiv.innerHTML = statusHtml;
// Enable chat
document.getElementById('question').disabled = false;
document.getElementById('send-btn').disabled = false;
// Clear conversation history on new index
conversationHistory = [];
// Refresh document list
await listDocuments();
} else {
// Handle detailed error responses
let errorHtml = '<span class="error">';
if (data.detail && typeof data.detail === 'object') {
errorHtml += `β <strong>${data.detail.error || 'Error'}</strong><br>`;
errorHtml += `${data.detail.message}<br>`;
if (data.detail.steps) {
errorHtml += '<br><strong>Steps to fix:</strong><ul>';
data.detail.steps.forEach(step => {
errorHtml += `<li>${step}</li>`;
});
errorHtml += '</ul>';
}
if (data.detail.failed_documents) {
errorHtml += '<br><strong>Failed documents:</strong><ul>';
data.detail.failed_documents.forEach(doc => {
errorHtml += `<li>${doc.name}: ${doc.error}</li>`;
});
errorHtml += '</ul>';
}
} else {
errorHtml += `β Error: ${data.detail || 'Unknown error'}`;
}
errorHtml += '</span>';
statusDiv.innerHTML = errorHtml;
}
} catch (error) {
statusDiv.innerHTML = `<span class="error">β Network Error: ${error.message}<br>Please check your connection and try again.</span>`;
} finally {
indexBtn.disabled = false;
indexBtn.textContent = 'π₯ Index All Documents';
}
}
async function reindexAll() {
if (!confirm('This will re-index all documents. Continue?')) {
return;
}
const statusDiv = document.getElementById('index-status');
const reindexBtn = document.getElementById('reindex-btn');
reindexBtn.disabled = true;
reindexBtn.textContent = 'β³ Re-indexing...';
statusDiv.innerHTML = '<span class="info">π Re-indexing all documents...</span>';
try {
const response = await fetch('/reindex', {
method: 'POST'
});
const data = await response.json();
if (response.ok) {
isIndexed = true;
statusDiv.innerHTML = `<span class="success">β
Re-indexing complete!<br>
π Documents: ${data.documents_processed}<br>
π¦ Chunks: ${data.chunks_indexed}</span>`;
// Clear conversation history on re-index
conversationHistory = [];
await listDocuments();
} else {
statusDiv.innerHTML = `<span class="error">β Error: ${data.detail}</span>`;
}
} catch (error) {
statusDiv.innerHTML = `<span class="error">β Error: ${error.message}</span>`;
} finally {
reindexBtn.disabled = false;
reindexBtn.textContent = 'π Re-Index';
}
}
async function sendMessage() {
const question = document.getElementById('question').value.trim();
const sendBtn = document.getElementById('send-btn');
if (!question) {
return;
}
if (!isIndexed) {
addMessage('Please index documents first by clicking "Index All Documents"!', 'bot');
return;
}
// Add user message
addMessage(question, 'user');
document.getElementById('question').value = '';
// Disable send button
sendBtn.disabled = true;
sendBtn.textContent = 'Thinking...';
try {
const response = await fetch('/chat', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
question: question,
conversation_history: conversationHistory
})
});
const data = await response.json();
if (response.ok) {
// Check if it's a clarification question
if (data.is_clarification) {
addMessage(data.answer, 'bot', [], true);
// Don't add clarification to history
} else {
// Show rephrased query if available
let answerText = data.answer;
if (data.rephrased_query && data.rephrased_query !== question) {
answerText = `<em>Understanding your question as: "${data.rephrased_query}"</em><br><br>${answerText}`;
}
addMessage(answerText, 'bot', data.sources);
// Update conversation history (keep last 5 exchanges)
conversationHistory.push({
role: 'user',
content: question
});
conversationHistory.push({
role: 'assistant',
content: data.answer
});
// Keep only last 10 messages (5 exchanges)
if (conversationHistory.length > 10) {
conversationHistory = conversationHistory.slice(-10);
}
}
} else {
// Handle detailed error responses
let errorMsg = 'Error: ';
if (data.detail && typeof data.detail === 'object') {
errorMsg += `<strong>${data.detail.error || 'Unknown Error'}</strong><br>${data.detail.message}`;
if (data.detail.steps) {
errorMsg += '<br><br><strong>Try this:</strong><ul>';
data.detail.steps.forEach(step => {
errorMsg += `<li>${step}</li>`;
});
errorMsg += '</ul>';
}
// Special handling for rate limits
if (data.detail.retry_after) {
errorMsg += `<br><em>Please retry after: ${data.detail.retry_after}</em>`;
}
} else {
errorMsg += data.detail || 'Unknown error occurred';
}
addMessage(errorMsg, 'bot');
}
} catch (error) {
addMessage(`Network Error: ${error.message}<br>Please check your connection and try again.`, 'bot');
} finally {
sendBtn.disabled = false;
sendBtn.textContent = 'Send';
}
}
function addMessage(text, type, sources = [], isClarification = false) {
const messagesDiv = document.getElementById('messages');
const messageDiv = document.createElement('div');
messageDiv.className = `message ${type}-message`;
if (isClarification) {
messageDiv.classList.add('clarification-message');
}
let content = `<div class="message-content">${text}</div>`;
if (sources && sources.length > 0) {
content += '<div class="sources"><strong>π Found in these documents:</strong><ul>';
sources.forEach(source => {
content += `<li>${source}</li>`;
});
content += '</ul></div>';
}
// Add copy button for bot messages
if (type === 'bot') {
content += `<button class="copy-btn" onclick="copyMessage(this)" title="Copy message">
π Copy
</button>`;
}
messageDiv.innerHTML = content;
messagesDiv.appendChild(messageDiv);
messagesDiv.scrollTop = messagesDiv.scrollHeight;
}
function copyMessage(button) {
// Get the message content (excluding sources and copy button)
const messageDiv = button.closest('.message');
const messageContent = messageDiv.querySelector('.message-content');
const textToCopy = messageContent.innerText || messageContent.textContent;
// Copy to clipboard
navigator.clipboard.writeText(textToCopy).then(() => {
// Visual feedback
const originalText = button.textContent;
button.textContent = 'β
Copied!';
button.style.background = '#28a745';
setTimeout(() => {
button.textContent = originalText;
button.style.background = '';
}, 2000);
}).catch(err => {
console.error('Failed to copy:', err);
button.textContent = 'β Failed';
setTimeout(() => {
button.textContent = 'π Copy';
}, 2000);
});
}
function handleKeyPress(event) {
if (event.key === 'Enter') {
sendMessage();
}
}
// Disable chat input initially
document.getElementById('question').disabled = true;
document.getElementById('send-btn').disabled = true;
// Auto-load documents on page load
window.addEventListener('load', () => {
listDocuments();
});
</script>
</body>
</html> |