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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>Бабуру | Voice Chat Interface</title>
|
7 |
+
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">
|
8 |
+
<style>
|
9 |
+
body {
|
10 |
+
font-family: 'Arial', sans-serif;
|
11 |
+
background-color: #f5f5f5;
|
12 |
+
}
|
13 |
+
.chat-container {
|
14 |
+
max-width: 800px;
|
15 |
+
margin: 50px auto;
|
16 |
+
border-radius: 10px;
|
17 |
+
overflow: hidden;
|
18 |
+
box-shadow: 0 0 20px rgba(0, 0, 0, 0.1);
|
19 |
+
}
|
20 |
+
.chat-header {
|
21 |
+
background: linear-gradient(135deg, #6e48aa 0%, #9d50bb 100%);
|
22 |
+
color: white;
|
23 |
+
padding: 15px 20px;
|
24 |
+
font-weight: bold;
|
25 |
+
font-size: 1.2em;
|
26 |
+
}
|
27 |
+
.chat-messages {
|
28 |
+
height: 400px;
|
29 |
+
overflow-y: auto;
|
30 |
+
padding: 15px;
|
31 |
+
background-color: white;
|
32 |
+
}
|
33 |
+
.message {
|
34 |
+
margin-bottom: 15px;
|
35 |
+
padding: 10px 15px;
|
36 |
+
border-radius: 20px;
|
37 |
+
max-width: 80%;
|
38 |
+
position: relative;
|
39 |
+
}
|
40 |
+
.user-message {
|
41 |
+
background-color: #e3f2fd;
|
42 |
+
color: #333;
|
43 |
+
margin-left: auto;
|
44 |
+
border-bottom-right-radius: 5px;
|
45 |
+
}
|
46 |
+
.bot-message {
|
47 |
+
background-color: #f3e5f5;
|
48 |
+
color: #333;
|
49 |
+
margin-right: auto;
|
50 |
+
border-bottom-left-radius: 5px;
|
51 |
+
}
|
52 |
+
.translation {
|
53 |
+
font-size: 0.85em;
|
54 |
+
margin-top: 5px;
|
55 |
+
font-style: italic;
|
56 |
+
color: #666;
|
57 |
+
}
|
58 |
+
.button-container {
|
59 |
+
display: flex;
|
60 |
+
justify-content: center;
|
61 |
+
gap: 10px;
|
62 |
+
padding: 15px;
|
63 |
+
background-color: white;
|
64 |
+
border-top: 1px solid #eee;
|
65 |
+
}
|
66 |
+
.button {
|
67 |
+
background-color: #9d50bb;
|
68 |
+
color: white;
|
69 |
+
border: none;
|
70 |
+
border-radius: 20px;
|
71 |
+
padding: 10px 20px;
|
72 |
+
cursor: pointer;
|
73 |
+
transition: background-color 0.3s;
|
74 |
+
font-size: 1.1em;
|
75 |
+
}
|
76 |
+
.button:hover {
|
77 |
+
background-color: #6e48aa;
|
78 |
+
}
|
79 |
+
.button:disabled {
|
80 |
+
background-color: #cccccc;
|
81 |
+
cursor: not-allowed;
|
82 |
+
}
|
83 |
+
.record-button {
|
84 |
+
background-color: #e74c3c;
|
85 |
+
}
|
86 |
+
.record-button.recording {
|
87 |
+
animation: pulse 1.5s infinite;
|
88 |
+
}
|
89 |
+
@keyframes pulse {
|
90 |
+
0% { background-color: #e74c3c; }
|
91 |
+
50% { background-color: #c0392b; }
|
92 |
+
100% { background-color: #e74c3c; }
|
93 |
+
}
|
94 |
+
.loading {
|
95 |
+
display: none;
|
96 |
+
text-align: center;
|
97 |
+
padding: 10px;
|
98 |
+
color: #666;
|
99 |
+
}
|
100 |
+
.loading span {
|
101 |
+
display: inline-block;
|
102 |
+
animation: dots 1.4s infinite;
|
103 |
+
}
|
104 |
+
@keyframes dots {
|
105 |
+
0%, 20% { transform: translateY(0); }
|
106 |
+
40% { transform: translateY(-5px); }
|
107 |
+
60% { transform: translateY(0); }
|
108 |
+
}
|
109 |
+
.clown-icon {
|
110 |
+
font-size: 1.5em;
|
111 |
+
margin-right: 10px;
|
112 |
+
}
|
113 |
+
.audio-player {
|
114 |
+
width: 100%;
|
115 |
+
margin-top: 5px;
|
116 |
+
}
|
117 |
+
.status-indicator {
|
118 |
+
text-align: center;
|
119 |
+
color: #666;
|
120 |
+
font-style: italic;
|
121 |
+
margin-top: 5px;
|
122 |
+
min-height: 20px;
|
123 |
+
}
|
124 |
+
</style>
|
125 |
+
</head>
|
126 |
+
<body>
|
127 |
+
<div class="container">
|
128 |
+
<div class="chat-container">
|
129 |
+
<div class="chat-header d-flex justify-content-between align-items-center">
|
130 |
+
<div>
|
131 |
+
<span class="clown-icon">🤡</span> Бабуру | Voice Chat Interface
|
132 |
+
</div>
|
133 |
+
</div>
|
134 |
+
|
135 |
+
<div class="chat-messages" id="chat-messages">
|
136 |
+
<!-- Messages will be added here -->
|
137 |
+
</div>
|
138 |
+
|
139 |
+
<div class="loading" id="loading">
|
140 |
+
<span>●</span><span>●</span><span>●</span>
|
141 |
+
</div>
|
142 |
+
|
143 |
+
<div class="button-container">
|
144 |
+
<button id="record-button" class="button record-button">
|
145 |
+
<i class="fa fa-microphone"></i> Speak to Baburu
|
146 |
+
</button>
|
147 |
+
</div>
|
148 |
+
|
149 |
+
<div class="status-indicator" id="status-indicator"></div>
|
150 |
+
</div>
|
151 |
+
|
152 |
+
<div class="mt-3 text-center">
|
153 |
+
<small class="text-muted">For best results, use Chrome and allow microphone access</small>
|
154 |
+
</div>
|
155 |
+
</div>
|
156 |
+
|
157 |
+
<script>
|
158 |
+
document.addEventListener('DOMContentLoaded', function() {
|
159 |
+
// DOM elements
|
160 |
+
const chatMessages = document.getElementById('chat-messages');
|
161 |
+
const loading = document.getElementById('loading');
|
162 |
+
const recordButton = document.getElementById('record-button');
|
163 |
+
const statusIndicator = document.getElementById('status-indicator');
|
164 |
+
|
165 |
+
// Speech recognition variables
|
166 |
+
let recognition;
|
167 |
+
let isListening = false;
|
168 |
+
let recognitionTimeout;
|
169 |
+
|
170 |
+
// Initialize with welcome message
|
171 |
+
initGreeting();
|
172 |
+
|
173 |
+
// Button event listeners
|
174 |
+
recordButton.addEventListener('click', toggleSpeechRecognition);
|
175 |
+
|
176 |
+
// Initialize Web Speech API
|
177 |
+
setupWebSpeechAPI();
|
178 |
+
|
179 |
+
function initGreeting() {
|
180 |
+
// Add initial greeting
|
181 |
+
fetch('/api/call', {
|
182 |
+
method: 'POST',
|
183 |
+
headers: { 'Content-Type': 'application/json' },
|
184 |
+
body: JSON.stringify({})
|
185 |
+
})
|
186 |
+
.then(response => response.json())
|
187 |
+
.then(data => {
|
188 |
+
addBotMessage(data.response, data.translated_response, data.audio_url);
|
189 |
+
})
|
190 |
+
.catch(error => console.error('Error:', error));
|
191 |
+
}
|
192 |
+
|
193 |
+
|
194 |
+
function setupWebSpeechAPI() {
|
195 |
+
// Check if Web Speech API is supported
|
196 |
+
if (!('webkitSpeechRecognition' in window) && !('SpeechRecognition' in window)) {
|
197 |
+
statusIndicator.textContent = 'Speech recognition is not supported in your browser. Try using Chrome.';
|
198 |
+
recordButton.disabled = true;
|
199 |
+
return;
|
200 |
+
}
|
201 |
+
|
202 |
+
// Create speech recognition object
|
203 |
+
const SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition;
|
204 |
+
recognition = new SpeechRecognition();
|
205 |
+
|
206 |
+
// Configure recognition
|
207 |
+
recognition.continuous = false;
|
208 |
+
recognition.interimResults = false;
|
209 |
+
recognition.lang = 'ru-RU'; // Default to Russian
|
210 |
+
|
211 |
+
// Set up event handlers
|
212 |
+
recognition.onstart = function() {
|
213 |
+
statusIndicator.textContent = 'Listening... Speak now in Russian or English';
|
214 |
+
recordButton.classList.add('recording');
|
215 |
+
recordButton.textContent = 'Stop Listening';
|
216 |
+
isListening = true;
|
217 |
+
};
|
218 |
+
|
219 |
+
recognition.onresult = function(event) {
|
220 |
+
// Get the transcript
|
221 |
+
const transcript = event.results[0][0].transcript;
|
222 |
+
const confidence = event.results[0][0].confidence;
|
223 |
+
|
224 |
+
// Log for debugging
|
225 |
+
console.log(`Recognized: "${transcript}" (Confidence: ${(confidence * 100).toFixed(1)}%)`);
|
226 |
+
|
227 |
+
// Add user message
|
228 |
+
const userMessage = addMessage(transcript, 'user');
|
229 |
+
|
230 |
+
// Process the message
|
231 |
+
processUserInput(transcript);
|
232 |
+
};
|
233 |
+
|
234 |
+
recognition.onerror = function(event) {
|
235 |
+
console.error('Speech recognition error', event.error);
|
236 |
+
statusIndicator.textContent = `Error: ${event.error}. Try again.`;
|
237 |
+
recordButton.classList.remove('recording');
|
238 |
+
recordButton.textContent = 'Speak to Baburu';
|
239 |
+
isListening = false;
|
240 |
+
clearTimeout(recognitionTimeout);
|
241 |
+
};
|
242 |
+
|
243 |
+
recognition.onend = function() {
|
244 |
+
if (isListening && !recognition.result) {
|
245 |
+
statusIndicator.textContent = 'No speech detected. Try again.';
|
246 |
+
}
|
247 |
+
recordButton.classList.remove('recording');
|
248 |
+
recordButton.textContent = 'Speak to Baburu';
|
249 |
+
isListening = false;
|
250 |
+
clearTimeout(recognitionTimeout);
|
251 |
+
};
|
252 |
+
}
|
253 |
+
|
254 |
+
function toggleSpeechRecognition() {
|
255 |
+
if (!isListening) {
|
256 |
+
// Start listening
|
257 |
+
try {
|
258 |
+
// Try to detect language automatically first with English fallback
|
259 |
+
const detectedLang = navigator.language || 'en-US';
|
260 |
+
// Check if it's a Russian locale
|
261 |
+
if (detectedLang.startsWith('ru')) {
|
262 |
+
recognition.lang = 'ru-RU';
|
263 |
+
} else {
|
264 |
+
// Default to English if not Russian
|
265 |
+
recognition.lang = 'en-US';
|
266 |
+
}
|
267 |
+
|
268 |
+
recognition.start();
|
269 |
+
console.log(`Started recognition in ${recognition.lang} mode`);
|
270 |
+
|
271 |
+
// Auto-stop after 10 seconds in case of no response
|
272 |
+
recognitionTimeout = setTimeout(() => {
|
273 |
+
if (isListening) {
|
274 |
+
recognition.stop();
|
275 |
+
statusIndicator.textContent = 'Listening timed out after 10 seconds';
|
276 |
+
}
|
277 |
+
}, 10000);
|
278 |
+
|
279 |
+
} catch (error) {
|
280 |
+
console.error('Error starting speech recognition:', error);
|
281 |
+
statusIndicator.textContent = `Error: ${error.message}. Try again.`;
|
282 |
+
}
|
283 |
+
} else {
|
284 |
+
// Stop listening
|
285 |
+
try {
|
286 |
+
recognition.stop();
|
287 |
+
statusIndicator.textContent = 'Stopped listening';
|
288 |
+
} catch (error) {
|
289 |
+
console.error('Error stopping speech recognition:', error);
|
290 |
+
}
|
291 |
+
}
|
292 |
+
}
|
293 |
+
|
294 |
+
function processUserInput(text) {
|
295 |
+
// Show loading indicator
|
296 |
+
loading.style.display = 'block';
|
297 |
+
statusIndicator.textContent = 'Getting response from Baburu...';
|
298 |
+
|
299 |
+
// Send message to server
|
300 |
+
fetch('/api/chat', {
|
301 |
+
method: 'POST',
|
302 |
+
headers: {
|
303 |
+
'Content-Type': 'application/json'
|
304 |
+
},
|
305 |
+
body: JSON.stringify({ message: text })
|
306 |
+
})
|
307 |
+
.then(response => response.json())
|
308 |
+
.then(data => {
|
309 |
+
// Hide loading indicator
|
310 |
+
loading.style.display = 'none';
|
311 |
+
statusIndicator.textContent = '';
|
312 |
+
|
313 |
+
// Add bot response to chat
|
314 |
+
addBotMessage(data.response, data.translated_response, data.audio_url);
|
315 |
+
})
|
316 |
+
.catch(error => {
|
317 |
+
console.error('Error:', error);
|
318 |
+
loading.style.display = 'none';
|
319 |
+
statusIndicator.textContent = 'Error getting response';
|
320 |
+
addBotMessage('Sorry, an error occurred. Please try again.', '', null);
|
321 |
+
});
|
322 |
+
}
|
323 |
+
|
324 |
+
function addMessage(text, sender, translation = '') {
|
325 |
+
const messageDiv = document.createElement('div');
|
326 |
+
messageDiv.className = `message ${sender}-message`;
|
327 |
+
messageDiv.textContent = text;
|
328 |
+
|
329 |
+
if (translation) {
|
330 |
+
const translationDiv = document.createElement('div');
|
331 |
+
translationDiv.className = 'translation';
|
332 |
+
translationDiv.textContent = translation;
|
333 |
+
messageDiv.appendChild(translationDiv);
|
334 |
+
}
|
335 |
+
|
336 |
+
chatMessages.appendChild(messageDiv);
|
337 |
+
chatMessages.scrollTop = chatMessages.scrollHeight;
|
338 |
+
|
339 |
+
return messageDiv;
|
340 |
+
}
|
341 |
+
|
342 |
+
function addBotMessage(text, translation = '', audioUrl = null) {
|
343 |
+
const messageDiv = addMessage(text, 'bot', translation);
|
344 |
+
|
345 |
+
// Add audio player if available
|
346 |
+
if (audioUrl) {
|
347 |
+
const audioElement = document.createElement('audio');
|
348 |
+
audioElement.className = 'audio-player';
|
349 |
+
audioElement.controls = true;
|
350 |
+
audioElement.autoplay = true; // Automatically play audio
|
351 |
+
|
352 |
+
const sourceElement = document.createElement('source');
|
353 |
+
sourceElement.src = audioUrl;
|
354 |
+
sourceElement.type = 'audio/mpeg';
|
355 |
+
|
356 |
+
// Add event listener for when the audio loads
|
357 |
+
audioElement.oncanplaythrough = function() {
|
358 |
+
audioElement.play().catch(e => {
|
359 |
+
console.error('Auto-play failed:', e);
|
360 |
+
statusIndicator.textContent = 'Click the audio player to hear Baburu';
|
361 |
+
});
|
362 |
+
};
|
363 |
+
|
364 |
+
audioElement.appendChild(sourceElement);
|
365 |
+
messageDiv.appendChild(audioElement);
|
366 |
+
}
|
367 |
+
|
368 |
+
return messageDiv;
|
369 |
+
}
|
370 |
+
});
|
371 |
+
</script>
|
372 |
+
</body>
|
373 |
+
</html>
|