umesh369 commited on
Commit
3f32c35
·
verified ·
1 Parent(s): 77b9e47

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -39
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 the saved model
7
- model = tf.keras.models.load_model("voice_authentication_model.keras")
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
- # Prediction function for user vs non-user
25
- def predict_user_or_nonuser(audio):
26
- # Extract features from the uploaded audio
27
- feature = extract_features(audio.name) # Audio file is passed as Gradio interface's input
28
- if feature is None:
29
- return "Error: Unable to process audio"
 
 
30
 
31
- # Reshape feature to match the model's input format (1, time_steps, 1)
32
- feature = feature.reshape(1, feature.shape[0], 1)
33
 
34
- # Make prediction
35
- prediction = model.predict(feature)
36
 
37
- # Return result: User or Non-User
38
- return "User" if prediction > 0.5 else "Non-User"
 
 
 
39
 
40
- # Create a Gradio interface for the app
41
  iface = gr.Interface(
42
- fn=predict_user_or_nonuser, # Function to call for prediction
43
- inputs=gr.Audio(source="upload", type="file"), # Audio input
44
- outputs="text", # Output: Text label ("User" or "Non-User")
45
- title="Voice Authentication",
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 Gradio app
50
- if __name__ == "__main__":
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()