Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,50 +1,25 @@
|
|
| 1 |
-
|
| 2 |
-
import joblib
|
| 3 |
-
import pandas as pd
|
| 4 |
-
import os
|
| 5 |
-
import traceback
|
| 6 |
-
|
| 7 |
-
# --- Load model and artifacts ---
|
| 8 |
-
try:
|
| 9 |
-
app_dir = os.path.dirname(os.path.abspath(__file__))
|
| 10 |
-
model = joblib.load(os.path.join(app_dir, "fall_detection_model.joblib"))
|
| 11 |
-
scaler = joblib.load(os.path.join(app_dir, "scaler.joblib"))
|
| 12 |
-
encoder = joblib.load(os.path.join(app_dir, "encoder.joblib"))
|
| 13 |
-
feature_names = joblib.load(os.path.join(app_dir, "feature_names.joblib")) # list of all features used during training
|
| 14 |
-
print("Model, scaler, encoder, and feature names loaded successfully.")
|
| 15 |
-
except FileNotFoundError as e:
|
| 16 |
-
print(f"Error: Missing file: {e}")
|
| 17 |
-
exit()
|
| 18 |
-
|
| 19 |
-
# Categorical features and their categories (from encoder)
|
| 20 |
-
categorical_features = ['Movement Activity', 'Location', 'day_of_week']
|
| 21 |
-
categories_map = {cat: encoder.categories_[i].tolist() for i, cat in enumerate(categorical_features)}
|
| 22 |
-
|
| 23 |
-
# --- Prediction function ---
|
| 24 |
-
def predict_fall(movement_activity, location, day_of_week, time_since_last_event):
|
| 25 |
try:
|
| 26 |
-
|
| 27 |
-
data = {f: 0 for f in feature_names}
|
| 28 |
|
| 29 |
-
# Set one-hot
|
| 30 |
data[f'Movement Activity_{movement_activity}'] = 1
|
| 31 |
data[f'Location_{location}'] = 1
|
| 32 |
data[f'day_of_week_{day_of_week}'] = 1
|
| 33 |
|
| 34 |
# Set numeric features
|
|
|
|
|
|
|
| 35 |
data['time_since_last_event'] = time_since_last_event
|
| 36 |
|
| 37 |
-
#
|
| 38 |
-
input_df = pd.DataFrame([data])
|
| 39 |
|
| 40 |
-
# Get columns scaler was trained on
|
| 41 |
scaler_feature_names = scaler.feature_names_in_
|
| 42 |
|
| 43 |
-
# Scale only those columns, keep the rest unchanged
|
| 44 |
scaled_array = scaler.transform(input_df[scaler_feature_names])
|
| 45 |
input_df.loc[:, scaler_feature_names] = scaled_array
|
| 46 |
|
| 47 |
-
# Predict using the model on fully prepared DataFrame
|
| 48 |
pred_proba = model.predict_proba(input_df)[0, 1]
|
| 49 |
threshold = 0.4
|
| 50 |
pred_label = "Fall Detected" if pred_proba >= threshold else "No Fall"
|
|
@@ -52,49 +27,7 @@ def predict_fall(movement_activity, location, day_of_week, time_since_last_event
|
|
| 52 |
return f"Prediction: {pred_label}\nFall Probability: {pred_proba:.2f}"
|
| 53 |
|
| 54 |
except Exception as e:
|
|
|
|
| 55 |
tb = traceback.format_exc()
|
| 56 |
print("Error in prediction:\n", tb)
|
| 57 |
return f"Error: {str(e)}\nCheck server logs for details."
|
| 58 |
-
|
| 59 |
-
# --- Build Gradio interface ---
|
| 60 |
-
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 61 |
-
gr.Markdown("# Fall Detection Model")
|
| 62 |
-
gr.Markdown("Provide sensor data to predict fall detection.")
|
| 63 |
-
|
| 64 |
-
movement_activity_input = gr.Dropdown(
|
| 65 |
-
label="Movement Activity",
|
| 66 |
-
choices=categories_map['Movement Activity'],
|
| 67 |
-
value=categories_map['Movement Activity'][0]
|
| 68 |
-
)
|
| 69 |
-
location_input = gr.Dropdown(
|
| 70 |
-
label="Location",
|
| 71 |
-
choices=categories_map['Location'],
|
| 72 |
-
value=categories_map['Location'][0]
|
| 73 |
-
)
|
| 74 |
-
day_of_week_input = gr.Dropdown(
|
| 75 |
-
label="Day of Week",
|
| 76 |
-
choices=categories_map['day_of_week'],
|
| 77 |
-
value=categories_map['day_of_week'][0]
|
| 78 |
-
)
|
| 79 |
-
hour_of_day_input = gr.Slider(0, 23, step=1, label="hour_of_day")
|
| 80 |
-
minute_of_day_input = gr.Slider(0, 59, step=1, label="minute_of_day")
|
| 81 |
-
time_since_last_event_input = gr.Number(label="time_since_last_event")
|
| 82 |
-
|
| 83 |
-
prediction_output = gr.Textbox(label="Prediction Result", lines=5)
|
| 84 |
-
|
| 85 |
-
predict_button = gr.Button("Run Prediction")
|
| 86 |
-
predict_button.click(
|
| 87 |
-
fn=predict_fall,
|
| 88 |
-
inputs=[
|
| 89 |
-
movement_activity_input,
|
| 90 |
-
location_input,
|
| 91 |
-
day_of_week_input,
|
| 92 |
-
hour_of_day_input,
|
| 93 |
-
minute_of_day_input,
|
| 94 |
-
time_since_last_event_input
|
| 95 |
-
],
|
| 96 |
-
outputs=prediction_output
|
| 97 |
-
)
|
| 98 |
-
|
| 99 |
-
if __name__ == "__main__":
|
| 100 |
-
demo.launch()
|
|
|
|
| 1 |
+
def predict_fall(movement_activity, location, day_of_week, hour_of_day, minute_of_day, time_since_last_event):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
try:
|
| 3 |
+
data = {f: 0 for f in feature_names} # fallback, keep for all known features
|
|
|
|
| 4 |
|
| 5 |
+
# Set your categorical one-hot
|
| 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 numeric features
|
| 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 **all keys of data** (guarantees no missing columns)
|
| 16 |
+
input_df = pd.DataFrame([data], columns=list(data.keys()))
|
| 17 |
|
|
|
|
| 18 |
scaler_feature_names = scaler.feature_names_in_
|
| 19 |
|
|
|
|
| 20 |
scaled_array = scaler.transform(input_df[scaler_feature_names])
|
| 21 |
input_df.loc[:, scaler_feature_names] = scaled_array
|
| 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"
|
|
|
|
| 27 |
return f"Prediction: {pred_label}\nFall Probability: {pred_proba:.2f}"
|
| 28 |
|
| 29 |
except Exception as e:
|
| 30 |
+
import traceback
|
| 31 |
tb = traceback.format_exc()
|
| 32 |
print("Error in prediction:\n", tb)
|
| 33 |
return f"Error: {str(e)}\nCheck server logs for details."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|