Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -156,29 +156,63 @@ def create_time_series_plot(metric, start_date, end_date):
|
|
156 |
|
157 |
def predict_crime_level(crime_felony, crime_misd, crime_viol, sr311_total, dob_permits_total):
|
158 |
"""Predicts crime level based on input features."""
|
|
|
|
|
159 |
if model is None:
|
160 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
161 |
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
182 |
|
183 |
def forecast_time_series(geoid):
|
184 |
"""Forecasts crime for a specific GEOID."""
|
@@ -298,11 +332,11 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
298 |
gr.Markdown("Adjust the sliders to reflect the current month's data for a census tract.")
|
299 |
with gr.Row():
|
300 |
with gr.Column():
|
301 |
-
felony_slider = gr.Slider(0, 100, label="Felony Count", step=1)
|
302 |
-
misd_slider = gr.Slider(0, 200, label="Misdemeanor Count", step=1)
|
303 |
-
viol_slider = gr.Slider(0, 200, label="Violation Count", step=1)
|
304 |
-
sr311_slider = gr.Slider(0, 1000, label="311 Service Requests", step=10)
|
305 |
-
dob_slider = gr.Slider(0, 50, label="DOB Permits Issued", step=1)
|
306 |
predict_button = gr.Button("Predict")
|
307 |
with gr.Column():
|
308 |
prediction_output = gr.Label(label="Prediction Result")
|
|
|
156 |
|
157 |
def predict_crime_level(crime_felony, crime_misd, crime_viol, sr311_total, dob_permits_total):
|
158 |
"""Predicts crime level based on input features."""
|
159 |
+
print(f"DEBUG: predict_crime_level called with inputs: {crime_felony}, {crime_misd}, {crime_viol}, {sr311_total}, {dob_permits_total}")
|
160 |
+
|
161 |
if model is None:
|
162 |
+
# Create a dummy prediction based on simple logic when model is not available
|
163 |
+
total_crime = crime_felony + crime_misd + crime_viol
|
164 |
+
|
165 |
+
# Simple rule-based classification for demonstration
|
166 |
+
if total_crime <= 20:
|
167 |
+
prediction = "Low"
|
168 |
+
confidence = {"Low": 0.7, "Medium": 0.2, "High": 0.1}
|
169 |
+
elif total_crime <= 50:
|
170 |
+
prediction = "Medium"
|
171 |
+
confidence = {"Low": 0.2, "Medium": 0.6, "High": 0.2}
|
172 |
+
else:
|
173 |
+
prediction = "High"
|
174 |
+
confidence = {"Low": 0.1, "Medium": 0.3, "High": 0.6}
|
175 |
+
|
176 |
+
# Factor in 311 requests and permits
|
177 |
+
if sr311_total > 500:
|
178 |
+
# High service requests might indicate more issues
|
179 |
+
if prediction == "Low":
|
180 |
+
prediction = "Medium"
|
181 |
+
confidence = {"Low": 0.4, "Medium": 0.5, "High": 0.1}
|
182 |
+
|
183 |
+
if dob_permits_total > 25:
|
184 |
+
# High construction activity might indicate development/change
|
185 |
+
confidence["Medium"] = min(0.8, confidence.get("Medium", 0) + 0.2)
|
186 |
+
|
187 |
+
print(f"DEBUG: Dummy prediction result: {prediction}, confidence: {confidence}")
|
188 |
+
return f"Predicted Crime Level: {prediction} (using fallback model)", confidence
|
189 |
|
190 |
+
try:
|
191 |
+
# Create a DataFrame for the model
|
192 |
+
input_data = pd.DataFrame({
|
193 |
+
'crime_felony': [crime_felony],
|
194 |
+
'crime_misd': [crime_misd],
|
195 |
+
'crime_viol': [crime_viol],
|
196 |
+
'sr311_total': [sr311_total],
|
197 |
+
'dob_permits_total': [dob_permits_total]
|
198 |
+
})
|
199 |
+
|
200 |
+
# Predict probabilities
|
201 |
+
probabilities = model.predict_proba(input_data)[0]
|
202 |
+
labels = model.classes_
|
203 |
+
|
204 |
+
# Get the prediction
|
205 |
+
prediction = labels[np.argmax(probabilities)]
|
206 |
+
|
207 |
+
# Create a confidence dictionary
|
208 |
+
confidence = {label: prob for label, prob in zip(labels, probabilities)}
|
209 |
+
|
210 |
+
print(f"DEBUG: Real model prediction result: {prediction}, confidence: {confidence}")
|
211 |
+
return f"Predicted Crime Level: {prediction}", confidence
|
212 |
+
|
213 |
+
except Exception as e:
|
214 |
+
print(f"DEBUG: Error in model prediction: {e}")
|
215 |
+
return f"Error in prediction: {str(e)}", {}
|
216 |
|
217 |
def forecast_time_series(geoid):
|
218 |
"""Forecasts crime for a specific GEOID."""
|
|
|
332 |
gr.Markdown("Adjust the sliders to reflect the current month's data for a census tract.")
|
333 |
with gr.Row():
|
334 |
with gr.Column():
|
335 |
+
felony_slider = gr.Slider(0, 100, label="Felony Count", step=1, value=5)
|
336 |
+
misd_slider = gr.Slider(0, 200, label="Misdemeanor Count", step=1, value=15)
|
337 |
+
viol_slider = gr.Slider(0, 200, label="Violation Count", step=1, value=10)
|
338 |
+
sr311_slider = gr.Slider(0, 1000, label="311 Service Requests", step=10, value=100)
|
339 |
+
dob_slider = gr.Slider(0, 50, label="DOB Permits Issued", step=1, value=3)
|
340 |
predict_button = gr.Button("Predict")
|
341 |
with gr.Column():
|
342 |
prediction_output = gr.Label(label="Prediction Result")
|