File size: 1,467 Bytes
3176335 d0e121f 3176335 a0a484e 3176335 a0a484e 3176335 a0a484e 3176335 a0a484e 3176335 a0a484e 3176335 a0a484e 3176335 a0a484e | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | import gradio as gr
import pandas as pd
import joblib
# Load model and feature names
model = joblib.load("crash_prediction_model.pkl")
feature_columns = joblib.load("feature_columns.pkl") # Must match training order
# Prediction function
def predict_crash(
frame_width, frame_height,
total_vehicle_count, car_count, truck_count,
bus_count, motorbike_count, vehicle_density,
timestamp_sec
):
# Create DataFrame with correct column names
input_data = pd.DataFrame([[
frame_width, frame_height,
total_vehicle_count, car_count, truck_count,
bus_count, motorbike_count, vehicle_density,
timestamp_sec
]], columns=feature_columns)
# Predict
prediction = model.predict(input_data)[0]
label = "🚨 Crash Likely" if prediction == 1 else "✅ No Crash"
return label
# Gradio interface
iface = gr.Interface(
fn=predict_crash,
title="Traffic Crash Prediction",
description="Enter traffic features to predict if a crash is likely.",
inputs=[
gr.Number(label="Frame Width"),
gr.Number(label="Frame Height"),
gr.Number(label="Total Vehicle Count"),
gr.Number(label="Car Count"),
gr.Number(label="Truck Count"),
gr.Number(label="Bus Count"),
gr.Number(label="Motorbike Count"),
gr.Number(label="Vehicle Density"),
gr.Number(label="Timestamp (in seconds)")
],
outputs="text"
)
# Launch app
iface.launch()
|