deepfakeaudio / app.py
shreyasr09's picture
Update app.py
819e887 verified
import streamlit as st
import numpy as np
import librosa
import tensorflow as tf
model = tf.keras.models.load_model("audio_classifier.h5")
SAMPLE_RATE = 16000
N_MELS = 128
DURATION = 5
max_time_steps = 109
def preprocess_audio(file):
audio, _ = librosa.load(file, sr=SAMPLE_RATE, duration=DURATION)
mel_spectrogram = librosa.feature.melspectrogram(y=audio, sr=SAMPLE_RATE, n_mels=N_MELS)
mel_spectrogram = librosa.power_to_db(mel_spectrogram, ref=np.max)
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]
mel_spectrogram = mel_spectrogram.reshape(N_MELS, max_time_steps, 1)
return mel_spectrogram
st.title("Deepfake Audio Classification App")
uploaded_file = st.file_uploader("Upload an audio file", type=["flac", "wav"])
if uploaded_file is not None:
st.audio(uploaded_file, format="audio/wav")
mel_spectrogram = preprocess_audio(uploaded_file)
prediction = model.predict(np.array([mel_spectrogram]))
predicted_class = np.argmax(prediction)
label_map = {0: "Spoofed", 1: "Bonafide"}
predicted_label = label_map[predicted_class]
st.write(f"Predicted Label: {predicted_label}")