SpoofDetection / app.py
thugCodeNinja's picture
Create app.py
41d4efd verified
raw
history blame
1.81 kB
import gradio as gr
import numpy as np
import librosa
from tensorflow.keras.models import load_model
# Constants
MAX_TIME_STEPS = 109
SAMPLE_RATE = 16000
DURATION = 5
N_MELS = 128
MODEL_PATH = "audio_classifier.h5" # Replace with the actual path to your saved model
# Load the pre-trained model
model = load_model(MODEL_PATH, compile=False)
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
def classify_audio(audio):
# Convert the audio data to NumPy array
rate, ar = audio
arone = ar.astype(np.float32)
mel_spectrogram = librosa.feature.melspectrogram(y=arone, sr=SAMPLE_RATE, n_mels=N_MELS)
mel_spectrogram = librosa.power_to_db(mel_spectrogram, ref=np.max)
# Ensure all spectrograms have the same width (time steps)
if mel_spectrogram.shape[1] < MAX_TIME_STEPS:
mel_spectrogram = np.pad(mel_spectrogram, ((0, 0), (0, MAX_TIME_STEPS - mel_spectrogram.shape[1])), mode='constant')
else:
mel_spectrogram = mel_spectrogram[:, :MAX_TIME_STEPS]
# Reshape for the model
X_test = np.expand_dims(mel_spectrogram, axis=-1)
X_test = np.expand_dims(X_test, axis=0)
# Predict using the loaded model
y_pred = model.predict(X_test)
# Convert probabilities to predicted classes
y_pred_classes = np.argmax(y_pred, axis=1)
if(y_pred_classes[0]==1):
return f"Prediction: {'Not spoof'}"
else:
return f"Prediction: {'Spoof'}"
title="Audios Spoof detection using CNN"
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."
iface = gr.Interface(classify_audio, inputs=["audio"], outputs=["text"],title=title,description=description)
iface.launch()