Update app.py
Browse files123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354import gradio as grimport joblibimport numpy as npimport pandas as pd# Load the saved modelmodel = joblib.load("crash_prediction_model.pkl")# Define input features (must match training features order!)input_features = ["frame_width", "frame_height","total_vehicle_count", "car_count", "truck_count", "bus_count", "motorbike_count","vehicle_density"]# Define the prediction functiondef predict_crash(frame_width, frame_height, total_vehicle_count,car_count, truck_count, bus_count, motorbike_count,vehicle_density):try:input_data = pd.DataFrame([[frame_width, frame_height,total_vehicle_count, car_count, truck_count, bus_count, motorbike_count,vehicle_density]], columns=input_features)prediction = model.predict(input_data)[0]label = "π¨ Crash Likely" if prediction == 1 else "β Normal Traffic"return labelexcept Exception as e:return f"β Error: {str(e)}"# Create Gradio interfacedemo = gr.Interface(fn=predict_crash,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"),],outputs=gr.Text(label="Prediction"),title="Traffic Crash Prediction",description="Enter traffic features to predict if a crash is likely.")# Launchif __name__ == "__main__":demo.launch()123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657import gradio as grimport pandas as pdimport joblibimport numpy as np# Load saved model and feature namesmodel = joblib.load("crash_prediction_model.pkl")features = joblib.load("feature_columns.pkl")# Define the prediction functiondef predict_crash(frame_width, frame_height, total_vehicle_count,car_count, truck_count, bus_count, motorbike_count,vehicle_density, timestamp_sec):try:# Input array in order of training featuresinput_data = pd.DataFrame([[frame_width, frame_height, total_vehicle_count,car_count, truck_count, bus_count, motorbike_count,vehicle_density, timestamp_sec]], columns=features)# Predict using the loaded modelprediction = model.predict(input_data)[0]prob = model.predict_proba(input_data)[0][1]# Output labellabel = "π¨ Crash Likely" if prediction == 1 else "β Normal Traffic"confidence = f"Confidence: {prob:.2%}"return f"{label}\n{confidence}"except Exception as e:return f"β Error: {str(e)}"# Gradio Interface Setupdemo = gr.Interface(fn=predict_crash,inputs=[gr.Number(label="Frame Width (e.g., 1920)"),gr.Number(label="Frame Height (e.g., 1080)"),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 (e.g., 0.02)"),gr.Number(label="Timestamp (seconds)"),],outputs=gr.Text(label="Prediction"),title="π¦ Traffic Crash Prediction",description="Enter traffic features to predict if a crash is likely using a hybrid ensemble model.",allow_flagging="never")# Launch app (for local testing)if __name__ == "__main__":demo.launch()
app.py+26-23