index.html: <!DOCTYPE html> <html lang="ar"> <head> <meta charset="UTF-8"> <meta…
- User
index.html:
<!-- مرئي التحدث --> <div id="visualizerContainer" class="mb-8"> <canvas id="visualizer"></canvas> <div id="circleVisualizer" class="circle hidden"></div> </div> <!-- التحكم في المحادثة --> <div class="w-2/3 flex flex-col items-center"> <button id="talkButton" onclick="toggleChat()" class="bg-green-500 px-6 py-3 mt-6 rounded hover:bg-green-700 w-full"> 🎤 ابدأ المحادثة </button> <button id="stopButton" onclick="stopRecognition()" class="bg-red-500 px-6 py-3 mt-4 rounded hover:bg-red-700 w-full hidden"> ⛔ إيقاف المحادثة </button> </div> <!-- سجل المحادثة --> <div id="historyContainer" class="mt-8 p-4 bg-gray-800 rounded w-2/3 overflow-auto max-h-96"> <!-- ستتم إضافة الرسائل هنا --> </div> <script src="/static/script.js" defer></script>script.js: const historyContainer = document.getElementById('historyContainer'); const talkButton = document.getElementById('talkButton'); const stopButton = document.getElementById('stopButton'); const circleVisualizer = document.getElementById('circleVisualizer');
let recognition; let isListening = false;
// سجل المحادثة let conversationHistory = [];
// تحميل سجل المحادثة من 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';
// رسالة المستخدم const userMessageDiv = document.createElement('div'); userMessageDiv.className = 'text-right'; const userMessageSpan = document.createElement('span'); userMessageSpan.className = 'user-message'; userMessageSpan.textContent = item.user; userMessageDiv.appendChild(userMessageSpan); // رسالة الذكاء الاصطناعي const aiMessageDiv = document.createElement('div'); aiMessageDiv.className = 'text-left'; const aiMessageSpan = document.createElement('span'); aiMessageSpan.className = 'ai-message'; aiMessageSpan.textContent = item.ai; aiMessageDiv.appendChild(aiMessageSpan); messageDiv.appendChild(userMessageDiv); messageDiv.appendChild(aiMessageDiv); historyContainer.appendChild(messageDiv); }); // التمرير إلى الأسفل historyContainer.scrollTop = historyContainer.scrollHeight;}
// إعداد Web Speech API function initRecognition() { recognition = new (window.SpeechRecognition || window.webkitSpeechRecognition)(); recognition.lang = 'ar-SA'; recognition.interimResults = false; recognition.maxAlternatives = 1;
recognition.onstart = function() { circleVisualizer.classList.remove('hidden'); }; recognition.onend = function() { circleVisualizer.classList.add('hidden'); }; recognition.onresult = async function(event) { const transcript = event.results[0][0].transcript; addMessageToHistory('user', transcript); await sendMessage(transcript); if (isListening) { recognition.start(); } }; recognition.onerror = function(event) { console.error('خطأ في التعرف على الكلام:', event.error); if (isListening) { recognition.start(); } };}
// بدء المحادثة async function toggleChat() { isListening = true; talkButton.classList.add('hidden'); stopButton.classList.remove('hidden');
if (!recognition) initRecognition(); recognition.start();}
// إيقاف المحادثة function stopRecognition() { isListening = false; recognition.stop(); talkButton.classList.remove('hidden'); stopButton.classList.add('hidden'); }
// إرسال الرسالة إلى الخادم والحصول على الرد async function sendMessage(message) { try { const response = await fetch('/chat', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ message: message }) });
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 }) });
const audioBlob = await response.blob(); const audioUrl = URL.createObjectURL(audioBlob); const audio = new Audio(audioUrl); audio.onplay = function() { circleVisualizer.classList.remove('hidden'); }; audio.onended = function() { circleVisualizer.classList.add('hidden'); if (isListening) { recognition.start(); } }; await audio.play(); } catch (error) { console.error('خطأ في تشغيل الصوت:', error); }}
// تحميل سجل المحادثة عند تحميل الصفحة loadConversationHistory();
improve this code make it more modern and remove circle visualizer visualize with audio more modern, add face on 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 and also make it move his mouth when talk add chat show with button slide button under the website and also use daisyui ,modern ui
use
- Assistant
Certainly! Below is the updated code for your application. I've made the following improvements based on your requirements:
- Modernized the UI using Tailwind CSS and DaisyUI.
- Removed the circle visualizer and added a face with eyes, mouth, and emotions.
- The face expresses emotions based on the current emotion from the chat API.
- The face's mouth moves when talking.
- Added a slide button under the website to show/hide the chat history.
Updated
index.html:<!DOCTYPE html> <html lang="ar" dir="rtl"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>محادثة الذكاء الاصطناعي</title> <!-- Tailwind CSS & DaisyUI --> <script src="https://cdn.tailwindcss.com"></script> <link href="https://cdn.jsdelivr.net/npm/daisyui@4.12.14/dist/full.min.css" rel="stylesheet"> <style> @import url('https://fonts.googleapis.com/css2?family=Amiri:wght@400;700&display=swap'); body { font-family: 'Amiri', serif; background-color: #1a1a1a; } .face { width: 200px; height: 200px; background-color: #FFDAB9; border-radius: 50%; position: relative; margin: 0 auto; } .eye { width: 20px; height: 20px; background-color: #000; border-radius: 50%; position: absolute; top: 60px; } .eye.left { right: 110px; } .eye.right { left: 110px; } .mouth { width: 100px; height: 50px; background-color: #000; border-radius: 0 0 50px 50px; position: absolute; bottom: 60px; left: 50%; transform: translateX(-50%); overflow: hidden; } .mouth.talking { animation: talk 0.5s infinite; } @keyframes talk { 0% { height: 50px; } 50% { height: 20px; } 100% { height: 50px; } } /* Emotions */ .face.happy .mouth { height: 80px; border-radius: 50%/0 0 100px 100px; } .face.sad .mouth { height: 20px; border-radius: 50%/100px 100px 0 0; top: 140px; } .face.thinking .eye::before { content: ''; width: 10px; height: 10px; background-color: #fff; border-radius: 50%; position: absolute; top: 5px; left: 5px; } .face.peaceful .eye { width: 30px; height: 5px; background-color: #000; border-radius: 50%; top: 80px; } .hidden { display: none; } /* Chat history toggle */ #historyContainer { transition: max-height 0.5s ease-in-out; } </style> </head> <body class="text-white flex flex-col items-center justify-center min-h-screen"> <h1 class="text-4xl mb-8">محادثة الذكاء الاصطناعي</h1> <!-- الوجه مع تعابير --> <div id="faceContainer" class="mb-8"> <div id="face" class="face"> <div class="eye left"></div> <div class="eye right"></div> <div class="mouth"></div> </div> </div> <!-- التحكم في المحادثة --> <div class="w-full flex flex-col items-center px-4"> <button id="talkButton" onclick="toggleChat()" class="btn btn-success w-full mt-6"> 🎤 ابدأ المحادثة </button> <button id="stopButton" onclick="stopRecognition()" class="btn btn-error w-full mt-4 hidden"> ⛔ إيقاف المحادثة </button> <!-- زر إظهار/إخفاء سجل المحادثة --> <button id="toggleHistoryButton" onclick="toggleHistory()" class="btn btn-secondary w-full mt-4"> 📜 عرض المحادثة </button> </div> <!-- سجل المحادثة --> <div id="historyContainer" class="mt-4 p-4 bg-gray-800 rounded w-full overflow-auto max-h-96 hidden"> <!-- ستتم إضافة الرسائل هنا --> </div> <script src="/static/script.js" defer></script> </body> </html>
Updated
script.js:const historyContainer = document.getElementById('historyContainer'); const talkButton = document.getElementById('talkButton'); const stopButton = document.getElementById('stopButton'); const face = document.getElementById('face'); const mouth = face.querySelector('.mouth'); const toggleHistoryButton = document.getElementById('toggleHistoryButton'); let recognition; let isListening = false; // سجل المحادثة let conversationHistory = []; // تحميل سجل المحادثة من 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'; // رسالة المستخدم const userMessageDiv = document.createElement('div'); userMessageDiv.className = 'text-right'; const userMessageSpan = document.createElement('span'); userMessageSpan.className = 'user-message bg-blue-600 text-white p-2 rounded-lg inline-block'; userMessageSpan.textContent = item.user; userMessageDiv.appendChild(userMessageSpan); // رسالة الذكاء الاصطناعي const aiMessageDiv = document.createElement('div'); aiMessageDiv.className = 'text-left'; const aiMessageSpan = document.createElement('span'); aiMessageSpan.className = 'ai-message bg-gray-600 text-white p-2 rounded-lg inline-block'; aiMessageSpan.textContent = item.ai; aiMessageDiv.appendChild(aiMessageSpan); messageDiv.appendChild(userMessageDiv); messageDiv.appendChild(aiMessageDiv); historyContainer.appendChild(messageDiv); }); // التمرير إلى الأسفل historyContainer.scrollTop = historyContainer.scrollHeight; } // إعداد Web Speech API function initRecognition() { recognition = new (window.SpeechRecognition || window.webkitSpeechRecognition)(); recognition.lang = 'ar-SA'; recognition.interimResults = false; recognition.maxAlternatives = 1; recognition.onstart = function() { console.log('بدء الاستماع...'); }; recognition.onend = function() { console.log('تم إيقاف الاستماع.'); }; recognition.onresult = async function(event) { const transcript = event.results[0][0].transcript; addMessageToHistory('user', transcript); await sendMessage(transcript); if (isListening) { recognition.start(); } }; recognition.onerror = function(event) { console.error('خطأ في التعرف على الكلام:', event.error); if (isListening) { recognition.start(); } }; } // بدء المحادثة async function toggleChat() { isListening = true; talkButton.classList.add('hidden'); stopButton.classList.remove('hidden'); if (!recognition) initRecognition(); recognition.start(); } // إيقاف المحادثة function stopRecognition() { isListening = false; recognition.stop(); talkButton.classList.remove('hidden'); stopButton.classList.add('hidden'); } // إرسال الرسالة إلى الخادم والحصول على الرد async function sendMessage(message) { try { const response = await fetch('/chat', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ message: message }) }); const result = await response.json(); addMessageToHistory('ai', result.response); // تحديث تعبير الوجه updateEmotion(result.emotion || 'thinking'); // تشغيل الرد باستخدام تحويل النص إلى كلام 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 }) }); const audioBlob = await response.blob(); const audioUrl = URL.createObjectURL(audioBlob); const audio = new Audio(audioUrl); audio.onplay = function() { mouth.classList.add('talking'); }; audio.onended = function() { mouth.classList.remove('talking'); if (isListening) { recognition.start(); } }; await audio.play(); } catch (error) { console.error('خطأ في تشغيل الصوت:', error); } } // تحديث تعابير الوجه بناءً على العاطفة function updateEmotion(emotion) { face.classList.remove('happy', 'sad', 'thinking', 'peaceful', 'trust'); face.classList.add(emotion); } // إظهار/إخفاء سجل المحادثة function toggleHistory() { historyContainer.classList.toggle('hidden'); if (historyContainer.classList.contains('hidden')) { toggleHistoryButton.textContent = '📜 عرض المحادثة'; } else { toggleHistoryButton.textContent = '📕 إخفاء المحادثة'; } } // تحميل سجل المحادثة عند تحميل الصفحة loadConversationHistory();
Notes:
-
Face and Emotions: The face is a simple circle with eyes and a mouth. The face's class is updated based on the emotion received from the chat API (
updateEmotionfunction). You can customize the emotions by modifying the CSS classes:.face.happy.face.sad.face.thinking.face.peaceful.face.trust
-
Mouth Movement: The mouth has a
talkingclass that triggers an animation when the AI is speaking. -
Chat History Toggle: A button (
toggleHistoryButton) is added to show/hide the chat history. The history container's visibility is toggled, and the button text updates accordingly. -
Modern UI with DaisyUI: Buttons and UI components use DaisyUI classes for a modern look.
-
Tailwind CSS and DaisyUI Scripts: Included in the
<head>section as per your request.
Feel free to further customize the face's appearance and animations. You can replace the simple CSS-based face with more complex SVG graphics or even images if desired. Also, ensure that your backend API (
/chat) returns theemotionproperty based on the AI's response so that the face updates correctly.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.