Upload app.py
Browse files
app.py
ADDED
@@ -0,0 +1,61 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import numpy as np
|
3 |
+
import tensorflow as tf
|
4 |
+
import librosa
|
5 |
+
import librosa.util
|
6 |
+
|
7 |
+
# Define your predict_class function
|
8 |
+
def predict_class(file_path, model, labels):
|
9 |
+
# Extract MFCC features
|
10 |
+
y, sr = librosa.load(file_path, sr=None)
|
11 |
+
mfcc = librosa.feature.mfcc(y=y, sr=sr, n_mfcc=13)
|
12 |
+
|
13 |
+
# Pad or truncate to 100 frames along axis 1
|
14 |
+
mfcc = librosa.util.fix_length(mfcc, size=100, axis=1)
|
15 |
+
|
16 |
+
# Ensure mfcc has shape (13, 100)
|
17 |
+
if mfcc.shape[0] != 13:
|
18 |
+
mfcc = librosa.util.fix_length(mfcc, size=13, axis=0)
|
19 |
+
|
20 |
+
# Add batch and channel dimensions
|
21 |
+
mfcc = mfcc[np.newaxis, ..., np.newaxis] # Shape: (1, 13, 100, 1)
|
22 |
+
|
23 |
+
# Predict using the model
|
24 |
+
prediction = model.predict(mfcc)
|
25 |
+
predicted_class = labels[np.argmax(prediction)]
|
26 |
+
return predicted_class
|
27 |
+
|
28 |
+
# Load your pre-trained model
|
29 |
+
model = tf.keras.models.load_model("voice_classification_modelm.h5")
|
30 |
+
|
31 |
+
# Define the class labels based on your folder names
|
32 |
+
labels = [
|
33 |
+
"all_vowels_healthy",
|
34 |
+
"allvowels_functional",
|
35 |
+
"allvowels_laryngitis",
|
36 |
+
"allvowels_lukoplakia",
|
37 |
+
"allvowels_psychogenic",
|
38 |
+
"allvowels_rlnp",
|
39 |
+
"allvowels_sd"
|
40 |
+
]
|
41 |
+
|
42 |
+
# Define the Gradio function
|
43 |
+
def classify_audio(audio_file):
|
44 |
+
try:
|
45 |
+
predicted_class = predict_class(audio_file, model, labels)
|
46 |
+
return f"Predicted Class: {predicted_class}"
|
47 |
+
except Exception as e:
|
48 |
+
return f"Error: {str(e)}"
|
49 |
+
|
50 |
+
# Create the Gradio interface
|
51 |
+
interface = gr.Interface(
|
52 |
+
fn=classify_audio,
|
53 |
+
inputs=gr.Audio(source="upload", type="filepath", label="Upload an Audio File"),
|
54 |
+
outputs=gr.Textbox(label="Predicted Class"),
|
55 |
+
title="Voice Classification",
|
56 |
+
description="Upload an audio file to classify its voice type.",
|
57 |
+
examples=["example_audio.wav"] # Replace with paths to sample audio files
|
58 |
+
)
|
59 |
+
|
60 |
+
# Launch the app
|
61 |
+
interface.launch()
|