| import streamlit as st |
| import pandas as pd |
| import pydeck as pdk |
| import time |
| import joblib |
|
|
| |
| X_test = pd.read_csv("X_test_ingestion_.csv").rename(columns={"Unnamed: 0": "crash_id"}) |
| X_crash = pd.read_csv("data/Motor_Vehicle_Collisions_-_Crashes_20250430.csv", low_memory=False) |
|
|
| |
| model = joblib.load("model.pkl") |
|
|
| |
| st.set_page_config(layout="wide") |
| st.title("π¨ Real-Time Crash Reporting (Simulation)") |
|
|
| |
| alert_placeholder = st.empty() |
|
|
| |
| col1, col2 = st.columns([3, 1]) |
|
|
| |
| placeholder_map = col1.empty() |
| placeholder_table = col1.empty() |
| serious_crashes_placeholder = col2.empty() |
|
|
| |
| if st.button("Start Reporting Crashes"): |
| crash_points = [] |
| serious_crashes = [] |
|
|
| for index, row in X_test.iterrows(): |
| |
| row_crash = X_crash.iloc[row['crash_id']] |
| lat = row_crash["LATITUDE"] |
| lon = row_crash["LONGITUDE"] |
|
|
| if pd.isna(lat) or pd.isna(lon): |
| continue |
|
|
| |
| severity = model.predict(row.iloc[1:].values.reshape(1, -1))[0] |
|
|
| |
| if severity == 1: |
| alert_placeholder.error( |
| f"π¨ Serious Crash Detected! Location: {row_crash['BOROUGH']} on {row_crash['ON STREET NAME']} ({lat:.4f}, {lon:.4f})" |
| ) |
| serious_crashes.append({ |
| "borough": row_crash["BOROUGH"], |
| "street": row_crash["ON STREET NAME"], |
| "latitude": lat, |
| "longitude": lon |
| }) |
| serious_crashes_df = pd.DataFrame(serious_crashes) |
| serious_crashes_placeholder.dataframe(serious_crashes_df) |
| else: |
| alert_placeholder.info("Monitoring for new serious crashes...") |
|
|
| |
| crash_points.append({ |
| "lat": lat, |
| "lon": lon, |
| "severity": "Serious Injury" if severity == 1 else "Not Serious", |
| "color": [255, 0, 0] if severity == 1 else [0, 0, 255], |
| "borough": row_crash["BOROUGH"], |
| "street": row_crash["ON STREET NAME"] |
| }) |
|
|
| df_points = pd.DataFrame(crash_points) |
|
|
| |
| layer = pdk.Layer( |
| "ScatterplotLayer", |
| data=df_points, |
| get_position='[lon, lat]', |
| get_radius=200, |
| get_fill_color='color', |
| pickable=True |
| ) |
|
|
| view_state = pdk.ViewState(latitude=40.75, longitude=-73.95, zoom=10) |
|
|
| |
| placeholder_map.pydeck_chart(pdk.Deck(layers=[layer], initial_view_state=view_state)) |
| placeholder_table.dataframe(df_points) |
|
|
| time.sleep(5) |
|
|
| st.success("β
All crash reports simulated!") |
|
|