test11 / index.html
joermd's picture
Update index.html
3f5d3d8 verified
raw
history blame
6.12 kB
<!DOCTYPE html>
<html dir="rtl" lang="ar">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>تطبيق المحادثة الصوتية</title>
<style>
body {
font-family: Arial, sans-serif;
max-width: 800px;
margin: 0 auto;
padding: 20px;
background-color: #f0f2f5;
}
.chat-container {
background-color: white;
border-radius: 10px;
padding: 20px;
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
}
.controls {
display: flex;
gap: 10px;
margin: 20px 0;
}
button {
padding: 10px 20px;
border: none;
border-radius: 5px;
background-color: #0084ff;
color: white;
cursor: pointer;
}
button:disabled {
background-color: #ccc;
}
.messages {
display: flex;
flex-direction: column;
gap: 10px;
margin-top: 20px;
}
.message {
padding: 10px;
border-radius: 10px;
max-width: 80%;
}
.user-message {
background-color: #e3f2fd;
align-self: flex-end;
}
.bot-message {
background-color: #f5f5f5;
align-self: flex-start;
}
#status {
margin-top: 10px;
color: #666;
}
</style>
</head>
<body>
<div class="chat-container">
<div class="controls">
<button id="startRecording">ابدأ التسجيل</button>
<button id="stopRecording" disabled>أوقف التسجيل</button>
</div>
<div id="status">جاهز للتسجيل...</div>
<div class="messages" id="messages"></div>
</div>
<script>
const API_URL = 'https://8o1mzdgh9f40t4-7777.proxy.runpod.net/proxy/8000/chat';
let mediaRecorder;
let audioChunks = [];
const startButton = document.getElementById('startRecording');
const stopButton = document.getElementById('stopRecording');
const statusDiv = document.getElementById('status');
const messagesDiv = document.getElementById('messages');
// إضافة رسالة إلى المحادثة
function addMessage(text, isUser) {
const messageDiv = document.createElement('div');
messageDiv.className = `message ${isUser ? 'user-message' : 'bot-message'}`;
messageDiv.textContent = text;
messagesDiv.appendChild(messageDiv);
messagesDiv.scrollTop = messagesDiv.scrollHeight;
}
// تحويل النص إلى كلام
async function speakText(text) {
const utterance = new SpeechSynthesisUtterance(text);
utterance.lang = 'ar-SA';
speechSynthesis.speak(utterance);
}
// إرسال النص إلى النموذج
async function sendToModel(text) {
try {
const response = await fetch(API_URL, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ message: text })
});
const data = await response.json();
const botResponse = data.response || 'عذراً، حدث خطأ في الاستجابة';
addMessage(botResponse, false);
speakText(botResponse);
} catch (error) {
console.error('Error:', error);
addMessage('عذراً، حدث خطأ في الاتصال بالنموذج', false);
}
}
startButton.onclick = async () => {
try {
const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
mediaRecorder = new MediaRecorder(stream);
mediaRecorder.ondataavailable = (event) => {
audioChunks.push(event.data);
};
mediaRecorder.onstop = async () => {
const audioBlob = new Blob(audioChunks, { type: 'audio/wav' });
audioChunks = [];
// تحويل الصوت إلى نص باستخدام Web Speech API
const recognition = new webkitSpeechRecognition();
recognition.lang = 'ar-SA';
recognition.continuous = false;
recognition.interimResults = false;
recognition.onresult = (event) => {
const text = event.results[0][0].transcript;
addMessage(text, true);
sendToModel(text);
};
recognition.onerror = (event) => {
console.error('Speech recognition error:', event.error);
statusDiv.textContent = 'حدث خطأ في التعرف على الكلام';
};
recognition.start();
};
mediaRecorder.start();
startButton.disabled = true;
stopButton.disabled = false;
statusDiv.textContent = 'جارِ التسجيل...';
} catch (error) {
console.error('Error:', error);
statusDiv.textContent = 'حدث خطأ في الوصول إلى الميكروفون';
}
};
stopButton.onclick = () => {
mediaRecorder.stop();
mediaRecorder.stream.getTracks().forEach(track => track.stop());
startButton.disabled = false;
stopButton.disabled = true;
statusDiv.textContent = 'تم إيقاف التسجيل، جارِ معالجة الكلام...';
};
</script>
</body>
</html>