Update app.py
Browse files
app.py
CHANGED
|
@@ -1,51 +1,40 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import tensorflow as tf
|
| 3 |
import librosa
|
| 4 |
import numpy as np
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
# Load
|
| 7 |
-
model =
|
| 8 |
-
|
| 9 |
-
# Function to extract features from audio
|
| 10 |
-
def extract_features(file_path):
|
| 11 |
-
try:
|
| 12 |
-
# Load the audio file
|
| 13 |
-
audio, sample_rate = librosa.load(file_path, res_type='kaiser_fast', duration=3, sr=None)
|
| 14 |
-
|
| 15 |
-
# Extract MFCC features (13 MFCC coefficients)
|
| 16 |
-
mfcc = librosa.feature.mfcc(y=audio, sr=sample_rate, n_mfcc=13)
|
| 17 |
-
mfcc = np.mean(mfcc.T, axis=0) # Taking the mean over time
|
| 18 |
-
|
| 19 |
-
return mfcc
|
| 20 |
-
except Exception as e:
|
| 21 |
-
print(f"Error encountered while parsing file: {file_path}. Error: {e}")
|
| 22 |
-
return None
|
| 23 |
|
| 24 |
-
#
|
| 25 |
-
def
|
| 26 |
-
#
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
|
|
|
|
|
|
| 30 |
|
| 31 |
-
# Reshape
|
| 32 |
-
|
| 33 |
|
| 34 |
-
#
|
| 35 |
-
prediction = model.predict(
|
| 36 |
|
| 37 |
-
#
|
| 38 |
-
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
-
#
|
| 41 |
iface = gr.Interface(
|
| 42 |
-
fn=
|
| 43 |
-
inputs=gr.Audio(source="
|
| 44 |
-
outputs="text", # Output
|
| 45 |
-
|
| 46 |
-
description="This application can authenticate users based on their voice. Upload an audio file to check if it's from the user or a non-user."
|
| 47 |
)
|
| 48 |
|
| 49 |
-
# Launch the
|
| 50 |
-
|
| 51 |
-
iface.launch()
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
import librosa
|
| 3 |
import numpy as np
|
| 4 |
+
import tensorflow as tf
|
| 5 |
+
from tensorflow.keras.models import load_model
|
| 6 |
|
| 7 |
+
# Load your pre-trained model (make sure it's in the same directory or provide a path)
|
| 8 |
+
model = load_model('voice_authentication_model.keras') # Replace with your model's filename
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
+
# Function to extract MFCC features and make a prediction
|
| 11 |
+
def predict_user_or_non_user(audio):
|
| 12 |
+
# Load the audio file using librosa
|
| 13 |
+
y, sr = librosa.load(audio, sr=None) # sr=None to keep the original sampling rate
|
| 14 |
+
|
| 15 |
+
# Extract MFCC features from the audio
|
| 16 |
+
mfccs = librosa.feature.mfcc(y=y, sr=sr, n_mfcc=13) # You can adjust n_mfcc as needed
|
| 17 |
+
mfccs = np.mean(mfccs.T, axis=0) # Take the mean of MFCCs over time to reduce dimension
|
| 18 |
|
| 19 |
+
# Reshape the MFCCs to match the input shape expected by the model
|
| 20 |
+
mfccs = mfccs.reshape(1, -1) # Reshape to 1 sample, with the number of features
|
| 21 |
|
| 22 |
+
# Predict the class (user or non-user)
|
| 23 |
+
prediction = model.predict(mfccs)
|
| 24 |
|
| 25 |
+
# Convert prediction to readable format
|
| 26 |
+
if prediction > 0.5:
|
| 27 |
+
return "Non-user"
|
| 28 |
+
else:
|
| 29 |
+
return "User"
|
| 30 |
|
| 31 |
+
# Define the Gradio interface
|
| 32 |
iface = gr.Interface(
|
| 33 |
+
fn=predict_user_or_non_user, # The function to call when an audio input is given
|
| 34 |
+
inputs=gr.Audio(source="microphone", type="filepath"), # Audio input from the microphone
|
| 35 |
+
outputs="text", # Output will be text (User or Non-user)
|
| 36 |
+
live=True # Live mode so the interface updates in real-time
|
|
|
|
| 37 |
)
|
| 38 |
|
| 39 |
+
# Launch the interface
|
| 40 |
+
iface.launch()
|
|
|