kowalsky commited on
Commit
7ac2a83
·
1 Parent(s): a3080e4
Files changed (2) hide show
  1. main.py +12 -2
  2. templates/index.html +2 -1
main.py CHANGED
@@ -9,6 +9,8 @@ import uvicorn
9
  import threading
10
  import asyncio
11
  import logging
 
 
12
  from typing import List
13
 
14
  logging.basicConfig(level=logging.INFO)
@@ -76,8 +78,16 @@ def extract_features(audio):
76
  return combined_features
77
 
78
  async def process_audio_data(audio_data):
79
- audio_np = np.frombuffer(audio_data, dtype=np.float32)
80
- features = extract_features(audio_np)
 
 
 
 
 
 
 
 
81
  features = features.reshape(1, -1)
82
  prediction = model.predict(features)
83
  is_fake = prediction[0]
 
9
  import threading
10
  import asyncio
11
  import logging
12
+ import io
13
+ import soundfile as sf
14
  from typing import List
15
 
16
  logging.basicConfig(level=logging.INFO)
 
78
  return combined_features
79
 
80
  async def process_audio_data(audio_data):
81
+ try:
82
+ audio, sr = sf.read(io.BytesIO(audio_data), dtype='float32')
83
+ except RuntimeError as e:
84
+ logger.error(f"Failed to read audio data: {e}")
85
+ return
86
+
87
+ if audio.ndim > 1: # If audio has more than one channel, average them
88
+ audio = np.mean(audio, axis=1)
89
+
90
+ features = extract_features(audio)
91
  features = features.reshape(1, -1)
92
  prediction = model.predict(features)
93
  is_fake = prediction[0]
templates/index.html CHANGED
@@ -63,7 +63,7 @@
63
  const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
64
  logMessage('Microphone access granted', 'info');
65
 
66
- mediaRecorder = new MediaRecorder(stream);
67
  mediaRecorder.ondataavailable = function(event) {
68
  if (websocket && websocket.readyState === WebSocket.OPEN) {
69
  websocket.send(event.data);
@@ -96,6 +96,7 @@
96
  }
97
  }
98
 
 
99
  async function stopDetection() {
100
  if (!isDetecting) {
101
  logMessage('Detection is not running...', 'info');
 
63
  const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
64
  logMessage('Microphone access granted', 'info');
65
 
66
+ mediaRecorder = new MediaRecorder(stream, { mimeType: 'audio/webm' });
67
  mediaRecorder.ondataavailable = function(event) {
68
  if (websocket && websocket.readyState === WebSocket.OPEN) {
69
  websocket.send(event.data);
 
96
  }
97
  }
98
 
99
+
100
  async function stopDetection() {
101
  if (!isDetecting) {
102
  logMessage('Detection is not running...', 'info');