Update static/script.js
Browse files- static/script.js +218 -0
static/script.js
CHANGED
|
@@ -1,3 +1,61 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
document.addEventListener("DOMContentLoaded", () => {
|
| 2 |
let mediaRecorder, audioChunks = [], audioStream, currentChatId = null;
|
| 3 |
const recordBtn = document.getElementById("record-btn");
|
|
@@ -215,6 +273,166 @@ document.addEventListener("DOMContentLoaded", () => {
|
|
| 215 |
chatBox.scrollTop = chatBox.scrollHeight;
|
| 216 |
}
|
| 217 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 218 |
function appendAndSaveMessage(sender, text) {
|
| 219 |
appendMessage(sender, text);
|
| 220 |
|
|
|
|
| 1 |
+
document.addEventListener("DOMContentLoaded", () => {
|
| 2 |
+
let mediaRecorder, audioChunks = [], audioStream, currentChatId = null;
|
| 3 |
+
const recordBtn = document.getElementById("record-btn");
|
| 4 |
+
const stopBtn = document.getElementById("stop-btn");
|
| 5 |
+
const sendBtn = document.getElementById("send-btn");
|
| 6 |
+
const uploadBtn = document.getElementById("upload-btn");
|
| 7 |
+
const userInput = document.getElementById("user-input");
|
| 8 |
+
const chatBox = document.getElementById("chat-box");
|
| 9 |
+
const audioFileInput = document.getElementById("audio-file");
|
| 10 |
+
const newChatBtn = document.getElementById("new-chat-btn");
|
| 11 |
+
const chatList = document.getElementById("chat-list");
|
| 12 |
+
const currentChatTitle = document.getElementById("current-chat-title");
|
| 13 |
+
|
| 14 |
+
// Инициализация при загрузке
|
| 15 |
+
initializeChats();
|
| 16 |
+
|
| 17 |
+
function initializeChats() {
|
| 18 |
+
const savedChatId = localStorage.getItem('currentChatId');
|
| 19 |
+
|
| 20 |
+
fetch("/get_chats")
|
| 21 |
+
.then(response => response.json())
|
| 22 |
+
.then(chats => {
|
| 23 |
+
renderChatList(chats);
|
| 24 |
+
|
| 25 |
+
if (savedChatId && chats.some(c => c.chat_id === savedChatId)) {
|
| 26 |
+
loadChat(savedChatId);
|
| 27 |
+
} else if (chats.length > 0) {
|
| 28 |
+
loadChat(chats[0].chat_id);
|
| 29 |
+
} else {
|
| 30 |
+
startNewChat();
|
| 31 |
+
}
|
| 32 |
+
})
|
| 33 |
+
.catch(error => {
|
| 34 |
+
console.error("Ошибка загрузки чатов:", error);
|
| 35 |
+
startNewChat();
|
| 36 |
+
});
|
| 37 |
+
}
|
| 38 |
+
|
| 39 |
+
function renderChatList(chats) {
|
| 40 |
+
chatList.innerHTML = '';
|
| 41 |
+
chats.forEach(chat => {
|
| 42 |
+
const chatItem = document.createElement("div");
|
| 43 |
+
chatItem.className = "chat-item";
|
| 44 |
+
chatItem.dataset.chatId = chat.chat_id;
|
| 45 |
+
chatItem.innerHTML = `
|
| 46 |
+
<i class="fas fa-comment"></i>
|
| 47 |
+
<span class="chat-title">${chat.title}</span>
|
| 48 |
+
`;
|
| 49 |
+
chatItem.addEventListener("click", () => {
|
| 50 |
+
loadChat(chat.chat_id);
|
| 51 |
+
localStorage.setItem('currentChatId', chat.chat_id);
|
| 52 |
+
});
|
| 53 |
+
chatList.appendChild(chatItem);
|
| 54 |
+
});
|
| 55 |
+
}
|
| 56 |
+
|
| 57 |
+
newChatBtn.addEventListener("click", startNewChat);
|
| 58 |
+
|
| 59 |
document.addEventListener("DOMContentLoaded", () => {
|
| 60 |
let mediaRecorder, audioChunks = [], audioStream, currentChatId = null;
|
| 61 |
const recordBtn = document.getElementById("record-btn");
|
|
|
|
| 273 |
chatBox.scrollTop = chatBox.scrollHeight;
|
| 274 |
}
|
| 275 |
|
| 276 |
+
function appendAndSaveMessage(sender, text) {
|
| 277 |
+
appendMessage(sender, text);
|
| 278 |
+
|
| 279 |
+
if (currentChatId) {
|
| 280 |
+
fetch("/save_message", {
|
| 281 |
+
method: "POST",
|
| 282 |
+
headers: { "Content-Type": "application/json" },
|
| 283 |
+
body: JSON.stringify({
|
| 284 |
+
chat_id: currentChatId,
|
| 285 |
+
sender: sender,
|
| 286 |
+
content: text
|
| 287 |
+
})
|
| 288 |
+
}).catch(console.error);
|
| 289 |
+
}
|
| 290 |
+
}
|
| 291 |
+
});
|
| 292 |
+
|
| 293 |
+
function loadChat(chatId) {
|
| 294 |
+
fetch(`/load_chat/${chatId}`)
|
| 295 |
+
.then(response => response.json())
|
| 296 |
+
.then(data => {
|
| 297 |
+
if (data.error) throw new Error(data.error);
|
| 298 |
+
|
| 299 |
+
currentChatId = chatId;
|
| 300 |
+
currentChatTitle.textContent = data.title;
|
| 301 |
+
updateActiveChat(chatId);
|
| 302 |
+
|
| 303 |
+
chatBox.innerHTML = "";
|
| 304 |
+
data.messages.forEach(msg => {
|
| 305 |
+
appendMessage(msg.sender, msg.content);
|
| 306 |
+
});
|
| 307 |
+
|
| 308 |
+
localStorage.setItem('currentChatId', chatId);
|
| 309 |
+
})
|
| 310 |
+
.catch(error => {
|
| 311 |
+
console.error("Ошибка загрузки чата:", error);
|
| 312 |
+
appendMessage("bot", `❌ Ошибка: ${error.message}`);
|
| 313 |
+
});
|
| 314 |
+
}
|
| 315 |
+
|
| 316 |
+
function updateActiveChat(chatId) {
|
| 317 |
+
document.querySelectorAll(".chat-item").forEach(item => {
|
| 318 |
+
item.classList.toggle("active", item.dataset.chatId === chatId);
|
| 319 |
+
});
|
| 320 |
+
}
|
| 321 |
+
|
| 322 |
+
sendBtn.addEventListener("click", sendMessage);
|
| 323 |
+
|
| 324 |
+
async function sendMessage() {
|
| 325 |
+
const text = userInput.value.trim();
|
| 326 |
+
if (!text || !currentChatId) return;
|
| 327 |
+
|
| 328 |
+
appendAndSaveMessage("user", text);
|
| 329 |
+
userInput.value = "";
|
| 330 |
+
|
| 331 |
+
try {
|
| 332 |
+
const response = await fetch("/analyze", {
|
| 333 |
+
method: "POST",
|
| 334 |
+
headers: { "Content-Type": "application/json" },
|
| 335 |
+
body: JSON.stringify({ text })
|
| 336 |
+
});
|
| 337 |
+
const data = await response.json();
|
| 338 |
+
appendAndSaveMessage("bot", `Эмоция: ${data.emotion} (${(data.confidence * 100).toFixed(1)}%)`);
|
| 339 |
+
} catch (error) {
|
| 340 |
+
console.error("Ошибка:", error);
|
| 341 |
+
appendAndSaveMessage("bot", `❌ Ошибка: ${error.message}`);
|
| 342 |
+
}
|
| 343 |
+
}
|
| 344 |
+
|
| 345 |
+
uploadBtn.addEventListener("click", async () => {
|
| 346 |
+
const file = audioFileInput.files[0];
|
| 347 |
+
if (!file || !currentChatId) return;
|
| 348 |
+
|
| 349 |
+
appendAndSaveMessage("user", "Загружен аудиофайл...");
|
| 350 |
+
|
| 351 |
+
try {
|
| 352 |
+
const formData = new FormData();
|
| 353 |
+
formData.append("audio", file);
|
| 354 |
+
|
| 355 |
+
const response = await fetch("/analyze_audio", {
|
| 356 |
+
method: "POST",
|
| 357 |
+
body: formData
|
| 358 |
+
});
|
| 359 |
+
const data = await response.json();
|
| 360 |
+
|
| 361 |
+
// Добавляем распознанный текст в чат
|
| 362 |
+
if (data.transcribed_text) {
|
| 363 |
+
appendAndSaveMessage("user", `Распознанный текст: ${data.transcribed_text}`);
|
| 364 |
+
}
|
| 365 |
+
|
| 366 |
+
appendAndSaveMessage("bot", `Эмоция: ${data.emotion} (${(data.confidence * 100).toFixed(1)}%)`);
|
| 367 |
+
} catch (error) {
|
| 368 |
+
console.error("Ошибка:", error);
|
| 369 |
+
appendAndSaveMessage("bot", `❌ Ошибка: ${error.message}`);
|
| 370 |
+
}
|
| 371 |
+
});
|
| 372 |
+
|
| 373 |
+
recordBtn.addEventListener("click", startRecording);
|
| 374 |
+
stopBtn.addEventListener("click", stopRecording);
|
| 375 |
+
|
| 376 |
+
async function startRecording() {
|
| 377 |
+
try {
|
| 378 |
+
audioStream = await navigator.mediaDevices.getUserMedia({ audio: true });
|
| 379 |
+
mediaRecorder = new MediaRecorder(audioStream);
|
| 380 |
+
audioChunks = [];
|
| 381 |
+
|
| 382 |
+
mediaRecorder.ondataavailable = e => audioChunks.push(e.data);
|
| 383 |
+
mediaRecorder.onstop = async () => {
|
| 384 |
+
try {
|
| 385 |
+
const audioBlob = new Blob(audioChunks, { type: "audio/wav" });
|
| 386 |
+
appendAndSaveMessage("user", "Отправлено голосовое сообщение...");
|
| 387 |
+
|
| 388 |
+
const formData = new FormData();
|
| 389 |
+
formData.append("audio", audioBlob, "recording.wav");
|
| 390 |
+
|
| 391 |
+
const response = await fetch("/analyze_audio", {
|
| 392 |
+
method: "POST",
|
| 393 |
+
body: formData
|
| 394 |
+
});
|
| 395 |
+
const data = await response.json();
|
| 396 |
+
|
| 397 |
+
// Добавляем распознанный текст в чат
|
| 398 |
+
if (data.transcribed_text) {
|
| 399 |
+
appendAndSaveMessage("user", `Распознанный текст: ${data.transcribed_text}`);
|
| 400 |
+
}
|
| 401 |
+
|
| 402 |
+
appendAndSaveMessage("bot", `Эмоция: ${data.emotion} (${(data.confidence * 100).toFixed(1)}%)`);
|
| 403 |
+
} catch (error) {
|
| 404 |
+
console.error("Ошибка:", error);
|
| 405 |
+
appendAndSaveMessage("bot", `❌ Ошибка: ${error.message}`);
|
| 406 |
+
} finally {
|
| 407 |
+
audioStream.getTracks().forEach(track => track.stop());
|
| 408 |
+
}
|
| 409 |
+
};
|
| 410 |
+
|
| 411 |
+
mediaRecorder.start();
|
| 412 |
+
recordBtn.disabled = true;
|
| 413 |
+
stopBtn.disabled = false;
|
| 414 |
+
} catch (error) {
|
| 415 |
+
console.error("Ошибка записи:", error);
|
| 416 |
+
appendMessage("bot", "❌ Не удалось получить доступ к микрофону");
|
| 417 |
+
}
|
| 418 |
+
}
|
| 419 |
+
|
| 420 |
+
function stopRecording() {
|
| 421 |
+
if (mediaRecorder?.state === "recording") {
|
| 422 |
+
mediaRecorder.stop();
|
| 423 |
+
recordBtn.disabled = false;
|
| 424 |
+
stopBtn.disabled = true;
|
| 425 |
+
}
|
| 426 |
+
}
|
| 427 |
+
|
| 428 |
+
function appendMessage(sender, text) {
|
| 429 |
+
const message = document.createElement("div");
|
| 430 |
+
message.className = `message ${sender}-message`;
|
| 431 |
+
message.innerHTML = text;
|
| 432 |
+
chatBox.appendChild(message);
|
| 433 |
+
chatBox.scrollTop = chatBox.scrollHeight;
|
| 434 |
+
}
|
| 435 |
+
|
| 436 |
function appendAndSaveMessage(sender, text) {
|
| 437 |
appendMessage(sender, text);
|
| 438 |
|