umesh369 commited on
Commit
298f686
·
verified ·
1 Parent(s): a84a126

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -32
app.py CHANGED
@@ -1,40 +1,64 @@
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 "User"
28
- else:
29
- return "Non-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(type="filepath"), # Corrected audio input setup
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()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
  import librosa
3
  import numpy as np
4
  import tensorflow as tf
5
+ from flask import Flask, request, jsonify
6
  from tensorflow.keras.models import load_model
7
 
8
+ # Load your pre-trained model (make sure it's in the same directory or provide the correct path)
9
+ MODEL_PATH = 'voice_authentication_model.keras'
10
+ model = load_model(MODEL_PATH)
11
 
12
+ app = Flask(__name__)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
 
14
+ # Helper function to extract MFCC features
15
+ def extract_mfcc_features(audio_path):
16
+ try:
17
+ # Load the audio file using librosa
18
+ y, sr = librosa.load(audio_path, sr=None) # sr=None keeps the original sampling rate
19
+
20
+ # Extract MFCC features
21
+ mfccs = librosa.feature.mfcc(y=y, sr=sr, n_mfcc=13) # Adjust n_mfcc as needed
22
+ mfccs = np.mean(mfccs.T, axis=0) # Average MFCCs over time to reduce dimensions
23
+
24
+ # Reshape MFCCs for model input
25
+ return mfccs.reshape(1, -1) # 1 sample with the number of features
26
+ except Exception as e:
27
+ raise ValueError(f"Error processing audio file: {e}")
28
 
29
+ # Define the home route for basic health check
30
+ @app.route('/')
31
+ def home():
32
+ return "Voice Authentication API is running!"
33
+
34
+ # Define the prediction endpoint
35
+ @app.route('/predict', methods=['POST'])
36
+ def predict():
37
+ try:
38
+ # Check if audio file is present in the request
39
+ if 'file' not in request.files:
40
+ return jsonify({"error": "No file provided. Please upload an audio file."}), 400
41
+
42
+ # Save the uploaded file to a temporary location
43
+ audio_file = request.files['file']
44
+ file_path = os.path.join("temp_audio.wav")
45
+ audio_file.save(file_path)
46
+
47
+ # Extract features from the audio file
48
+ mfccs = extract_mfcc_features(file_path)
49
+
50
+ # Perform prediction
51
+ prediction = model.predict(mfccs)
52
+
53
+ # Convert prediction to user-readable format
54
+ result = "User" if prediction > 0.5 else "Non-User"
55
+
56
+ # Clean up the temporary file
57
+ os.remove(file_path)
58
+
59
+ return jsonify({"prediction": result})
60
+ except Exception as e:
61
+ return jsonify({"error": str(e)}), 500
62
+
63
+ if __name__ == "__main__":
64
+ app.run(host="0.0.0.0", port=7860)