Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -158,6 +158,13 @@ def predict_crime_level(crime_felony, crime_misd, crime_viol, sr311_total, dob_p
|
|
| 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
|
|
@@ -183,9 +190,12 @@ def predict_crime_level(crime_felony, crime_misd, crime_viol, sr311_total, dob_p
|
|
| 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 |
-
|
| 188 |
-
|
|
|
|
|
|
|
|
|
|
| 189 |
|
| 190 |
try:
|
| 191 |
# The model expects many more features than we have available in this interface
|
|
@@ -222,19 +232,25 @@ def predict_crime_level(crime_felony, crime_misd, crime_viol, sr311_total, dob_p
|
|
| 222 |
# Determine final prediction
|
| 223 |
prediction = max(confidence.items(), key=lambda x: x[1])[0]
|
| 224 |
|
| 225 |
-
|
| 226 |
-
|
|
|
|
|
|
|
|
|
|
| 227 |
|
| 228 |
except Exception as e:
|
| 229 |
print(f"DEBUG: Error in model prediction: {e}")
|
| 230 |
# Even if there's an error, provide a basic prediction
|
| 231 |
total_crime = crime_felony + crime_misd + crime_viol
|
| 232 |
if total_crime <= 20:
|
| 233 |
-
|
|
|
|
| 234 |
elif total_crime <= 50:
|
| 235 |
-
|
|
|
|
| 236 |
else:
|
| 237 |
-
|
|
|
|
| 238 |
|
| 239 |
def forecast_time_series(geoid, selected_metric):
|
| 240 |
"""Forecasts crime for a specific GEOID."""
|
|
|
|
| 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 |
+
# Define emoji mapping
|
| 162 |
+
emoji_map = {
|
| 163 |
+
"Low": "π’",
|
| 164 |
+
"Medium": "π‘",
|
| 165 |
+
"High": "π΄"
|
| 166 |
+
}
|
| 167 |
+
|
| 168 |
if model is None:
|
| 169 |
# Create a dummy prediction based on simple logic when model is not available
|
| 170 |
total_crime = crime_felony + crime_misd + crime_viol
|
|
|
|
| 190 |
if dob_permits_total > 25:
|
| 191 |
# High construction activity might indicate development/change
|
| 192 |
confidence["Medium"] = min(0.8, confidence.get("Medium", 0) + 0.2)
|
| 193 |
+
|
| 194 |
+
# Add emojis to confidence labels
|
| 195 |
+
confidence_with_emojis = {f"{level} {emoji_map[level]}": prob for level, prob in confidence.items()}
|
| 196 |
+
|
| 197 |
+
print(f"DEBUG: Dummy prediction result: {prediction}, confidence: {confidence_with_emojis}")
|
| 198 |
+
return emoji_map[prediction], confidence_with_emojis
|
| 199 |
|
| 200 |
try:
|
| 201 |
# The model expects many more features than we have available in this interface
|
|
|
|
| 232 |
# Determine final prediction
|
| 233 |
prediction = max(confidence.items(), key=lambda x: x[1])[0]
|
| 234 |
|
| 235 |
+
# Add emojis to confidence labels
|
| 236 |
+
confidence_with_emojis = {f"{level} {emoji_map[level]}": prob for level, prob in confidence.items()}
|
| 237 |
+
|
| 238 |
+
print(f"DEBUG: Enhanced fallback prediction result: {prediction}, confidence: {confidence_with_emojis}")
|
| 239 |
+
return emoji_map[prediction], confidence_with_emojis
|
| 240 |
|
| 241 |
except Exception as e:
|
| 242 |
print(f"DEBUG: Error in model prediction: {e}")
|
| 243 |
# Even if there's an error, provide a basic prediction
|
| 244 |
total_crime = crime_felony + crime_misd + crime_viol
|
| 245 |
if total_crime <= 20:
|
| 246 |
+
confidence = {"Low π’": 0.6, "Medium π‘": 0.3, "High π΄": 0.1}
|
| 247 |
+
return "π’", confidence
|
| 248 |
elif total_crime <= 50:
|
| 249 |
+
confidence = {"Low π’": 0.2, "Medium π‘": 0.6, "High π΄": 0.2}
|
| 250 |
+
return "π‘", confidence
|
| 251 |
else:
|
| 252 |
+
confidence = {"Low π’": 0.1, "Medium π‘": 0.3, "High π΄": 0.6}
|
| 253 |
+
return "π΄", confidence
|
| 254 |
|
| 255 |
def forecast_time_series(geoid, selected_metric):
|
| 256 |
"""Forecasts crime for a specific GEOID."""
|