Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -35,7 +35,7 @@
|
|
| 35 |
</div>
|
| 36 |
<h1 class="text-xl font-semibold">Chat Flow</h1>
|
| 37 |
</div>
|
| 38 |
-
<button
|
| 39 |
<svg class="w-4 h-4" fill="currentColor" viewBox="0 0 24 24">
|
| 40 |
<path d="M19 13h-6v6h-2v-6H5v-2h6V5h2v6h6v2z"/>
|
| 41 |
</svg>
|
|
@@ -68,7 +68,7 @@
|
|
| 68 |
</div>
|
| 69 |
</div>
|
| 70 |
|
| 71 |
-
<button
|
| 72 |
🔄 Refresh Users
|
| 73 |
</button>
|
| 74 |
</div>
|
|
@@ -99,17 +99,17 @@
|
|
| 99 |
|
| 100 |
<!-- Controls -->
|
| 101 |
<div class="flex gap-2">
|
| 102 |
-
<button
|
| 103 |
<svg class="w-4 h-4" fill="currentColor" viewBox="0 0 24 24">
|
| 104 |
<path d="M19 9h-4V3H9v6H5l7 7 7-7zM5 18v2h14v-2H5z"/>
|
| 105 |
</svg>
|
| 106 |
</button>
|
| 107 |
-
<button
|
| 108 |
<svg class="w-4 h-4" fill="currentColor" viewBox="0 0 24 24">
|
| 109 |
<path d="M6 19c0 1.1.9 2 2 2h8c1.1 0 2-.9 2-2V7H6v12zM19 4h-3.5l-1-1h-5l-1 1H5v2h14V4z"/>
|
| 110 |
</svg>
|
| 111 |
</button>
|
| 112 |
-
<button
|
| 113 |
<svg class="w-4 h-4" fill="currentColor" viewBox="0 0 24 24">
|
| 114 |
<path d="M17.65 6.35C16.2 4.9 14.21 4 12 4c-4.42 0-7.99 3.58-7.99 8s3.57 8 7.99 8c3.73 0 6.84-2.55 7.73-6h-2.08c-.82 2.33-3.04 4-5.65 4-3.31 0-6-2.69-6-6s2.69-6 6-6c1.66 0 3.14.69 4.22 1.78L13 11h7V4l-2.35 2.35z"/>
|
| 115 |
</svg>
|
|
@@ -197,15 +197,25 @@
|
|
| 197 |
|
| 198 |
// Initialize
|
| 199 |
document.addEventListener('DOMContentLoaded', async function() {
|
|
|
|
|
|
|
|
|
|
| 200 |
setupEventListeners();
|
| 201 |
|
| 202 |
// Get user location first (one time only)
|
| 203 |
await getUserLocation();
|
|
|
|
| 204 |
|
| 205 |
// Initialize database with current user only
|
| 206 |
updateOnlineUsers();
|
| 207 |
checkAPIStatus();
|
| 208 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 209 |
// Update users display every 30 seconds (like Streamlit file checking)
|
| 210 |
setInterval(updateOnlineUsers, 30000);
|
| 211 |
|
|
@@ -213,9 +223,13 @@
|
|
| 213 |
document.addEventListener('click', trackActivity);
|
| 214 |
document.addEventListener('keypress', trackActivity);
|
| 215 |
document.addEventListener('scroll', trackActivity);
|
|
|
|
|
|
|
| 216 |
});
|
| 217 |
|
| 218 |
function setupEventListeners() {
|
|
|
|
|
|
|
| 219 |
// Model selection
|
| 220 |
const modelSelect = document.getElementById('model-select');
|
| 221 |
if (modelSelect) {
|
|
@@ -223,6 +237,7 @@
|
|
| 223 |
selectedModel = e.target.value;
|
| 224 |
document.getElementById('model-id').textContent = selectedModel;
|
| 225 |
document.getElementById('current-model-name').textContent = models[selectedModel];
|
|
|
|
| 226 |
});
|
| 227 |
}
|
| 228 |
|
|
@@ -232,23 +247,93 @@
|
|
| 232 |
messageInput.addEventListener('keydown', function(e) {
|
| 233 |
if (e.key === 'Enter' && !e.shiftKey) {
|
| 234 |
e.preventDefault();
|
|
|
|
| 235 |
sendMessage();
|
| 236 |
}
|
| 237 |
});
|
|
|
|
| 238 |
}
|
| 239 |
|
| 240 |
// Send button click
|
| 241 |
const sendButton = document.getElementById('send-button');
|
| 242 |
if (sendButton) {
|
| 243 |
-
sendButton.addEventListener('click',
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 244 |
}
|
|
|
|
|
|
|
| 245 |
}
|
| 246 |
|
| 247 |
async function sendMessage() {
|
|
|
|
|
|
|
| 248 |
const input = document.getElementById('message-input');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 249 |
const message = input.value.trim();
|
|
|
|
| 250 |
|
| 251 |
-
if (!message || isLoading)
|
|
|
|
|
|
|
|
|
|
| 252 |
|
| 253 |
// Track activity when sending message
|
| 254 |
trackActivity();
|
|
@@ -262,6 +347,7 @@
|
|
| 262 |
|
| 263 |
messages.push(userMessage);
|
| 264 |
input.value = '';
|
|
|
|
| 265 |
|
| 266 |
// Add empty assistant message for streaming
|
| 267 |
const assistantMessage = {
|
|
@@ -275,13 +361,15 @@
|
|
| 275 |
setLoading(true);
|
| 276 |
|
| 277 |
try {
|
|
|
|
| 278 |
// The streaming will update the message in real-time
|
| 279 |
await getAIResponse(message);
|
| 280 |
|
| 281 |
// Track activity after receiving response
|
| 282 |
trackActivity();
|
|
|
|
| 283 |
} catch (error) {
|
| 284 |
-
console.error('Error:', error);
|
| 285 |
// Replace the empty message with error
|
| 286 |
messages[messages.length - 1].content = 'Sorry, I encountered an error. Please try again.';
|
| 287 |
updateMessagesDisplay();
|
|
|
|
| 35 |
</div>
|
| 36 |
<h1 class="text-xl font-semibold">Chat Flow</h1>
|
| 37 |
</div>
|
| 38 |
+
<button id="new-chat-btn" class="w-full flex items-center gap-2 px-4 py-2 bg-blue-600 hover:bg-blue-700 rounded-lg transition-colors">
|
| 39 |
<svg class="w-4 h-4" fill="currentColor" viewBox="0 0 24 24">
|
| 40 |
<path d="M19 13h-6v6h-2v-6H5v-2h6V5h2v6h6v2z"/>
|
| 41 |
</svg>
|
|
|
|
| 68 |
</div>
|
| 69 |
</div>
|
| 70 |
|
| 71 |
+
<button id="refresh-users-btn" class="w-full text-xs px-2 py-1 bg-gray-600 hover:bg-gray-500 rounded transition-colors">
|
| 72 |
🔄 Refresh Users
|
| 73 |
</button>
|
| 74 |
</div>
|
|
|
|
| 99 |
|
| 100 |
<!-- Controls -->
|
| 101 |
<div class="flex gap-2">
|
| 102 |
+
<button id="download-btn" class="flex-1 flex items-center justify-center px-3 py-2 bg-gray-700 hover:bg-gray-600 rounded-lg transition-colors text-sm" title="Download History">
|
| 103 |
<svg class="w-4 h-4" fill="currentColor" viewBox="0 0 24 24">
|
| 104 |
<path d="M19 9h-4V3H9v6H5l7 7 7-7zM5 18v2h14v-2H5z"/>
|
| 105 |
</svg>
|
| 106 |
</button>
|
| 107 |
+
<button id="clear-btn" class="flex-1 flex items-center justify-center px-3 py-2 bg-gray-700 hover:bg-gray-600 rounded-lg transition-colors text-sm" title="Clear Chat">
|
| 108 |
<svg class="w-4 h-4" fill="currentColor" viewBox="0 0 24 24">
|
| 109 |
<path d="M6 19c0 1.1.9 2 2 2h8c1.1 0 2-.9 2-2V7H6v12zM19 4h-3.5l-1-1h-5l-1 1H5v2h14V4z"/>
|
| 110 |
</svg>
|
| 111 |
</button>
|
| 112 |
+
<button id="refresh-btn" class="flex-1 flex items-center justify-center px-3 py-2 bg-gray-700 hover:bg-gray-600 rounded-lg transition-colors text-sm" title="Refresh">
|
| 113 |
<svg class="w-4 h-4" fill="currentColor" viewBox="0 0 24 24">
|
| 114 |
<path d="M17.65 6.35C16.2 4.9 14.21 4 12 4c-4.42 0-7.99 3.58-7.99 8s3.57 8 7.99 8c3.73 0 6.84-2.55 7.73-6h-2.08c-.82 2.33-3.04 4-5.65 4-3.31 0-6-2.69-6-6s2.69-6 6-6c1.66 0 3.14.69 4.22 1.78L13 11h7V4l-2.35 2.35z"/>
|
| 115 |
</svg>
|
|
|
|
| 197 |
|
| 198 |
// Initialize
|
| 199 |
document.addEventListener('DOMContentLoaded', async function() {
|
| 200 |
+
console.log('DOM loaded, initializing app...');
|
| 201 |
+
|
| 202 |
+
// Setup all event listeners first
|
| 203 |
setupEventListeners();
|
| 204 |
|
| 205 |
// Get user location first (one time only)
|
| 206 |
await getUserLocation();
|
| 207 |
+
console.log('User location obtained:', currentUserLocation);
|
| 208 |
|
| 209 |
// Initialize database with current user only
|
| 210 |
updateOnlineUsers();
|
| 211 |
checkAPIStatus();
|
| 212 |
|
| 213 |
+
// Initialize send button text
|
| 214 |
+
const sendButton = document.getElementById('send-button');
|
| 215 |
+
if (sendButton) {
|
| 216 |
+
sendButton.innerHTML = '<svg class="w-4 h-4" fill="currentColor" viewBox="0 0 24 24"><path d="M2.01 21L23 12 2.01 3 2 10l15 2-15 2z"/></svg> Send';
|
| 217 |
+
}
|
| 218 |
+
|
| 219 |
// Update users display every 30 seconds (like Streamlit file checking)
|
| 220 |
setInterval(updateOnlineUsers, 30000);
|
| 221 |
|
|
|
|
| 223 |
document.addEventListener('click', trackActivity);
|
| 224 |
document.addEventListener('keypress', trackActivity);
|
| 225 |
document.addEventListener('scroll', trackActivity);
|
| 226 |
+
|
| 227 |
+
console.log('App initialization complete');
|
| 228 |
});
|
| 229 |
|
| 230 |
function setupEventListeners() {
|
| 231 |
+
console.log('Setting up event listeners...');
|
| 232 |
+
|
| 233 |
// Model selection
|
| 234 |
const modelSelect = document.getElementById('model-select');
|
| 235 |
if (modelSelect) {
|
|
|
|
| 237 |
selectedModel = e.target.value;
|
| 238 |
document.getElementById('model-id').textContent = selectedModel;
|
| 239 |
document.getElementById('current-model-name').textContent = models[selectedModel];
|
| 240 |
+
console.log('Model changed to:', selectedModel);
|
| 241 |
});
|
| 242 |
}
|
| 243 |
|
|
|
|
| 247 |
messageInput.addEventListener('keydown', function(e) {
|
| 248 |
if (e.key === 'Enter' && !e.shiftKey) {
|
| 249 |
e.preventDefault();
|
| 250 |
+
console.log('Enter key pressed, sending message...');
|
| 251 |
sendMessage();
|
| 252 |
}
|
| 253 |
});
|
| 254 |
+
console.log('Message input event listener added');
|
| 255 |
}
|
| 256 |
|
| 257 |
// Send button click
|
| 258 |
const sendButton = document.getElementById('send-button');
|
| 259 |
if (sendButton) {
|
| 260 |
+
sendButton.addEventListener('click', function(e) {
|
| 261 |
+
e.preventDefault();
|
| 262 |
+
console.log('Send button clicked, sending message...');
|
| 263 |
+
sendMessage();
|
| 264 |
+
});
|
| 265 |
+
console.log('Send button event listener added');
|
| 266 |
+
}
|
| 267 |
+
|
| 268 |
+
// New chat button
|
| 269 |
+
const newChatBtn = document.getElementById('new-chat-btn');
|
| 270 |
+
if (newChatBtn) {
|
| 271 |
+
newChatBtn.addEventListener('click', function(e) {
|
| 272 |
+
e.preventDefault();
|
| 273 |
+
console.log('New chat button clicked');
|
| 274 |
+
startNewChat();
|
| 275 |
+
});
|
| 276 |
+
}
|
| 277 |
+
|
| 278 |
+
// Download button
|
| 279 |
+
const downloadBtn = document.getElementById('download-btn');
|
| 280 |
+
if (downloadBtn) {
|
| 281 |
+
downloadBtn.addEventListener('click', function(e) {
|
| 282 |
+
e.preventDefault();
|
| 283 |
+
console.log('Download button clicked');
|
| 284 |
+
downloadHistory();
|
| 285 |
+
});
|
| 286 |
+
}
|
| 287 |
+
|
| 288 |
+
// Clear button
|
| 289 |
+
const clearBtn = document.getElementById('clear-btn');
|
| 290 |
+
if (clearBtn) {
|
| 291 |
+
clearBtn.addEventListener('click', function(e) {
|
| 292 |
+
e.preventDefault();
|
| 293 |
+
console.log('Clear button clicked');
|
| 294 |
+
clearChat();
|
| 295 |
+
});
|
| 296 |
+
}
|
| 297 |
+
|
| 298 |
+
// Refresh button
|
| 299 |
+
const refreshBtn = document.getElementById('refresh-btn');
|
| 300 |
+
if (refreshBtn) {
|
| 301 |
+
refreshBtn.addEventListener('click', function(e) {
|
| 302 |
+
e.preventDefault();
|
| 303 |
+
console.log('Refresh button clicked');
|
| 304 |
+
window.location.reload();
|
| 305 |
+
});
|
| 306 |
+
}
|
| 307 |
+
|
| 308 |
+
// Refresh users button
|
| 309 |
+
const refreshUsersBtn = document.getElementById('refresh-users-btn');
|
| 310 |
+
if (refreshUsersBtn) {
|
| 311 |
+
refreshUsersBtn.addEventListener('click', function(e) {
|
| 312 |
+
e.preventDefault();
|
| 313 |
+
console.log('Refresh users button clicked');
|
| 314 |
+
refreshUsers();
|
| 315 |
+
});
|
| 316 |
}
|
| 317 |
+
|
| 318 |
+
console.log('All event listeners set up successfully');
|
| 319 |
}
|
| 320 |
|
| 321 |
async function sendMessage() {
|
| 322 |
+
console.log('sendMessage function called');
|
| 323 |
+
|
| 324 |
const input = document.getElementById('message-input');
|
| 325 |
+
if (!input) {
|
| 326 |
+
console.error('Message input not found');
|
| 327 |
+
return;
|
| 328 |
+
}
|
| 329 |
+
|
| 330 |
const message = input.value.trim();
|
| 331 |
+
console.log('Message to send:', message);
|
| 332 |
|
| 333 |
+
if (!message || isLoading) {
|
| 334 |
+
console.log('No message or already loading');
|
| 335 |
+
return;
|
| 336 |
+
}
|
| 337 |
|
| 338 |
// Track activity when sending message
|
| 339 |
trackActivity();
|
|
|
|
| 347 |
|
| 348 |
messages.push(userMessage);
|
| 349 |
input.value = '';
|
| 350 |
+
console.log('User message added to messages array');
|
| 351 |
|
| 352 |
// Add empty assistant message for streaming
|
| 353 |
const assistantMessage = {
|
|
|
|
| 361 |
setLoading(true);
|
| 362 |
|
| 363 |
try {
|
| 364 |
+
console.log('Calling getAIResponse...');
|
| 365 |
// The streaming will update the message in real-time
|
| 366 |
await getAIResponse(message);
|
| 367 |
|
| 368 |
// Track activity after receiving response
|
| 369 |
trackActivity();
|
| 370 |
+
console.log('AI response completed');
|
| 371 |
} catch (error) {
|
| 372 |
+
console.error('Error getting AI response:', error);
|
| 373 |
// Replace the empty message with error
|
| 374 |
messages[messages.length - 1].content = 'Sorry, I encountered an error. Please try again.';
|
| 375 |
updateMessagesDisplay();
|