Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import joblib
|
| 4 |
+
import os
|
| 5 |
+
import warnings
|
| 6 |
+
|
| 7 |
+
# Suppress the FutureWarning from pandas
|
| 8 |
+
warnings.simplefilter(action='ignore', category=FutureWarning)
|
| 9 |
+
|
| 10 |
+
# --- Gradio App Components ---
|
| 11 |
+
# List of possible values for the dropdown menus
|
| 12 |
+
locations = ['Bathroom', 'Bedroom', 'Kitchen', 'Living Room']
|
| 13 |
+
days_of_week = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
|
| 14 |
+
|
| 15 |
+
# Load the trained model pipeline from the joblib file
|
| 16 |
+
try:
|
| 17 |
+
# Load the complete pipeline, which includes the pre-processors and the model
|
| 18 |
+
model_pipeline = joblib.load('fall_detection_pipeline.joblib')
|
| 19 |
+
# Load the final prediction threshold
|
| 20 |
+
with open('prediction_threshold.txt', 'r') as f:
|
| 21 |
+
threshold = float(f.read())
|
| 22 |
+
print("Model and threshold loaded successfully.")
|
| 23 |
+
except FileNotFoundError as e:
|
| 24 |
+
model_pipeline = None
|
| 25 |
+
threshold = 0.4 # Default threshold in case of loading error
|
| 26 |
+
print(f"Error: {e}. Model files not found. The app will run in a placeholder mode.")
|
| 27 |
+
except Exception as e:
|
| 28 |
+
model_pipeline = None
|
| 29 |
+
threshold = 0.4
|
| 30 |
+
print(f"An unexpected error occurred while loading files: {e}. The app will run in a placeholder mode.")
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
def predict_fall(location, day_of_week, hour_of_day, minute_of_day, time_since_last_event):
|
| 34 |
+
"""
|
| 35 |
+
Makes a fall prediction based on user inputs using the pre-trained model.
|
| 36 |
+
The function expects raw inputs and the pipeline handles all transformations.
|
| 37 |
+
"""
|
| 38 |
+
# Check if the model was loaded successfully. If not, return an error message.
|
| 39 |
+
if model_pipeline is None:
|
| 40 |
+
return "Prediction model not available. Please ensure model files are correctly saved."
|
| 41 |
+
|
| 42 |
+
try:
|
| 43 |
+
# Create a dictionary to hold the feature values based on the original data columns.
|
| 44 |
+
# This is the format the pipeline expects.
|
| 45 |
+
# The 'Movement Activity' column is not used based on previous analysis, but
|
| 46 |
+
# we can include it if the model requires it. For now, let's stick to the features
|
| 47 |
+
# identified in the previous steps.
|
| 48 |
+
|
| 49 |
+
# The feature names from the notebook cells were:
|
| 50 |
+
# ['Location', 'day_of_week', 'hour_of_day', 'minute_of_day', 'time_since_last_event']
|
| 51 |
+
|
| 52 |
+
# Create a DataFrame from the inputs
|
| 53 |
+
input_data = pd.DataFrame([{
|
| 54 |
+
'Location': location,
|
| 55 |
+
'day_of_week': day_of_week,
|
| 56 |
+
'hour_of_day': hour_of_day,
|
| 57 |
+
'minute_of_day': minute_of_day,
|
| 58 |
+
'time_since_last_event': time_since_last_event
|
| 59 |
+
}])
|
| 60 |
+
|
| 61 |
+
# Use the trained pipeline to get the probability. The pipeline
|
| 62 |
+
# automatically handles the scaling and one-hot encoding.
|
| 63 |
+
pred_proba = model_pipeline.predict_proba(input_data)[0, 1]
|
| 64 |
+
|
| 65 |
+
# Get the final prediction label based on the threshold.
|
| 66 |
+
pred_label = "Fall Detected" if pred_proba >= threshold else "No Fall"
|
| 67 |
+
|
| 68 |
+
return f"Prediction: {pred_label}\nFall Probability: {pred_proba:.2f}"
|
| 69 |
+
|
| 70 |
+
except Exception as e:
|
| 71 |
+
import traceback
|
| 72 |
+
traceback.print_exc()
|
| 73 |
+
return f"An error occurred during prediction: {str(e)}"
|
| 74 |
+
|
| 75 |
+
# Define the Gradio interface
|
| 76 |
+
with gr.Blocks() as demo:
|
| 77 |
+
gr.Markdown("## Elderly Fall Prediction System")
|
| 78 |
+
gr.Markdown("This system uses a trained machine learning model to predict the likelihood of a fall based on behavioral data. **Note:** This is a demonstration and should not be used for medical diagnosis.")
|
| 79 |
+
|
| 80 |
+
with gr.Row():
|
| 81 |
+
location_input = gr.Dropdown(choices=locations, label="Location", value='Living Room')
|
| 82 |
+
day_input = gr.Dropdown(choices=days_of_week, label="Day of Week", value='Monday')
|
| 83 |
+
|
| 84 |
+
with gr.Row():
|
| 85 |
+
hour_input = gr.Slider(minimum=0, maximum=23, step=1, label="Hour of Day", value=12)
|
| 86 |
+
minute_input = gr.Slider(minimum=0, maximum=59, step=1, label="Minute of Day", value=30)
|
| 87 |
+
|
| 88 |
+
time_since_input = gr.Number(label="Time Since Last Event (minutes)", value=60)
|
| 89 |
+
|
| 90 |
+
predict_button = gr.Button("Predict Fall")
|
| 91 |
+
output = gr.Textbox(label="Prediction Result")
|
| 92 |
+
|
| 93 |
+
predict_button.click(
|
| 94 |
+
predict_fall,
|
| 95 |
+
inputs=[location_input, day_input, hour_input, minute_input, time_since_input],
|
| 96 |
+
outputs=output
|
| 97 |
+
)
|
| 98 |
+
|
| 99 |
+
if __name__ == "__main__":
|
| 100 |
+
demo.launch()
|