Skip to content

Commit e54a4d3

Browse files
committed
fix: add local fallback for risk scores on Cloud Run
1 parent 5aecfe4 commit e54a4d3

5 files changed

Lines changed: 26 additions & 4 deletions

File tree

.dockerignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@ docs/
33
catboost_info/
44
cache/
55
scripts/
6-
backend/model/output/
6+
backend/model/output/*
7+
!backend/model/output/intersection_scores.parquet
8+
!backend/model/output/shap_top3.pkl
9+
!backend/model/output/hourly_risk_factors.parquet
710
backend/__pycache__/
811
backend/.env
912
**/__pycache__/

backend/main.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -442,8 +442,8 @@ def compute_route(payload: RouteRequest):
442442
"time_band": safety.get("time_band"),
443443
"segment_risks": safety.get("segment_risks", []),
444444
}
445-
except Exception:
446-
pass
445+
except Exception as score_err:
446+
print(f"[route] score_coordinates failed for route: {score_err}", flush=True)
447447
result_routes.append({
448448
"distance_meters": route.get("distanceMeters"),
449449
"duration": route.get("duration"),
452 KB
Binary file not shown.

backend/model/train_model.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -682,6 +682,12 @@ def main():
682682
tod_df.to_parquet(OUTPUT_DIR / "hourly_risk_factors.parquet", index=False)
683683
print(f" Saved hourly_risk_factors.parquet ({len(tod_df)} rows).", flush=True)
684684

685+
# Save intersection_scores.parquet locally (used as fallback on Cloud Run)
686+
scores_local = df[["node_id", "predicted_risk"]].copy()
687+
scores_local["predicted_risk"] = scores_local["predicted_risk"].fillna(0)
688+
scores_local.to_parquet(OUTPUT_DIR / "intersection_scores.parquet", index=False)
689+
print(f" Saved intersection_scores.parquet locally ({len(scores_local)} rows).", flush=True)
690+
685691
if args.export_csv:
686692
df.to_csv(OUTPUT_DIR / "chicago_intersection_training_dataset.csv", index=False)
687693
print(f" Saved dataset CSV.", flush=True)

backend/risk_cache.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,20 @@ def get_risk_map() -> dict[str, float]:
9292
_tod_map = None
9393
_prepared_graph = None
9494

95-
# Try GCS first (local key file or Cloud Run default credentials)
95+
# Try local file first (bundled in Docker image from training output)
96+
local_path = OUTPUT_DIR / "intersection_scores.parquet"
97+
if local_path.exists():
98+
try:
99+
import pandas as pd
100+
scores = pd.read_parquet(local_path)
101+
_risk_map = dict(zip(scores["node_id"].astype(str), scores["predicted_risk"].fillna(0).astype(float)))
102+
_cache_loaded_at = time.time()
103+
print(f"[risk_cache] loaded {len(_risk_map)} nodes from local file", flush=True)
104+
return _risk_map
105+
except Exception as e:
106+
print(f"[risk_cache] local parquet load failed ({e}), trying GCS", flush=True)
107+
108+
# Try GCS (local key file or Cloud Run default credentials)
96109
try:
97110
import pandas as pd
98111
fs = _get_gcs_fs()

0 commit comments

Comments
 (0)