Kabatubare commited on
Commit
a4ace8a
1 Parent(s): 0ea07d6
Files changed (1) hide show
  1. app.py +17 -11
app.py CHANGED
@@ -6,27 +6,33 @@ import traceback
6
 
7
  def detect_watermark(audio_file_path):
8
  try:
9
- # Load the audio file using torchaudio
10
  waveform, sample_rate = torchaudio.load(audio_file_path)
11
 
12
- # Ensure waveform is 2D (channels, samples). If it's mono, add an axis.
13
- if waveform.ndim == 2:
14
- waveform = waveform.unsqueeze(0) # Add batch dimension
15
 
16
- # Initialize and use the AudioSeal detector
17
  detector = AudioSeal.load_detector("audioseal_detector_16bits")
18
- result, _ = detector.detect_watermark(waveform, message_threshold=1)
 
 
 
 
19
 
20
- # Interpret and return the detection result
21
- detection_result = "AI-generated" if result else "genuine"
22
- return f"This audio is likely {detection_result} based on watermark detection."
 
 
23
 
 
24
  except Exception as e:
25
- # Log the error with traceback for debugging
26
  error_traceback = traceback.format_exc()
27
  return f"Error occurred: {e}\n\n{error_traceback}"
28
 
29
- # Define Gradio interface with "filepath" type for audio input
30
  interface = gr.Interface(
31
  fn=detect_watermark,
32
  inputs=gr.Audio(label="Upload your audio", type="filepath"),
 
6
 
7
  def detect_watermark(audio_file_path):
8
  try:
9
+ # Load the audio file
10
  waveform, sample_rate = torchaudio.load(audio_file_path)
11
 
12
+ # Ensure waveform has a batch dimension for processing
13
+ if waveform.ndim < 3:
14
+ waveform = waveform.unsqueeze(0)
15
 
16
+ # Initialize the AudioSeal detector
17
  detector = AudioSeal.load_detector("audioseal_detector_16bits")
18
+
19
+ # Set a conservative threshold for watermark detection
20
+ message_threshold = 0.7 # A higher threshold means more confidence is required to classify as AI-generated
21
+
22
+ result, confidence = detector.detect_watermark(waveform, message_threshold=message_threshold)
23
 
24
+ # Interpret the detection result
25
+ if result:
26
+ detection_result = f"AI-generated with confidence {confidence}"
27
+ else:
28
+ detection_result = "Genuine or the AI watermark is undetectable at the current threshold"
29
 
30
+ return f"This audio is likely {detection_result}."
31
  except Exception as e:
 
32
  error_traceback = traceback.format_exc()
33
  return f"Error occurred: {e}\n\n{error_traceback}"
34
 
35
+ # Define the Gradio interface
36
  interface = gr.Interface(
37
  fn=detect_watermark,
38
  inputs=gr.Audio(label="Upload your audio", type="filepath"),