Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,42 +3,34 @@ import pandas as pd
|
|
| 3 |
import gradio as gr
|
| 4 |
|
| 5 |
# Load model, scaler, feature names etc.
|
| 6 |
-
model = joblib.load('
|
| 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"
|
|
@@ -50,7 +42,6 @@ def predict_fall(movement_activity, location, day_of_week, hour_of_day, minute_o
|
|
| 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 |
|
|
|
|
| 3 |
import gradio as gr
|
| 4 |
|
| 5 |
# Load model, scaler, feature names etc.
|
| 6 |
+
model = joblib.load('fall_detection_model.joblib') # updated here
|
| 7 |
scaler = joblib.load('scaler.joblib')
|
| 8 |
feature_names = joblib.load('feature_names.joblib') # list of all features in correct order
|
| 9 |
|
|
|
|
| 10 |
movement_activities = ['Lying', 'No Movement', 'Sitting', 'Walking']
|
| 11 |
locations = ['Bathroom', 'Bedroom', 'Kitchen', 'Living Room']
|
| 12 |
days_of_week = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
|
| 13 |
|
| 14 |
def predict_fall(movement_activity, location, day_of_week, hour_of_day, minute_of_day, time_since_last_event):
|
| 15 |
try:
|
|
|
|
| 16 |
data = {f: 0 for f in feature_names}
|
| 17 |
|
|
|
|
| 18 |
data[f'Movement Activity_{movement_activity}'] = 1
|
| 19 |
data[f'Location_{location}'] = 1
|
| 20 |
data[f'day_of_week_{day_of_week}'] = 1
|
| 21 |
|
|
|
|
| 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 |
input_df = pd.DataFrame([data], columns=feature_names, dtype=float)
|
| 27 |
|
|
|
|
| 28 |
scaler_cols = scaler.feature_names_in_
|
| 29 |
scaled_features = scaler.transform(input_df[scaler_cols])
|
| 30 |
input_df.loc[:, scaler_cols] = scaled_features
|
| 31 |
|
|
|
|
| 32 |
input_df = input_df[model.feature_names_in_]
|
| 33 |
|
|
|
|
| 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 |
traceback.print_exc()
|
| 43 |
return f"Error: {str(e)}. Check server logs."
|
| 44 |
|
|
|
|
| 45 |
with gr.Blocks() as demo:
|
| 46 |
gr.Markdown("## Fall Prediction")
|
| 47 |
|