Update index.html
Browse files- index.html +96 -17
index.html
CHANGED
@@ -31,19 +31,27 @@
|
|
31 |
color: white;
|
32 |
cursor: pointer;
|
33 |
}
|
|
|
|
|
|
|
34 |
button:disabled {
|
35 |
background-color: #ccc;
|
|
|
36 |
}
|
37 |
.messages {
|
38 |
display: flex;
|
39 |
flex-direction: column;
|
40 |
gap: 10px;
|
41 |
margin-top: 20px;
|
|
|
|
|
|
|
42 |
}
|
43 |
.message {
|
44 |
padding: 10px;
|
45 |
border-radius: 10px;
|
46 |
max-width: 80%;
|
|
|
47 |
}
|
48 |
.user-message {
|
49 |
background-color: #e3f2fd;
|
@@ -56,6 +64,23 @@
|
|
56 |
#status {
|
57 |
margin-top: 10px;
|
58 |
color: #666;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
59 |
}
|
60 |
</style>
|
61 |
</head>
|
@@ -66,20 +91,42 @@
|
|
66 |
<button id="stopRecording" disabled>أوقف التسجيل</button>
|
67 |
</div>
|
68 |
<div id="status">جاهز للتسجيل...</div>
|
|
|
|
|
69 |
<div class="messages" id="messages"></div>
|
70 |
</div>
|
71 |
|
72 |
<script>
|
73 |
const API_URL = 'https://8o1mzdgh9f40t4-7777.proxy.runpod.net/proxy/8000/chat';
|
74 |
-
let mediaRecorder;
|
75 |
let audioChunks = [];
|
76 |
|
77 |
const startButton = document.getElementById('startRecording');
|
78 |
const stopButton = document.getElementById('stopRecording');
|
79 |
const statusDiv = document.getElementById('status');
|
80 |
const messagesDiv = document.getElementById('messages');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
81 |
|
82 |
-
// إضافة رسالة إلى المحادثة
|
83 |
function addMessage(text, isUser) {
|
84 |
const messageDiv = document.createElement('div');
|
85 |
messageDiv.className = `message ${isUser ? 'user-message' : 'bot-message'}`;
|
@@ -88,16 +135,20 @@
|
|
88 |
messagesDiv.scrollTop = messagesDiv.scrollHeight;
|
89 |
}
|
90 |
|
91 |
-
// تحويل النص إلى كلام
|
92 |
async function speakText(text) {
|
93 |
-
|
94 |
-
|
95 |
-
|
|
|
|
|
|
|
|
|
|
|
96 |
}
|
97 |
|
98 |
-
// إرسال النص إلى النموذج
|
99 |
async function sendToModel(text) {
|
100 |
try {
|
|
|
101 |
const response = await fetch(API_URL, {
|
102 |
method: 'POST',
|
103 |
headers: {
|
@@ -106,16 +157,31 @@
|
|
106 |
body: JSON.stringify({ message: text })
|
107 |
});
|
108 |
|
|
|
|
|
|
|
|
|
109 |
const data = await response.json();
|
110 |
const botResponse = data.response || 'عذراً، حدث خطأ في الاستجابة';
|
111 |
addMessage(botResponse, false);
|
112 |
speakText(botResponse);
|
|
|
113 |
} catch (error) {
|
114 |
console.error('Error:', error);
|
115 |
-
|
116 |
}
|
117 |
}
|
118 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
119 |
startButton.onclick = async () => {
|
120 |
try {
|
121 |
const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
|
@@ -129,8 +195,8 @@
|
|
129 |
const audioBlob = new Blob(audioChunks, { type: 'audio/wav' });
|
130 |
audioChunks = [];
|
131 |
|
132 |
-
|
133 |
-
const recognition = new
|
134 |
recognition.lang = 'ar-SA';
|
135 |
recognition.continuous = false;
|
136 |
recognition.interimResults = false;
|
@@ -143,7 +209,7 @@
|
|
143 |
|
144 |
recognition.onerror = (event) => {
|
145 |
console.error('Speech recognition error:', event.error);
|
146 |
-
|
147 |
};
|
148 |
|
149 |
recognition.start();
|
@@ -155,17 +221,30 @@
|
|
155 |
statusDiv.textContent = 'جارِ التسجيل...';
|
156 |
} catch (error) {
|
157 |
console.error('Error:', error);
|
158 |
-
|
159 |
}
|
160 |
};
|
161 |
|
162 |
stopButton.onclick = () => {
|
163 |
-
mediaRecorder.
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
|
|
|
|
|
168 |
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
169 |
</script>
|
170 |
</body>
|
171 |
</html>
|
|
|
31 |
color: white;
|
32 |
cursor: pointer;
|
33 |
}
|
34 |
+
button:hover {
|
35 |
+
background-color: #0066cc;
|
36 |
+
}
|
37 |
button:disabled {
|
38 |
background-color: #ccc;
|
39 |
+
cursor: not-allowed;
|
40 |
}
|
41 |
.messages {
|
42 |
display: flex;
|
43 |
flex-direction: column;
|
44 |
gap: 10px;
|
45 |
margin-top: 20px;
|
46 |
+
max-height: 400px;
|
47 |
+
overflow-y: auto;
|
48 |
+
padding: 10px;
|
49 |
}
|
50 |
.message {
|
51 |
padding: 10px;
|
52 |
border-radius: 10px;
|
53 |
max-width: 80%;
|
54 |
+
word-wrap: break-word;
|
55 |
}
|
56 |
.user-message {
|
57 |
background-color: #e3f2fd;
|
|
|
64 |
#status {
|
65 |
margin-top: 10px;
|
66 |
color: #666;
|
67 |
+
padding: 10px;
|
68 |
+
border-radius: 5px;
|
69 |
+
}
|
70 |
+
.error {
|
71 |
+
color: #d32f2f;
|
72 |
+
background-color: #ffebee;
|
73 |
+
padding: 10px;
|
74 |
+
border-radius: 5px;
|
75 |
+
margin-top: 10px;
|
76 |
+
}
|
77 |
+
#textInput {
|
78 |
+
width: 100%;
|
79 |
+
padding: 10px;
|
80 |
+
margin-top: 10px;
|
81 |
+
border: 1px solid #ddd;
|
82 |
+
border-radius: 5px;
|
83 |
+
font-size: 16px;
|
84 |
}
|
85 |
</style>
|
86 |
</head>
|
|
|
91 |
<button id="stopRecording" disabled>أوقف التسجيل</button>
|
92 |
</div>
|
93 |
<div id="status">جاهز للتسجيل...</div>
|
94 |
+
<textarea id="textInput" placeholder="أو اكتب رسالتك هنا..." rows="3"></textarea>
|
95 |
+
<button id="sendText">إرسال النص</button>
|
96 |
<div class="messages" id="messages"></div>
|
97 |
</div>
|
98 |
|
99 |
<script>
|
100 |
const API_URL = 'https://8o1mzdgh9f40t4-7777.proxy.runpod.net/proxy/8000/chat';
|
101 |
+
let mediaRecorder = null;
|
102 |
let audioChunks = [];
|
103 |
|
104 |
const startButton = document.getElementById('startRecording');
|
105 |
const stopButton = document.getElementById('stopRecording');
|
106 |
const statusDiv = document.getElementById('status');
|
107 |
const messagesDiv = document.getElementById('messages');
|
108 |
+
const textInput = document.getElementById('textInput');
|
109 |
+
const sendTextButton = document.getElementById('sendText');
|
110 |
+
|
111 |
+
// التحقق من دعم المتصفح للميزات المطلوبة
|
112 |
+
function checkBrowserSupport() {
|
113 |
+
if (!navigator.mediaDevices || !navigator.mediaDevices.getUserMedia) {
|
114 |
+
showError('متصفحك لا يدعم تسجيل الصوت. الرجاء استخدام متصفح حديث.');
|
115 |
+
startButton.disabled = true;
|
116 |
+
return false;
|
117 |
+
}
|
118 |
+
if (!window.SpeechRecognition && !window.webkitSpeechRecognition) {
|
119 |
+
showError('متصفحك لا يدعم التعرف على الكلام. الرجاء استخدام Google Chrome.');
|
120 |
+
startButton.disabled = true;
|
121 |
+
return false;
|
122 |
+
}
|
123 |
+
return true;
|
124 |
+
}
|
125 |
+
|
126 |
+
function showError(message) {
|
127 |
+
statusDiv.innerHTML = `<div class="error">${message}</div>`;
|
128 |
+
}
|
129 |
|
|
|
130 |
function addMessage(text, isUser) {
|
131 |
const messageDiv = document.createElement('div');
|
132 |
messageDiv.className = `message ${isUser ? 'user-message' : 'bot-message'}`;
|
|
|
135 |
messagesDiv.scrollTop = messagesDiv.scrollHeight;
|
136 |
}
|
137 |
|
|
|
138 |
async function speakText(text) {
|
139 |
+
try {
|
140 |
+
const utterance = new SpeechSynthesisUtterance(text);
|
141 |
+
utterance.lang = 'ar-SA';
|
142 |
+
speechSynthesis.speak(utterance);
|
143 |
+
} catch (error) {
|
144 |
+
console.error('Error speaking:', error);
|
145 |
+
showError('حدث خطأ في تحويل النص إلى كلام');
|
146 |
+
}
|
147 |
}
|
148 |
|
|
|
149 |
async function sendToModel(text) {
|
150 |
try {
|
151 |
+
statusDiv.textContent = 'جارِ إرسال الرسالة...';
|
152 |
const response = await fetch(API_URL, {
|
153 |
method: 'POST',
|
154 |
headers: {
|
|
|
157 |
body: JSON.stringify({ message: text })
|
158 |
});
|
159 |
|
160 |
+
if (!response.ok) {
|
161 |
+
throw new Error(`HTTP error! status: ${response.status}`);
|
162 |
+
}
|
163 |
+
|
164 |
const data = await response.json();
|
165 |
const botResponse = data.response || 'عذراً، حدث خطأ في الاستجابة';
|
166 |
addMessage(botResponse, false);
|
167 |
speakText(botResponse);
|
168 |
+
statusDiv.textContent = 'جاهز للتسجيل...';
|
169 |
} catch (error) {
|
170 |
console.error('Error:', error);
|
171 |
+
showError('حدث خطأ في الاتصال بالنموذج. الرجاء المحاولة مرة أخرى.');
|
172 |
}
|
173 |
}
|
174 |
|
175 |
+
// إرسال النص المكتوب
|
176 |
+
sendTextButton.onclick = () => {
|
177 |
+
const text = textInput.value.trim();
|
178 |
+
if (text) {
|
179 |
+
addMessage(text, true);
|
180 |
+
sendToModel(text);
|
181 |
+
textInput.value = '';
|
182 |
+
}
|
183 |
+
};
|
184 |
+
|
185 |
startButton.onclick = async () => {
|
186 |
try {
|
187 |
const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
|
|
|
195 |
const audioBlob = new Blob(audioChunks, { type: 'audio/wav' });
|
196 |
audioChunks = [];
|
197 |
|
198 |
+
const SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition;
|
199 |
+
const recognition = new SpeechRecognition();
|
200 |
recognition.lang = 'ar-SA';
|
201 |
recognition.continuous = false;
|
202 |
recognition.interimResults = false;
|
|
|
209 |
|
210 |
recognition.onerror = (event) => {
|
211 |
console.error('Speech recognition error:', event.error);
|
212 |
+
showError('حدث خطأ في التعرف على الكلام');
|
213 |
};
|
214 |
|
215 |
recognition.start();
|
|
|
221 |
statusDiv.textContent = 'جارِ التسجيل...';
|
222 |
} catch (error) {
|
223 |
console.error('Error:', error);
|
224 |
+
showError('حدث خطأ في الوصول إلى الميكروفون. تأكد من السماح للموقع باستخدام الميكروفون.');
|
225 |
}
|
226 |
};
|
227 |
|
228 |
stopButton.onclick = () => {
|
229 |
+
if (mediaRecorder && mediaRecorder.state === 'recording') {
|
230 |
+
mediaRecorder.stop();
|
231 |
+
mediaRecorder.stream.getTracks().forEach(track => track.stop());
|
232 |
+
startButton.disabled = false;
|
233 |
+
stopButton.disabled = true;
|
234 |
+
statusDiv.textContent = 'تم إيقاف التسجيل، جارِ معالجة الكلام...';
|
235 |
+
}
|
236 |
};
|
237 |
+
|
238 |
+
// التحقق من دعم المتصفح عند تحميل الصفحة
|
239 |
+
window.onload = checkBrowserSupport;
|
240 |
+
|
241 |
+
// إضافة دعم للإرسال بضغط Enter
|
242 |
+
textInput.addEventListener('keypress', (e) => {
|
243 |
+
if (e.key === 'Enter' && !e.shiftKey) {
|
244 |
+
e.preventDefault();
|
245 |
+
sendTextButton.click();
|
246 |
+
}
|
247 |
+
});
|
248 |
</script>
|
249 |
</body>
|
250 |
</html>
|