crash_detection / app.py
Kankshi's picture
Create app.py
1b889bb verified
Raw
History Blame Contribute Delete
1.6 kB
import gradio as gr
import joblib
import pandas as pd
from datetime import datetime
import numpy as np
# Load the model and scaler
model = joblib.load('crash_detection_model.pkl')
scaler = joblib.load('scaler.pkl')
# Define the prediction function
def predict_crash(accel_x, accel_y, accel_z, gyro_x, gyro_y, gyro_z, timestamp):
# Convert timestamp
timestamp = pd.to_datetime(timestamp)
hour_of_day = timestamp.hour
day_of_week = timestamp.dayofweek
# Create DataFrame
sensor_data = pd.DataFrame([[accel_x, accel_y, accel_z, gyro_x, gyro_y, gyro_z, hour_of_day, day_of_week]],
columns=['accel_x', 'accel_y', 'accel_z', 'gyro_x', 'gyro_y', 'gyro_z', 'hour_of_day', 'day_of_week'])
# Scale the input data
sensor_data_scaled = scaler.transform(sensor_data)
# Predict crash
prediction = model.predict(sensor_data_scaled)
return "Crash Detected" if prediction[0] == 1 else "No Crash Detected"
# Create Gradio interface
iface = gr.Interface(
fn=predict_crash,
inputs=[
gr.Number(label="Accelerometer X"),
gr.Number(label="Accelerometer Y"),
gr.Number(label="Accelerometer Z"),
gr.Number(label="Gyroscope X"),
gr.Number(label="Gyroscope Y"),
gr.Number(label="Gyroscope Z"),
gr.Textbox(label="Timestamp (YYYY-MM-DD HH:MM:SS)")
],
outputs="text",
title="Crash Detection Model",
description="Predicts if a crash has occurred based on accelerometer and gyroscope data."
)
# Launch the Gradio app
if __name__ == "__main__":
iface.launch()