File size: 6,122 Bytes
3f5d3d8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
<!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>