Kabatubare commited on
Commit
e7c7540
1 Parent(s): 04a53dc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -30
app.py CHANGED
@@ -1,35 +1,16 @@
1
  import gradio as gr
2
  from audioseal import AudioSeal
3
  import torch
4
- import numpy as np
5
- import traceback
6
 
7
- def detect_watermark(audio_data):
8
  try:
9
- # Ensure that audio_data is a tuple with two elements
10
- if not (isinstance(audio_data, tuple) and len(audio_data) == 2):
11
- return f"Invalid input: expected a tuple with two elements, got {type(audio_data)} with length {len(audio_data)}"
12
-
13
- # Ensure the first element of the tuple is a NumPy array
14
- audio_array, sample_rate = audio_data
15
- if not isinstance(audio_array, np.ndarray):
16
- return f"Invalid input: expected the first element of the tuple to be a np.ndarray, got {type(audio_array)}"
17
-
18
- # Ensure the second element of the tuple is an integer (sample rate)
19
- if not isinstance(sample_rate, int):
20
- return f"Invalid input: expected the second element of the tuple to be an int (sample rate), got {type(sample_rate)}"
21
-
22
- # Now we can proceed with the assurance that audio_array is an np.ndarray and sample_rate is an int
23
- # Ensure audio_array is 2D (channels, samples). If it's mono, add an axis.
24
- if audio_array.ndim == 1:
25
- audio_array = np.expand_dims(audio_array, axis=0)
26
-
27
- # Convert NumPy array to tensor
28
- waveform = torch.tensor(audio_array, dtype=torch.float32)
29
-
30
- # Ensure waveform is 3D (batch, channels, samples) for AudioSeal
31
  if waveform.ndim == 2:
32
- waveform = waveform.unsqueeze(0)
33
 
34
  # Initialize and use the AudioSeal detector
35
  detector = AudioSeal.load_detector("audioseal_detector_16bits")
@@ -40,14 +21,13 @@ def detect_watermark(audio_data):
40
  return f"This audio is likely {detection_result} based on watermark detection."
41
 
42
  except Exception as e:
43
- # Capture the full traceback in case of an error and return it as a string
44
- error_traceback = traceback.format_exc()
45
- return f"Error occurred: {str(e)}\n\n{error_traceback}"
46
 
47
  # Define Gradio interface
48
  interface = gr.Interface(
49
  fn=detect_watermark,
50
- inputs=gr.Audio(label="Upload your audio", type="numpy"),
51
  outputs="text",
52
  title="Deep Fake Defender: AI Voice Cloning Detection",
53
  description="Upload an audio file to check if it's AI-generated or genuine."
@@ -55,3 +35,4 @@ interface = gr.Interface(
55
 
56
  if __name__ == "__main__":
57
  interface.launch()
 
 
1
  import gradio as gr
2
  from audioseal import AudioSeal
3
  import torch
4
+ import torchaudio
 
5
 
6
+ def detect_watermark(audio_filepath):
7
  try:
8
+ # Load the audio file using torchaudio
9
+ waveform, sample_rate = torchaudio.load(audio_filepath)
10
+
11
+ # Ensure waveform is 2D (channels, samples). If it's mono, add an axis.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
  if waveform.ndim == 2:
13
+ waveform = waveform.unsqueeze(0) # Add batch dimension
14
 
15
  # Initialize and use the AudioSeal detector
16
  detector = AudioSeal.load_detector("audioseal_detector_16bits")
 
21
  return f"This audio is likely {detection_result} based on watermark detection."
22
 
23
  except Exception as e:
24
+ # Return the error message directly
25
+ return f"Error occurred: {e}"
 
26
 
27
  # Define Gradio interface
28
  interface = gr.Interface(
29
  fn=detect_watermark,
30
+ inputs=gr.Audio(label="Upload your audio", type="filepath"),
31
  outputs="text",
32
  title="Deep Fake Defender: AI Voice Cloning Detection",
33
  description="Upload an audio file to check if it's AI-generated or genuine."
 
35
 
36
  if __name__ == "__main__":
37
  interface.launch()
38
+