Spaces:
Running
Running
Add application file
Browse files
app.py
ADDED
@@ -0,0 +1,115 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import numpy as np
|
3 |
+
import torch
|
4 |
+
from transformers import Wav2Vec2FeatureExtractor, Wav2Vec2Model
|
5 |
+
import torchaudio
|
6 |
+
from torchaudio.transforms import Resample
|
7 |
+
from tensorflow.keras.models import load_model
|
8 |
+
|
9 |
+
# Load models
|
10 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
11 |
+
feature_extractor = Wav2Vec2FeatureExtractor.from_pretrained("facebook/mms-1b")
|
12 |
+
wav2vec_model = Wav2Vec2Model.from_pretrained("facebook/mms-1b").to(device)
|
13 |
+
|
14 |
+
saved_model_path = "CNN-MODEL"
|
15 |
+
try:
|
16 |
+
cnn_model = load_model(saved_model_path)
|
17 |
+
except Exception as e:
|
18 |
+
st.error(f"Error loading TensorFlow model: {e}")
|
19 |
+
st.stop()
|
20 |
+
|
21 |
+
# Preprocessing Function
|
22 |
+
def preprocess_audio(audio_path):
|
23 |
+
try:
|
24 |
+
waveform, sampling_rate = torchaudio.load(audio_path)
|
25 |
+
desired_sampling_rate = 16000
|
26 |
+
if sampling_rate != desired_sampling_rate:
|
27 |
+
resampler = Resample(sampling_rate, desired_sampling_rate)
|
28 |
+
waveform = resampler(waveform)
|
29 |
+
if waveform.shape[0] > 1:
|
30 |
+
waveform = waveform.mean(dim=0, keepdim=True)
|
31 |
+
return waveform, desired_sampling_rate
|
32 |
+
except Exception as e:
|
33 |
+
st.error(f"Error processing audio file: {e}")
|
34 |
+
return None, None
|
35 |
+
|
36 |
+
# Feature Extraction
|
37 |
+
def extract_features(audio_path, feature_extractor, wav2vec_model, device):
|
38 |
+
waveform, fs = preprocess_audio(audio_path)
|
39 |
+
if waveform is None:
|
40 |
+
return None
|
41 |
+
inputs = feature_extractor(waveform.squeeze().numpy(), sampling_rate=fs, return_tensors="pt").to(device)
|
42 |
+
with torch.no_grad():
|
43 |
+
outputs = wav2vec_model(**inputs)
|
44 |
+
embeddings = outputs.last_hidden_state.cpu().numpy()
|
45 |
+
avg_embeddings = np.mean(embeddings.squeeze(), axis=0)
|
46 |
+
return avg_embeddings
|
47 |
+
|
48 |
+
# Prediction
|
49 |
+
def predict_with_cnn(audio_path, cnn_model, feature_extractor, wav2vec_model, device):
|
50 |
+
features = extract_features(audio_path, feature_extractor, wav2vec_model, device)
|
51 |
+
if features is None:
|
52 |
+
return None, None, None
|
53 |
+
features = np.expand_dims(features, axis=0)
|
54 |
+
features = np.expand_dims(features, axis=2)
|
55 |
+
predictions = cnn_model.predict(features)
|
56 |
+
predicted_class = np.argmax(predictions, axis=1)
|
57 |
+
class_names = ["bonafide", "spoof"]
|
58 |
+
confidence = predictions[0][predicted_class[0]] # Extract confidence for predicted class
|
59 |
+
return class_names[predicted_class[0]], predictions[0], confidence
|
60 |
+
|
61 |
+
# Streamlit Application
|
62 |
+
st.set_page_config(page_title="π΅ Audio Spoof Detection", layout="wide")
|
63 |
+
st.title("π΅ Audio Spoof Detection")
|
64 |
+
st.markdown(
|
65 |
+
"""
|
66 |
+
This application uses advanced machine learning models to detect whether an audio file is **bonafide** (real) or **spoofed** (fake).
|
67 |
+
Upload a `.wav` file to get started!
|
68 |
+
"""
|
69 |
+
)
|
70 |
+
|
71 |
+
# File Upload
|
72 |
+
uploaded_file = st.file_uploader(
|
73 |
+
"Upload your audio file (WAV format only):", type=["wav"]
|
74 |
+
)
|
75 |
+
|
76 |
+
if uploaded_file:
|
77 |
+
# Save uploaded file to a temporary path
|
78 |
+
temp_file_path = "temp_audio.wav"
|
79 |
+
with open(temp_file_path, "wb") as f:
|
80 |
+
f.write(uploaded_file.getbuffer())
|
81 |
+
|
82 |
+
# Display Audio Player
|
83 |
+
st.audio(temp_file_path, format="audio/wav")
|
84 |
+
|
85 |
+
# Processing Audio
|
86 |
+
st.write("π§ **Processing the audio...**")
|
87 |
+
predicted_class, probabilities, confidence = predict_with_cnn(
|
88 |
+
temp_file_path, cnn_model, feature_extractor, wav2vec_model, device
|
89 |
+
)
|
90 |
+
|
91 |
+
# Display Results
|
92 |
+
if predicted_class:
|
93 |
+
col1, col2 = st.columns(2)
|
94 |
+
|
95 |
+
with col1:
|
96 |
+
st.markdown(
|
97 |
+
f"""
|
98 |
+
## π **Prediction: `{predicted_class.upper()}`**
|
99 |
+
"""
|
100 |
+
)
|
101 |
+
st.markdown(f"### **Confidence**: `{confidence:.2f}`")
|
102 |
+
|
103 |
+
with col2:
|
104 |
+
st.write("### Class Probabilities")
|
105 |
+
st.bar_chart(probabilities)
|
106 |
+
|
107 |
+
# Display Detailed Probabilities
|
108 |
+
st.markdown("### Class Details")
|
109 |
+
st.write(f"**Bonafide Probability**: `{probabilities[0]:.2f}`")
|
110 |
+
st.write(f"**Spoof Probability**: `{probabilities[1]:.2f}`")
|
111 |
+
|
112 |
+
else:
|
113 |
+
st.error("Failed to process the audio file. Please try again.")
|
114 |
+
else:
|
115 |
+
st.info("Please upload a `.wav` audio file to analyze.")
|