Spaces:
Running on Zero
Running on Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -9,9 +9,10 @@ import librosa.display
|
|
| 9 |
import matplotlib.pyplot as plt
|
| 10 |
import numpy as np
|
| 11 |
import soundfile as sf
|
|
|
|
| 12 |
|
| 13 |
# ==========================================================
|
| 14 |
-
# AudioShield — Watermarking
|
| 15 |
# ==========================================================
|
| 16 |
|
| 17 |
def _generate_pn_pattern(shape, seed=42, chip_hold=4, smooth_taps=5):
|
|
@@ -67,18 +68,17 @@ def _process_single_channel(y_channel, sr, watermark_key, alpha, apply_masking,
|
|
| 67 |
return y_wm, magnitude, watermarked_magnitude, hop_length
|
| 68 |
|
| 69 |
|
|
|
|
| 70 |
def embed_watermark(audio_path, watermark_key=42, alpha=0.015, apply_masking=True):
|
| 71 |
"""Injecte le tatouage sur n'importe quel fichier audio (Mono ou Stéréo)."""
|
| 72 |
if not audio_path:
|
| 73 |
return None, None, "Veuillez fournir un fichier audio."
|
| 74 |
|
| 75 |
try:
|
| 76 |
-
# mono=False permet de conserver la stéréo si le fichier d'origine l'est
|
| 77 |
y, sr = librosa.load(audio_path, sr=None, mono=False, duration=300)
|
| 78 |
except Exception as e:
|
| 79 |
return None, None, f"❌ Erreur de lecture du fichier : {str(e)}"
|
| 80 |
|
| 81 |
-
# Traitement Mono vs Stéréo/Multicanal
|
| 82 |
if y.ndim == 1: # Audio mono
|
| 83 |
y_wm, orig_mag, wm_mag, hop_len = _process_single_channel(
|
| 84 |
y, sr, watermark_key, alpha, apply_masking
|
|
@@ -96,10 +96,8 @@ def embed_watermark(audio_path, watermark_key=42, alpha=0.015, apply_masking=Tru
|
|
| 96 |
uid = uuid.uuid4().hex[:8]
|
| 97 |
output_path = f"audio_watermarked_{uid}.wav"
|
| 98 |
|
| 99 |
-
# Écriture dans un fichier WAV haute qualité (sans perte)
|
| 100 |
sf.write(output_path, y_out, sr)
|
| 101 |
|
| 102 |
-
# Génération du spectrogramme de contrôle (sur le canal 1)
|
| 103 |
fig, ax = plt.subplots(2, 1, figsize=(10, 6), sharex=True)
|
| 104 |
D_orig = librosa.amplitude_to_db(orig_mag, ref=np.max)
|
| 105 |
D_wm = librosa.amplitude_to_db(wm_mag, ref=np.max)
|
|
@@ -118,6 +116,7 @@ def embed_watermark(audio_path, watermark_key=42, alpha=0.015, apply_masking=Tru
|
|
| 118 |
return output_path, spec_path, "✅ Tatouage appliqué avec succès (compatible tout format, Mono/Stéréo)."
|
| 119 |
|
| 120 |
|
|
|
|
| 121 |
def detect_watermark(watermarked_audio_path, watermark_key=42):
|
| 122 |
"""Détecte le watermark sur n'importe quel fichier audio."""
|
| 123 |
if not watermarked_audio_path:
|
|
@@ -134,7 +133,6 @@ def detect_watermark(watermarked_audio_path, watermark_key=42):
|
|
| 134 |
|
| 135 |
pn_pattern = _generate_pn_pattern(magnitude.shape, seed=int(watermark_key))
|
| 136 |
|
| 137 |
-
# Masque fréquentiel (ignore les zones non tatouées pour un meilleur score)
|
| 138 |
freqs = librosa.fft_frequencies(sr=sr, n_fft=n_fft)
|
| 139 |
freq_mask = (freqs > 300) & (freqs < sr / 2 - 1000)
|
| 140 |
|
|
@@ -200,5 +198,5 @@ with gr.Blocks(title="AudioShield Watermark — ACoNum") as demo:
|
|
| 200 |
|
| 201 |
btn_detect.click(detect_watermark, inputs=[audio_verify, key_verify], outputs=[detect_out])
|
| 202 |
|
| 203 |
-
#
|
| 204 |
-
demo.launch()
|
|
|
|
| 9 |
import matplotlib.pyplot as plt
|
| 10 |
import numpy as np
|
| 11 |
import soundfile as sf
|
| 12 |
+
import spaces # Prise en charge de ZeroGPU / spaces
|
| 13 |
|
| 14 |
# ==========================================================
|
| 15 |
+
# AudioShield — Watermarking Multi-Format
|
| 16 |
# ==========================================================
|
| 17 |
|
| 18 |
def _generate_pn_pattern(shape, seed=42, chip_hold=4, smooth_taps=5):
|
|
|
|
| 68 |
return y_wm, magnitude, watermarked_magnitude, hop_length
|
| 69 |
|
| 70 |
|
| 71 |
+
@spaces.GPU
|
| 72 |
def embed_watermark(audio_path, watermark_key=42, alpha=0.015, apply_masking=True):
|
| 73 |
"""Injecte le tatouage sur n'importe quel fichier audio (Mono ou Stéréo)."""
|
| 74 |
if not audio_path:
|
| 75 |
return None, None, "Veuillez fournir un fichier audio."
|
| 76 |
|
| 77 |
try:
|
|
|
|
| 78 |
y, sr = librosa.load(audio_path, sr=None, mono=False, duration=300)
|
| 79 |
except Exception as e:
|
| 80 |
return None, None, f"❌ Erreur de lecture du fichier : {str(e)}"
|
| 81 |
|
|
|
|
| 82 |
if y.ndim == 1: # Audio mono
|
| 83 |
y_wm, orig_mag, wm_mag, hop_len = _process_single_channel(
|
| 84 |
y, sr, watermark_key, alpha, apply_masking
|
|
|
|
| 96 |
uid = uuid.uuid4().hex[:8]
|
| 97 |
output_path = f"audio_watermarked_{uid}.wav"
|
| 98 |
|
|
|
|
| 99 |
sf.write(output_path, y_out, sr)
|
| 100 |
|
|
|
|
| 101 |
fig, ax = plt.subplots(2, 1, figsize=(10, 6), sharex=True)
|
| 102 |
D_orig = librosa.amplitude_to_db(orig_mag, ref=np.max)
|
| 103 |
D_wm = librosa.amplitude_to_db(wm_mag, ref=np.max)
|
|
|
|
| 116 |
return output_path, spec_path, "✅ Tatouage appliqué avec succès (compatible tout format, Mono/Stéréo)."
|
| 117 |
|
| 118 |
|
| 119 |
+
@spaces.GPU
|
| 120 |
def detect_watermark(watermarked_audio_path, watermark_key=42):
|
| 121 |
"""Détecte le watermark sur n'importe quel fichier audio."""
|
| 122 |
if not watermarked_audio_path:
|
|
|
|
| 133 |
|
| 134 |
pn_pattern = _generate_pn_pattern(magnitude.shape, seed=int(watermark_key))
|
| 135 |
|
|
|
|
| 136 |
freqs = librosa.fft_frequencies(sr=sr, n_fft=n_fft)
|
| 137 |
freq_mask = (freqs > 300) & (freqs < sr / 2 - 1000)
|
| 138 |
|
|
|
|
| 198 |
|
| 199 |
btn_detect.click(detect_watermark, inputs=[audio_verify, key_verify], outputs=[detect_out])
|
| 200 |
|
| 201 |
+
# Activer la file d'attente (nécessaire pour ZeroGPU / spaces)
|
| 202 |
+
demo.queue().launch()
|