Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import pandas as pd
|
3 |
+
import numpy as np
|
4 |
+
from catboost import CatBoostClassifier
|
5 |
+
|
6 |
+
# Load the trained model
|
7 |
+
@st.cache_resource
|
8 |
+
def load_model():
|
9 |
+
model = CatBoostClassifier()
|
10 |
+
model.load_model('model.cbm') # Ensure you have saved your model as 'model.cbm'
|
11 |
+
return model
|
12 |
+
|
13 |
+
def main():
|
14 |
+
st.title('San Francisco Crime Predictor')
|
15 |
+
|
16 |
+
# Input form
|
17 |
+
st.sidebar.header('Input Parameters')
|
18 |
+
hour = st.sidebar.slider('Hour of Day', 0, 23, 12)
|
19 |
+
month = st.sidebar.slider('Month', 1, 12, 6)
|
20 |
+
day_of_week = st.sidebar.selectbox('Day of Week',
|
21 |
+
['Monday', 'Tuesday', 'Wednesday', 'Thursday',
|
22 |
+
'Friday', 'Saturday', 'Sunday'])
|
23 |
+
pd_district = st.sidebar.selectbox('Police District',
|
24 |
+
['NORTHERN', 'SOUTHERN', 'MISSION', 'CENTRAL',
|
25 |
+
'PARK', 'RICHMOND', 'TARAVAL', 'INGLESIDE',
|
26 |
+
'BAYVIEW', 'TENDERLOIN'])
|
27 |
+
x = st.sidebar.number_input('Longitude', value=-122.42)
|
28 |
+
y = st.sidebar.number_input('Latitude', value=37.77)
|
29 |
+
|
30 |
+
# Encode categorical inputs
|
31 |
+
day_of_week_encoded = pd.Categorical([day_of_week], categories=['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']).codes[0]
|
32 |
+
pd_district_encoded = pd.Categorical([pd_district], categories=['NORTHERN', 'SOUTHERN', 'MISSION', 'CENTRAL', 'PARK', 'RICHMOND', 'TARAVAL', 'INGLESIDE', 'BAYVIEW', 'TENDERLOIN']).codes[0]
|
33 |
+
|
34 |
+
# Make prediction
|
35 |
+
if st.button('Predict Crime Category'):
|
36 |
+
model = load_model()
|
37 |
+
input_data = np.array([[hour, month, day_of_week_encoded, pd_district_encoded, x, y]])
|
38 |
+
prediction = model.predict(input_data)
|
39 |
+
st.write(f'Predicted Crime Category: {prediction[0]}')
|
40 |
+
|
41 |
+
if __name__ == '__main__':
|
42 |
+
main()
|