Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -10,17 +10,17 @@ scaler = None
|
|
| 10 |
encoder = None
|
| 11 |
all_feature_columns = None
|
| 12 |
PREDICTION_THRESHOLD = 0.4
|
|
|
|
| 13 |
|
| 14 |
try:
|
| 15 |
app_dir = os.path.dirname(os.path.abspath(__file__))
|
| 16 |
|
| 17 |
# Load the model and preprocessing objects directly from the current directory
|
| 18 |
-
model = joblib.load(os.path.join(app_dir, '
|
| 19 |
scaler = joblib.load(os.path.join(app_dir, 'scaler.joblib'))
|
| 20 |
encoder = joblib.load(os.path.join(app_dir, 'encoder.joblib'))
|
| 21 |
|
| 22 |
-
|
| 23 |
-
|
| 24 |
encoded_feature_names = encoder.get_feature_names_out(features_to_encode)
|
| 25 |
all_feature_columns = encoded_feature_names.tolist() + ['time_since_last_event']
|
| 26 |
|
|
@@ -46,6 +46,8 @@ def predict_fall(movement_activity, location, day_of_week, time_since_last_event
|
|
| 46 |
input_df = pd.DataFrame(input_data)
|
| 47 |
|
| 48 |
# --- Preprocessing the Input Data ---
|
|
|
|
|
|
|
| 49 |
encoded_features = encoder.transform(input_df[features_to_encode])
|
| 50 |
encoded_df = pd.DataFrame(
|
| 51 |
encoded_features,
|
|
@@ -53,11 +55,14 @@ def predict_fall(movement_activity, location, day_of_week, time_since_last_event
|
|
| 53 |
index=input_df.index
|
| 54 |
)
|
| 55 |
|
| 56 |
-
|
| 57 |
-
processed_df = pd.concat([
|
| 58 |
|
|
|
|
|
|
|
| 59 |
processed_df = processed_df.reindex(columns=all_feature_columns, fill_value=0)
|
| 60 |
|
|
|
|
| 61 |
processed_df['time_since_last_event'] = scaler.transform(processed_df[['time_since_last_event']])
|
| 62 |
|
| 63 |
# --- Prediction ---
|
|
|
|
| 10 |
encoder = None
|
| 11 |
all_feature_columns = None
|
| 12 |
PREDICTION_THRESHOLD = 0.4
|
| 13 |
+
features_to_encode = ['Movement Activity', 'Location', 'day_of_week']
|
| 14 |
|
| 15 |
try:
|
| 16 |
app_dir = os.path.dirname(os.path.abspath(__file__))
|
| 17 |
|
| 18 |
# Load the model and preprocessing objects directly from the current directory
|
| 19 |
+
model = joblib.load(os.path.join(app_dir, 'model.joblib'))
|
| 20 |
scaler = joblib.load(os.path.join(app_dir, 'scaler.joblib'))
|
| 21 |
encoder = joblib.load(os.path.join(app_dir, 'encoder.joblib'))
|
| 22 |
|
| 23 |
+
# Get the complete list of column names that the model was trained on
|
|
|
|
| 24 |
encoded_feature_names = encoder.get_feature_names_out(features_to_encode)
|
| 25 |
all_feature_columns = encoded_feature_names.tolist() + ['time_since_last_event']
|
| 26 |
|
|
|
|
| 46 |
input_df = pd.DataFrame(input_data)
|
| 47 |
|
| 48 |
# --- Preprocessing the Input Data ---
|
| 49 |
+
|
| 50 |
+
# One-hot encode the categorical features
|
| 51 |
encoded_features = encoder.transform(input_df[features_to_encode])
|
| 52 |
encoded_df = pd.DataFrame(
|
| 53 |
encoded_features,
|
|
|
|
| 55 |
index=input_df.index
|
| 56 |
)
|
| 57 |
|
| 58 |
+
# Combine numerical and categorical data
|
| 59 |
+
processed_df = pd.concat([encoded_df, input_df[['time_since_last_event']]], axis=1)
|
| 60 |
|
| 61 |
+
# Reindex the DataFrame to ensure it has all the columns the model expects.
|
| 62 |
+
# This is the key fix. Any missing columns will be added with a value of 0.
|
| 63 |
processed_df = processed_df.reindex(columns=all_feature_columns, fill_value=0)
|
| 64 |
|
| 65 |
+
# Scale the numerical feature
|
| 66 |
processed_df['time_since_last_event'] = scaler.transform(processed_df[['time_since_last_event']])
|
| 67 |
|
| 68 |
# --- Prediction ---
|