joermd commited on
Commit
27fbe7f
·
verified ·
1 Parent(s): 20709bb

Update index.html

Browse files
Files changed (1) hide show
  1. index.html +79 -201
index.html CHANGED
@@ -3,248 +3,126 @@
3
  <head>
4
  <meta charset="UTF-8">
5
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
- <title>تطبيق المحادثة الصوتية</title>
7
  <style>
8
  body {
9
  font-family: Arial, sans-serif;
10
- max-width: 800px;
11
- margin: 0 auto;
12
- padding: 20px;
13
- background-color: #f0f2f5;
14
  }
15
- .chat-container {
16
- background-color: white;
17
- border-radius: 10px;
 
18
  padding: 20px;
19
- box-shadow: 0 2px 10px rgba(0,0,0,0.1);
20
- }
21
- .controls {
22
- display: flex;
23
- gap: 10px;
24
- margin: 20px 0;
25
  }
26
  button {
27
- padding: 10px 20px;
 
28
  border: none;
 
29
  border-radius: 5px;
30
- background-color: #0084ff;
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;
58
- align-self: flex-end;
59
- }
60
- .bot-message {
61
- background-color: #f5f5f5;
62
- align-self: flex-start;
63
- }
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>
87
  <body>
88
- <div class="chat-container">
89
- <div class="controls">
90
- <button id="startRecording">ابدأ التسجيل</button>
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'}`;
133
- messageDiv.textContent = text;
134
- messagesDiv.appendChild(messageDiv);
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: {
155
- 'Content-Type': 'application/json',
156
  },
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 });
188
- mediaRecorder = new MediaRecorder(stream);
189
-
190
- mediaRecorder.ondataavailable = (event) => {
191
- audioChunks.push(event.data);
192
- };
193
-
194
- mediaRecorder.onstop = async () => {
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;
203
-
204
- recognition.onresult = (event) => {
205
- const text = event.results[0][0].transcript;
206
- addMessage(text, true);
207
- sendToModel(text);
208
- };
209
-
210
- recognition.onerror = (event) => {
211
- console.error('Speech recognition error:', event.error);
212
- showError('حدث خطأ في التعرف على الكلام');
213
- };
214
-
215
- recognition.start();
216
- };
217
-
218
- mediaRecorder.start();
219
- startButton.disabled = true;
220
- stopButton.disabled = false;
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>
 
3
  <head>
4
  <meta charset="UTF-8">
5
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
+ <title>محادثة صوتية</title>
7
  <style>
8
  body {
9
  font-family: Arial, sans-serif;
10
+ margin: 20px;
11
+ background: #f0f2f5;
 
 
12
  }
13
+ .container {
14
+ max-width: 600px;
15
+ margin: 0 auto;
16
+ background: white;
17
  padding: 20px;
18
+ border-radius: 8px;
19
+ box-shadow: 0 2px 4px rgba(0,0,0,0.1);
 
 
 
 
20
  }
21
  button {
22
+ background: #0084ff;
23
+ color: white;
24
  border: none;
25
+ padding: 10px 20px;
26
  border-radius: 5px;
 
 
27
  cursor: pointer;
28
+ margin: 5px;
 
 
29
  }
30
  button:disabled {
31
+ background: #ccc;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32
  }
33
+ #transcription, #response {
34
+ margin: 20px 0;
35
+ padding: 15px;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36
  border-radius: 5px;
37
+ background: #f8f9fa;
38
  }
39
+ .label {
40
+ font-weight: bold;
41
+ color: #666;
42
+ margin-bottom: 5px;
 
 
 
43
  }
44
  </style>
45
  </head>
46
  <body>
47
+ <div class="container">
48
+ <button id="startBtn">ابدأ التحدث</button>
49
+ <button id="stopBtn" disabled>توقف</button>
50
+
51
+ <div id="transcription">
52
+ <div class="label">ما قلته:</div>
53
+ <div id="spokenText"></div>
54
+ </div>
55
+
56
+ <div id="response">
57
+ <div class="label">رد النموذج:</div>
58
+ <div id="modelResponse"></div>
59
  </div>
 
 
 
 
60
  </div>
61
 
62
  <script>
63
  const API_URL = 'https://8o1mzdgh9f40t4-7777.proxy.runpod.net/proxy/8000/chat';
64
+ const startBtn = document.getElementById('startBtn');
65
+ const stopBtn = document.getElementById('stopBtn');
66
+ const spokenText = document.getElementById('spokenText');
67
+ const modelResponse = document.getElementById('modelResponse');
68
+
69
+ // إعداد التعرف على الكلام
70
+ const recognition = new (window.SpeechRecognition || window.webkitSpeechRecognition)();
71
+ recognition.lang = 'ar-SA';
72
+ recognition.continuous = false;
73
+ recognition.interimResults = false;
74
+
75
+ recognition.onresult = async (event) => {
76
+ const text = event.results[0][0].transcript;
77
+ spokenText.textContent = text;
78
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
79
  try {
80
+ // إرسال النص إلى النموذج
81
  const response = await fetch(API_URL, {
82
  method: 'POST',
83
  headers: {
84
+ 'Content-Type': 'application/json'
85
  },
86
  body: JSON.stringify({ message: text })
87
  });
88
+
 
 
 
 
89
  const data = await response.json();
90
+ const botReply = data.response;
91
+ modelResponse.textContent = botReply;
 
 
 
 
 
 
 
92
 
93
+ // تحويل الرد إلى صوت
94
+ const utterance = new SpeechSynthesisUtterance(botReply);
95
+ utterance.lang = 'ar-SA';
96
+ speechSynthesis.speak(utterance);
97
+ } catch (error) {
98
+ modelResponse.textContent = 'حدث خطأ في الاتصال بالنموذج';
 
99
  }
100
  };
101
 
102
+ recognition.onerror = (event) => {
103
+ console.error('خطأ:', event.error);
104
+ spokenText.textContent = 'حدث خطأ في التعرف على الكلام';
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
105
  };
106
 
107
+ startBtn.onclick = () => {
108
+ recognition.start();
109
+ startBtn.disabled = true;
110
+ stopBtn.disabled = false;
111
+ spokenText.textContent = '';
112
+ modelResponse.textContent = '';
 
 
113
  };
114
 
115
+ stopBtn.onclick = () => {
116
+ recognition.stop();
117
+ startBtn.disabled = false;
118
+ stopBtn.disabled = true;
119
+ };
120
 
121
+ // عند انتهاء التعرف على الكلام
122
+ recognition.onend = () => {
123
+ startBtn.disabled = false;
124
+ stopBtn.disabled = true;
125
+ };
 
 
126
  </script>
127
  </body>
128
  </html>