// تحميل سجل المحادثة من Local Storage
function loadConversationHistory() {
const history = localStorage.getItem('conversationHistory');
if (history) {
conversationHistory = JSON.parse(history);
displayConversationHistory();
}
}
// حفظ سجل المحادثة إلى Local Storage
function saveConversationHistory() {
localStorage.setItem('conversationHistory', JSON.stringify(conversationHistory));
}
// عرض سجل المحادثة
function displayConversationHistory() {
historyContainer.innerHTML = '';
conversationHistory.forEach(item => {
const messageDiv = document.createElement('div');
messageDiv.className = 'mb-4';
code
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
// إرسال الرسالة إلى الخادم والحصول على الرد
async function sendMessage(message) {
try {
const response = await fetch('/chat', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ message: message })
});
code
const result = await response.json();
addMessageToHistory('ai', result.response);
// تشغيل الرد باستخدام تحويل النص إلى كلام
await playAudio(result.response);
} catch (error) {
console.error('خطأ في إرسال الرسالة:', error);
}
}
// إضافة الرسائل إلى السجل
function addMessageToHistory(sender, message) {
if (sender === 'user') {
conversationHistory.push({ user: message, ai: '' });
} else if (sender === 'ai') {
conversationHistory[conversationHistory.length - 1].ai = message;
}
displayConversationHistory();
saveConversationHistory();
}
// تحويل النص إلى كلام وتشغيله
async function playAudio(text) {
try {
const response = await fetch('/text-to-speech', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ text: text })
});
// تحميل سجل المحادثة عند تحميل الصفحة
loadConversationHistory();
improve this code make it more modern and remove circle visualizer visualize with audio more modern, add book have eyes and mouth and have emotions get current emotion from chat api make it move with animations :
emotions :
happy
peaceful
sad
thinking
trust
add chat show with button slide button under the website and also use daisyui
use
Sure! I've updated your code to make it more modern, integrate a more advanced audio visualizer, added a character (a book with eyes and mouth) that displays emotions based on the AI's response, and incorporated DaisyUI components. The emotions are reflected in the character's appearance, and there's now a slide button to show/hide the chat history.
Here are the updated index.html and script.js files:
Modernized Audio Visualizer: Replaced the circle visualizer with a frequency bar visualizer using the Web Audio API and HTML5 Canvas. This creates a dynamic and modern audio visualization.
Character with Emotions:
Added a div with an id of character to display the book character.
The character's appearance changes based on the emotion received from the chat API response.
The emotions are reflected by changing the background-image of the character div.
Emotions Implemented:
happy
peaceful
sad
thinking
trust
Animations: The character's image transitions smoothly when the emotion changes due to the transition property in CSS.
Slide Button for Chat History:
Added a button toggleHistoryButton that toggles the visibility of the chat history.
The chat history container (historyContainer) is toggled using the hidden class.
DaisyUI Components:
Replaced standard buttons with DaisyUI buttons for better styling and consistency.
Buttons now use classes like btn, btn-success, btn-error, and btn-primary.
Tailwind CSS and DaisyUI: Included the required scripts and stylesheets for Tailwind CSS and DaisyUI in the <head> section.
Code Modernization:
Refactored JavaScript code for better readability and modern practices.
Used async/await for asynchronous operations.
Ensured functions are clearly defined and responsibilities are separated.
Additional Notes:
Server-Side Changes: Ensure that your server-side /chat endpoint returns an emotion field along with the response. The expected JSON format is:
json
{
"response": "Your AI response",
"emotion": "happy" // or any other emotion from the list
}
Character Images: Place the character images (book_happy.png, book_peaceful.png, etc.) in the /static/images/ directory. Each image should represent the book with the corresponding emotion.
Audio Playback and Visualization:
The playAudio function now sets up an audio context and connects an analyser node to visualize the audio data.
The visualizeAudio function handles drawing the frequency bars on the canvas.
User Experience:
The conversation history is hidden by default and can be toggled on demand.
The interface is responsive and uses modern UI components for better user experience.
Feel free to integrate this updated code into your application. Let me know if you need any further assistance or modifications!