Kabatubare commited on
Commit
f03ec98
1 Parent(s): a7c6ced

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -30
app.py CHANGED
@@ -1,40 +1,34 @@
 
1
  import gradio as gr
2
- import torch
3
  import torchaudio
4
  from audioseal import AudioSeal
5
- from io import BytesIO
6
-
7
- def watermark_audio(file_info):
8
- audio, sr = torchaudio.load(file_info)
9
- model = AudioSeal.load_generator("audioseal_wm_16bits")
10
- audios = audio.unsqueeze(0) # Add batch dimension
11
- watermarked_audio = model(audios, alpha=1)
12
- output = BytesIO()
13
- torchaudio.save(output, watermarked_audio.squeeze(0), sr)
14
- output.seek(0)
15
- return output, 'audio/wav'
16
 
 
17
  def detect_watermark(file_info):
 
18
  audio, sr = torchaudio.load(file_info)
19
- audios = audio.unsqueeze(0) # Add batch dimension
 
 
20
  detector = AudioSeal.load_detector("audioseal_detector_16bits")
21
- result, message = detector.detect_watermark(audios, message_threshold=0.5)
22
- return f"Watermark Detected: {result}, Message: {message.numpy()}"
23
 
24
- with gr.Blocks() as demo:
25
- with gr.Row():
26
- with gr.Column():
27
- gr.Markdown("### Upload Audio for Watermarking")
28
- audio_input = gr.Audio(label="Upload Audio", type="file")
29
- watermark_btn = gr.Button("Watermark Audio")
30
- watermarked_audio_output = gr.Audio(label="Download Watermarked Audio", type="numpy", autoplay=True)
31
- watermark_btn.click(watermark_audio, inputs=audio_input, outputs=watermarked_audio_output)
32
- with gr.Column():
33
- gr.Markdown("### Upload Audio for Watermark Detection")
34
- audio_input_detect = gr.Audio(label="Upload Audio", type="file")
35
- detect_btn = gr.Button("Detect Watermark")
36
- detection_output = gr.Textbox(label="Detection Result")
37
- detect_btn.click(detect_watermark, inputs=audio_input_detect, outputs=detection_output)
 
 
 
38
 
39
- demo.launch()
40
 
 
1
+ # Import necessary libraries
2
  import gradio as gr
 
3
  import torchaudio
4
  from audioseal import AudioSeal
5
+ import torch
 
 
 
 
 
 
 
 
 
 
6
 
7
+ # Function to detect watermarks in the audio and determine if it's AI-generated
8
  def detect_watermark(file_info):
9
+ # Load the audio file from the uploaded file
10
  audio, sr = torchaudio.load(file_info)
11
+ audio = audio.unsqueeze(0) # Add batch dimension as expected by the AudioSeal detector
12
+
13
+ # Initialize the AudioSeal detector
14
  detector = AudioSeal.load_detector("audioseal_detector_16bits")
 
 
15
 
16
+ # Detect watermarks in the audio
17
+ result, message = detector.detect_watermark(audio, message_threshold=0.5)
18
+
19
+ # Interpret the detection result
20
+ detection_result = "AI-generated" if result else "genuine"
21
+ return f"This audio is likely {detection_result} based on watermark detection."
22
+
23
+ # Define Gradio interface
24
+ interface = gr.Interface(fn=detect_watermark,
25
+ inputs=gr.Audio(source="upload", type="file", label="Upload your audio"),
26
+ outputs="text",
27
+ title="Deep Fake Defender: AI Voice Cloning Detection",
28
+ description="Upload an audio file to check if it's AI-generated or genuine.")
29
+
30
+ # Launch the Gradio app
31
+ if __name__ == "__main__":
32
+ interface.launch()
33
 
 
34