DavidCombei commited on
Commit
2ea16ba
1 Parent(s): c29cd4d

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +93 -0
app.py ADDED
@@ -0,0 +1,93 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import joblib
2
+ from transformers import AutoFeatureExtractor, WavLMModel
3
+ import torch
4
+ import soundfile as sf
5
+ import numpy as np
6
+ import gradio as gr
7
+
8
+
9
+ class HuggingFaceFeatureExtractor:
10
+ def __init__(self, model_class, name):
11
+ self.device = "cuda" if torch.cuda.is_available() else "cpu"
12
+ self.feature_extractor = AutoFeatureExtractor.from_pretrained(name)
13
+ self.model = model_class.from_pretrained(name)
14
+ self.model.eval()
15
+ self.model.to(self.device)
16
+
17
+ def __call__(self, audio, sr):
18
+ inputs = self.feature_extractor(
19
+ audio,
20
+ sampling_rate=sr,
21
+ return_tensors="pt",
22
+ padding=True,
23
+ )
24
+ inputs = {k: v.to(self.device) for k, v in inputs.items()}
25
+ with torch.no_grad():
26
+ outputs = self.model(**inputs)
27
+ return outputs.last_hidden_state
28
+
29
+
30
+ FEATURE_EXTRACTORS = {
31
+ "wavlm-base": lambda: HuggingFaceFeatureExtractor(WavLMModel, "microsoft/wavlm-base"),
32
+ "wavLM-V1": lambda: HuggingFaceFeatureExtractor(WavLMModel, "DavidCombei/wavLM-base-DeepFake_UTCN"),
33
+ "wavLM-V2": lambda: HuggingFaceFeatureExtractor(WavLMModel, "DavidCombei/wavLM-base-UTCN"),
34
+ "wavLM-V3": lambda: HuggingFaceFeatureExtractor(WavLMModel, "DavidCombei/wavLM-base-UTCN_114k"),
35
+ }
36
+
37
+
38
+ model1 = joblib.load('model1.joblib')
39
+ model2 = joblib.load('model2.joblib')
40
+ model3 = joblib.load('model3.joblib')
41
+ model4 = joblib.load('model4.joblib')
42
+ final_model = joblib.load('final_model.joblib')
43
+
44
+
45
+ def process_audio(file_audio):
46
+ audio, sr = sf.read(file_audio)
47
+
48
+ extractor_1 = FEATURE_EXTRACTORS['wavlm-base']()
49
+ extractor_2 = FEATURE_EXTRACTORS['wavLM-V1']()
50
+ extractor_3 = FEATURE_EXTRACTORS['wavLM-V2']()
51
+ extractor_4 = FEATURE_EXTRACTORS['wavLM-V3']()
52
+
53
+ eval1 = extractor_1(audio, sr)
54
+ eval1 = torch.mean(eval1, dim=1).cpu().numpy()
55
+
56
+ eval2 = extractor_2(audio, sr)
57
+ eval2 = torch.mean(eval2, dim=1).cpu().numpy()
58
+
59
+ eval3 = extractor_3(audio, sr)
60
+ eval3 = torch.mean(eval3, dim=1).cpu().numpy()
61
+
62
+ eval4 = extractor_4(audio, sr)
63
+ eval4 = torch.mean(eval4, dim=1).cpu().numpy()
64
+
65
+ eval1 = eval1.reshape(1, -1)
66
+ eval2 = eval2.reshape(1, -1)
67
+ eval3 = eval3.reshape(1, -1)
68
+ eval4 = eval4.reshape(1, -1)
69
+
70
+ eval_prob1 = model1.predict_proba(eval1)[:, 1].reshape(-1, 1)
71
+ eval_prob2 = model2.predict_proba(eval2)[:, 1].reshape(-1, 1)
72
+ eval_prob3 = model3.predict_proba(eval3)[:, 1].reshape(-1, 1)
73
+ eval_prob4 = model4.predict_proba(eval4)[:, 1].reshape(-1, 1)
74
+
75
+ eval_combined_probs = np.hstack((eval_prob1, eval_prob2, eval_prob3, eval_prob4))
76
+
77
+ final_prob = final_model.predict_proba(eval_combined_probs)[:, 1]
78
+
79
+ if final_prob < 0.5:
80
+ return f"Fake with a confidence of: {final_prob[0]:.4f}"
81
+ else:
82
+ return f"Real with a confidence of: {final_prob[0]:.4f}"
83
+
84
+
85
+ interface = gr.Interface(
86
+ fn=process_audio,
87
+ inputs=gr.Audio(source="upload", type="filepath"),
88
+ outputs="text",
89
+ title="Audio Deepfake Detection",
90
+ description="Upload an audio file to detect whether it is fake or real.",
91
+ )
92
+
93
+ interface.launch()