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

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -48
app.py CHANGED
@@ -6,61 +6,54 @@ from torch import Tensor
6
  import torch.nn as nn
7
  from model import Model
8
 
9
- # Function to load and preprocess audio data
10
- def load_audio_data(path, target_length=64600):
11
- try:
12
- X, fs = librosa.load(path)
13
- if len(X) >= target_length:
14
- X = X[:target_length]
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
- # Function for deepfake detection
35
- def detect_deepfake(audio_path, model, prediction_dict):
36
- try:
37
- audio_data, fs = load_audio_data(audio_path)
38
- if audio_data is None:
39
- return "Error: Audio file could not be loaded."
40
- with torch.no_grad():
41
- validity_probs = model(audio_data)
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 = load_model(model_path, device)
 
 
 
 
 
 
 
53
  prediction_dict = {0: 'Fake', 1: 'Real'}
 
 
 
 
 
 
 
 
 
 
 
 
54
 
55
- # Setting up Gradio interface
56
- audio_input = gr.Audio(type="file", label="Upload Audio")
57
- text_output = gr.Textbox(label="Real Or Fake")
58
 
59
- # Launch Gradio interface
 
 
60
  gr.Interface(
61
- fn=detect_deepfake,
62
- inputs=audio_input,
63
  outputs=text_output,
64
  title="Audio Deepfake Detection",
65
- description="Use a pre-trained model to detect if the uploaded audio is real or fake.",
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()