Spaces:
Sleeping
Sleeping
File size: 2,764 Bytes
8f460b5 1840ab8 d481617 0897c41 d481617 17e052c 4158e15 d481617 4158e15 353254e 1840ab8 f4ba322 4158e15 f4ba322 0897c41 4158e15 d481617 4158e15 d481617 4158e15 1840ab8 4158e15 f4ba322 4158e15 17e052c 4158e15 f4ba322 4158e15 f4ba322 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
import streamlit as st
import pandas as pd
from sklearn.linear_model import LinearRegression
import matplotlib.pyplot as plt
# Page configuration
st.set_page_config(page_title="Crime Rate Predictor", layout="centered")
st.title("🔮 Crime Rate Prediction for Indian States/UTs")
# CSV path (Hosted online)
csv_path = "https://huggingface.co/spaces/MLDeveloper/crime_rate_predicition/resolve/main/RS_Session_255_AS_116.1%20(2).csv"
try:
# Load and preprocess data
df = pd.read_csv(csv_path)
data = df[[
'State/UT',
'Number of Cases Registered - 2018-19',
'Number of Cases Registered - 2019-20',
'Number of Cases Registered - 2020-21',
'Number of Cases Registered - 2021-22 (up to 31.10.2021)'
]].copy()
data.columns = ['State/UT', '2018', '2019', '2020', '2021']
for col in ['2018', '2019', '2020', '2021']:
data[col] = pd.to_numeric(data[col], errors='coerce').fillna(0).astype(int)
# --- User Inputs ---
st.subheader("📝 Enter Details to Predict Future Crime Rates")
state_input = st.text_input("Enter State/UT Name (e.g., Maharashtra)", "")
year_input = st.slider("Select Starting Year", 2022, 2026, 2022)
if state_input:
if state_input in data['State/UT'].values:
selected_row = data[data['State/UT'] == state_input].iloc[0]
X_train = pd.DataFrame({'Year': [2018, 2019, 2020, 2021]})
y_train = selected_row[['2018', '2019', '2020', '2021']].values
# Train model and predict
model = LinearRegression()
model.fit(X_train, y_train)
future_years = list(range(year_input, 2028))
predictions = model.predict(pd.DataFrame({'Year': future_years}))
result_df = pd.DataFrame({
'Year': future_years,
'Predicted Crime Cases': [max(0, int(pred)) for pred in predictions]
})
# Show predictions
st.subheader(f"📈 Predicted Crime Rate for {state_input} ({year_input} to 2027)")
st.dataframe(result_df, use_container_width=True)
# Plot
fig, ax = plt.subplots()
ax.plot(result_df['Year'], result_df['Predicted Crime Cases'], marker='o', linestyle='--', color='orangered')
ax.set_xlabel("Year")
ax.set_ylabel("Predicted Crime Cases")
ax.set_title(f"{state_input} Crime Rate Prediction")
st.pyplot(fig)
else:
st.warning("⚠️ Please enter a valid State/UT name from the dataset.")
else:
st.info("👈 Please enter a State/UT name to begin prediction.")
except FileNotFoundError:
st.error(f"❌ File not found at path: {csv_path}. Please check the path.")
|