Spaces:
Runtime error
Runtime error
| ## libraries for data preprocessing | |
| import numpy as np | |
| import pandas as pd | |
| ## libraries for training dl models | |
| import tensorflow as tf | |
| from tensorflow import keras | |
| ## libraries for reading audio files | |
| import librosa as lib | |
| import gradio as gr | |
| ## lets load the model | |
| model = keras.models.load_model('best_heartbeatsound_classification.h5') | |
| def loading_sound_file(sound_file, sr=22050, duration=10): | |
| input_length = sr * duration | |
| X, sr = lib.load(sound_file, sr=sr, duration=duration) | |
| dur = lib.get_duration(y=X, sr=sr) | |
| # # pad audio file same duration | |
| # if (round(dur) < duration): | |
| # print ("fixing audio lenght :", file_name) | |
| # y = lib.util.fix_length(X, input_length) | |
| # extract normalized mfcc feature from data | |
| # ## pad audio to same duration | |
| # if round(dur) < duration: | |
| # X = lib.util.fix_length(X, input_length) | |
| # Pad or truncate audio file to the same duration | |
| if round(dur) < duration: | |
| pad_amount = input_length - len(X) | |
| X = np.pad(X, (0, pad_amount), mode='constant') | |
| elif round(dur) > duration: | |
| X = X[:input_length] | |
| mfccs = np.mean(lib.feature.mfcc(y=X, sr=sr, n_mfcc=25).T,axis=0) | |
| ## Reshape to match the model's input shape | |
| data = np.array(mfccs).reshape(1, -1, 1) | |
| return data | |
| def heart_signal_classification(data): | |
| X = loading_sound_file(data) | |
| pred = model.predict(X) | |
| ## Define the threshold | |
| threshold = 0.6 | |
| max_prob = np.max(pred) | |
| ## Create labels | |
| labels = { | |
| 0: 'artifact', | |
| 1: 'unlabel', | |
| 2: 'extrastole', | |
| 3: 'extrahls', | |
| 4: 'normal', | |
| 5: 'murmur' | |
| } | |
| if max_prob < threshold: | |
| label = 'unknown' | |
| else: | |
| result = pred[0].argmax() | |
| label = labels[result] | |
| return label | |
| ################### Gradio Web APP ################################ | |
| title = "Heart Signal Classification App" | |
| Input = gr.Audio(sources=["upload"], type="filepath") | |
| Output1 = gr.Textbox(label="Type Of Heart Signal") | |
| description = "Type Of Signal: Artifact, Murmur, Normal, Extrastole, Extrahls" | |
| iface = gr.Interface(fn=heart_signal_classification, inputs=Input, outputs=Output1, title=title, description=description) | |
| iface.launch(inline=False) |