kushal1506 commited on
Commit
7f6586a
·
verified ·
1 Parent(s): 8d750a0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +59 -59
app.py CHANGED
@@ -1,59 +1,59 @@
1
- import gradio as gr
2
- import librosa
3
- import numpy as np
4
- import torch
5
- from torch import Tensor
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()
 
1
+ import gradio as gr
2
+ import librosa
3
+ import numpy as np
4
+ import torch
5
+ from torch import Tensor
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):
36
+
37
+ x_inp,fs = load_data(audio)
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 = gr.Audio(type="filepath", label="Audio")
51
+ # text_output = gr.Textbox(label="Prediction")
52
+ text_output = gr.Textbox(label="Real Or Fake")
53
+ gr.Interface(
54
+ fn=Detection,
55
+ inputs=audio,
56
+ outputs=text_output,
57
+ title="Audio Deepfake Detection",
58
+ description="Audio Deepfake Detection.",
59
+ ).launch()