Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,50 +1,45 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
import joblib
|
| 3 |
import pandas as pd
|
|
|
|
| 4 |
|
| 5 |
-
# Load model
|
| 6 |
-
model = joblib.load('
|
| 7 |
scaler = joblib.load('scaler.joblib')
|
| 8 |
-
|
| 9 |
-
feature_names = joblib.load('feature_names.joblib') # your full feature list
|
| 10 |
|
| 11 |
-
#
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
|
| 16 |
def predict_fall(movement_activity, location, day_of_week, hour_of_day, minute_of_day, time_since_last_event):
|
| 17 |
try:
|
| 18 |
-
#
|
| 19 |
data = {f: 0 for f in feature_names}
|
| 20 |
|
| 21 |
-
#
|
| 22 |
data[f'Movement Activity_{movement_activity}'] = 1
|
| 23 |
data[f'Location_{location}'] = 1
|
| 24 |
data[f'day_of_week_{day_of_week}'] = 1
|
| 25 |
|
| 26 |
-
#
|
| 27 |
data['hour_of_day'] = hour_of_day
|
| 28 |
data['minute_of_day'] = minute_of_day
|
| 29 |
data['time_since_last_event'] = time_since_last_event
|
| 30 |
|
| 31 |
-
#
|
| 32 |
-
input_df = pd.DataFrame([data], columns=feature_names)
|
| 33 |
|
| 34 |
-
#
|
| 35 |
scaler_cols = scaler.feature_names_in_
|
| 36 |
-
|
|
|
|
| 37 |
|
| 38 |
-
#
|
| 39 |
-
|
| 40 |
|
| 41 |
-
#
|
| 42 |
-
# Use .loc to avoid dtype warning, cast scaled_features to float explicitly
|
| 43 |
-
input_df.loc[:, scaler_cols] = scaled_features.astype(float)
|
| 44 |
-
|
| 45 |
-
# 8. Now predict using the model, passing full input_df in model feature order
|
| 46 |
pred_proba = model.predict_proba(input_df)[0, 1]
|
| 47 |
-
|
| 48 |
threshold = 0.4
|
| 49 |
pred_label = "Fall Detected" if pred_proba >= threshold else "No Fall"
|
| 50 |
|
|
@@ -55,22 +50,29 @@ def predict_fall(movement_activity, location, day_of_week, hour_of_day, minute_o
|
|
| 55 |
traceback.print_exc()
|
| 56 |
return f"Error: {str(e)}. Check server logs."
|
| 57 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
gr.Dropdown(location_choices, label="Location"),
|
| 65 |
-
gr.Dropdown(day_choices, label="Day of Week"),
|
| 66 |
-
gr.Slider(0, 23, step=1, label="Hour of Day"),
|
| 67 |
-
gr.Slider(0, 59, step=1, label="Minute of Day"),
|
| 68 |
-
gr.Number(label="Time Since Last Event (seconds)", value=60),
|
| 69 |
-
],
|
| 70 |
-
outputs="text",
|
| 71 |
-
title="Fall Detection Model",
|
| 72 |
-
description="Predicts if a fall is detected based on sensor input."
|
| 73 |
-
)
|
| 74 |
|
| 75 |
if __name__ == "__main__":
|
| 76 |
-
|
|
|
|
|
|
|
| 1 |
import joblib
|
| 2 |
import pandas as pd
|
| 3 |
+
import gradio as gr
|
| 4 |
|
| 5 |
+
# Load model, scaler, feature names etc.
|
| 6 |
+
model = joblib.load('model.joblib')
|
| 7 |
scaler = joblib.load('scaler.joblib')
|
| 8 |
+
feature_names = joblib.load('feature_names.joblib') # list of all features in correct order
|
|
|
|
| 9 |
|
| 10 |
+
# For dropdown options, extract from encoder info or hardcode:
|
| 11 |
+
movement_activities = ['Lying', 'No Movement', 'Sitting', 'Walking']
|
| 12 |
+
locations = ['Bathroom', 'Bedroom', 'Kitchen', 'Living Room']
|
| 13 |
+
days_of_week = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
|
| 14 |
|
| 15 |
def predict_fall(movement_activity, location, day_of_week, hour_of_day, minute_of_day, time_since_last_event):
|
| 16 |
try:
|
| 17 |
+
# Initialize zero data dict for all features
|
| 18 |
data = {f: 0 for f in feature_names}
|
| 19 |
|
| 20 |
+
# One-hot encode categorical features
|
| 21 |
data[f'Movement Activity_{movement_activity}'] = 1
|
| 22 |
data[f'Location_{location}'] = 1
|
| 23 |
data[f'day_of_week_{day_of_week}'] = 1
|
| 24 |
|
| 25 |
+
# Set numeric features
|
| 26 |
data['hour_of_day'] = hour_of_day
|
| 27 |
data['minute_of_day'] = minute_of_day
|
| 28 |
data['time_since_last_event'] = time_since_last_event
|
| 29 |
|
| 30 |
+
# Create DataFrame with float dtype to avoid warnings
|
| 31 |
+
input_df = pd.DataFrame([data], columns=feature_names, dtype=float)
|
| 32 |
|
| 33 |
+
# Scale numeric features only (assumes scaler was fit on these)
|
| 34 |
scaler_cols = scaler.feature_names_in_
|
| 35 |
+
scaled_features = scaler.transform(input_df[scaler_cols])
|
| 36 |
+
input_df.loc[:, scaler_cols] = scaled_features
|
| 37 |
|
| 38 |
+
# Ensure columns are in model's expected order
|
| 39 |
+
input_df = input_df[model.feature_names_in_]
|
| 40 |
|
| 41 |
+
# Predict probability
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
pred_proba = model.predict_proba(input_df)[0, 1]
|
|
|
|
| 43 |
threshold = 0.4
|
| 44 |
pred_label = "Fall Detected" if pred_proba >= threshold else "No Fall"
|
| 45 |
|
|
|
|
| 50 |
traceback.print_exc()
|
| 51 |
return f"Error: {str(e)}. Check server logs."
|
| 52 |
|
| 53 |
+
# Build Gradio interface
|
| 54 |
+
with gr.Blocks() as demo:
|
| 55 |
+
gr.Markdown("## Fall Prediction")
|
| 56 |
+
|
| 57 |
+
with gr.Row():
|
| 58 |
+
movement_input = gr.Dropdown(choices=movement_activities, label="Movement Activity")
|
| 59 |
+
location_input = gr.Dropdown(choices=locations, label="Location")
|
| 60 |
+
day_input = gr.Dropdown(choices=days_of_week, label="Day of Week")
|
| 61 |
+
|
| 62 |
+
with gr.Row():
|
| 63 |
+
hour_input = gr.Slider(minimum=0, maximum=23, step=1, label="Hour of Day")
|
| 64 |
+
minute_input = gr.Slider(minimum=0, maximum=59, step=1, label="Minute of Day")
|
| 65 |
+
|
| 66 |
+
time_since_input = gr.Number(label="Time Since Last Event (minutes)")
|
| 67 |
+
|
| 68 |
+
predict_button = gr.Button("Predict")
|
| 69 |
+
output = gr.Textbox(label="Prediction Result")
|
| 70 |
|
| 71 |
+
predict_button.click(
|
| 72 |
+
predict_fall,
|
| 73 |
+
inputs=[movement_input, location_input, day_input, hour_input, minute_input, time_since_input],
|
| 74 |
+
outputs=output
|
| 75 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
|
| 77 |
if __name__ == "__main__":
|
| 78 |
+
demo.launch()
|