// تحميل سجل المحادثة من 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! Below is an improved version of your index.html and script.js files incorporating modern design practices, DaisyUI for enhanced UI components, a book visualizer with animated eyes and mouth reflecting emotions, and a sliding chat interface activated by a button at the bottom of the website.
Overview of Improvements:
Modern Styling with Tailwind CSS and DaisyUI: Incorporated the latest Tailwind CSS and DaisyUI for responsive and aesthetically pleasing components.
Book Visualizer: Replaced the circular visualizer with a book that has animated eyes and mouth to convey different emotions (happy, peaceful, sad, thinking, trust) based on AI responses.
Sliding Chat Interface: Added a chat interface that slides up from the bottom when a button is clicked, enhancing user interaction.
Enhanced JavaScript: Updated the JavaScript to handle emotion states and control the book's animations accordingly.
Created a #bookVisualizer div representing the book.
Added .eye and .mouth elements inside the book for animations.
Defined CSS classes for different emotions affecting the mouth's appearance.
Chat Interface:
Added a floating chat button (#chatButton) fixed at the bottom-right corner.
Created a #chatModal that slides up when the chat button is clicked.
The chat modal includes a header with a close button, a history container, and an input section for sending messages.
Styling:
Added animations for blinking eyes using CSS keyframes.
Defined styles for different emotion states that the book can display.
Updated script.js
javascript
Explanation of Changes:
Event Listener for DOM Content Loaded:
Ensures that the script runs after the DOM is fully loaded.
Elements Selection:
Selected elements for chat history, buttons, modal, input field, and book visualizer.
Web Speech API Initialization:
Initializes the Web Speech API for speech recognition.
Handles recognition start, end, result, and error events.
Converts speech to text and sends it to the server.
Messaging Functions:
sendMessage: Sends user message to the server and awaits AI response.
addMessageToHistory: Adds messages to the conversation history and updates the UI.
displayConversationHistory: Renders the conversation history in the chat modal.
Emotion Handling:
updateEmotion: Updates the book visualizer's emotion based on AI's response.
Removes previous emotion classes and adds the current emotion class.
Chat Interface Controls:
Event listeners for opening and closing the chat modal.
Start listening for speech when chat opens.
Stop recognition when chat closes.
Handle sending messages via button click or pressing Enter.
Error Handling:
Handles network or server errors gracefully by displaying an error message in the chat and setting a default sad emotion.
Additional Notes:
Book Image:
Replace 'https://path-to-your-book-image.png' in the #bookVisualizer background image URL with the actual path to your book image.
Ensure the book image has areas where eyes and mouth can be overlaid appropriately.
AI Response Structure:
The script assumes that the AI's response from the /chat endpoint includes an emotion field, e.g.:
json
{
"message": "Your response message here.",
"emotion": "happy" // Possible values: happy, peaceful, sad, thinking, trust
}
Ensure your backend API sends responses in this format to facilitate emotion handling.
Responsive Design:
The chat modal is designed to be responsive and should work well on different screen sizes. Adjust the max-h-4/5 and other Tailwind classes if needed to better fit your design preferences.
Accessibility:
Consider adding ARIA attributes and enhancing keyboard navigation for improved accessibility.
Animations and Enhancements:
Feel free to add more animations or enhance existing ones to make the book visualizer more interactive and engaging.
Security:
Ensure that your backend API endpoints (/chat and /text-to-speech) are secure and handle inputs appropriately to prevent security vulnerabilities.
By implementing these changes, your AI chat interface will have a modern look and feel, with interactive elements that provide visual feedback based on the AI's emotions, enhancing user engagement and experience.