Spaces:
Sleeping
Sleeping
import streamlit as st | |
import pandas as pd | |
import numpy as np | |
from catboost import CatBoostClassifier | |
# Load the trained model | |
def load_model(): | |
model = CatBoostClassifier() | |
model.load_model('model.cbm') # Ensure you have saved your model as 'model.cbm' | |
return model | |
def main(): | |
st.title('San Francisco Crime Predictor') | |
# Input form | |
st.sidebar.header('Input Parameters') | |
hour = st.sidebar.slider('Hour of Day', 0, 23, 12) | |
month = st.sidebar.slider('Month', 1, 12, 6) | |
day_of_week = st.sidebar.selectbox('Day of Week', | |
['Monday', 'Tuesday', 'Wednesday', 'Thursday', | |
'Friday', 'Saturday', 'Sunday']) | |
pd_district = st.sidebar.selectbox('Police District', | |
['NORTHERN', 'SOUTHERN', 'MISSION', 'CENTRAL', | |
'PARK', 'RICHMOND', 'TARAVAL', 'INGLESIDE', | |
'BAYVIEW', 'TENDERLOIN']) | |
x = st.sidebar.number_input('Longitude', value=-122.42) | |
y = st.sidebar.number_input('Latitude', value=37.77) | |
# Encode categorical inputs | |
day_of_week_encoded = pd.Categorical([day_of_week], categories=['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']).codes[0] | |
pd_district_encoded = pd.Categorical([pd_district], categories=['NORTHERN', 'SOUTHERN', 'MISSION', 'CENTRAL', 'PARK', 'RICHMOND', 'TARAVAL', 'INGLESIDE', 'BAYVIEW', 'TENDERLOIN']).codes[0] | |
# Make prediction | |
if st.button('Predict Crime Category'): | |
model = load_model() | |
input_data = np.array([[hour, month, day_of_week_encoded, pd_district_encoded, x, y]]) | |
prediction = model.predict(input_data) | |
st.write(f'Predicted Crime Category: {prediction[0]}') | |
if __name__ == '__main__': | |
main() |