Explore / Programming index.html: <!DOCTYPE html> <html lang="ar"> <head> <meta charset="UTF-8"> <meta… Save Share Download ▾ Reportindex.html:
code Copy code
<!-- مرئي التحدث -->
<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';
Show full message · 549 lines Assistant2024-11-13 13:51 UTC Copy response
Copy first prompt
Other models, same or similar prompt Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Details
Provider OpenAI
Model provenance As recorded by source dataset
Turns 1 (2 messages)
Language en
Conversation date Nov 13, 2024
Added to archive Nov 16, 2026
ID 01m7h428gzpbh537w1jqdssn4tSource & license Imported from WildChat-4.8M (AI2) (source ). Imported records earn no Research Points.
Upstream record: 868114efc9b8fd7ee30d4f6daf5457fe
Zhao et al., WildChat: 1M ChatGPT Interaction Logs in the Wild (ICLR 2024). Contains information from WildChat-4.8M, made available under the ODC Attribution License.
License: ODC-BY-1.0
// رسالة المستخدم
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;
code Copy code
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');
code Copy code
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 })
});
code Copy 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 })
});
code Copy code
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 make circle visualizer visualize with audio more modern, use three.js for audio visualizer
ok chat show with button slide button under the website and also use daisyui
use
use this
import * as THREE from 'three';
import {GUI} from 'dat.gui';
import {EffectComposer} from 'three/examples/jsm/postprocessing/EffectComposer';
import {RenderPass} from 'three/examples/jsm/postprocessing/RenderPass';
import {UnrealBloomPass} from 'three/examples/jsm/postprocessing/UnrealBloomPass';
import {OutputPass} from 'three/examples/jsm/postprocessing/OutputPass';
const renderer = new THREE.WebGLRenderer({antialias: true});
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
//renderer.setClearColor(0x222222);
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(
45,
window.innerWidth / window.innerHeight,
0.1,
1000
);
const params = {
red: 1.0,
green: 1.0,
blue: 1.0,
threshold: 0.5,
strength: 0.5,
radius: 0.8
}
renderer.outputColorSpace = THREE.SRGBColorSpace;
const renderScene = new RenderPass(scene, camera);
const bloomPass = new UnrealBloomPass(new THREE.Vector2(window.innerWidth, window.innerHeight));
bloomPass.threshold = params.threshold;
bloomPass.strength = params.strength;
bloomPass.radius = params.radius;
const bloomComposer = new EffectComposer(renderer);
bloomComposer.addPass(renderScene);
bloomComposer.addPass(bloomPass);
const outputPass = new OutputPass();
bloomComposer.addPass(outputPass);
camera.position.set(0, -2, 14);
camera.lookAt(0, 0, 0);
const uniforms = {
u_time: {type: 'f', value: 0.0},
u_frequency: {type: 'f', value: 0.0},
u_red: {type: 'f', value: 1.0},
u_green: {type: 'f', value: 1.0},
u_blue: {type: 'f', value: 1.0}
}
const mat = new THREE.ShaderMaterial({
uniforms,
vertexShader: document.getElementById('vertexshader').textContent,
fragmentShader: document.getElementById('fragmentshader').textContent
});
const geo = new THREE.IcosahedronGeometry(4, 30 );
const mesh = new THREE.Mesh(geo, mat);
scene.add(mesh);
mesh.material.wireframe = true;
const listener = new THREE.AudioListener();
camera.add(listener);
const sound = new THREE.Audio(listener);
const audioLoader = new THREE.AudioLoader();
audioLoader.load('./assets/Beats.mp3', function(buffer) {
sound.setBuffer(buffer);
window.addEventListener('click', function() {
sound.play();
});
});
const analyser = new THREE.AudioAnalyser(sound, 32);
const colorsFolder = gui.addFolder('Colors');
colorsFolder.add(params, 'red', 0, 1).onChange(function(value) {
uniforms.u_red.value = Number(value);
});
colorsFolder.add(params, 'green', 0, 1).onChange(function(value) {
uniforms.u_green.value = Number(value);
});
colorsFolder.add(params, 'blue', 0, 1).onChange(function(value) {
uniforms.u_blue.value = Number(value);
});
const bloomFolder = gui.addFolder('Bloom');
bloomFolder.add(params, 'threshold', 0, 1).onChange(function(value) {
bloomPass.threshold = Number(value);
});
bloomFolder.add(params, 'strength', 0, 3).onChange(function(value) {
bloomPass.strength = Number(value);
});
bloomFolder.add(params, 'radius', 0, 1).onChange(function(value) {
bloomPass.radius = Number(value);
});
let mouseX = 0;
let mouseY = 0;
document.addEventListener('mousemove', function(e) {
let windowHalfX = window.innerWidth / 2;
let windowHalfY = window.innerHeight / 2;
mouseX = (e.clientX - windowHalfX) / 100;
mouseY = (e.clientY - windowHalfY) / 100;
});
const clock = new THREE.Clock();
function animate() {
camera.position.x += (mouseX - camera.position.x) * .05;
camera.position.y += (-mouseY - camera.position.y) * 0.5;
camera.lookAt(scene.position);
uniforms.u_time.value = clock.getElapsedTime();
uniforms.u_frequency.value = analyser.getAverageFrequency();
bloomComposer.render();
requestAnimationFrame(animate);
}
animate();
window.addEventListener('resize', function() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
bloomComposer.setSize(window.innerWidth, window.innerHeight);
});
code Copy code
vec3 mod289(vec3 x)
{
return x - floor(x * (1.0 / 289.0)) * 289.0;
}
vec4 mod289(vec4 x)
{
return x - floor(x * (1.0 / 289.0)) * 289.0;
}
vec4 permute(vec4 x)
{
return mod289(((x*34.0)+10.0)*x);
}
vec4 taylorInvSqrt(vec4 r)
{
return 1.79284291400159 - 0.85373472095314 * r;
}
vec3 fade(vec3 t) {
return t*t*t*(t*(t*6.0-15.0)+10.0);
}
// Classic Perlin noise, periodic variant
float pnoise(vec3 P, vec3 rep)
{
vec3 Pi0 = mod(floor(P), rep); // Integer part, modulo period
vec3 Pi1 = mod(Pi0 + vec3(1.0), rep); // Integer part + 1, mod period
Pi0 = mod289(Pi0);
Pi1 = mod289(Pi1);
vec3 Pf0 = fract(P); // Fractional part for interpolation
vec3 Pf1 = Pf0 - vec3(1.0); // Fractional part - 1.0
vec4 ix = vec4(Pi0.x, Pi1.x, Pi0.x, Pi1.x);
vec4 iy = vec4(Pi0.yy, Pi1.yy);
vec4 iz0 = Pi0.zzzz;
vec4 iz1 = Pi1.zzzz;
vec4 ixy = permute(permute(ix) + iy);
vec4 ixy0 = permute(ixy + iz0);
vec4 ixy1 = permute(ixy + iz1);
vec4 gx0 = ixy0 * (1.0 / 7.0);
vec4 gy0 = fract(floor(gx0) * (1.0 / 7.0)) - 0.5;
gx0 = fract(gx0);
vec4 gz0 = vec4(0.5) - abs(gx0) - abs(gy0);
vec4 sz0 = step(gz0, vec4(0.0));
gx0 -= sz0 * (step(0.0, gx0) - 0.5);
gy0 -= sz0 * (step(0.0, gy0) - 0.5);
vec4 gx1 = ixy1 * (1.0 / 7.0);
vec4 gy1 = fract(floor(gx1) * (1.0 / 7.0)) - 0.5;
gx1 = fract(gx1);
vec4 gz1 = vec4(0.5) - abs(gx1) - abs(gy1);
vec4 sz1 = step(gz1, vec4(0.0));
gx1 -= sz1 * (step(0.0, gx1) - 0.5);
gy1 -= sz1 * (step(0.0, gy1) - 0.5);
vec3 g000 = vec3(gx0.x,gy0.x,gz0.x);
vec3 g100 = vec3(gx0.y,gy0.y,gz0.y);
vec3 g010 = vec3(gx0.z,gy0.z,gz0.z);
vec3 g110 = vec3(gx0.w,gy0.w,gz0.w);
vec3 g001 = vec3(gx1.x,gy1.x,gz1.x);
vec3 g101 = vec3(gx1.y,gy1.y,gz1.y);
vec3 g011 = vec3(gx1.z,gy1.z,gz1.z);
vec3 g111 = vec3(gx1.w,gy1.w,gz1.w);
vec4 norm0 = taylorInvSqrt(vec4(dot(g000, g000), dot(g010, g010), dot(g100, g100), dot(g110, g110)));
g000 *= norm0.x;
g010 *= norm0.y;
g100 *= norm0.z;
g110 *= norm0.w;
vec4 norm1 = taylorInvSqrt(vec4(dot(g001, g001), dot(g011, g011), dot(g101, g101), dot(g111, g111)));
g001 *= norm1.x;
g011 *= norm1.y;
g101 *= norm1.z;
g111 *= norm1.w;
float n000 = dot(g000, Pf0);
float n100 = dot(g100, vec3(Pf1.x, Pf0.yz));
float n010 = dot(g010, vec3(Pf0.x, Pf1.y, Pf0.z));
float n110 = dot(g110, vec3(Pf1.xy, Pf0.z));
float n001 = dot(g001, vec3(Pf0.xy, Pf1.z));
float n101 = dot(g101, vec3(Pf1.x, Pf0.y, Pf1.z));
float n011 = dot(g011, vec3(Pf0.x, Pf1.yz));
float n111 = dot(g111, Pf1);
vec3 fade_xyz = fade(Pf0);
vec4 n_z = mix(vec4(n000, n100, n010, n110), vec4(n001, n101, n011, n111), fade_xyz.z);
vec2 n_yz = mix(n_z.xy, n_z.zw, fade_xyz.y);
float n_xyz = mix(n_yz.x, n_yz.y, fade_xyz.x);
return 2.2 * n_xyz;
}
uniform float u_frequency;
void main() {
float noise = 3.0 * pnoise(position + u_time, vec3(10.0));
float displacement = (u_frequency / 30.) * (noise / 10.);
vec3 newPosition = position + normal * displacement;
gl_Position = projectionMatrix * modelViewMatrix * vec4(newPosition, 1.0);
}
</script>
<script id="fragmentshader" type="fragment">
uniform float u_red;
uniform float u_blue;
uniform float u_green;
void main() {
gl_FragColor = vec4(vec3(u_red, u_green, u_blue), 1. );
}
</script>
<script src="./js/scripts.js" type="module"></script>
Sure! Let's modernize your AI chat application by integrating Three.js for a sophisticated audio visualizer, utilizing DaisyUI for enhanced UI components, and refining the overall structure with Tailwind CSS . We'll also implement a sliding chat button for a more interactive user experience.
Below are the updated index.html and script.js files incorporating all these enhancements.
Updated index.html
html Copy code
<!DOCTYPE html>
<html lang="ar">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>محادثة الذكاء الاصطناعي</title>
<!-- Tailwind CSS -->
<script src="https://cdn.tailwindcss.com"></script>
<!-- DaisyUI -->
<script src="https://cdn.jsdelivr.net/npm/daisyui@4.12.14/dist/full.min.js"></script>
<link href="https://cdn.jsdelivr.net/npm/daisyui@4.12.14/dist/full.min.css" rel="stylesheet">
<!-- Three.js -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r170/three.min.js"></script>
<!-- Custom Styles -->
<style>
@import url('https://fonts.googleapis.com/css2?family=Amiri:wght@400;700&display=swap');
body {
font-family: 'Amiri', serif;
background-color: #1a1a1a;
overflow: hidden;
}
/* Container for Three.js Visualizer */
#visualizerContainer {
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
width: 100%;
height: 100%;
z-index: -1;
}
/* Chat History Styling */
#historyContainer {
background-color: rgba(55, 57, 63, 0.8);
border-radius: 1rem;
padding: 1rem;
width: 90%;
max-width: 800px;
height: 60vh;
overflow-y: auto;
}
/* Sliding Button Styles */
.slide-button-container {
position: fixed;
bottom: 0;
left: 50%;
transform: translateX(-50%);
width: 100%;
max-width: 400px;
padding: 1rem;
display: flex;
justify-content: center;
gap: 1rem;
}
</style>
</head>
<body class="text-white flex flex-col items-center justify-center min-h-screen relative">
<h1 class="text-4xl mb-8">محادثة الذكاء الاصطناعي</h1>
<!-- Three.js Visualizer -->
<div id="visualizerContainer"></div>
<!-- Chat History -->
<div id="historyContainer" class="mb-8">
<!-- الرسائل ستتم إضافتها هنا -->
</div>
<!-- Sliding Buttons -->
<div class="slide-button-container">
<button id="talkButton" onclick="toggleChat()" class="btn btn-success">
🎤 ابدأ المحادثة
</button>
<button id="stopButton" onclick="stopRecognition()" class="btn btn-error hidden">
⛔ إيقاف المحادثة
</button>
</div>
<!-- Shader Scripts -->
<script id="vertexshader" type="x-shader/x-vertex">
uniform float u_time;
uniform float u_frequency;
varying float vFrequency;
void main() {
vFrequency = u_frequency;
vec3 pos = position;
pos.y += sin(u_time + pos.x * 2.0) * 0.5;
gl_Position = projectionMatrix * modelViewMatrix * vec4(pos, 1.0);
}
</script>
<script id="fragmentshader" type="x-shader/x-fragment">
precision mediump float;
uniform float u_red;
uniform float u_green;
uniform float u_blue;
varying float vFrequency;
void main() {
gl_FragColor = vec4(u_red * vFrequency, u_green * vFrequency, u_blue * vFrequency, 1.0);
}
</script>
<!-- Main Script -->
<script src="/static/script.js" type="module"></script>
</body>
</html>
Key Enhancements:
Three.js Integration : Added a #visualizerContainer div to house the Three.js audio visualizer.
DaisyUI Components : Utilized DaisyUI buttons for the "Start" and "Stop" conversation actions, offering a more polished look.
Sliding Buttons : Positioned the chat control buttons at the bottom center with a sliding effect.
Shaders : Included vertex and fragment shaders for enhanced visual effects in the Three.js visualizer.
Responsive Design : Ensured the chat history and visualizer are responsive across various screen sizes.
Updated script.js
Key Enhancements:
Three.js Audio Visualizer :
Initialization : Set up a Three.js scene with a dynamic icosahedron that reacts to audio frequencies.
Shaders : Utilized vertex and fragment shaders for animated geometry and color manipulation based on audio input.
Animation Loop : Continuous rendering loop that updates the visualizer based on real-time audio data.
Responsive Design : Adjusts the renderer size on window resize events to maintain full-screen coverage.
Speech Recognition & Text-to-Speech :
Modern Syntax : Utilized async/await for cleaner asynchronous operations.
Error Handling : Improved error handling for network requests and speech recognition.
Conversation History : Displayed using DaisyUI badges for better visual separation of user and AI messages.
UI Enhancements :
DaisyUI Buttons : Replaced default buttons with DaisyUI-styled buttons (btn, btn-success, btn-error) for a more appealing look.
Sliding Button Container : Positioned the chat control buttons at the bottom center with some spacing for better accessibility.
Code Organization :
Modular Approach : Encapsulated the Three.js visualizer within a visualization object for better code management.
Comments : Added descriptive comments for better readability and maintainability.
Additional Notes
Server-Side Endpoints :
Ensure that your server has the following endpoints to handle chat and text-to-speech functionalities:
/chat : Accepts POST requests with a JSON body containing the user message and returns the AI response.
/text-to-speech : Accepts POST requests with a JSON body containing the text and returns an audio blob.
Three.js Audio Source :
In the provided script.js, a sample audio file from SoundHelix is loaded. Replace the URL with an actual audio source if necessary.
The visualizer is set to loop a silent audio initially. You might want to connect it to actual audio streams from speech recognition or text-to-speech for real-time visualization.
CORS & Security :
Make sure that your server is configured to handle CORS requests if your frontend and backend are on different domains.
Implement necessary security measures to protect your endpoints from unauthorized access.
Browser Compatibility :
The Web Speech API used for speech recognition may not be supported in all browsers. It's primarily supported in Chrome and Edge.
Test the application across different browsers to ensure consistent behavior.
Performance Optimization :
Three.js can be resource-intensive. Ensure that the visualizer runs smoothly by optimizing shader calculations and limiting the number of rendered objects if necessary.
Accessibility :
Consider adding accessibility features such as ARIA labels and keyboard navigation to enhance the user experience for all users.
By implementing these enhancements, your AI chat application will not only look more modern and visually appealing but also offer a more interactive and engaging user experience. Feel free to further customize the shaders and UI components to match your specific design preferences.
Show full message · 475 lines Copy first response
Not yet included in a published release.
// Import Three.js and necessary modules (Ensure your server supports ES Modules)
import * as THREE from 'three';
// Get DOM Elements
const historyContainer = document.getElementById('historyContainer');
const talkButton = document.getElementById('talkButton');
const stopButton = document.getElementById('stopButton');
// Speech Recognition Setup
let recognition;
let isListening = false;
// Conversation History
let conversationHistory = [];
// Load Conversation History from Local Storage
function loadConversationHistory() {
const history = localStorage.getItem('conversationHistory');
if (history) {
conversationHistory = JSON.parse(history);
displayConversationHistory();
}
}
// Save Conversation History to Local Storage
function saveConversationHistory() {
localStorage.setItem('conversationHistory', JSON.stringify(conversationHistory));
}
// Display Conversation History
function displayConversationHistory() {
historyContainer.innerHTML = '';
conversationHistory.forEach(item => {
const messageDiv = document.createElement('div');
messageDiv.className = 'mb-4';
// User Message
const userMessageDiv = document.createElement('div');
userMessageDiv.className = 'text-right';
const userMessageSpan = document.createElement('span');
userMessageSpan.className = 'badge badge-primary';
userMessageSpan.textContent = item.user;
userMessageDiv.appendChild(userMessageSpan);
// AI Message
const aiMessageDiv = document.createElement('div');
aiMessageDiv.className = 'text-left mt-2';
const aiMessageSpan = document.createElement('span');
aiMessageSpan.className = 'badge badge-secondary';
aiMessageSpan.textContent = item.ai;
aiMessageDiv.appendChild(aiMessageSpan);
messageDiv.appendChild(userMessageDiv);
messageDiv.appendChild(aiMessageDiv);
historyContainer.appendChild(messageDiv);
});
// Auto-scroll to the latest message
historyContainer.scrollTop = historyContainer.scrollHeight;
}
// Initialize Speech Recognition
function initRecognition() {
const SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition;
if (!SpeechRecognition) {
alert('متصفحك لا يدعم التعرف على الصوت.');
return;
}
recognition = new SpeechRecognition();
recognition.lang = 'ar-SA';
recognition.interimResults = false;
recognition.maxAlternatives = 1;
recognition.onstart = function() {
visualization.start();
};
recognition.onend = function() {
visualization.stop();
};
recognition.onresult = async function(event) {
const transcript = event.results[0][0].transcript.trim();
addMessageToHistory('user', transcript);
await sendMessage(transcript);
if (isListening) {
recognition.start();
}
};
recognition.onerror = function(event) {
console.error('خطأ في التعرف على الكلام:', event.error);
if (isListening) {
recognition.start();
}
};
}
// Start Chatting
function toggleChat() {
isListening = true;
talkButton.classList.add('hidden');
stopButton.classList.remove('hidden');
if (!recognition) initRecognition();
recognition.start();
}
// Stop Chatting
function stopRecognition() {
isListening = false;
recognition.stop();
talkButton.classList.remove('hidden');
stopButton.classList.add('hidden');
}
// Send Message to Server and Get AI Response
async function sendMessage(message) {
try {
const response = await fetch('/chat', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ message: message })
});
if (!response.ok) throw new Error('Network response was not ok');
const result = await response.json();
addMessageToHistory('ai', result.response);
// Play AI Response Audio
await playAudio(result.response);
} catch (error) {
console.error('خطأ في إرسال الرسالة:', error);
}
}
// Add Messages to History
function addMessageToHistory(sender, message) {
if (sender === 'user') {
conversationHistory.push({ user: message, ai: '' });
} else if (sender === 'ai') {
conversationHistory[conversationHistory.length - 1].ai = message;
}
displayConversationHistory();
saveConversationHistory();
}
// Text-to-Speech Functionality
async function playAudio(text) {
try {
const response = await fetch('/text-to-speech', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ text: text })
});
if (!response.ok) throw new Error('Network response was not ok');
const audioBlob = await response.blob();
const audioUrl = URL.createObjectURL(audioBlob);
const audio = new Audio(audioUrl);
audio.onplay = function() {
visualization.start();
};
audio.onended = function() {
visualization.stop();
if (isListening) {
recognition.start();
}
};
await audio.play();
} catch (error) {
console.error('خطأ في تشغيل الصوت:', error);
}
}
// Three.js Audio Visualizer Setup
const visualization = {
scene: null,
camera: null,
renderer: null,
mesh: null,
analyser: null,
uniforms: {},
animationId: null,
init: function() {
// Scene & Camera
this.scene = new THREE.Scene();
this.camera = new THREE.PerspectiveCamera(
75,
window.innerWidth / window.innerHeight,
0.1,
1000
);
this.camera.position.z = 50;
// Renderer
this.renderer = new THREE.WebGLRenderer({ alpha: true, antialias: true });
this.renderer.setSize(window.innerWidth, window.innerHeight);
document.getElementById('visualizerContainer').appendChild(this.renderer.domElement);
// Lighting
const light = new THREE.PointLight(0xffffff, 1);
light.position.set(50, 50, 50);
this.scene.add(light);
// Geometry & Material
const geometry = new THREE.IcosahedronGeometry(15, 1);
this.uniforms = {
u_time: { value: 1.0 },
u_frequency: { value: 0.0 },
u_red: { value: 1.0 },
u_green: { value: 1.0 },
u_blue: { value: 1.0 }
};
const material = new THREE.ShaderMaterial({
uniforms: this.uniforms,
vertexShader: document.getElementById('vertexshader').textContent,
fragmentShader: document.getElementById('fragmentshader').textContent,
wireframe: true
});
this.mesh = new THREE.Mesh(geometry, material);
this.scene.add(this.mesh);
// Audio Setup
const listener = new THREE.AudioListener();
this.camera.add(listener);
const sound = new THREE.Audio(listener);
const audioLoader = new THREE.AudioLoader();
// Using a silent buffer initially
audioLoader.load('https://www.soundhelix.com/examples/mp3/SoundHelix-Song-1.mp3', function(buffer) {
sound.setBuffer(buffer);
sound.setLoop(true);
sound.setVolume(0);
// sound.play(); // Play sound when actual audio is available
});
this.analyser = new THREE.AudioAnalyser(sound, 32);
// Handle Window Resize
window.addEventListener('resize', () => {
this.camera.aspect = window.innerWidth / window.innerHeight;
this.camera.updateProjectionMatrix();
this.renderer.setSize(window.innerWidth, window.innerHeight);
});
},
animate: function() {
this.animationId = requestAnimationFrame(() => this.animate());
this.uniforms.u_time.value += 0.05;
this.uniforms.u_frequency.value = this.analyser.getAverageFrequency() / 256;
this.mesh.rotation.x += 0.005;
this.mesh.rotation.y += 0.01;
this.renderer.render(this.scene, this.camera);
},
start: function() {
if (!this.scene) this.init();
if (!this.animationId) this.animate();
},
stop: function() {
if (this.animationId) {
cancelAnimationFrame(this.animationId);
this.animationId = null;
}
}
};
// Initialize Visualization
visualization.init();
// Load Conversation History on Page Load
loadConversationHistory();