jialitan23 commited on
Commit
d38ebb9
·
verified ·
1 Parent(s): d4b6c38

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -10
app.py CHANGED
@@ -10,6 +10,7 @@ model = None
10
  scaler = None
11
  encoder = None
12
  all_feature_columns = None
 
13
  prediction_threshold = 0.5 # Default threshold, will be updated from file
14
 
15
  # --- 2. Load the Model and Preprocessing Tools with Error Handling ---
@@ -35,6 +36,11 @@ try:
35
 
36
  print("All model and preprocessing files loaded successfully.")
37
 
 
 
 
 
 
38
  except FileNotFoundError as e:
39
  print(f"Error: A required file was not found. Please ensure all model artifacts "
40
  f"are in the same directory as this script. Missing file: {e.filename}")
@@ -52,21 +58,15 @@ def predict_fall(movement_activity, location, day_of_week, hour_of_day, minute_o
52
  return "Error: Model not loaded. Check server logs for details."
53
 
54
  try:
55
- # Create a dictionary to hold the input data.
56
- # It is important to initialize with all features set to 0.0 to match the model's
57
- # expected input shape, especially for one-hot encoded columns.
58
  input_data = {col: 0.0 for col in all_feature_columns}
59
-
60
- # Define the numerical features to be scaled
61
- numerical_features = ['hour_of_day', 'minute_of_day', 'time_since_last_event']
62
 
63
- # Populate the numerical features
64
  input_data['hour_of_day'] = float(hour_of_day)
65
  input_data['minute_of_day'] = float(minute_of_day)
66
  input_data['time_since_last_event'] = float(time_since_last_event)
67
 
68
- # Populate the one-hot encoded features
69
- # We need to construct the correct column name for the one-hot encoded features
70
  input_data[f'Movement Activity_{movement_activity}'] = 1.0
71
  input_data[f'Location_{location}'] = 1.0
72
  input_data[f'day_of_week_{day_of_week}'] = 1.0
@@ -74,7 +74,7 @@ def predict_fall(movement_activity, location, day_of_week, hour_of_day, minute_o
74
  # Create a DataFrame from the dictionary, ensuring the column order is correct
75
  input_df = pd.DataFrame([input_data], columns=all_feature_columns)
76
 
77
- # Scale the numerical features using the loaded scaler
78
  input_df[numerical_features] = scaler.transform(input_df[numerical_features])
79
 
80
  # Make the prediction
 
10
  scaler = None
11
  encoder = None
12
  all_feature_columns = None
13
+ numerical_features = [] # Will be populated dynamically
14
  prediction_threshold = 0.5 # Default threshold, will be updated from file
15
 
16
  # --- 2. Load the Model and Preprocessing Tools with Error Handling ---
 
36
 
37
  print("All model and preprocessing files loaded successfully.")
38
 
39
+ # --- NEW: Dynamically separate numerical from categorical features ---
40
+ # This is the most reliable way to ensure consistency.
41
+ # We assume features with an underscore are from the one-hot encoder.
42
+ numerical_features = [col for col in all_feature_columns if '_' not in col]
43
+
44
  except FileNotFoundError as e:
45
  print(f"Error: A required file was not found. Please ensure all model artifacts "
46
  f"are in the same directory as this script. Missing file: {e.filename}")
 
58
  return "Error: Model not loaded. Check server logs for details."
59
 
60
  try:
61
+ # Create a dictionary to hold the input data with all expected columns.
 
 
62
  input_data = {col: 0.0 for col in all_feature_columns}
 
 
 
63
 
64
+ # Populate the numerical features with user input
65
  input_data['hour_of_day'] = float(hour_of_day)
66
  input_data['minute_of_day'] = float(minute_of_day)
67
  input_data['time_since_last_event'] = float(time_since_last_event)
68
 
69
+ # Populate the one-hot encoded features with user input
 
70
  input_data[f'Movement Activity_{movement_activity}'] = 1.0
71
  input_data[f'Location_{location}'] = 1.0
72
  input_data[f'day_of_week_{day_of_week}'] = 1.0
 
74
  # Create a DataFrame from the dictionary, ensuring the column order is correct
75
  input_df = pd.DataFrame([input_data], columns=all_feature_columns)
76
 
77
+ # Now, the scaling should work as the `numerical_features` list is correct
78
  input_df[numerical_features] = scaler.transform(input_df[numerical_features])
79
 
80
  # Make the prediction