Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,32 +1,42 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import joblib
|
| 3 |
import pandas as pd
|
| 4 |
-
|
| 5 |
-
|
| 6 |
|
| 7 |
# --- 1. Define all expected features for each model ---
|
|
|
|
| 8 |
SAFETY_FEATURES = [
|
| 9 |
-
'Impact Force Level',
|
| 10 |
'Post-Fall Inactivity Duration (Seconds)',
|
| 11 |
'hour_of_day',
|
| 12 |
-
'day_of_week',
|
| 13 |
-
'
|
| 14 |
-
'
|
| 15 |
-
'
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
]
|
| 17 |
|
| 18 |
HEALTH_FEATURES = [
|
| 19 |
'Heart Rate',
|
| 20 |
'Glucose Levels',
|
| 21 |
-
'Oxygen Saturation (
|
| 22 |
-
'Systolic BP',
|
| 23 |
'Diastolic BP',
|
| 24 |
'hour_of_day',
|
| 25 |
-
'day_of_week'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
]
|
| 27 |
|
| 28 |
# All possible locations for one-hot encoding
|
| 29 |
-
all_locations = ['Living Room', 'Kitchen', 'Bedroom']
|
| 30 |
|
| 31 |
# --- 2. Load the Models and Label Encoders with Error Handling ---
|
| 32 |
try:
|
|
@@ -35,28 +45,27 @@ try:
|
|
| 35 |
safety_label_encoder = joblib.load('safety_label_encoder.joblib')
|
| 36 |
health_label_encoder = joblib.load('health_label_encoder.joblib')
|
| 37 |
print("Models and Label Encoders loaded successfully.")
|
| 38 |
-
# Insert the print statements here to see the expected feature names
|
| 39 |
-
print("Safety Model expects these features:", rf_safety_model.feature_names_in_)
|
| 40 |
-
print("Health Model expects these features:", rf_health_model.feature_names_in_)
|
| 41 |
-
|
| 42 |
except FileNotFoundError as e:
|
| 43 |
print(f"Error: One of the required files was not found. Please ensure all files are in the same directory. {e}")
|
| 44 |
exit()
|
| 45 |
|
| 46 |
# --- 3. Prediction Function for the Safety Model ---
|
| 47 |
-
def predict_safety(
|
| 48 |
"""
|
| 49 |
Prepares and predicts a single row of safety data.
|
| 50 |
"""
|
| 51 |
data = {
|
| 52 |
-
'Impact Force Level': impact_force_level,
|
| 53 |
'Post-Fall Inactivity Duration (Seconds)': post_fall_inactivity_duration,
|
| 54 |
'hour_of_day': hour_of_day,
|
| 55 |
-
'day_of_week': day_of_week
|
|
|
|
|
|
|
|
|
|
| 56 |
}
|
| 57 |
|
|
|
|
| 58 |
for loc in all_locations:
|
| 59 |
-
data[f'
|
| 60 |
|
| 61 |
input_df = pd.DataFrame([data])
|
| 62 |
input_df = input_df[SAFETY_FEATURES]
|
|
@@ -64,10 +73,8 @@ def predict_safety(impact_force_level, post_fall_inactivity_duration, location,
|
|
| 64 |
prediction_encoded = rf_safety_model.predict(input_df)
|
| 65 |
prediction_label = safety_label_encoder.inverse_transform(prediction_encoded)
|
| 66 |
|
| 67 |
-
# This line returns the formatted string with "Yes" or "No"
|
| 68 |
return f"Fall Detected: {prediction_label[0]}"
|
| 69 |
|
| 70 |
-
|
| 71 |
# --- 4. Prediction Function for the Health Model ---
|
| 72 |
def predict_health(heart_rate, glucose_levels, oxygen_saturation, systolic_bp, diastolic_bp, hour_of_day, day_of_week):
|
| 73 |
"""
|
|
@@ -76,11 +83,18 @@ def predict_health(heart_rate, glucose_levels, oxygen_saturation, systolic_bp, d
|
|
| 76 |
data = {
|
| 77 |
'Heart Rate': heart_rate,
|
| 78 |
'Glucose Levels': glucose_levels,
|
| 79 |
-
'Oxygen Saturation (
|
| 80 |
'Systolic BP': systolic_bp,
|
| 81 |
'Diastolic BP': diastolic_bp,
|
| 82 |
'hour_of_day': hour_of_day,
|
| 83 |
-
'day_of_week': day_of_week
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
}
|
| 85 |
|
| 86 |
input_df = pd.DataFrame([data])
|
|
@@ -101,7 +115,6 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 101 |
with gr.Tab("Fall Detection (Safety Model)"):
|
| 102 |
gr.Markdown("### Predict if a fall has occurred based on sensor data.")
|
| 103 |
with gr.Row():
|
| 104 |
-
impact_force = gr.Slider(0.0, 15.0, label="Impact Force Level (g)", info="Impact force detected by the accelerometer.", step=0.1)
|
| 105 |
duration = gr.Slider(0.0, 30.0, label="Post-Fall Inactivity Duration (Seconds)", info="Duration of no movement after impact.", step=0.1)
|
| 106 |
|
| 107 |
location_dropdown = gr.Dropdown(all_locations, label="Location", info="Location where the event occurred.")
|
|
@@ -114,7 +127,7 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 114 |
safety_button = gr.Button("Run Safety Prediction")
|
| 115 |
safety_button.click(
|
| 116 |
fn=predict_safety,
|
| 117 |
-
inputs=[
|
| 118 |
outputs=safety_output
|
| 119 |
)
|
| 120 |
|
|
@@ -125,22 +138,4 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 125 |
glucose_slider = gr.Slider(60, 200, label="Glucose Levels (mg/dL)")
|
| 126 |
|
| 127 |
with gr.Row():
|
| 128 |
-
oxygen_saturation_slider = gr.Slider(85, 100, label="Oxygen Saturation (
|
| 129 |
-
systolic_bp_slider = gr.Slider(80, 180, label="Systolic Blood Pressure (mmHg)")
|
| 130 |
-
|
| 131 |
-
with gr.Row():
|
| 132 |
-
diastolic_bp_slider = gr.Slider(50, 120, label="Diastolic Blood Pressure (mmHg)")
|
| 133 |
-
hour_slider_health = gr.Slider(0, 23, label="Hour of Day", step=1)
|
| 134 |
-
day_dropdown_health = gr.Dropdown(choices=[0, 1, 2, 3, 4, 5, 6], label="Day of Week (0=Monday, 6=Sunday)")
|
| 135 |
-
|
| 136 |
-
health_output = gr.Textbox(label="Prediction Result")
|
| 137 |
-
health_button = gr.Button("Run Health Prediction")
|
| 138 |
-
health_button.click(
|
| 139 |
-
fn=predict_health,
|
| 140 |
-
inputs=[heart_rate_slider, glucose_slider, oxygen_saturation_slider, systolic_bp_slider, diastolic_bp_slider, hour_slider_health, day_dropdown_health],
|
| 141 |
-
outputs=health_output
|
| 142 |
-
)
|
| 143 |
-
|
| 144 |
-
# --- 6. Launch the Gradio App ---
|
| 145 |
-
if __name__ == "__main__":
|
| 146 |
-
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import joblib
|
| 3 |
import pandas as pd
|
| 4 |
+
import numpy as np
|
|
|
|
| 5 |
|
| 6 |
# --- 1. Define all expected features for each model ---
|
| 7 |
+
# These lists are based on the output you provided, but with the one-hot encoded target features removed.
|
| 8 |
SAFETY_FEATURES = [
|
|
|
|
| 9 |
'Post-Fall Inactivity Duration (Seconds)',
|
| 10 |
'hour_of_day',
|
| 11 |
+
'day_of_week',
|
| 12 |
+
'rolling_Post-Fall Inactivity Duration (Seconds)_mean',
|
| 13 |
+
'rolling_hour_of_day_mean',
|
| 14 |
+
'rolling_day_of_week_mean',
|
| 15 |
+
'safety_Bathroom',
|
| 16 |
+
'safety_Bedroom',
|
| 17 |
+
'safety_Kitchen',
|
| 18 |
+
'safety_Living Room'
|
| 19 |
]
|
| 20 |
|
| 21 |
HEALTH_FEATURES = [
|
| 22 |
'Heart Rate',
|
| 23 |
'Glucose Levels',
|
| 24 |
+
'Oxygen Saturation (SpO₂%)',
|
| 25 |
+
'Systolic BP',
|
| 26 |
'Diastolic BP',
|
| 27 |
'hour_of_day',
|
| 28 |
+
'day_of_week',
|
| 29 |
+
'rolling_Heart Rate_mean',
|
| 30 |
+
'rolling_Glucose Levels_mean',
|
| 31 |
+
'rolling_Oxygen Saturation (SpO₂%)_mean',
|
| 32 |
+
'rolling_Systolic BP_mean',
|
| 33 |
+
'rolling_Diastolic BP_mean',
|
| 34 |
+
'rolling_hour_of_day_mean',
|
| 35 |
+
'rolling_day_of_week_mean'
|
| 36 |
]
|
| 37 |
|
| 38 |
# All possible locations for one-hot encoding
|
| 39 |
+
all_locations = ['Living Room', 'Kitchen', 'Bedroom', 'Bathroom']
|
| 40 |
|
| 41 |
# --- 2. Load the Models and Label Encoders with Error Handling ---
|
| 42 |
try:
|
|
|
|
| 45 |
safety_label_encoder = joblib.load('safety_label_encoder.joblib')
|
| 46 |
health_label_encoder = joblib.load('health_label_encoder.joblib')
|
| 47 |
print("Models and Label Encoders loaded successfully.")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
except FileNotFoundError as e:
|
| 49 |
print(f"Error: One of the required files was not found. Please ensure all files are in the same directory. {e}")
|
| 50 |
exit()
|
| 51 |
|
| 52 |
# --- 3. Prediction Function for the Safety Model ---
|
| 53 |
+
def predict_safety(post_fall_inactivity_duration, hour_of_day, day_of_week, location):
|
| 54 |
"""
|
| 55 |
Prepares and predicts a single row of safety data.
|
| 56 |
"""
|
| 57 |
data = {
|
|
|
|
| 58 |
'Post-Fall Inactivity Duration (Seconds)': post_fall_inactivity_duration,
|
| 59 |
'hour_of_day': hour_of_day,
|
| 60 |
+
'day_of_week': day_of_week,
|
| 61 |
+
'rolling_Post-Fall Inactivity Duration (Seconds)_mean': 0, # Placeholder for single prediction
|
| 62 |
+
'rolling_hour_of_day_mean': 0, # Placeholder
|
| 63 |
+
'rolling_day_of_week_mean': 0, # Placeholder
|
| 64 |
}
|
| 65 |
|
| 66 |
+
# Dynamically create one-hot encoded columns for all possible locations
|
| 67 |
for loc in all_locations:
|
| 68 |
+
data[f'safety_{loc}'] = 1 if location == loc else 0
|
| 69 |
|
| 70 |
input_df = pd.DataFrame([data])
|
| 71 |
input_df = input_df[SAFETY_FEATURES]
|
|
|
|
| 73 |
prediction_encoded = rf_safety_model.predict(input_df)
|
| 74 |
prediction_label = safety_label_encoder.inverse_transform(prediction_encoded)
|
| 75 |
|
|
|
|
| 76 |
return f"Fall Detected: {prediction_label[0]}"
|
| 77 |
|
|
|
|
| 78 |
# --- 4. Prediction Function for the Health Model ---
|
| 79 |
def predict_health(heart_rate, glucose_levels, oxygen_saturation, systolic_bp, diastolic_bp, hour_of_day, day_of_week):
|
| 80 |
"""
|
|
|
|
| 83 |
data = {
|
| 84 |
'Heart Rate': heart_rate,
|
| 85 |
'Glucose Levels': glucose_levels,
|
| 86 |
+
'Oxygen Saturation (SpO₂%)': oxygen_saturation,
|
| 87 |
'Systolic BP': systolic_bp,
|
| 88 |
'Diastolic BP': diastolic_bp,
|
| 89 |
'hour_of_day': hour_of_day,
|
| 90 |
+
'day_of_week': day_of_week,
|
| 91 |
+
'rolling_Heart Rate_mean': 0, # Placeholder
|
| 92 |
+
'rolling_Glucose Levels_mean': 0, # Placeholder
|
| 93 |
+
'rolling_Oxygen Saturation (SpO₂%)_mean': 0, # Placeholder
|
| 94 |
+
'rolling_Systolic BP_mean': 0, # Placeholder
|
| 95 |
+
'rolling_Diastolic BP_mean': 0, # Placeholder
|
| 96 |
+
'rolling_hour_of_day_mean': 0, # Placeholder
|
| 97 |
+
'rolling_day_of_week_mean': 0 # Placeholder
|
| 98 |
}
|
| 99 |
|
| 100 |
input_df = pd.DataFrame([data])
|
|
|
|
| 115 |
with gr.Tab("Fall Detection (Safety Model)"):
|
| 116 |
gr.Markdown("### Predict if a fall has occurred based on sensor data.")
|
| 117 |
with gr.Row():
|
|
|
|
| 118 |
duration = gr.Slider(0.0, 30.0, label="Post-Fall Inactivity Duration (Seconds)", info="Duration of no movement after impact.", step=0.1)
|
| 119 |
|
| 120 |
location_dropdown = gr.Dropdown(all_locations, label="Location", info="Location where the event occurred.")
|
|
|
|
| 127 |
safety_button = gr.Button("Run Safety Prediction")
|
| 128 |
safety_button.click(
|
| 129 |
fn=predict_safety,
|
| 130 |
+
inputs=[duration, hour_slider, day_dropdown, location_dropdown],
|
| 131 |
outputs=safety_output
|
| 132 |
)
|
| 133 |
|
|
|
|
| 138 |
glucose_slider = gr.Slider(60, 200, label="Glucose Levels (mg/dL)")
|
| 139 |
|
| 140 |
with gr.Row():
|
| 141 |
+
oxygen_saturation_slider = gr.Slider(85, 100, label="Oxygen Saturation (SpO₂%)")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|