Update app.py
Browse files
app.py
CHANGED
|
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import joblib
|
| 3 |
+
import numpy as np
|
| 4 |
+
import pandas as pd
|
| 5 |
+
|
| 6 |
+
# Load the saved model
|
| 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 |
+
|
| 21 |
+
# Prepare input as DataFrame
|
| 22 |
+
input_data = pd.DataFrame([[
|
| 23 |
+
frame_width, frame_height,
|
| 24 |
+
total_vehicle_count, car_count, truck_count, bus_count, motorbike_count,
|
| 25 |
+
vehicle_density
|
| 26 |
+
]], columns=input_features)
|
| 27 |
+
|
| 28 |
+
prediction = model.predict(input_data)[0]
|
| 29 |
+
label = "🚨 Crash Likely" if prediction == 1 else "✅ Normal Traffic"
|
| 30 |
+
return label
|
| 31 |
+
|
| 32 |
+
# Create Gradio interface
|
| 33 |
+
demo = gr.Interface(
|
| 34 |
+
fn=predict_crash,
|
| 35 |
+
inputs=[
|
| 36 |
+
gr.Number(label="Frame Width"),
|
| 37 |
+
gr.Number(label="Frame Height"),
|
| 38 |
+
gr.Number(label="Total Vehicle Count"),
|
| 39 |
+
gr.Number(label="Car Count"),
|
| 40 |
+
gr.Number(label="Truck Count"),
|
| 41 |
+
gr.Number(label="Bus Count"),
|
| 42 |
+
gr.Number(label="Motorbike Count"),
|
| 43 |
+
gr.Number(label="Vehicle Density"),
|
| 44 |
+
],
|
| 45 |
+
outputs=gr.Text(label="Prediction"),
|
| 46 |
+
title="Traffic Crash Prediction",
|
| 47 |
+
description="Enter traffic features to predict if a crash is likely."
|
| 48 |
+
)
|
| 49 |
+
|
| 50 |
+
# Launch
|
| 51 |
+
if __name__ == "__main__":
|
| 52 |
+
demo.launch()
|