File size: 22,592 Bytes
0b3e78c 4645bbc 0b3e78c 3dfb66f 0b3e78c 3dfb66f 0b3e78c 4645bbc 0b3e78c 3dfb66f 0b3e78c 3dfb66f 0b3e78c 3dfb66f 0b3e78c 3dfb66f 0b3e78c 3dfb66f 0b3e78c 3dfb66f 0b3e78c 3dfb66f 0b3e78c 3dfb66f 0b3e78c 3dfb66f 0b3e78c 3dfb66f 4645bbc 0b3e78c 3dfb66f 0b3e78c 3dfb66f 0b3e78c 3dfb66f 0b3e78c 4645bbc 3dfb66f 4645bbc 3dfb66f 4645bbc 3dfb66f 4645bbc 3dfb66f 4645bbc 3dfb66f 4645bbc 3dfb66f 4645bbc 3dfb66f 0b3e78c 3dfb66f 0b3e78c 3dfb66f 0b3e78c 3dfb66f 0b3e78c 3dfb66f 0b3e78c 3dfb66f 0b3e78c 3dfb66f 0b3e78c 3dfb66f 0b3e78c 3dfb66f 0b3e78c 3dfb66f 0b3e78c 3dfb66f 0b3e78c 3dfb66f 0b3e78c 3dfb66f 0b3e78c 3dfb66f 0b3e78c 3dfb66f 0b3e78c 3dfb66f 0b3e78c 3dfb66f 0b3e78c 3dfb66f 4645bbc 0b3e78c 3dfb66f 0b3e78c 4645bbc 0b3e78c 4645bbc 0b3e78c 4645bbc 0b3e78c 3dfb66f 0b3e78c 3dfb66f 0b3e78c 3dfb66f 0b3e78c 3dfb66f 0b3e78c 3dfb66f 0b3e78c 3dfb66f 0b3e78c 4645bbc 3dfb66f 4645bbc 3dfb66f 4645bbc 3dfb66f 4645bbc 0b3e78c 4645bbc 0b3e78c 3dfb66f 0b3e78c 3dfb66f 0b3e78c 3dfb66f 0b3e78c 3dfb66f 0b3e78c 3dfb66f 0b3e78c 3dfb66f 0b3e78c 3dfb66f 0b3e78c 3dfb66f 0b3e78c 3dfb66f 0b3e78c 3dfb66f 0b3e78c 3dfb66f 0b3e78c 3dfb66f 0b3e78c 3dfb66f 0b3e78c 3dfb66f 0b3e78c 3dfb66f 4645bbc 3dfb66f 4645bbc 3dfb66f 4645bbc 3dfb66f 4645bbc 3dfb66f 4645bbc 3dfb66f 4645bbc 0b3e78c |
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 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 |
<!-- static/index.html -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<title>AI Chat with Gemini</title>
<style>
:root {
--background-color: #212121;
--input-area-color: #212121;
--user-bubble-color: #303030;
--ai-bubble-color: #2a2a2a;
--text-color: #e0e0e0;
--placeholder-color: #888888;
--icon-color: #b0b0b0;
--send-button-color: #4CAF50;
--border-color: #333333;
--code-bg-color: #1e1e1e;
--code-text-color: #f8f8f2;
--link-color: #64b5f6;
--quote-color: #4CAF50;
}
/* Reset and Base Styles */
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
html, body {
height: 100%;
overflow: hidden; /* Prevent scrolling on main body */
}
body {
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
background-color: var(--background-color);
color: var(--text-color);
display: flex;
flex-direction: column;
height: 100%;
line-height: 1.6;
}
.chat-container {
display: flex;
flex-direction: column;
height: 100%;
width: 100%;
max-width: 800px; /* Max width for larger screens */
margin: 0 auto; /* Center the container */
border-left: 1px solid var(--border-color); /* Optional: add subtle borders */
border-right: 1px solid var(--border-color);
}
.chat-area {
flex-grow: 1; /* Takes remaining vertical space */
overflow-y: auto; /* Enable scrolling for chat messages */
padding: 20px 16px;
display: flex;
flex-direction: column;
}
/* Welcome Screen */
.welcome-screen {
text-align: center;
margin: auto; /* Center content vertically and horizontally */
color: var(--placeholder-color);
padding: 20px;
}
.welcome-screen h2 {
font-size: 1.8rem;
font-weight: 400;
margin-bottom: 10px;
}
.welcome-screen p {
font-size: 1rem;
line-height: 1.5;
}
.welcome-screen.hidden {
display: none; /* Hide when chat starts */
}
/* Message Bubbles */
.message {
max-width: 85%; /* Limit bubble width */
padding: 12px 16px;
border-radius: 18px;
margin-bottom: 12px;
word-wrap: break-word; /* Ensure long words wrap */
line-height: 1.6;
font-size: 1rem;
position: relative; /* For audio button positioning */
animation: fadeIn 0.3s ease-out; /* Fade in animation for new messages */
}
.user-message {
background-color: var(--user-bubble-color);
align-self: flex-end; /* Align user messages to the right */
border-bottom-right-radius: 4px; /* Slight variation for visual appeal */
}
.ai-message {
background-color: var(--ai-bubble-color);
align-self: flex-start; /* Align AI messages to the left */
border-bottom-left-radius: 4px; /* Slight variation for visual appeal */
padding-bottom: 30px; /* Space for audio button at the bottom */
}
/* AI Message Content Styling (for markdown-converted HTML) */
.ai-message-content {
overflow: hidden; /* Contains floats/margins */
}
.ai-message-content p { margin-bottom: 12px; }
.ai-message-content p:last-child { margin-bottom: 0; } /* No bottom margin on last paragraph */
.ai-message-content strong { color: #ffffff; font-weight: 600; }
.ai-message-content em { color: #d1d1d1; font-style: italic; }
.ai-message-content a { color: var(--link-color); text-decoration: none; word-break: break-all; }
.ai-message-content a:hover { text-decoration: underline; }
.ai-message-content ul, .ai-message-content ol { padding-left: 24px; margin-bottom: 12px; }
.ai-message-content li { margin-bottom: 6px; }
.ai-message-content blockquote {
border-left: 3px solid var(--quote-color);
padding-left: 12px;
margin-left: 0;
color: #bdbdbd;
margin-bottom: 12px;
}
/* Code Blocks */
.ai-message-content pre {
background-color: var(--code-bg-color);
border-radius: 6px;
padding: 12px;
overflow-x: auto; /* Enable horizontal scrolling for long code lines */
margin-bottom: 12px;
}
.ai-message-content code {
font-family: 'Courier New', Courier, monospace;
background-color: var(--code-bg-color);
padding: 2px 4px;
border-radius: 3px;
color: var(--code-text-color);
font-size: 0.9em;
white-space: pre-wrap; /* Preserve whitespace and wrap long lines */
}
/* Inline code styling specific for the `code-block` class which is on `pre` */
.ai-message-content .code-block code {
padding: 0; /* Code inside pre shouldn't have extra padding */
background-color: transparent; /* No background for code inside pre */
color: inherit; /* Inherit color from pre */
}
/* Audio Button Styling */
.audio-button {
position: absolute;
bottom: 6px;
right: 8px;
background: rgba(255, 255, 255, 0.1); /* Semi-transparent background */
border: none;
border-radius: 50%;
width: 30px;
height: 30px;
display: flex;
align-items: center;
justify-content: center;
cursor: pointer;
transition: background-color 0.2s;
z-index: 10; /* Ensure it's above message content */
}
.audio-button:hover {
background: rgba(255, 255, 255, 0.2);
}
.audio-button svg {
width: 18px;
height: 18px;
fill: var(--icon-color);
transition: fill 0.2s;
}
.audio-button:hover svg {
fill: white; /* Icon brighter on hover */
}
/* Loading spinner for audio button */
.audio-button.loading .speaker-icon {
display: none; /* Hide speaker icon when loading */
}
.audio-button .loader {
display: none; /* Hidden by default */
width: 18px;
height: 18px;
border: 2px solid #f3f3f3; /* Light grey border */
border-top: 2px solid var(--send-button-color); /* Green top border for spinner */
border-radius: 50%;
animation: spin 1s linear infinite; /* Spin animation */
}
.audio-button.loading .loader {
display: block; /* Show spinner when loading */
}
@keyframes spin {
0% { transform: rotate(0deg); }
100% { transform: rotate(360deg); }
}
/* Typing Indicator */
.typing-indicator {
display: flex;
align-items: center;
padding: 12px 16px;
}
.typing-indicator span {
height: 8px; width: 8px; background-color: var(--icon-color); border-radius: 50%; display: inline-block; margin: 0 2px;
animation: bounce 1.4s infinite both;
}
.typing-indicator span:nth-child(2) { animation-delay: 0.2s; }
.typing-indicator span:nth-child(3) { animation-delay: 0.4s; }
@keyframes bounce {
0%, 80%, 100% { transform: scale(0); }
40% { transform: scale(1.0); }
}
/* Input Area */
.input-area {
display: flex;
align-items: flex-end; /* Align items to the bottom (textarea can grow) */
padding: 10px 16px;
border-top: 1px solid var(--border-color);
background-color: var(--input-area-color);
flex-shrink: 0; /* Prevent shrinking */
}
.input-wrapper {
display: flex;
align-items: center;
width: 100%;
background-color: var(--user-bubble-color);
border-radius: 24px; /* Pill-shaped input */
padding: 4px;
}
.input-area textarea {
flex-grow: 1; /* Takes up available space */
border: none;
background: transparent;
color: var(--text-color);
resize: none; /* Disable manual resize */
font-size: 1rem;
line-height: 1.5;
max-height: 120px; /* Max height for textarea */
overflow-y: auto; /* Scroll if content exceeds max height */
padding: 8px 12px;
font-family: inherit; /* Inherit font from body */
outline: none; /* Remove focus outline */
}
.input-area textarea::placeholder { color: var(--placeholder-color); }
/* Icon Buttons (Attach, Send) */
.input-area .icon-button {
background: none;
border: none;
padding: 8px;
cursor: pointer;
display: flex;
align-items: center;
justify-content: center;
transition: opacity 0.2s;
}
.input-area .icon-button:hover {
opacity: 0.8;
}
.input-area .icon-button svg { width: 24px; height: 24px; fill: var(--icon-color); }
.send-button {
background-color: var(--send-button-color);
border-radius: 50%;
padding: 8px;
transition: background-color 0.2s, opacity 0.2s;
}
.send-button.disabled {
background-color: #555;
cursor: not-allowed;
opacity: 0.6;
}
.send-button.disabled svg {
fill: #888;
}
.send-button svg { fill: white; width: 24px; height: 24px; }
/* Animation for new messages */
@keyframes fadeIn {
from { opacity: 0; transform: translateY(10px); }
to { opacity: 1; transform: translateY(0); }
}
</style>
</head>
<body>
<div class="chat-container">
<main class="chat-area">
<div class="welcome-screen">
<h2>Welcome to AstroChat!</h2>
<p>Hello! I'm your AI assistant powered by Gemini. Ask me anything - I can explain concepts, generate ideas, or help with coding!</p>
<p>Try asking me something like: <em>"Explain quantum computing in simple terms"</em> or <em>"Help me debug this Python code"</em></p>
</div>
</main>
<footer class="input-area">
<form id="chat-form" class="input-wrapper">
<button type="button" class="icon-button" id="attach-button">
<svg viewBox="0 0 24 24"><path d="M16.5 6v11.5c0 2.21-1.79 4-4 4s-4-1.79-4-4V5a2.5 2.5 0 0 1 5 0v10.5c0 .83-.67 1.5-1.5 1.5s-1.5-.67-1.5-1.5V6H10v9.5a2.5 2.5 0 0 0 5 0V5c-1.38 0-2.5 1.12-2.5 2.5v10.5c0 1.38-1.12 2.5-2.5 2.5s-2.5-1.12-2.5-2.5V5a4 4 0 0 1 8 0v11.5c-1.1 0-2-.9-2-2V6h-1.5z"></path></svg>
</button>
<textarea id="message-input" placeholder="Ask anything..." rows="1"></textarea>
<button type="submit" id="send-button" class="icon-button send-button disabled">
<svg viewBox="0 0 24 24"><path d="M2 21l21-9L2 3v7l15 2-15 2v7z"></path></svg>
</button>
</form>
</footer>
</div>
<script>
document.addEventListener('DOMContentLoaded', () => {
const chatForm = document.getElementById('chat-form');
const messageInput = document.getElementById('message-input');
const chatArea = document.querySelector('.chat-area');
const welcomeScreen = document.querySelector('.welcome-screen');
const sendButton = document.getElementById('send-button');
const attachButton = document.getElementById('attach-button');
// Global variable to hold the currently playing audio object
let currentAudio = null;
// Adjusts the height of the textarea based on content
const adjustTextareaHeight = () => {
messageInput.style.height = 'auto'; // Reset height
messageInput.style.height = `${messageInput.scrollHeight}px`; // Set to scroll height
updateSendButtonState(); // Update send button state after height adjustment
};
// Updates the disabled state of the send button
const updateSendButtonState = () => {
const isDisabled = messageInput.value.trim() === '';
sendButton.classList.toggle('disabled', isDisabled);
sendButton.disabled = isDisabled;
};
// Scrolls the chat area to the bottom
const scrollToBottom = () => {
chatArea.scrollTop = chatArea.scrollHeight;
};
// Adds a message to the chat area
// `plainText` is used specifically for audio generation, keeping original text without HTML
const addMessage = (content, sender, isHTML = false, plainText = '') => {
welcomeScreen.classList.add('hidden'); // Hide welcome screen once chat begins
const messageElement = document.createElement('div');
messageElement.classList.add('message', `${sender}-message`);
const contentDiv = document.createElement('div');
if (isHTML) {
contentDiv.classList.add(`${sender}-message-content`);
contentDiv.innerHTML = content; // Set innerHTML for HTML content
} else {
contentDiv.textContent = content; // Set textContent for plain text
}
messageElement.appendChild(contentDiv);
// Add audio button for AI messages
if (sender === 'ai' && plainText) {
const audioButton = document.createElement('button');
audioButton.classList.add('audio-button');
// Store the plain text directly on the button for easy retrieval
audioButton.dataset.plainText = plainText;
audioButton.innerHTML = `
<svg class="speaker-icon" viewBox="0 0 24 24">
<path d="M3 10v4c0 .55.45 1 1 1h3.5l5.5 5V5L7.5 9H4c-.55 0-1 .45-1 1zm14 0h-1.5c-2.31 0-4.22-1.74-4.47-4H10v12h1.5v-2.22c.25-2.26 2.16-4.22 4.47-4.22H17c1.1 0 2 .9 2 2s-.9 2-2 2h-1.5v2H17c2.21 0 4-1.79 4-4s-1.79-4-4-4z"/>
</svg>
<div class="loader"></div>
`;
audioButton.title = "Listen to response"; // Add tooltip
audioButton.addEventListener('click', () => playAudio(audioButton, plainText));
messageElement.appendChild(audioButton);
}
chatArea.appendChild(messageElement);
scrollToBottom();
return messageElement;
};
// Displays a typing indicator for the AI
const showTypingIndicator = () => {
const indicatorElement = document.createElement('div');
indicatorElement.classList.add('message', 'ai-message', 'typing-indicator');
indicatorElement.innerHTML = '<span></span><span></span><span></span>';
chatArea.appendChild(indicatorElement);
scrollToBottom();
return indicatorElement; // Return element to remove it later
};
// Plays audio for a given text
const playAudio = async (buttonElement, text) => {
// Stop any currently playing audio
if (currentAudio) {
currentAudio.pause();
currentAudio.currentTime = 0;
currentAudio = null;
}
buttonElement.classList.add('loading'); // Show loading spinner
buttonElement.disabled = true; // Disable button during loading
try {
const response = await fetch('/generate-audio', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ text: text })
});
const data = await response.json();
if (data.error) {
throw new Error(data.error);
}
currentAudio = new Audio(data.audio_url);
currentAudio.play();
// Remove loading state when audio ends
currentAudio.onended = () => {
buttonElement.classList.remove('loading');
buttonElement.disabled = false;
currentAudio = null;
};
// Handle audio playback errors
currentAudio.onerror = () => {
console.error("Error playing audio.");
alert("Could not play audio. Please try again.");
buttonElement.classList.remove('loading');
buttonElement.disabled = false;
currentAudio = null;
};
} catch (error) {
console.error("Error generating audio:", error);
alert("Failed to generate audio. " + (error.message || "Please try again."));
buttonElement.classList.remove('loading');
buttonElement.disabled = false;
}
};
// Fetches AI response from the backend
const getAIResponse = async (userMessage) => {
const indicator = showTypingIndicator(); // Show typing indicator
try {
const response = await fetch('/chat', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ message: userMessage })
});
const data = await response.json();
if (data.error) {
throw new Error(data.error);
}
// Remove typing indicator before adding the actual message
chatArea.removeChild(indicator);
// Add AI message with both HTML and plain text
addMessage(data.response_html, 'ai', true, data.response_text);
} catch (error) {
chatArea.removeChild(indicator); // Remove indicator on error too
addMessage("Sorry, I encountered an error. Please try again.", 'ai');
console.error("Error:", error);
}
};
// Event listener for form submission
chatForm.addEventListener('submit', async (e) => {
e.preventDefault(); // Prevent default form submission behavior (page reload)
const message = messageInput.value.trim();
if (message) {
addMessage(message, 'user'); // Add user message to chat
messageInput.value = ''; // Clear input field
adjustTextareaHeight(); // Reset textarea height
await getAIResponse(message); // Get AI response
}
});
// Event listener for Enter key (send message) and Shift+Enter (new line)
messageInput.addEventListener('keydown', (e) => {
if (e.key === 'Enter' && !e.shiftKey) {
e.preventDefault(); // Prevent new line
chatForm.dispatchEvent(new Event('submit')); // Trigger form submission
}
});
// Event listener for input changes to adjust textarea height and send button state
messageInput.addEventListener('input', adjustTextareaHeight);
// Attach button (placeholder functionality)
attachButton.addEventListener('click', () => {
// In a real app, this would open a file dialog
alert("Attachment feature not implemented yet!");
messageInput.focus(); // Keep focus on message input
});
// Initial setup on page load
updateSendButtonState(); // Set initial state of send button
messageInput.focus(); // Focus on the input field
// Optional: Initial welcome message from AI on first visit
const isFirstVisit = !localStorage.getItem('chatVisited');
if (isFirstVisit) {
localStorage.setItem('chatVisited', 'true');
// Use a timeout to allow page to load fully before first message appears
setTimeout(() => {
// The welcome screen now has content, so we just let it display initially.
// If you want an AI message to appear *after* the welcome screen and replace it,
// you'd use addMessage here instead of directly rendering text in welcome-screen div.
// For now, the welcome screen itself has the initial greeting.
}, 500);
}
});
</script>
</body>
</html> |