Spaces:
Runtime error
Runtime error
yohn-maistre
commited on
Commit
·
62596f6
1
Parent(s):
68c099a
Add application files
Browse files- app.py +84 -0
- model/CNN-MFCC.h5 +3 -0
- requirements.txt +0 -0
app.py
ADDED
@@ -0,0 +1,84 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
import matplotlib.pyplot as plt
|
3 |
+
import pandas as pd
|
4 |
+
|
5 |
+
import streamlit as st
|
6 |
+
import tensorflow as tf
|
7 |
+
from keras.models import load_model
|
8 |
+
|
9 |
+
import librosa
|
10 |
+
import librosa.display
|
11 |
+
import seaborn as sns
|
12 |
+
|
13 |
+
#import sounddevice as sd
|
14 |
+
#from scipy.io.wavfile import write
|
15 |
+
|
16 |
+
#import pdb
|
17 |
+
|
18 |
+
st.title('Prediksi Penyakit Saluran Pernapasan')
|
19 |
+
with st.expander('**Konteks Model AI dan Database**'):
|
20 |
+
#st.markdown('**Konteks Model AI dan Database:**')
|
21 |
+
st.caption('*Made with ❤️ by Yose Marthin Giyay*')
|
22 |
+
st.caption('Ini merupakan laman *interface* untuk model Convolutional Neural Network (CNN) yang dilatih menggunakan **TensorFlow 2.11.0**. Model ini dilatih dengan data dari **Respiratory Sound Database** yang dikemas 2 tim peneliti dengan subset populasi pasien di Portugal dan Yunani atas nama *International Conference on Biomedical Health Informatics* (ICHBI)')
|
23 |
+
st.caption('Di sini _library_ **Librosa** digunakan untuk mengekstraksi MFCCs dari *file* audio. MFCC (*Mel-Frequency Cepstral Coefficients*) merupakan format representasi audio. Dengan proses matematis ini, fitur-fitur penting di jangkauan frekuensi alami telinga manusia dapat diekstraksi dari *file* audio dan dijadikan *input* ke model CNN untuk proses pelatihan model/prediksi.')
|
24 |
+
st.caption('Database yang digunakan dapat dijelajah dan/atau diunduh di sini: https://bhichallenge.med.auth.gr/')
|
25 |
+
st.caption('Jurnal ilmiah menyangkut pengumpulan data oleh tim dapat dilihat di sini: https://link.springer.com/chapter/10.1007/978-981-10-7419-6_6')
|
26 |
+
st.caption('Project roadmap: developing a low-cost wireless stethoscope')
|
27 |
+
|
28 |
+
st.subheader('**Kategori diagnosis:**')
|
29 |
+
st.markdown('*- Sehat* \n*- Bronkiektasis* \n*- Bronkiolitis* \n*- Penyakit Paru Obstruktif Kronis (PPOK)* \n*- Pneumonia* \n*- Infeksi Saluran Pernapasan Atas*')
|
30 |
+
st.subheader('Unggah *file* audio dan mulai prediksi')
|
31 |
+
st.caption('*Dalam pengembangan: rekam langsung di laman ini*. Idealnya audio yang digunakan direkam dengan stetoskop di area trakea, bisa gunakan mata stetoskop yang disambung dengan *mic* headset Bluetooth, misalnya. Untuk sekarang, bisa coba fitur *interface* dulu dengan rekaman pernapasan langsung dari mic HP.')
|
32 |
+
st.caption('**Silakan unggah *fail* audio .wav berdurasi ~20 detik**')
|
33 |
+
|
34 |
+
# Definisikan Function untuk prediksi
|
35 |
+
def predict_disease(model, features):
|
36 |
+
# Predict
|
37 |
+
prediction = model.predict(features)
|
38 |
+
c_pred = np.argmax(prediction)
|
39 |
+
|
40 |
+
return prediction, c_pred
|
41 |
+
|
42 |
+
# Muat model yang di latih - version mismatch, manual compile
|
43 |
+
model = load_model('./model/CNN-MFCC.h5', compile=False)
|
44 |
+
model.compile(loss='categorical_crossentropy', metrics=['accuracy'], optimizer='adam')
|
45 |
+
|
46 |
+
# Label
|
47 |
+
clabels = ['Bronchiectasis', 'Bronchiolitis', 'Chronic Obstructive Pulmonary Disease (COPD)', 'Healthy', 'Pneumonia', 'Upper Respiratory Tract Infection (URTI)']
|
48 |
+
clabels_idn = ['Bronkiektasis', 'Bronkiolitis', 'Penyakit Paru Obstruktif Kronis (PPOK)', 'Sehat', 'Radang Paru-Paru', 'Infeksi Saluran Pernapasan Atas']
|
49 |
+
|
50 |
+
# Create a form for input and output components
|
51 |
+
with st.form(key="prediction_form"):
|
52 |
+
# Upload and display audio file
|
53 |
+
uploaded_file = st.file_uploader("Pilih *file* audio (hanya format .WAV)")
|
54 |
+
|
55 |
+
# Proses audio yang diunggah, ekstraksi MFCCs
|
56 |
+
if uploaded_file is not None:
|
57 |
+
# Memuat berkas audio
|
58 |
+
audio, sample_rate = librosa.load(uploaded_file, duration=20)
|
59 |
+
|
60 |
+
# Tampilkan Spektogram Mel
|
61 |
+
st.markdown('Mel Spectrogram')
|
62 |
+
fig, ax = plt.subplots()
|
63 |
+
sns.heatmap(librosa.power_to_db(librosa.feature.melspectrogram(y=audio, sr=sample_rate), ref=np.max))
|
64 |
+
st.pyplot(fig)
|
65 |
+
|
66 |
+
# Ekstraksi MFCCs
|
67 |
+
mfccs = librosa.feature.mfcc(y=audio, sr=sample_rate, n_mfcc=40)
|
68 |
+
# Padding dimensi, dari (20, 862) ke (1, 40, 862, 1)
|
69 |
+
max_pad_len = 862
|
70 |
+
pad_width = max_pad_len - mfccs.shape[1]
|
71 |
+
mfccs = np.pad(mfccs, pad_width=((0, 0), (0, pad_width)), mode='constant')
|
72 |
+
features = np.expand_dims(np.array(mfccs), axis=(0, -1))
|
73 |
+
|
74 |
+
# Submit the prediction request
|
75 |
+
submit_button = st.form_submit_button("Prediksi kemungkinan penyakit")
|
76 |
+
|
77 |
+
# Display the prediction results
|
78 |
+
if submit_button:
|
79 |
+
prediction, c_pred = predict_disease(model, features)
|
80 |
+
max_value = np.max(prediction)
|
81 |
+
formatted_max = np.format_float_positional(max_value*100, precision=2)
|
82 |
+
st.title('Prediksi: ')
|
83 |
+
st.subheader(f'**{clabels_idn[c_pred]}**: {formatted_max}%')
|
84 |
+
st.subheader(f'*{clabels[c_pred]}*')
|
model/CNN-MFCC.h5
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:63b65224e8c8f1a05674bcdf2755675a0181a12ee513aa2bc11eef358b10b636
|
3 |
+
size 585352
|
requirements.txt
ADDED
Binary file (168 Bytes). View file
|
|