Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,25 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
def predict_fall(movement_activity, location, day_of_week, hour_of_day, minute_of_day, time_since_last_event):
|
| 2 |
try:
|
| 3 |
-
|
|
|
|
| 4 |
|
| 5 |
-
#
|
| 6 |
data[f'Movement Activity_{movement_activity}'] = 1
|
| 7 |
data[f'Location_{location}'] = 1
|
| 8 |
data[f'day_of_week_{day_of_week}'] = 1
|
| 9 |
|
| 10 |
-
# Set
|
| 11 |
data['hour_of_day'] = hour_of_day
|
| 12 |
data['minute_of_day'] = minute_of_day
|
| 13 |
data['time_since_last_event'] = time_since_last_event
|
| 14 |
|
| 15 |
-
# Build DataFrame with
|
| 16 |
-
input_df = pd.DataFrame([data], columns=
|
| 17 |
-
|
| 18 |
-
scaler_feature_names = scaler.feature_names_in_
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
|
|
|
| 22 |
|
|
|
|
| 23 |
pred_proba = model.predict_proba(input_df)[0, 1]
|
| 24 |
threshold = 0.4
|
| 25 |
pred_label = "Fall Detected" if pred_proba >= threshold else "No Fall"
|
|
@@ -31,3 +42,27 @@ def predict_fall(movement_activity, location, day_of_week, hour_of_day, minute_o
|
|
| 31 |
tb = traceback.format_exc()
|
| 32 |
print("Error in prediction:\n", tb)
|
| 33 |
return f"Error: {str(e)}\nCheck server logs for details."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import joblib
|
| 3 |
+
import pandas as pd
|
| 4 |
+
|
| 5 |
+
# Load saved objects
|
| 6 |
+
model = joblib.load('fall_detection_model.joblib')
|
| 7 |
+
scaler = joblib.load('scaler.joblib')
|
| 8 |
+
encoder = joblib.load('encoder.joblib')
|
| 9 |
+
feature_names = joblib.load('feature_names.joblib') # make sure this has hour_of_day and minute_of_day
|
| 10 |
+
|
| 11 |
def predict_fall(movement_activity, location, day_of_week, hour_of_day, minute_of_day, time_since_last_event):
|
| 12 |
try:
|
| 13 |
+
# Initialize all features with zero
|
| 14 |
+
data = {f: 0 for f in feature_names}
|
| 15 |
|
| 16 |
+
# One-hot encode categoricals
|
| 17 |
data[f'Movement Activity_{movement_activity}'] = 1
|
| 18 |
data[f'Location_{location}'] = 1
|
| 19 |
data[f'day_of_week_{day_of_week}'] = 1
|
| 20 |
|
| 21 |
+
# Set numerical features
|
| 22 |
data['hour_of_day'] = hour_of_day
|
| 23 |
data['minute_of_day'] = minute_of_day
|
| 24 |
data['time_since_last_event'] = time_since_last_event
|
| 25 |
|
| 26 |
+
# Build DataFrame with columns ordered as feature_names
|
| 27 |
+
input_df = pd.DataFrame([data], columns=feature_names)
|
|
|
|
|
|
|
| 28 |
|
| 29 |
+
# Scale numeric features
|
| 30 |
+
scaled_array = scaler.transform(input_df)
|
| 31 |
+
input_df.loc[:, feature_names] = scaled_array
|
| 32 |
|
| 33 |
+
# Predict probability
|
| 34 |
pred_proba = model.predict_proba(input_df)[0, 1]
|
| 35 |
threshold = 0.4
|
| 36 |
pred_label = "Fall Detected" if pred_proba >= threshold else "No Fall"
|
|
|
|
| 42 |
tb = traceback.format_exc()
|
| 43 |
print("Error in prediction:\n", tb)
|
| 44 |
return f"Error: {str(e)}\nCheck server logs for details."
|
| 45 |
+
|
| 46 |
+
# Prepare dropdown choices from encoder categories
|
| 47 |
+
movement_choices = encoder.categories_[0].tolist()
|
| 48 |
+
location_choices = encoder.categories_[1].tolist()
|
| 49 |
+
day_choices = [col.replace('day_of_week_', '') for col in feature_names if col.startswith('day_of_week_')]
|
| 50 |
+
|
| 51 |
+
# Gradio Interface
|
| 52 |
+
iface = gr.Interface(
|
| 53 |
+
fn=predict_fall,
|
| 54 |
+
inputs=[
|
| 55 |
+
gr.Dropdown(choices=movement_choices, label="Movement Activity"),
|
| 56 |
+
gr.Dropdown(choices=location_choices, label="Location"),
|
| 57 |
+
gr.Dropdown(choices=day_choices, label="Day of Week"),
|
| 58 |
+
gr.Slider(0, 23, step=1, label="Hour of Day"),
|
| 59 |
+
gr.Slider(0, 59, step=1, label="Minute of Day"),
|
| 60 |
+
gr.Number(label="Time Since Last Event (seconds)", value=60)
|
| 61 |
+
],
|
| 62 |
+
outputs="text",
|
| 63 |
+
title="Fall Detection Model",
|
| 64 |
+
description="Predicts fall based on sensor data."
|
| 65 |
+
)
|
| 66 |
+
|
| 67 |
+
if __name__ == "__main__":
|
| 68 |
+
iface.launch()
|