Spaces:
Runtime error
Runtime error
Upload app.py
Browse files
app.py
CHANGED
@@ -6,61 +6,54 @@ from torch import Tensor
|
|
6 |
import torch.nn as nn
|
7 |
from model import Model
|
8 |
|
9 |
-
|
10 |
-
def
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
else:
|
16 |
-
X = np.pad(X, (0, target_length - len(X)))
|
17 |
-
return Tensor(X).unsqueeze(0), fs
|
18 |
-
except Exception as e:
|
19 |
-
print("Error loading audio:", e)
|
20 |
-
return None, None
|
21 |
-
|
22 |
-
# Load the pre-trained model
|
23 |
-
def load_model(model_path, device):
|
24 |
-
try:
|
25 |
-
model = Model(None, device)
|
26 |
-
model = nn.DataParallel(model).to(device)
|
27 |
-
model.load_state_dict(torch.load(model_path, map_location=device))
|
28 |
-
model.eval()
|
29 |
-
return model
|
30 |
-
except Exception as e:
|
31 |
-
print("Error loading model:", e)
|
32 |
-
return None
|
33 |
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
validity_probs = torch.nn.functional.softmax(validity_probs, dim=1)
|
43 |
-
emotion = torch.argmax(validity_probs).item()
|
44 |
-
return prediction_dict[emotion]
|
45 |
-
except Exception as e:
|
46 |
-
print("Error during detection:", e)
|
47 |
-
return "Error occurred during detection."
|
48 |
|
49 |
-
# Model and prediction dictionary initialization
|
50 |
-
model_path = 'final_model.pth'
|
51 |
device = 'cuda' if torch.cuda.is_available() else 'cpu'
|
52 |
-
model =
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
53 |
prediction_dict = {0: 'Fake', 1: 'Real'}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
54 |
|
55 |
-
|
56 |
-
audio_input = gr.Audio(type="file", label="Upload Audio")
|
57 |
-
text_output = gr.Textbox(label="Real Or Fake")
|
58 |
|
59 |
-
|
|
|
|
|
60 |
gr.Interface(
|
61 |
-
fn=
|
62 |
-
inputs=
|
63 |
outputs=text_output,
|
64 |
title="Audio Deepfake Detection",
|
65 |
-
description="
|
66 |
).launch()
|
|
|
6 |
import torch.nn as nn
|
7 |
from model import Model
|
8 |
|
9 |
+
model_path = 'final_model.pth'
|
10 |
+
def load_data(path):
|
11 |
+
X, fs = librosa.load(path)
|
12 |
+
X_pad = pad(X,64600)
|
13 |
+
x_inp = Tensor(X_pad).unsqueeze(0)
|
14 |
+
return x_inp,fs
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
|
16 |
+
def pad(x, max_len=64600):
|
17 |
+
x_len = x.shape[0]
|
18 |
+
if x_len >= max_len:
|
19 |
+
return x[:max_len]
|
20 |
+
# need to pad
|
21 |
+
num_repeats = int(max_len / x_len)+1
|
22 |
+
padded_x = np.tile(x, (1, num_repeats))[:, :max_len][0]
|
23 |
+
return padded_x
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
|
|
|
|
|
25 |
device = 'cuda' if torch.cuda.is_available() else 'cpu'
|
26 |
+
model = Model(None, device)
|
27 |
+
nb_params = sum([param.view(-1).size()[0] for param in model.parameters()])
|
28 |
+
model =nn.DataParallel(model).to(device)
|
29 |
+
|
30 |
+
model.load_state_dict(torch.load(model_path, map_location=device))
|
31 |
+
print("Model loaded : {}".format(model_path))
|
32 |
+
|
33 |
+
model.eval()
|
34 |
prediction_dict = {0: 'Fake', 1: 'Real'}
|
35 |
+
def Detection(audio_1):
|
36 |
+
|
37 |
+
x_inp,fs = load_data(audio_1)
|
38 |
+
print(x_inp.shape)
|
39 |
+
validity_probs = model(x_inp)
|
40 |
+
validity_probs = torch.nn.functional.softmax(validity_probs, dim=1)
|
41 |
+
|
42 |
+
emotion = torch.argmax(validity_probs).item()
|
43 |
+
print(emotion)
|
44 |
+
validity = prediction_dict[emotion]
|
45 |
+
# validity as a dictionary of class probabilities
|
46 |
+
# validity = {prediction_dict[i]: float(validity_probs[0][i]) for i in range(2)}
|
47 |
|
48 |
+
return validity
|
|
|
|
|
49 |
|
50 |
+
audio_1 = gr.Audio(type="filepath", label="Audio 1")
|
51 |
+
# text_output = gr.Textbox(label="Prediction")
|
52 |
+
text_output = gr.Textbox(label="Similarity Score")
|
53 |
gr.Interface(
|
54 |
+
fn=Detection,
|
55 |
+
inputs=audio_1,
|
56 |
outputs=text_output,
|
57 |
title="Audio Deepfake Detection",
|
58 |
+
description="Audio Deepfake Detection using finetuned model on for-2seconds dataset.",
|
59 |
).launch()
|