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()