Update app.py
Browse files
app.py
CHANGED
|
@@ -7,8 +7,8 @@ from app.ml_logic import get_combined_prediction
|
|
| 7 |
# --- Description for the Gradio Interface ---
|
| 8 |
title = "Parkinson's Disease Detection System"
|
| 9 |
description = """
|
| 10 |
-
Upload a voice recording and input symptom information to get a prediction for Parkinson's disease.
|
| 11 |
-
This tool uses a combination of a Convolutional Neural Network (CNN) on a voice spectrogram
|
| 12 |
and a machine learning model on user-reported symptoms.
|
| 13 |
"""
|
| 14 |
|
|
@@ -18,20 +18,16 @@ def predict(tremor, stiffness, walking_issue, age, audio_input):
|
|
| 18 |
This function takes inputs from the Gradio interface, processes them,
|
| 19 |
and calls the core machine learning logic.
|
| 20 |
"""
|
| 21 |
-
# 1. Handle the symptom data (convert
|
|
|
|
| 22 |
symptom_data = {
|
| 23 |
-
'tremor': 1 if tremor else 0,
|
| 24 |
-
'stiffness': 1 if stiffness else 0,
|
| 25 |
-
'walking_issue': 1 if walking_issue else 0
|
| 26 |
}
|
| 27 |
|
| 28 |
# 2. Handle the audio data
|
| 29 |
-
# Gradio provides the audio as a tuple (sample_rate, numpy_array)
|
| 30 |
-
# Our ml_logic function expects a file path. So, we save it to a temporary file.
|
| 31 |
sample_rate, audio_data = audio_input
|
| 32 |
-
|
| 33 |
-
# Create a temporary file to store the audio
|
| 34 |
-
# Using a 'with' statement ensures the file is deleted automatically afterward
|
| 35 |
with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as temp_audio_file:
|
| 36 |
sf.write(temp_audio_file.name, audio_data, sample_rate)
|
| 37 |
temp_audio_path = temp_audio_file.name
|
|
@@ -43,7 +39,6 @@ def predict(tremor, stiffness, walking_issue, age, audio_input):
|
|
| 43 |
# 3. Call your existing ML logic function
|
| 44 |
try:
|
| 45 |
final_label, raw_label, score = get_combined_prediction(symptom_data, temp_audio_path, age)
|
| 46 |
-
# Format the score to be more readable
|
| 47 |
score_percentage = f"{score:.2%}"
|
| 48 |
return final_label, raw_label, score_percentage
|
| 49 |
except Exception as e:
|
|
@@ -57,16 +52,18 @@ def predict(tremor, stiffness, walking_issue, age, audio_input):
|
|
| 57 |
|
| 58 |
|
| 59 |
# --- Define the Gradio Interface Components ---
|
|
|
|
| 60 |
inputs = [
|
| 61 |
-
gr.
|
| 62 |
-
gr.
|
| 63 |
-
gr.
|
| 64 |
gr.Number(label="Age", value=50),
|
| 65 |
gr.Audio(type="numpy", label="Upload Voice Recording (.wav, .mp3)")
|
| 66 |
]
|
| 67 |
|
|
|
|
| 68 |
outputs = [
|
| 69 |
-
gr.Textbox(label="Final Diagnosis"),
|
| 70 |
gr.Textbox(label="Raw Model Prediction"),
|
| 71 |
gr.Textbox(label="Confidence Score")
|
| 72 |
]
|
|
@@ -82,4 +79,4 @@ demo = gr.Interface(
|
|
| 82 |
)
|
| 83 |
|
| 84 |
if __name__ == "__main__":
|
| 85 |
-
demo.launch()
|
|
|
|
| 7 |
# --- Description for the Gradio Interface ---
|
| 8 |
title = "Parkinson's Disease Detection System"
|
| 9 |
description = """
|
| 10 |
+
Upload a voice recording and input symptom information to get a prediction for Parkinson's disease.
|
| 11 |
+
This tool uses a combination of a Convolutional Neural Network (CNN) on a voice spectrogram
|
| 12 |
and a machine learning model on user-reported symptoms.
|
| 13 |
"""
|
| 14 |
|
|
|
|
| 18 |
This function takes inputs from the Gradio interface, processes them,
|
| 19 |
and calls the core machine learning logic.
|
| 20 |
"""
|
| 21 |
+
# 1. Handle the symptom data (convert "Yes"/"No" from radio buttons to 0/1)
|
| 22 |
+
# THIS SECTION IS MODIFIED
|
| 23 |
symptom_data = {
|
| 24 |
+
'tremor': 1 if tremor == "Yes" else 0,
|
| 25 |
+
'stiffness': 1 if stiffness == "Yes" else 0,
|
| 26 |
+
'walking_issue': 1 if walking_issue == "Yes" else 0
|
| 27 |
}
|
| 28 |
|
| 29 |
# 2. Handle the audio data
|
|
|
|
|
|
|
| 30 |
sample_rate, audio_data = audio_input
|
|
|
|
|
|
|
|
|
|
| 31 |
with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as temp_audio_file:
|
| 32 |
sf.write(temp_audio_file.name, audio_data, sample_rate)
|
| 33 |
temp_audio_path = temp_audio_file.name
|
|
|
|
| 39 |
# 3. Call your existing ML logic function
|
| 40 |
try:
|
| 41 |
final_label, raw_label, score = get_combined_prediction(symptom_data, temp_audio_path, age)
|
|
|
|
| 42 |
score_percentage = f"{score:.2%}"
|
| 43 |
return final_label, raw_label, score_percentage
|
| 44 |
except Exception as e:
|
|
|
|
| 52 |
|
| 53 |
|
| 54 |
# --- Define the Gradio Interface Components ---
|
| 55 |
+
# THIS SECTION IS MODIFIED
|
| 56 |
inputs = [
|
| 57 |
+
gr.Radio(["Yes", "No"], label="Do you experience tremors at rest?"),
|
| 58 |
+
gr.Radio(["Yes", "No"], label="Do you experience muscle stiffness or rigidity?"),
|
| 59 |
+
gr.Radio(["Yes", "No"], label="Do you have issues with walking (e.g., shuffling, freezing)?"),
|
| 60 |
gr.Number(label="Age", value=50),
|
| 61 |
gr.Audio(type="numpy", label="Upload Voice Recording (.wav, .mp3)")
|
| 62 |
]
|
| 63 |
|
| 64 |
+
# Changed gr.Label to gr.Textbox to be safe
|
| 65 |
outputs = [
|
| 66 |
+
gr.Textbox(label="Final Diagnosis"),
|
| 67 |
gr.Textbox(label="Raw Model Prediction"),
|
| 68 |
gr.Textbox(label="Confidence Score")
|
| 69 |
]
|
|
|
|
| 79 |
)
|
| 80 |
|
| 81 |
if __name__ == "__main__":
|
| 82 |
+
demo.launch() # No share=True needed for Hugging Face
|