Update app.py
Browse files
app.py
CHANGED
@@ -50,18 +50,30 @@ def plot_spectrogram(waveform, sample_rate):
|
|
50 |
buf.seek(0)
|
51 |
return Image.open(buf)
|
52 |
|
53 |
-
def detect_watermark(
|
54 |
-
|
55 |
-
|
56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
57 |
detector = AudioSeal.load_detector("audioseal_detector_16bits")
|
58 |
-
|
59 |
-
|
|
|
60 |
detect_probs = results[:, 1, :]
|
61 |
result = detect_probs.mean().cpu().item()
|
62 |
message = f"Detection result: {'Watermarked or AI-Generated audio' if result > 0.5 else 'Not watermarked or AI-Generated'}\nProbability of watermark: {result}"
|
63 |
-
spectrogram_image = plot_spectrogram(waveform,
|
64 |
-
|
|
|
65 |
|
66 |
|
67 |
def main(audio_file_path):
|
|
|
50 |
buf.seek(0)
|
51 |
return Image.open(buf)
|
52 |
|
53 |
+
def detect_watermark(audio_data, sample_rate):
|
54 |
+
# Ensure AudioSeal is available
|
55 |
+
if 'AudioSeal' not in globals():
|
56 |
+
spectrogram_image = plot_spectrogram(audio_data, sample_rate)
|
57 |
+
return "AudioSeal not available", spectrogram_image
|
58 |
+
|
59 |
+
# Load audio data correctly
|
60 |
+
waveform, sr = load_and_resample_audio(audio_data, target_sample_rate=16000)
|
61 |
+
|
62 |
+
# Ensure waveform is a tensor before passing to the detector
|
63 |
+
if not isinstance(waveform, torch.Tensor):
|
64 |
+
return "Error: waveform is not a tensor.", plot_spectrogram(waveform, sr)
|
65 |
+
|
66 |
+
# Load the detector
|
67 |
detector = AudioSeal.load_detector("audioseal_detector_16bits")
|
68 |
+
|
69 |
+
# Process waveform with the detector
|
70 |
+
results, messages = detector.forward(waveform.unsqueeze(0), sample_rate=sample_rate) # Ensure waveform is in batch form
|
71 |
detect_probs = results[:, 1, :]
|
72 |
result = detect_probs.mean().cpu().item()
|
73 |
message = f"Detection result: {'Watermarked or AI-Generated audio' if result > 0.5 else 'Not watermarked or AI-Generated'}\nProbability of watermark: {result}"
|
74 |
+
spectrogram_image = plot_spectrogram(waveform, sr)
|
75 |
+
|
76 |
+
return message, spectrogram_image
|
77 |
|
78 |
|
79 |
def main(audio_file_path):
|