File size: 2,043 Bytes
65d1755 3aa0b09 e31f525 3f32c35 d2613e3 34ced66 e31f525 34ced66 2330c01 f8635c8 2330c01 f8635c8 2330c01 f8635c8 d0fe1ef 2330c01 e31f525 34ced66 f8635c8 34ced66 e31f525 34ced66 f8635c8 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | import gradio as gr
import librosa
import numpy as np
import tensorflow as tf
from tensorflow.keras.models import load_model
# Load your pre-trained model (make sure it's in the same directory or provide a path)
model = load_model('voice_authentication_model.keras') # Replace with your model's filename
# Function to extract MFCC features and make a prediction
def predict_user_or_non_user(audio):
try:
# Load the audio file using librosa
y, sr = librosa.load(audio, sr=16000) # sr=None to keep the original sampling rate
# Optional: Normalize the audio volume (if necessary)
y = librosa.util.normalize(y)
# Extract MFCC features from the audio
mfccs = librosa.feature.mfcc(y=y, sr=sr, n_mfcc=13) # You can adjust n_mfcc as needed
mfccs = np.mean(mfccs.T, axis=0) # Take the mean of MFCCs over time to reduce dimension
# Reshape the MFCCs to match the input shape expected by the model
mfccs = mfccs.reshape(1, -1) # Reshape to 1 sample, with the number of features
# Predict the class (user or non-user)
prediction = model.predict(mfccs)
# Debugging: print raw model output
print(f"Raw Prediction Output: {prediction}")
# Apply thresholding based on the raw prediction value
# If the model outputs a probability, try adjusting the threshold (e.g., 0.6 instead of 0.5)
if prediction[0] > 0.5:
return "User"
else:
return "Non-User"
except Exception as e:
print(f"Error in prediction: {e}")
return "Error during prediction"
# Define the Gradio interface
iface = gr.Interface(
fn=predict_user_or_non_user, # The function to call when an audio input is given
inputs=gr.Audio(type="filepath"), # Corrected audio input setup
outputs="text", # Output will be text (User or Non-user)
live=True # Live mode so the interface updates in real-time
)
# Launch the interface
iface.launch() |