Kabatubare commited on
Commit
faee536
1 Parent(s): 1c53cc7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +4 -20
app.py CHANGED
@@ -4,42 +4,26 @@ import torch
4
  import numpy as np
5
  import traceback
6
 
7
- # Function to handle audio data as NumPy arrays
8
  def detect_watermark(audio_data, sample_rate):
9
  try:
10
- # Extract the audio array from the tuple (audio_data, sample_rate)
11
  audio_array, _ = audio_data
12
-
13
- # Ensure audio_array is 2D (channels, samples). If it's mono, add an axis.
14
  if audio_array.ndim == 1:
15
  audio_array = np.expand_dims(audio_array, axis=0)
16
-
17
- # Convert NumPy array to tensor
18
  waveform = torch.tensor(audio_array, dtype=torch.float32)
19
-
20
- # Ensure waveform is 2D (batch, channels, samples) for AudioSeal
21
  if waveform.ndim == 2:
22
  waveform = waveform.unsqueeze(0)
23
-
24
- # Initialize and use the AudioSeal detector
25
  detector = AudioSeal.load_detector("audioseal_detector_16bits")
26
  result, message = detector.detect_watermark(waveform, message_threshold=0.5)
27
-
28
- # Interpret and return the detection result
29
  detection_result = "AI-generated" if result else "genuine"
30
  return f"This audio is likely {detection_result} based on watermark detection."
31
  except Exception as e:
32
- error_message = f"An error occurred: {str(e)}"
33
- traceback_str = ''.join(traceback.format_tb(e.__traceback__))
34
- full_error_message = f"{error_message}\n{traceback_str}"
35
- return full_error_message
36
 
37
  interface = gr.Interface(fn=detect_watermark,
38
  inputs=[gr.Audio(label="Upload your audio", type="numpy"),
39
  gr.Number(label="Sample Rate", value=44100, visible=False)],
40
- outputs="text",
41
- title="Deep Fake Defender: AI Voice Cloning Detection",
42
- description="Upload an audio file to check if it's AI-generated or genuine.")
43
 
44
  if __name__ == "__main__":
45
- interface.launch(debug=True)
 
4
  import numpy as np
5
  import traceback
6
 
 
7
  def detect_watermark(audio_data, sample_rate):
8
  try:
 
9
  audio_array, _ = audio_data
 
 
10
  if audio_array.ndim == 1:
11
  audio_array = np.expand_dims(audio_array, axis=0)
 
 
12
  waveform = torch.tensor(audio_array, dtype=torch.float32)
 
 
13
  if waveform.ndim == 2:
14
  waveform = waveform.unsqueeze(0)
 
 
15
  detector = AudioSeal.load_detector("audioseal_detector_16bits")
16
  result, message = detector.detect_watermark(waveform, message_threshold=0.5)
 
 
17
  detection_result = "AI-generated" if result else "genuine"
18
  return f"This audio is likely {detection_result} based on watermark detection."
19
  except Exception as e:
20
+ error_traceback = traceback.format_exc()
21
+ return f"Error: {str(e)}\n{error_traceback}"
 
 
22
 
23
  interface = gr.Interface(fn=detect_watermark,
24
  inputs=[gr.Audio(label="Upload your audio", type="numpy"),
25
  gr.Number(label="Sample Rate", value=44100, visible=False)],
26
+ outputs="text")
 
 
27
 
28
  if __name__ == "__main__":
29
+ interface.launch()