Spaces:
Runtime error
Runtime error
Commit
·
ed33636
1
Parent(s):
2813636
first upload test
Browse files- app.py +71 -0
- audio_diffusion_attacks +1 -0
app.py
ADDED
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
import torchaudio
|
3 |
+
import audio_diffusion_attacks.src.test_encoder_attack as attack
|
4 |
+
import numpy as np
|
5 |
+
|
6 |
+
'''
|
7 |
+
Files edited:
|
8 |
+
- test_encoder_attack.py
|
9 |
+
- losses.py
|
10 |
+
- audio_signal.py; print statements only
|
11 |
+
'''
|
12 |
+
|
13 |
+
'''
|
14 |
+
#Set default parameters (if necessary)
|
15 |
+
args = 0
|
16 |
+
|
17 |
+
#Load pretrained protection model:
|
18 |
+
modelD = md.model(args)
|
19 |
+
modelD.load_state_dict(torch.load('path to pretrained weights', map_location=torch.device('cpu')), strict=True)
|
20 |
+
modelD.eval()
|
21 |
+
|
22 |
+
#Load pretrained mimickry model
|
23 |
+
modelM = mm.model(args)
|
24 |
+
modelM.load_state_dict(torch.load('path to pretrained weights', map_location=torch.device('cpu')), strict=True)
|
25 |
+
modelM.eval()
|
26 |
+
'''
|
27 |
+
|
28 |
+
#Define function to convert final audio format:
|
29 |
+
def float32_to_int16(waveform):
|
30 |
+
waveform = waveform / np.abs(waveform).max()
|
31 |
+
waveform = waveform * 32767
|
32 |
+
waveform = waveform.astype(np.int16)
|
33 |
+
waveform = waveform.ravel()
|
34 |
+
return waveform
|
35 |
+
|
36 |
+
#Define predict function:
|
37 |
+
def predict(inp):
|
38 |
+
#How to transform audio from string to tensor
|
39 |
+
waveform, sample_rate = torchaudio.load(inp)
|
40 |
+
|
41 |
+
encoders = [EncodecModel.from_pretrained("facebook/encodec_48khz")]
|
42 |
+
|
43 |
+
#Run modelD to disguise audio
|
44 |
+
with torch.no_grad():
|
45 |
+
waveformD = attack.poison_audio(waveform, sample_rate, encoders)
|
46 |
+
|
47 |
+
#Transform output audio into gradio-readable format
|
48 |
+
waveform = waveform.numpy()
|
49 |
+
waveform = float32_to_int16(waveform)
|
50 |
+
'''waveformD = waveformD.numpy()
|
51 |
+
waveformD = float32_to_int16(waveformD)
|
52 |
+
waveformM = waveformM.numpy()
|
53 |
+
waveformM = float32_to_int16(waveformM)
|
54 |
+
waveformDM = waveformDM.numpy()
|
55 |
+
waveformDM = float32_to_int16(waveformDM)'''
|
56 |
+
|
57 |
+
#return (sample_rate, waveformD), (sample_rate, waveformM), (sample_rate, waveformDM)
|
58 |
+
return (sample_rate, waveform), (sample_rate, waveform), (sample_rate, waveform)
|
59 |
+
|
60 |
+
#Set up gradio interface
|
61 |
+
import gradio as gr
|
62 |
+
|
63 |
+
interface = gr.Interface(
|
64 |
+
fn=predict,
|
65 |
+
inputs=gr.Audio(type="filepath"),
|
66 |
+
outputs=[gr.Audio(), gr.Audio(), gr.Audio()],
|
67 |
+
title="Music Protection Net",
|
68 |
+
description="This model is designed to add perturbations to a musical clip so that musical cloning models fail to properly reproduce the song. \n \n 1) Upload (or record) an audio file of your music. \n 2) Click submit to run the model. \n 3) Listen to and download your protected audio.",
|
69 |
+
)
|
70 |
+
|
71 |
+
interface.launch()
|
audio_diffusion_attacks
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
Subproject commit 1aaf4563762c407f31436ad452a72dd5af929443
|