Spaces:
Runtime error
Runtime error
feat: :man_dancing: YOLO
Browse files- app.py +90 -0
- patient_outcome_classifier.keras +0 -0
- requirements.txt +4 -0
app.py
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torchaudio
|
| 2 |
+
import torch
|
| 3 |
+
import gradio as gr
|
| 4 |
+
import keras
|
| 5 |
+
import pandas as pd
|
| 6 |
+
|
| 7 |
+
from transformers import pipeline
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
def get_murmur_from_recordings(audio):
|
| 11 |
+
pipe = pipeline("audio-classification",
|
| 12 |
+
model="cogniveon/eeem069_heart_murmur_classification")
|
| 13 |
+
|
| 14 |
+
sampling_rate, data = audio
|
| 15 |
+
waveform = torch.tensor(data).float()
|
| 16 |
+
|
| 17 |
+
# Resample the audio to 16 kHz (if necessary)
|
| 18 |
+
if sampling_rate != 16000:
|
| 19 |
+
resampler = torchaudio.transforms.Resample(sampling_rate, 16000)
|
| 20 |
+
waveform = resampler(waveform)
|
| 21 |
+
|
| 22 |
+
results = pipe(waveform.numpy())
|
| 23 |
+
|
| 24 |
+
sorted_results = sorted(results, key=lambda x: x['score'], reverse=True)
|
| 25 |
+
label_scores = {item['label']: item['score'] for item in sorted_results}
|
| 26 |
+
return label_scores
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
def get_patient_outcome(age, sex, height, weight, is_pregnant, murmur):
|
| 30 |
+
model = keras.models.load_model('patient_outcome_classifier.keras')
|
| 31 |
+
|
| 32 |
+
is_pregnant = 1 if is_pregnant else 0
|
| 33 |
+
sex2int = {'Male': 0, 'Female': 1}
|
| 34 |
+
sex = sex2int[sex]
|
| 35 |
+
|
| 36 |
+
age2int = {'Neonate': 0, 'Infant': 1, 'Child': 2, 'Adolescent': 3}
|
| 37 |
+
age = age2int[age]
|
| 38 |
+
|
| 39 |
+
murmur = 0 if murmur == 'Absent' else (1 if murmur == 'Present' else 2)
|
| 40 |
+
|
| 41 |
+
data = pd.DataFrame({
|
| 42 |
+
'Age': float(age),
|
| 43 |
+
'Sex': float(sex),
|
| 44 |
+
'Height': float(height),
|
| 45 |
+
'Weight': float(weight),
|
| 46 |
+
'Pregnancy status': float(is_pregnant),
|
| 47 |
+
'Murmur': float(murmur),
|
| 48 |
+
}, index=[0])
|
| 49 |
+
|
| 50 |
+
output = model.predict(data)[0]
|
| 51 |
+
# 0 - Normal, 1 - Abnormal
|
| 52 |
+
results = {
|
| 53 |
+
'Normal': output[0],
|
| 54 |
+
'Abnormal': output[1]
|
| 55 |
+
}
|
| 56 |
+
|
| 57 |
+
return results
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
def predict(audio, age, sex, height, weight, is_pregnant):
|
| 61 |
+
assert audio is not None, 'Audio cannot be None'
|
| 62 |
+
|
| 63 |
+
murmur_scores = get_murmur_from_recordings(audio)
|
| 64 |
+
murmur = "Unknown"
|
| 65 |
+
if murmur_scores['Present'] > 0.70:
|
| 66 |
+
murmur = "Present"
|
| 67 |
+
if murmur_scores['Absent'] > 0.80:
|
| 68 |
+
murmur = "Absent"
|
| 69 |
+
|
| 70 |
+
outcome = get_patient_outcome(
|
| 71 |
+
age, sex, height, weight, is_pregnant, murmur)
|
| 72 |
+
|
| 73 |
+
return outcome
|
| 74 |
+
|
| 75 |
+
|
| 76 |
+
demo = gr.Interface(
|
| 77 |
+
fn=predict,
|
| 78 |
+
inputs=[
|
| 79 |
+
"audio",
|
| 80 |
+
gr.Radio(label="Age", choices=[
|
| 81 |
+
"Neonate", "Infant", "Child", "Adolescent"],
|
| 82 |
+
value="Adolescent"),
|
| 83 |
+
gr.Radio(label="Sex", choices=["Male", "Female"], value="Male"),
|
| 84 |
+
gr.Number(label="Height", value="98.0"),
|
| 85 |
+
gr.Number(label="Weight", value="38.1"),
|
| 86 |
+
gr.Checkbox(label="Pregnant", value=False)
|
| 87 |
+
],
|
| 88 |
+
outputs="label"
|
| 89 |
+
)
|
| 90 |
+
demo.launch()
|
patient_outcome_classifier.keras
ADDED
|
Binary file (312 kB). View file
|
|
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
pandas
|
| 2 |
+
torch
|
| 3 |
+
torchaudio
|
| 4 |
+
keras
|