Update app.py
Browse files
app.py
CHANGED
|
@@ -1,54 +1,57 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
import joblib
|
| 3 |
import numpy as np
|
| 4 |
-
import pandas as pd
|
| 5 |
|
| 6 |
-
# Load
|
| 7 |
model = joblib.load("crash_prediction_model.pkl")
|
| 8 |
-
|
| 9 |
-
# Define input features (must match training features order!)
|
| 10 |
-
input_features = [
|
| 11 |
-
"frame_width", "frame_height",
|
| 12 |
-
"total_vehicle_count", "car_count", "truck_count", "bus_count", "motorbike_count",
|
| 13 |
-
"vehicle_density"
|
| 14 |
-
]
|
| 15 |
|
| 16 |
# Define the prediction function
|
| 17 |
def predict_crash(frame_width, frame_height, total_vehicle_count,
|
| 18 |
car_count, truck_count, bus_count, motorbike_count,
|
| 19 |
-
vehicle_density):
|
|
|
|
| 20 |
try:
|
|
|
|
| 21 |
input_data = pd.DataFrame([[
|
| 22 |
-
frame_width, frame_height,
|
| 23 |
-
|
| 24 |
-
vehicle_density
|
| 25 |
-
]], columns=
|
| 26 |
|
|
|
|
| 27 |
prediction = model.predict(input_data)[0]
|
|
|
|
|
|
|
|
|
|
| 28 |
label = "🚨 Crash Likely" if prediction == 1 else "✅ Normal Traffic"
|
| 29 |
-
|
|
|
|
|
|
|
| 30 |
except Exception as e:
|
| 31 |
return f"❌ Error: {str(e)}"
|
| 32 |
|
| 33 |
-
|
| 34 |
-
# Create Gradio interface
|
| 35 |
demo = gr.Interface(
|
| 36 |
fn=predict_crash,
|
| 37 |
inputs=[
|
| 38 |
-
gr.Number(label="Frame Width"),
|
| 39 |
-
gr.Number(label="Frame Height"),
|
| 40 |
gr.Number(label="Total Vehicle Count"),
|
| 41 |
gr.Number(label="Car Count"),
|
| 42 |
gr.Number(label="Truck Count"),
|
| 43 |
gr.Number(label="Bus Count"),
|
| 44 |
gr.Number(label="Motorbike Count"),
|
| 45 |
-
gr.Number(label="Vehicle Density"),
|
|
|
|
| 46 |
],
|
| 47 |
outputs=gr.Text(label="Prediction"),
|
| 48 |
-
title="Traffic Crash Prediction",
|
| 49 |
-
description="Enter traffic features to predict if a crash is likely."
|
|
|
|
| 50 |
)
|
| 51 |
|
| 52 |
-
# Launch
|
| 53 |
if __name__ == "__main__":
|
| 54 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import pandas as pd
|
| 3 |
import joblib
|
| 4 |
import numpy as np
|
|
|
|
| 5 |
|
| 6 |
+
# Load saved model and feature names
|
| 7 |
model = joblib.load("crash_prediction_model.pkl")
|
| 8 |
+
features = joblib.load("feature_columns.pkl")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
# Define the prediction function
|
| 11 |
def predict_crash(frame_width, frame_height, total_vehicle_count,
|
| 12 |
car_count, truck_count, bus_count, motorbike_count,
|
| 13 |
+
vehicle_density, timestamp_sec):
|
| 14 |
+
|
| 15 |
try:
|
| 16 |
+
# Input array in order of training features
|
| 17 |
input_data = pd.DataFrame([[
|
| 18 |
+
frame_width, frame_height, total_vehicle_count,
|
| 19 |
+
car_count, truck_count, bus_count, motorbike_count,
|
| 20 |
+
vehicle_density, timestamp_sec
|
| 21 |
+
]], columns=features)
|
| 22 |
|
| 23 |
+
# Predict using the loaded model
|
| 24 |
prediction = model.predict(input_data)[0]
|
| 25 |
+
prob = model.predict_proba(input_data)[0][1]
|
| 26 |
+
|
| 27 |
+
# Output label
|
| 28 |
label = "🚨 Crash Likely" if prediction == 1 else "✅ Normal Traffic"
|
| 29 |
+
confidence = f"Confidence: {prob:.2%}"
|
| 30 |
+
return f"{label}\n{confidence}"
|
| 31 |
+
|
| 32 |
except Exception as e:
|
| 33 |
return f"❌ Error: {str(e)}"
|
| 34 |
|
| 35 |
+
# Gradio Interface Setup
|
|
|
|
| 36 |
demo = gr.Interface(
|
| 37 |
fn=predict_crash,
|
| 38 |
inputs=[
|
| 39 |
+
gr.Number(label="Frame Width (e.g., 1920)"),
|
| 40 |
+
gr.Number(label="Frame Height (e.g., 1080)"),
|
| 41 |
gr.Number(label="Total Vehicle Count"),
|
| 42 |
gr.Number(label="Car Count"),
|
| 43 |
gr.Number(label="Truck Count"),
|
| 44 |
gr.Number(label="Bus Count"),
|
| 45 |
gr.Number(label="Motorbike Count"),
|
| 46 |
+
gr.Number(label="Vehicle Density (e.g., 0.02)"),
|
| 47 |
+
gr.Number(label="Timestamp (seconds)"),
|
| 48 |
],
|
| 49 |
outputs=gr.Text(label="Prediction"),
|
| 50 |
+
title="🚦 Traffic Crash Prediction",
|
| 51 |
+
description="Enter traffic features to predict if a crash is likely using a hybrid ensemble model.",
|
| 52 |
+
allow_flagging="never"
|
| 53 |
)
|
| 54 |
|
| 55 |
+
# Launch app (for local testing)
|
| 56 |
if __name__ == "__main__":
|
| 57 |
demo.launch()
|