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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<!DOCTYPE html>
|
| 2 |
+
<html lang="en">
|
| 3 |
+
<head>
|
| 4 |
+
<meta charset="UTF-8">
|
| 5 |
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
| 6 |
+
<title>Hotel Assistant Chat</title>
|
| 7 |
+
<script src="https://cdn.tailwindcss.com"></script>
|
| 8 |
+
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap" rel="stylesheet">
|
| 9 |
+
<style>
|
| 10 |
+
body {
|
| 11 |
+
font-family: 'Inter', sans-serif;
|
| 12 |
+
}
|
| 13 |
+
.custom-scrollbar::-webkit-scrollbar { width: 6px; }
|
| 14 |
+
.custom-scrollbar::-webkit-scrollbar-track { background: #f1f1f1; border-radius: 10px; }
|
| 15 |
+
.custom-scrollbar::-webkit-scrollbar-thumb { background: #888; border-radius: 10px; }
|
| 16 |
+
.custom-scrollbar::-webkit-scrollbar-thumb:hover { background: #555; }
|
| 17 |
+
.dark .custom-scrollbar::-webkit-scrollbar-track { background: #2d3748; }
|
| 18 |
+
.dark .custom-scrollbar::-webkit-scrollbar-thumb { background: #718096; }
|
| 19 |
+
.dark .custom-scrollbar::-webkit-scrollbar-thumb:hover { background: #a0aec0; }
|
| 20 |
+
|
| 21 |
+
/* Speaker Icon Style */
|
| 22 |
+
.speaker-btn {
|
| 23 |
+
opacity: 0.6; /* Make it visible but subtle */
|
| 24 |
+
transition: opacity 0.2s ease-in-out, color 0.2s ease-in-out;
|
| 25 |
+
cursor: pointer;
|
| 26 |
+
}
|
| 27 |
+
.speaker-btn:hover {
|
| 28 |
+
opacity: 1.0;
|
| 29 |
+
color: #3b82f6; /* blue-500 */
|
| 30 |
+
}
|
| 31 |
+
</style>
|
| 32 |
+
</head>
|
| 33 |
+
<body class="bg-gray-100 dark:bg-gray-900 text-gray-800 dark:text-gray-200 flex items-center justify-center h-screen">
|
| 34 |
+
|
| 35 |
+
<div class="flex flex-col w-full max-w-3xl h-[95vh] bg-white dark:bg-gray-800 rounded-2xl shadow-2xl border border-gray-200 dark:border-gray-700">
|
| 36 |
+
<!-- Header -->
|
| 37 |
+
<header class="p-4 border-b border-gray-200 dark:border-gray-700 flex items-center justify-between">
|
| 38 |
+
<h1 class="text-xl font-bold text-gray-800 dark:text-white">Hotel Assistant</h1>
|
| 39 |
+
<!-- Language dropdown removed -->
|
| 40 |
+
</header>
|
| 41 |
+
|
| 42 |
+
<!-- Chat Messages -->
|
| 43 |
+
<main id="chat-messages" class="flex-1 p-6 space-y-6 overflow-y-auto custom-scrollbar">
|
| 44 |
+
<!-- Initial bot message -->
|
| 45 |
+
<div class="flex items-start gap-3">
|
| 46 |
+
<div class="w-10 h-10 rounded-full bg-blue-500 flex items-center justify-center text-white font-bold text-lg shrink-0">H</div>
|
| 47 |
+
<div class="bg-gray-200 dark:bg-gray-700 p-4 rounded-lg rounded-tl-none max-w-lg shadow relative group">
|
| 48 |
+
<p class="text-sm" id="msg-init">Welcome to our hotel! How can I assist you today? Feel free to ask me anything about the hotel's amenities or services.</p>
|
| 49 |
+
<div class="absolute -right-8 top-1/2 -translate-y-1/2">
|
| 50 |
+
<svg onclick="speakText('msg-init')" class="speaker-btn h-5 w-5 text-gray-500 dark:text-gray-400" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor">
|
| 51 |
+
<path d="M13.5 4.06c0-1.336-1.616-2.005-2.56-1.06l-4.5 4.5H4.5a1.5 1.5 0 0 0-1.5 1.5v6a1.5 1.5 0 0 0 1.5 1.5h1.94l4.5 4.5c.944.945 2.56.276 2.56-1.06V4.06ZM18.584 14.828a1.5 1.5 0 0 0 0-2.121l-1.414-1.414a1.5 1.5 0 1 0-2.121 2.121l1.414 1.414a1.5 1.5 0 0 0 2.121 0ZM18.584 9.172a1.5 1.5 0 1 0-2.121-2.121L15.05 8.465a1.5 1.5 0 1 0 2.121 2.121l1.413-1.414Z"/>
|
| 52 |
+
</svg>
|
| 53 |
+
</div>
|
| 54 |
+
</div>
|
| 55 |
+
</div>
|
| 56 |
+
</main>
|
| 57 |
+
|
| 58 |
+
<!-- Chat Input -->
|
| 59 |
+
<footer class="p-4 border-t border-gray-200 dark:border-gray-700">
|
| 60 |
+
<div class="relative">
|
| 61 |
+
<textarea
|
| 62 |
+
id="chat-input"
|
| 63 |
+
class="w-full bg-gray-100 dark:bg-gray-700 rounded-lg p-4 pr-32 text-sm focus:outline-none focus:ring-2 focus:ring-blue-500 transition duration-200 resize-none"
|
| 64 |
+
placeholder="Type your message or use the microphone..."
|
| 65 |
+
rows="1"
|
| 66 |
+
></textarea>
|
| 67 |
+
<div class="absolute inset-y-0 right-4 flex items-center">
|
| 68 |
+
<button id="voice-btn" class="p-2 text-gray-500 dark:text-gray-400 hover:text-blue-500 dark:hover:text-blue-400 rounded-full transition duration-200 focus:outline-none">
|
| 69 |
+
<svg id="mic-icon" xmlns="http://www.w3.org/2000/svg" class="h-6 w-6" fill="none" viewBox="0 0 24 24" stroke="currentColor"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 11a7 7 0 01-7 7m0 0a7 7 0 01-7-7m7 7v4m0 0H8m4 0h4m-4-8a3 3 0 01-3-3V5a3 3 0 116 0v6a3 3 0 01-3 3z" /></svg>
|
| 70 |
+
<svg id="recording-icon" xmlns="http://www.w3.org/2000/svg" class="h-6 w-6 hidden animate-pulse" fill="red" viewBox="0 0 24 24" stroke="red"><circle cx="12" cy="12" r="10" /></svg>
|
| 71 |
+
</button>
|
| 72 |
+
<button id="send-btn" class="ml-2 p-2 bg-blue-500 text-white rounded-full hover:bg-blue-600 transition duration-200 focus:outline-none disabled:bg-gray-400 disabled:cursor-not-allowed">
|
| 73 |
+
<svg xmlns="http://www.w3.org/2000/svg" class="h-6 w-6" fill="none" viewBox="0 0 24 24" stroke="currentColor"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5 13l4 4L19 7" /></svg>
|
| 74 |
+
</button>
|
| 75 |
+
</div>
|
| 76 |
+
</div>
|
| 77 |
+
</footer>
|
| 78 |
+
</div>
|
| 79 |
+
|
| 80 |
+
<script>
|
| 81 |
+
// --- DOM Elements ---
|
| 82 |
+
const chatMessages = document.getElementById('chat-messages');
|
| 83 |
+
const chatInput = document.getElementById('chat-input');
|
| 84 |
+
const sendBtn = document.getElementById('send-btn');
|
| 85 |
+
const voiceBtn = document.getElementById('voice-btn');
|
| 86 |
+
const micIcon = document.getElementById('mic-icon');
|
| 87 |
+
const recordingIcon = document.getElementById('recording-icon');
|
| 88 |
+
|
| 89 |
+
// --- API Configuration ---
|
| 90 |
+
// Pointing to our local Flask server
|
| 91 |
+
const API_ENDPOINT = 'http://127.0.0.1:5000/chat';
|
| 92 |
+
|
| 93 |
+
// --- Speech Recognition Setup (English Only) ---
|
| 94 |
+
const SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition;
|
| 95 |
+
let recognition;
|
| 96 |
+
let isRecording = false;
|
| 97 |
+
|
| 98 |
+
if (SpeechRecognition) {
|
| 99 |
+
recognition = new SpeechRecognition();
|
| 100 |
+
recognition.continuous = true;
|
| 101 |
+
recognition.interimResults = true;
|
| 102 |
+
recognition.lang = 'en-US'; // Hardcoded to English
|
| 103 |
+
|
| 104 |
+
recognition.onstart = () => {
|
| 105 |
+
isRecording = true;
|
| 106 |
+
micIcon.classList.add('hidden');
|
| 107 |
+
recordingIcon.classList.remove('hidden');
|
| 108 |
+
voiceBtn.classList.add('text-red-500');
|
| 109 |
+
chatInput.placeholder = 'Listening... Click the mic again to stop.';
|
| 110 |
+
};
|
| 111 |
+
|
| 112 |
+
recognition.onend = () => {
|
| 113 |
+
isRecording = false;
|
| 114 |
+
micIcon.classList.remove('hidden');
|
| 115 |
+
recordingIcon.classList.add('hidden');
|
| 116 |
+
voiceBtn.classList.remove('text-red-500');
|
| 117 |
+
chatInput.placeholder = 'Type your message or use the microphone...';
|
| 118 |
+
};
|
| 119 |
+
|
| 120 |
+
recognition.onresult = (event) => {
|
| 121 |
+
let interimTranscript = '';
|
| 122 |
+
let finalTranscript = '';
|
| 123 |
+
for (let i = event.resultIndex; i < event.results.length; ++i) {
|
| 124 |
+
if (event.results[i].isFinal) {
|
| 125 |
+
finalTranscript += event.results[i][0].transcript;
|
| 126 |
+
} else {
|
| 127 |
+
interimTranscript += event.results[i][0].transcript;
|
| 128 |
+
}
|
| 129 |
+
}
|
| 130 |
+
chatInput.value = finalTranscript + interimTranscript;
|
| 131 |
+
autoResizeTextarea();
|
| 132 |
+
};
|
| 133 |
+
|
| 134 |
+
recognition.onerror = (event) => {
|
| 135 |
+
console.error("Speech recognition error:", event.error);
|
| 136 |
+
chatInput.placeholder = `Mic error: ${event.error}`;
|
| 137 |
+
};
|
| 138 |
+
|
| 139 |
+
} else {
|
| 140 |
+
console.warn("Speech Recognition not supported in this browser.");
|
| 141 |
+
voiceBtn.disabled = true;
|
| 142 |
+
}
|
| 143 |
+
|
| 144 |
+
// --- Speech Synthesis Setup ---
|
| 145 |
+
const synth = window.speechSynthesis;
|
| 146 |
+
|
| 147 |
+
function speakText(elementId) {
|
| 148 |
+
const textElement = document.getElementById(elementId);
|
| 149 |
+
if (textElement && textElement.textContent && synth.speaking) {
|
| 150 |
+
synth.cancel(); // Stop any current speech
|
| 151 |
+
}
|
| 152 |
+
if (textElement && textElement.textContent) {
|
| 153 |
+
const utterance = new SpeechSynthesisUtterance(textElement.textContent);
|
| 154 |
+
utterance.onerror = (e) => console.error("Speech synthesis error:", e);
|
| 155 |
+
synth.speak(utterance);
|
| 156 |
+
}
|
| 157 |
+
}
|
| 158 |
+
|
| 159 |
+
// --- Event Listeners ---
|
| 160 |
+
sendBtn.addEventListener('click', sendMessage);
|
| 161 |
+
chatInput.addEventListener('keydown', (e) => {
|
| 162 |
+
if (e.key === 'Enter' && !e.shiftKey) {
|
| 163 |
+
e.preventDefault();
|
| 164 |
+
sendMessage();
|
| 165 |
+
}
|
| 166 |
+
});
|
| 167 |
+
voiceBtn.addEventListener('click', toggleRecording);
|
| 168 |
+
chatInput.addEventListener('input', autoResizeTextarea);
|
| 169 |
+
|
| 170 |
+
// --- Functions ---
|
| 171 |
+
function toggleRecording() {
|
| 172 |
+
if (!SpeechRecognition) return;
|
| 173 |
+
if (isRecording) {
|
| 174 |
+
recognition.stop();
|
| 175 |
+
} else {
|
| 176 |
+
recognition.start();
|
| 177 |
+
}
|
| 178 |
+
}
|
| 179 |
+
|
| 180 |
+
async function sendMessage() {
|
| 181 |
+
const messageText = chatInput.value.trim();
|
| 182 |
+
if (messageText === '') return;
|
| 183 |
+
|
| 184 |
+
addMessageToUI(messageText, 'user');
|
| 185 |
+
chatInput.value = '';
|
| 186 |
+
autoResizeTextarea();
|
| 187 |
+
|
| 188 |
+
// Show a "thinking" message from the bot
|
| 189 |
+
addMessageToUI("...", 'bot', true); // a "thinking" message
|
| 190 |
+
|
| 191 |
+
try {
|
| 192 |
+
// Call the stateless backend
|
| 193 |
+
const botResponse = await getBotResponseFromBackend(messageText);
|
| 194 |
+
|
| 195 |
+
// Update the "thinking" message with the real response
|
| 196 |
+
updateLastBotMessage(botResponse.response); // We now get an object
|
| 197 |
+
|
| 198 |
+
} catch (error) {
|
| 199 |
+
console.error("Error getting response from backend:", error);
|
| 200 |
+
updateLastBotMessage("I'm sorry, I'm having trouble connecting at the moment. Please try again later.");
|
| 201 |
+
}
|
| 202 |
+
}
|
| 203 |
+
|
| 204 |
+
async function getBotResponseFromBackend(text) {
|
| 205 |
+
const response = await fetch(API_ENDPOINT, {
|
| 206 |
+
method: 'POST',
|
| 207 |
+
headers: {
|
| 208 |
+
'Content-Type': 'application/json',
|
| 209 |
+
},
|
| 210 |
+
// Send only the query, as the backend is stateless
|
| 211 |
+
body: JSON.stringify({ query: text }),
|
| 212 |
+
});
|
| 213 |
+
|
| 214 |
+
if (!response.ok) {
|
| 215 |
+
throw new Error(`API call failed with status: ${response.status}`);
|
| 216 |
+
}
|
| 217 |
+
|
| 218 |
+
const data = await response.json();
|
| 219 |
+
// The backend now sends an object: { intent: "qa", response: "..." }
|
| 220 |
+
return data;
|
| 221 |
+
}
|
| 222 |
+
|
| 223 |
+
function addMessageToUI(text, sender, isThinking = false) {
|
| 224 |
+
const messageElement = document.createElement('div');
|
| 225 |
+
const uniqueId = `msg-${Date.now()}`;
|
| 226 |
+
|
| 227 |
+
if (sender === 'user') {
|
| 228 |
+
messageElement.className = 'flex items-start gap-3 justify-end';
|
| 229 |
+
messageElement.innerHTML = `
|
| 230 |
+
<div class="bg-blue-500 text-white p-4 rounded-lg rounded-br-none max-w-lg shadow">
|
| 231 |
+
<p class="text-sm">${text}</p>
|
| 232 |
+
</div>
|
| 233 |
+
<div class="w-10 h-10 rounded-full bg-gray-300 flex items-center justify-center font-bold text-gray-600 shrink-0">You</div>
|
| 234 |
+
`;
|
| 235 |
+
} else {
|
| 236 |
+
messageElement.className = 'flex items-start gap-3';
|
| 237 |
+
messageElement.innerHTML = `
|
| 238 |
+
<div class="w-10 h-10 rounded-full bg-blue-500 flex items-center justify-center text-white font-bold text-lg shrink-0">H</div>
|
| 239 |
+
<div class="bg-gray-200 dark:bg-gray-700 p-4 rounded-lg rounded-tl-none max-w-lg shadow relative group">
|
| 240 |
+
<p class="text-sm" id="${uniqueId}">${isThinking ? '<span class="animate-pulse">Thinking...</span>' : text}</p>
|
| 241 |
+
<!-- Add speaker button here, but wait for final content -->
|
| 242 |
+
<div class="absolute -right-8 top-1/2 -translate-y-1/2" style="display: none;">
|
| 243 |
+
<svg onclick="speakText('${uniqueId}')" class="speaker-btn h-5 w-5 text-gray-500 dark:text-gray-400" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor">
|
| 244 |
+
<path d="M13.5 4.06c0-1.336-1.616-2.005-2.56-1.06l-4.5 4.5H4.5a1.5 1.5 0 0 0-1.5 1.5v6a1.5 1.5 0 0 0 1.5 1.5h1.94l4.5 4.5c.944.945 2.56.276 2.56-1.06V4.06ZM18.584 14.828a1.5 1.5 0 0 0 0-2.121l-1.414-1.414a1.5 1.5 0 1 0-2.121 2.121l1.414 1.414a1.5 1.5 0 0 0 2.121 0ZM18.584 9.172a1.5 1.5 0 1 0-2.121-2.121L15.05 8.465a1.5 1.5 0 1 0 2.121 2.121l1.413-1.414Z"/>
|
| 245 |
+
</svg>
|
| 246 |
+
</div>
|
| 247 |
+
</div>
|
| 248 |
+
`;
|
| 249 |
+
}
|
| 250 |
+
chatMessages.appendChild(messageElement);
|
| 251 |
+
chatMessages.scrollTop = chatMessages.scrollHeight;
|
| 252 |
+
}
|
| 253 |
+
|
| 254 |
+
function updateLastBotMessage(text) {
|
| 255 |
+
const allBotMessages = chatMessages.querySelectorAll('.flex.items-start.gap-3:not(.justify-end)');
|
| 256 |
+
if (allBotMessages.length > 0) {
|
| 257 |
+
const lastMessageContainer = allBotMessages[allBotMessages.length - 1];
|
| 258 |
+
const textElement = lastMessageContainer.querySelector('p');
|
| 259 |
+
const speakerButtonContainer = lastMessageContainer.querySelector('.absolute');
|
| 260 |
+
|
| 261 |
+
if (textElement) {
|
| 262 |
+
textElement.innerHTML = text; // Update the text
|
| 263 |
+
}
|
| 264 |
+
if (speakerButtonContainer) {
|
| 265 |
+
speakerButtonContainer.style.display = 'block'; // Show the speaker button
|
| 266 |
+
}
|
| 267 |
+
}
|
| 268 |
+
}
|
| 269 |
+
|
| 270 |
+
function autoResizeTextarea() {
|
| 271 |
+
chatInput.style.height = 'auto';
|
| 272 |
+
chatInput.style.height = (chatInput.scrollHeight) + 'px';
|
| 273 |
+
if (chatInput.scrollHeight > 200) {
|
| 274 |
+
chatInput.style.overflowY = 'auto';
|
| 275 |
+
} else {
|
| 276 |
+
chatInput.style.overflowY = 'hidden';
|
| 277 |
+
}
|
| 278 |
+
}
|
| 279 |
+
|
| 280 |
+
// Initial resize
|
| 281 |
+
autoResizeTextarea();
|
| 282 |
+
</script>
|
| 283 |
+
</body>
|
| 284 |
+
</html>
|
| 285 |
+
|