Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
|
| 2 |
import librosa
|
| 3 |
import numpy as np
|
| 4 |
import tensorflow as tf
|
|
@@ -10,14 +10,14 @@ model = load_model('voice_authentication_model.keras') # Replace with your mode
|
|
| 10 |
# Function to extract MFCC features and make a prediction
|
| 11 |
def predict_user_or_non_user(audio):
|
| 12 |
try:
|
| 13 |
-
# Load the audio file using librosa
|
| 14 |
-
y, sr = librosa.load(audio, sr=16000) #
|
| 15 |
|
| 16 |
# Optional: Normalize the audio volume (if necessary)
|
| 17 |
y = librosa.util.normalize(y)
|
| 18 |
|
| 19 |
# Extract MFCC features from the audio
|
| 20 |
-
mfccs = librosa.feature.mfcc(y=y, sr=sr, n_mfcc=13)
|
| 21 |
mfccs = np.mean(mfccs.T, axis=0) # Take the mean of MFCCs over time to reduce dimension
|
| 22 |
|
| 23 |
# Reshape the MFCCs to match the input shape expected by the model
|
|
@@ -30,6 +30,7 @@ def predict_user_or_non_user(audio):
|
|
| 30 |
print(f"Raw Prediction Output: {prediction}")
|
| 31 |
|
| 32 |
# Apply thresholding based on the raw prediction value
|
|
|
|
| 33 |
if prediction[0] > 0.5:
|
| 34 |
return "User"
|
| 35 |
else:
|
|
@@ -41,10 +42,10 @@ def predict_user_or_non_user(audio):
|
|
| 41 |
# Define the Gradio interface
|
| 42 |
iface = gr.Interface(
|
| 43 |
fn=predict_user_or_non_user, # The function to call when an audio input is given
|
| 44 |
-
inputs=gr.Audio(type="
|
| 45 |
outputs="text", # Output will be text (User or Non-user)
|
| 46 |
live=True # Live mode so the interface updates in real-time
|
| 47 |
)
|
| 48 |
|
| 49 |
# Launch the interface
|
| 50 |
-
iface.launch()
|
|
|
|
| 1 |
+
mport gradio as gr
|
| 2 |
import librosa
|
| 3 |
import numpy as np
|
| 4 |
import tensorflow as tf
|
|
|
|
| 10 |
# Function to extract MFCC features and make a prediction
|
| 11 |
def predict_user_or_non_user(audio):
|
| 12 |
try:
|
| 13 |
+
# Load the audio file using librosa
|
| 14 |
+
y, sr = librosa.load(audio, sr=16000) # sr=None to keep the original sampling rate
|
| 15 |
|
| 16 |
# Optional: Normalize the audio volume (if necessary)
|
| 17 |
y = librosa.util.normalize(y)
|
| 18 |
|
| 19 |
# Extract MFCC features from the audio
|
| 20 |
+
mfccs = librosa.feature.mfcc(y=y, sr=sr, n_mfcc=13) # You can adjust n_mfcc as needed
|
| 21 |
mfccs = np.mean(mfccs.T, axis=0) # Take the mean of MFCCs over time to reduce dimension
|
| 22 |
|
| 23 |
# Reshape the MFCCs to match the input shape expected by the model
|
|
|
|
| 30 |
print(f"Raw Prediction Output: {prediction}")
|
| 31 |
|
| 32 |
# Apply thresholding based on the raw prediction value
|
| 33 |
+
# If the model outputs a probability, try adjusting the threshold (e.g., 0.6 instead of 0.5)
|
| 34 |
if prediction[0] > 0.5:
|
| 35 |
return "User"
|
| 36 |
else:
|
|
|
|
| 42 |
# Define the Gradio interface
|
| 43 |
iface = gr.Interface(
|
| 44 |
fn=predict_user_or_non_user, # The function to call when an audio input is given
|
| 45 |
+
inputs=gr.Audio(type="filepath"), # Corrected audio input setup
|
| 46 |
outputs="text", # Output will be text (User or Non-user)
|
| 47 |
live=True # Live mode so the interface updates in real-time
|
| 48 |
)
|
| 49 |
|
| 50 |
# Launch the interface
|
| 51 |
+
iface.launch()
|