Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import numpy as np
|
3 |
+
import librosa
|
4 |
+
from tensorflow.keras.models import load_model
|
5 |
+
|
6 |
+
# Constants
|
7 |
+
MAX_TIME_STEPS = 109
|
8 |
+
SAMPLE_RATE = 16000
|
9 |
+
DURATION = 5
|
10 |
+
N_MELS = 128
|
11 |
+
MODEL_PATH = "audio_classifier.h5" # Replace with the actual path to your saved model
|
12 |
+
|
13 |
+
# Load the pre-trained model
|
14 |
+
model = load_model(MODEL_PATH, compile=False)
|
15 |
+
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
|
16 |
+
|
17 |
+
def classify_audio(audio):
|
18 |
+
# Convert the audio data to NumPy array
|
19 |
+
rate, ar = audio
|
20 |
+
arone = ar.astype(np.float32)
|
21 |
+
mel_spectrogram = librosa.feature.melspectrogram(y=arone, sr=SAMPLE_RATE, n_mels=N_MELS)
|
22 |
+
mel_spectrogram = librosa.power_to_db(mel_spectrogram, ref=np.max)
|
23 |
+
|
24 |
+
# Ensure all spectrograms have the same width (time steps)
|
25 |
+
if mel_spectrogram.shape[1] < MAX_TIME_STEPS:
|
26 |
+
mel_spectrogram = np.pad(mel_spectrogram, ((0, 0), (0, MAX_TIME_STEPS - mel_spectrogram.shape[1])), mode='constant')
|
27 |
+
else:
|
28 |
+
mel_spectrogram = mel_spectrogram[:, :MAX_TIME_STEPS]
|
29 |
+
|
30 |
+
# Reshape for the model
|
31 |
+
X_test = np.expand_dims(mel_spectrogram, axis=-1)
|
32 |
+
X_test = np.expand_dims(X_test, axis=0)
|
33 |
+
|
34 |
+
# Predict using the loaded model
|
35 |
+
y_pred = model.predict(X_test)
|
36 |
+
|
37 |
+
# Convert probabilities to predicted classes
|
38 |
+
y_pred_classes = np.argmax(y_pred, axis=1)
|
39 |
+
|
40 |
+
if(y_pred_classes[0]==1):
|
41 |
+
return f"Prediction: {'Not spoof'}"
|
42 |
+
else:
|
43 |
+
return f"Prediction: {'Spoof'}"
|
44 |
+
|
45 |
+
title="Audios Spoof detection using CNN"
|
46 |
+
description="The model was trained on the ASVspoof 2015 dataset with an aim to detect spoof audios through deep learning.To use it please upload an audio file of suitable length."
|
47 |
+
|
48 |
+
iface = gr.Interface(classify_audio, inputs=["audio"], outputs=["text"],title=title,description=description)
|
49 |
+
iface.launch()
|