File size: 8,848 Bytes
3d055a1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17ecdd8
3d055a1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8a4ec28
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3d055a1
 
 
 
 
 
 
 
 
 
 
 
8a4ec28
 
 
 
 
 
 
3d055a1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b5d69b2
 
3d055a1
 
 
 
 
 
 
 
 
 
b5d69b2
 
3d055a1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8a4ec28
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b5d69b2
 
8a4ec28
 
 
 
 
 
 
 
 
 
 
 
 
 
3d055a1
 
 
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Fake Audio Detection</title>
    <style>
        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background: url('/static/i.jpg') no-repeat center center fixed;
            background-size: cover;
            color: #f0f0f0;
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
            height: 101vh;
            margin: 0;
        }
        h1 {
            margin-bottom: 20px;
        }
        #buttons {
            display: flex;
            gap: 10px;
            margin-bottom: 20px;
        }
        button {
            padding: 10px 20px;
            font-size: 16px;
            cursor: pointer;
            border: none;
            border-radius: 5px;
            background-color: #007BFF;
            color: #fff;
            transition: background-color 0.3s;
        }
        button:hover {
            background-color: #0056b3;
        }
        button:disabled {
            background-color: #555;
            cursor: not-allowed;
        }
        #console {
            width: 80%;
            max-width: 600px;
            border: 1px solid #444;
            border-radius: 5px;
            padding: 10px;
            background-color: rgba(30, 30, 30, 0.9);
            height: 200px;
            overflow-y: scroll;
        }
        .log {
            margin: 0;
            padding: 5px;
            border-bottom: 1px solid #333;
        }
        .log.real {
            color: green;
        }
        .log.fake {
            color: red;
        }
        #notification {
            position: fixed;
            bottom: 50%;
            left: 50%;
            transform: translateX(-50%);
            background-color: red;
            color: white;
            padding: 10px 20px;
            border-radius: 5px;
            display: none;
        }
        #fileUploadSection {
            margin-top: 30px;
            text-align: center;
        }
        #uploadButton {
            padding: 10px 20px;
            font-size: 16px;
            cursor: pointer;
            border: none;
            border-radius: 5px;
            background-color: #28a745;
            color: #fff;
            transition: background-color 0.3s;
        }
        #uploadButton:hover {
            background-color: #218838;
        }
        #fileInput {
            margin-bottom: 10px;
        }
    </style>
</head>
<body>
    <h1>Fake Audio Detection</h1>
    <div id="buttons">
        <button id="startButton">Start Detection</button>
        <button id="stopButton" disabled>Stop Detection</button>
    </div>

    <div id="console"></div>
    <div id="notification">Fake Call Detected!</div>

    <div id="fileUploadSection">
        <h2>Upload Audio for Detection</h2>
        <input type="file" id="fileInput" accept="audio/*">
        <button id="uploadButton">Upload and Detect</button>
        <p id="uploadResult"></p>
    </div>

    <script>
        let isDetecting = false;
        const startButton = document.getElementById('startButton');
        const stopButton = document.getElementById('stopButton');
        const consoleDiv = document.getElementById('console');
        const notification = document.getElementById('notification');
        let websocket = null;
        let mediaRecorder = null;

        function logMessage(message, type) {
            const log = document.createElement('p');
            log.className = `log ${type}`;
            log.textContent = message;
            consoleDiv.appendChild(log);
            consoleDiv.scrollTop = consoleDiv.scrollHeight;
        }

        function showNotification() {
            notification.style.display = 'block';
            setTimeout(() => {
                notification.style.display = 'none';
            }, 10000)
        }

        async function startDetection() {
            if (isDetecting) {
                logMessage('Detection is already running...', 'info');
                return;
            }
            isDetecting = true;
            logMessage('Starting detection...', 'info');
            startButton.disabled = true;
            stopButton.disabled = false;

            try {
                const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
                logMessage('Microphone access granted', 'info');

                mediaRecorder = new MediaRecorder(stream, { mimeType: 'audio/webm' });
                mediaRecorder.ondataavailable = function(event) {
                    if (websocket && websocket.readyState === WebSocket.OPEN) {
                        websocket.send(event.data);
                    }
                };

                const response = await fetch(`${window.location.origin}/start_detection`, {
                // const response = await fetch(`http://localhost:7860/start_detection`, {
                    method: 'POST',
                });

                if (!response.ok) {
                    throw new Error('Network response was not ok');
                }

                const result = await response.json();
                logMessage(`Status: ${result.status}`, 'info');

                websocket = new WebSocket(`wss://${window.location.host}/ws`);
                // websocket = new WebSocket(`ws://localhost:7860/ws`);
                websocket.onmessage = async function(event) {
                    const data = event.data;
                    if (data === 'global-fake') {
                        logMessage(`The detection is being stopped, the audio is fake`);
                        showNotification();
                        await stopDetection();
                    } else {
                        logMessage(`Detected ${data} audio`, data);
                    }
                };
                websocket.onclose = function() {
                    logMessage('WebSocket connection closed', 'info');
                };

                mediaRecorder.start(3000);  // Start recording with 3-second intervals
            } catch (error) {
                logMessage(`Error: ${error.message}`, 'error');
            }
        }

        async function stopDetection() {
            if (!isDetecting) {
                logMessage('Detection is not running...', 'info');
                return;
            }
            isDetecting = false;
            logMessage('Stopping detection...', 'info');
            startButton.disabled = false;
            stopButton.disabled = true;

            try {
                if (mediaRecorder && mediaRecorder.state === 'recording') {
                    mediaRecorder.stop();
                }

                const response = await fetch(`${window.location.origin}/stop_detection`, {
                    method: 'POST',
                });

                if (!response.ok) {
                    throw new Error('Network response was not ok');
                }

                const result = await response.json();
                logMessage(`Status: ${result.status}`, 'info');

                if (websocket) {
                    websocket.close();
                    websocket = null;
                }
            } catch (error) {
                logMessage(`Error: ${error.message}`, 'error');
            }
        }

        startButton.addEventListener('click', startDetection);
        stopButton.addEventListener('click', stopDetection);

        const fileInput = document.getElementById('fileInput');
        const uploadButton = document.getElementById('uploadButton');
        const uploadResult = document.getElementById('uploadResult');

        uploadButton.addEventListener('click', async () => {
            const file = fileInput.files[0];
            if (!file) {
                uploadResult.textContent = 'Please select a file first.';
                return;
            }

            const formData = new FormData();
            formData.append('file', file);

            try {
                const response = await fetch(`${window.location.origin}/upload_audio/`, {
                // const response = await fetch(`http://localhost:7860/upload_audio/`, {
                    method: 'POST',
                    body: formData,
                });

                if (!response.ok) {
                    throw new Error('Network response was not ok');
                }

                const result = await response.json();
                uploadResult.textContent = `Detected audio is: ${result.result}`;
            } catch (error) {
                uploadResult.textContent = `Error: ${error.message}`;
            }
        });
    </script>
</body>
</html>