umesh369 commited on
Commit
2330c01
·
verified ·
1 Parent(s): aa5e5da

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -19
app.py CHANGED
@@ -9,24 +9,35 @@ model = load_model('voice_authentication_model.keras') # Replace with your mode
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 "User"
28
- else:
29
- return "Non-User"
 
 
 
 
 
 
 
 
 
 
 
30
 
31
  # Define the Gradio interface
32
  iface = gr.Interface(
@@ -37,4 +48,4 @@ iface = gr.Interface(
37
  )
38
 
39
  # Launch the interface
40
- iface.launch()
 
9
 
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=None) # 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
24
+ mfccs = mfccs.reshape(1, -1) # Reshape to 1 sample, with the number of features
25
+
26
+ # Predict the class (user or non-user)
27
+ prediction = model.predict(mfccs)
28
+
29
+ # Debugging: print raw model output
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.6:
35
+ return "User"
36
+ else:
37
+ return "Non-User"
38
+ except Exception as e:
39
+ print(f"Error in prediction: {e}")
40
+ return "Error during prediction"
41
 
42
  # Define the Gradio interface
43
  iface = gr.Interface(
 
48
  )
49
 
50
  # Launch the interface
51
+ iface.launch()