|
import gradio as gr
|
|
import numpy as np
|
|
import tensorflow as tf
|
|
import librosa
|
|
import librosa.util
|
|
|
|
|
|
def predict_class(file_path, model, labels):
|
|
|
|
y, sr = librosa.load(file_path, sr=None)
|
|
mfcc = librosa.feature.mfcc(y=y, sr=sr, n_mfcc=13)
|
|
|
|
|
|
mfcc = librosa.util.fix_length(mfcc, size=100, axis=1)
|
|
|
|
|
|
if mfcc.shape[0] != 13:
|
|
mfcc = librosa.util.fix_length(mfcc, size=13, axis=0)
|
|
|
|
|
|
mfcc = mfcc[np.newaxis, ..., np.newaxis]
|
|
|
|
|
|
prediction = model.predict(mfcc)
|
|
predicted_class = labels[np.argmax(prediction)]
|
|
return predicted_class
|
|
|
|
|
|
model = tf.keras.models.load_model("voice_classification_modelm.h5")
|
|
|
|
|
|
labels = [
|
|
"all_vowels_healthy",
|
|
"allvowels_functional",
|
|
"allvowels_laryngitis",
|
|
"allvowels_lukoplakia",
|
|
"allvowels_psychogenic",
|
|
"allvowels_rlnp",
|
|
"allvowels_sd"
|
|
]
|
|
|
|
|
|
def classify_audio(audio_file):
|
|
try:
|
|
predicted_class = predict_class(audio_file, model, labels)
|
|
return f"Predicted Class: {predicted_class}"
|
|
except Exception as e:
|
|
return f"Error: {str(e)}"
|
|
|
|
|
|
interface = gr.Interface(
|
|
fn=classify_audio,
|
|
inputs=gr.Audio(source="upload", type="filepath", label="Upload an Audio File"),
|
|
outputs=gr.Textbox(label="Predicted Class"),
|
|
title="Voice Classification",
|
|
description="Upload an audio file to classify its voice type.",
|
|
examples=["example_audio.wav"]
|
|
)
|
|
|
|
|
|
interface.launch()
|
|
|