kowalsky commited on
Commit
cfb7438
·
1 Parent(s): efebc5f
Files changed (1) hide show
  1. main.py +22 -6
main.py CHANGED
@@ -80,11 +80,21 @@ def extract_features(audio):
80
 
81
  async def process_audio_data(audio_data):
82
  try:
83
- # Convert audio data from webm/ogg to wav format using pydub
 
84
  audio_segment = AudioSegment.from_file(io.BytesIO(audio_data), format="webm")
 
 
 
 
 
 
85
  wav_io = io.BytesIO()
86
  audio_segment.export(wav_io, format="wav")
 
87
  wav_io.seek(0)
 
 
88
  audio, sr = sf.read(wav_io, dtype='float32')
89
  except Exception as e:
90
  logger.error(f"Failed to read audio data: {e}")
@@ -92,14 +102,20 @@ async def process_audio_data(audio_data):
92
 
93
  if audio.ndim > 1: # If audio has more than one channel, average them
94
  audio = np.mean(audio, axis=1)
95
- logger.info("Here!!!")
 
 
96
  features = extract_features(audio)
97
  features = features.reshape(1, -1)
98
- prediction = model.predict(features)
99
- is_fake = prediction[0]
100
 
101
- result = 'fake' if is_fake else 'real'
102
-
 
 
 
 
 
 
103
  await manager.send_message(result)
104
 
105
  @app.post("/start_detection")
 
80
 
81
  async def process_audio_data(audio_data):
82
  try:
83
+ logger.info(f"Audio data type: {type(audio_data)}")
84
+ # Attempt to convert audio data from webm/ogg to wav format using pydub
85
  audio_segment = AudioSegment.from_file(io.BytesIO(audio_data), format="webm")
86
+ except Exception as e:
87
+ logger.error(f"Failed to convert audio data using pydub: {e}")
88
+ return
89
+
90
+ try:
91
+ # Export the audio segment to wav format
92
  wav_io = io.BytesIO()
93
  audio_segment.export(wav_io, format="wav")
94
+ logger.info(f"Audio data type: {type(audio_segment)}")
95
  wav_io.seek(0)
96
+
97
+ # Read the audio data
98
  audio, sr = sf.read(wav_io, dtype='float32')
99
  except Exception as e:
100
  logger.error(f"Failed to read audio data: {e}")
 
102
 
103
  if audio.ndim > 1: # If audio has more than one channel, average them
104
  audio = np.mean(audio, axis=1)
105
+
106
+ logger.info(f"The len of audio: {len(audio)}")
107
+ logger.info("Extracting features")
108
  features = extract_features(audio)
109
  features = features.reshape(1, -1)
 
 
110
 
111
+ try:
112
+ prediction = model.predict(features)
113
+ is_fake = prediction[0]
114
+ result = 'fake' if is_fake else 'real'
115
+ except Exception as e:
116
+ logger.error(f"Model prediction failed: {e}")
117
+ return
118
+
119
  await manager.send_message(result)
120
 
121
  @app.post("/start_detection")