Spaces:
Sleeping
Sleeping
| def predict_fall(movement_activity, location, day_of_week, hour_of_day, minute_of_day, time_since_last_event): | |
| try: | |
| data = {f: 0 for f in feature_names} # fallback, keep for all known features | |
| # Set your categorical one-hot | |
| data[f'Movement Activity_{movement_activity}'] = 1 | |
| data[f'Location_{location}'] = 1 | |
| data[f'day_of_week_{day_of_week}'] = 1 | |
| # Set numeric features | |
| data['hour_of_day'] = hour_of_day | |
| data['minute_of_day'] = minute_of_day | |
| data['time_since_last_event'] = time_since_last_event | |
| # Build DataFrame with **all keys of data** (guarantees no missing columns) | |
| input_df = pd.DataFrame([data], columns=list(data.keys())) | |
| scaler_feature_names = scaler.feature_names_in_ | |
| scaled_array = scaler.transform(input_df[scaler_feature_names]) | |
| input_df.loc[:, scaler_feature_names] = scaled_array | |
| pred_proba = model.predict_proba(input_df)[0, 1] | |
| threshold = 0.4 | |
| pred_label = "Fall Detected" if pred_proba >= threshold else "No Fall" | |
| return f"Prediction: {pred_label}\nFall Probability: {pred_proba:.2f}" | |
| except Exception as e: | |
| import traceback | |
| tb = traceback.format_exc() | |
| print("Error in prediction:\n", tb) | |
| return f"Error: {str(e)}\nCheck server logs for details." | |