Reinforcement Learning
stable-baselines3
deep-reinforcement-learning
agricultural-ai
weather-modelling
curriculum-learning
edge-ai
Instructions to use DHDRL/monsoon-rl with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- stable-baselines3
How to use DHDRL/monsoon-rl with stable-baselines3:
from huggingface_sb3 import load_from_hub checkpoint = load_from_hub( repo_id="DHDRL/monsoon-rl", filename="{MODEL FILENAME}.zip", ) - Notebooks
- Google Colab
- Kaggle
Update product_alert_service.py
Browse files- product_alert_service.py +91 -34
product_alert_service.py
CHANGED
|
@@ -2,15 +2,6 @@
|
|
| 2 |
product_alert_service.py
|
| 3 |
========================
|
| 4 |
Point 2: product decision loop — gate + idempotent emit onto NodeTransport.
|
| 5 |
-
|
| 6 |
-
Backend SSOT for "should we notify?" Product emission never trusts a client
|
| 7 |
-
boolean; it recomputes the gate from RiskScore fields + ProductGateConfig.
|
| 8 |
-
|
| 9 |
-
States (per emission_id):
|
| 10 |
-
ABSENT → EMITTED
|
| 11 |
-
EMITTED + duplicate → ALREADY_EMITTED (no second transport side effect)
|
| 12 |
-
|
| 13 |
-
SUPERSEDED (same-day severity upgrade) is explicitly UNDEFINED — not implemented.
|
| 14 |
"""
|
| 15 |
|
| 16 |
from __future__ import annotations
|
|
@@ -18,7 +9,9 @@ from __future__ import annotations
|
|
| 18 |
import hashlib
|
| 19 |
import logging
|
| 20 |
from dataclasses import dataclass, field
|
| 21 |
-
from datetime import date, datetime, timezone
|
|
|
|
|
|
|
| 22 |
from enum import Enum
|
| 23 |
from typing import Any, Dict, Optional, Protocol, runtime_checkable
|
| 24 |
|
|
@@ -44,6 +37,7 @@ class ProductGateConfig:
|
|
| 44 |
flood_threshold: float = 0.25
|
| 45 |
min_alert_level: AlertLevel = AlertLevel.WARNING
|
| 46 |
allow_hazard_override: bool = True
|
|
|
|
| 47 |
|
| 48 |
def __post_init__(self) -> None:
|
| 49 |
if not (0.0 <= self.drought_threshold <= 1.0):
|
|
@@ -72,6 +66,7 @@ class AlertEmissionRecord:
|
|
| 72 |
flood_risk: float
|
| 73 |
emitted_at: datetime
|
| 74 |
payload_sha256: str
|
|
|
|
| 75 |
|
| 76 |
|
| 77 |
@dataclass
|
|
@@ -96,6 +91,12 @@ def is_elevated(score: RiskScore) -> bool:
|
|
| 96 |
return score.alert_level.severity() >= AlertLevel.ADVISORY.severity()
|
| 97 |
|
| 98 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 99 |
def is_product_actionable(
|
| 100 |
score: RiskScore,
|
| 101 |
gate: ProductGateConfig = DEFAULT_PRODUCT_GATE,
|
|
@@ -104,12 +105,8 @@ def is_product_actionable(
|
|
| 104 |
return True
|
| 105 |
if not gate.allow_hazard_override:
|
| 106 |
return False
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
if score.flood_risk >= gate.flood_threshold:
|
| 110 |
-
return True
|
| 111 |
-
return False
|
| 112 |
-
|
| 113 |
|
| 114 |
def product_trigger_reason(
|
| 115 |
score: RiskScore,
|
|
@@ -117,12 +114,14 @@ def product_trigger_reason(
|
|
| 117 |
) -> str:
|
| 118 |
reasons = []
|
| 119 |
if score.alert_level.severity() >= gate.min_alert_level.severity():
|
| 120 |
-
reasons.append(f
|
| 121 |
-
if gate.allow_hazard_override
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
|
|
|
|
|
|
| 126 |
|
| 127 |
|
| 128 |
# ---------------------------------------------------------------------------
|
|
@@ -147,6 +146,56 @@ class EmissionLedger:
|
|
| 147 |
return len(self._records)
|
| 148 |
|
| 149 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 150 |
def make_emission_id(zone_id: str, valid_date: date) -> str:
|
| 151 |
raw = f"{zone_id}|{valid_date.isoformat()}|product_v1"
|
| 152 |
return hashlib.sha256(raw.encode("utf-8")).hexdigest()[:32]
|
|
@@ -163,6 +212,7 @@ async def emit_product_alert(
|
|
| 163 |
ledger: EmissionLedger,
|
| 164 |
gate: ProductGateConfig = DEFAULT_PRODUCT_GATE,
|
| 165 |
valid_time: Optional[datetime] = None,
|
|
|
|
| 166 |
) -> ServiceResult:
|
| 167 |
if not score.zone_id or not str(score.zone_id).strip():
|
| 168 |
return ServiceResult(False, "INVALID_INPUT", {"error": "empty zone_id"})
|
|
@@ -177,21 +227,24 @@ async def emit_product_alert(
|
|
| 177 |
{"error": "valid_time must be timezone-aware"},
|
| 178 |
)
|
| 179 |
|
| 180 |
-
valid_date = valid_time.astimezone(
|
| 181 |
emission_id = make_emission_id(score.zone_id, valid_date)
|
| 182 |
|
| 183 |
existing = ledger.get(emission_id)
|
|
|
|
| 184 |
if existing is not None and existing.state == EmissionState.EMITTED:
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
|
|
|
|
|
|
| 195 |
|
| 196 |
if not is_product_actionable(score, gate):
|
| 197 |
return ServiceResult(
|
|
@@ -253,12 +306,13 @@ async def emit_product_alert(
|
|
| 253 |
flood_risk=score.flood_risk,
|
| 254 |
emitted_at=datetime.now(timezone.utc),
|
| 255 |
payload_sha256=payload_hash,
|
|
|
|
| 256 |
)
|
| 257 |
ledger.put(record)
|
| 258 |
|
| 259 |
return ServiceResult(
|
| 260 |
True,
|
| 261 |
-
"EMITTED",
|
| 262 |
{
|
| 263 |
"emission_id": emission_id,
|
| 264 |
"zone_id": score.zone_id,
|
|
@@ -266,6 +320,7 @@ async def emit_product_alert(
|
|
| 266 |
"trigger": trigger,
|
| 267 |
"alert_level": score.alert_level.value,
|
| 268 |
"payload_sha256": payload_hash,
|
|
|
|
| 269 |
},
|
| 270 |
)
|
| 271 |
|
|
@@ -277,6 +332,7 @@ async def score_and_maybe_emit(
|
|
| 277 |
ledger: EmissionLedger,
|
| 278 |
gate: ProductGateConfig = DEFAULT_PRODUCT_GATE,
|
| 279 |
valid_time: Optional[datetime] = None,
|
|
|
|
| 280 |
) -> ServiceResult:
|
| 281 |
return await emit_product_alert(
|
| 282 |
transport,
|
|
@@ -284,6 +340,7 @@ async def score_and_maybe_emit(
|
|
| 284 |
ledger=ledger,
|
| 285 |
gate=gate,
|
| 286 |
valid_time=valid_time,
|
|
|
|
| 287 |
)
|
| 288 |
|
| 289 |
|
|
|
|
| 2 |
product_alert_service.py
|
| 3 |
========================
|
| 4 |
Point 2: product decision loop — gate + idempotent emit onto NodeTransport.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
"""
|
| 6 |
|
| 7 |
from __future__ import annotations
|
|
|
|
| 9 |
import hashlib
|
| 10 |
import logging
|
| 11 |
from dataclasses import dataclass, field
|
| 12 |
+
from datetime import date, datetime, timezone, tzinfo
|
| 13 |
+
import sqlite3
|
| 14 |
+
import threading
|
| 15 |
from enum import Enum
|
| 16 |
from typing import Any, Dict, Optional, Protocol, runtime_checkable
|
| 17 |
|
|
|
|
| 37 |
flood_threshold: float = 0.25
|
| 38 |
min_alert_level: AlertLevel = AlertLevel.WARNING
|
| 39 |
allow_hazard_override: bool = True
|
| 40 |
+
use_unscaled_for_override: bool = False
|
| 41 |
|
| 42 |
def __post_init__(self) -> None:
|
| 43 |
if not (0.0 <= self.drought_threshold <= 1.0):
|
|
|
|
| 66 |
flood_risk: float
|
| 67 |
emitted_at: datetime
|
| 68 |
payload_sha256: str
|
| 69 |
+
supersedes: Optional[str] = None
|
| 70 |
|
| 71 |
|
| 72 |
@dataclass
|
|
|
|
| 91 |
return score.alert_level.severity() >= AlertLevel.ADVISORY.severity()
|
| 92 |
|
| 93 |
|
| 94 |
+
def _override_risks(score: RiskScore, gate: ProductGateConfig):
|
| 95 |
+
if gate.use_unscaled_for_override:
|
| 96 |
+
return (float(score.extras.get('drought_raw', score.drought_risk)),
|
| 97 |
+
float(score.extras.get('flood_raw', score.flood_risk)))
|
| 98 |
+
return score.drought_risk, score.flood_risk
|
| 99 |
+
|
| 100 |
def is_product_actionable(
|
| 101 |
score: RiskScore,
|
| 102 |
gate: ProductGateConfig = DEFAULT_PRODUCT_GATE,
|
|
|
|
| 105 |
return True
|
| 106 |
if not gate.allow_hazard_override:
|
| 107 |
return False
|
| 108 |
+
drought, flood = _override_risks(score, gate)
|
| 109 |
+
return drought >= gate.drought_threshold or flood >= gate.flood_threshold
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
|
| 111 |
def product_trigger_reason(
|
| 112 |
score: RiskScore,
|
|
|
|
| 114 |
) -> str:
|
| 115 |
reasons = []
|
| 116 |
if score.alert_level.severity() >= gate.min_alert_level.severity():
|
| 117 |
+
reasons.append(f'alert_{score.alert_level.value}')
|
| 118 |
+
if gate.allow_hazard_override:
|
| 119 |
+
drought, flood = _override_risks(score, gate)
|
| 120 |
+
if drought >= gate.drought_threshold:
|
| 121 |
+
reasons.append('drought_threshold')
|
| 122 |
+
if flood >= gate.flood_threshold:
|
| 123 |
+
reasons.append('flood_threshold')
|
| 124 |
+
return '+'.join(reasons) if reasons else 'none'
|
| 125 |
|
| 126 |
|
| 127 |
# ---------------------------------------------------------------------------
|
|
|
|
| 146 |
return len(self._records)
|
| 147 |
|
| 148 |
|
| 149 |
+
class SqliteEmissionLedger(EmissionLedger):
|
| 150 |
+
'''Persistent drop-in for EmissionLedger (same get/put/clear/len interface).'''
|
| 151 |
+
|
| 152 |
+
_COLS = ('emission_id', 'zone_id', 'valid_date', 'state', 'alert_level', 'trigger',
|
| 153 |
+
'drought_risk', 'flood_risk', 'emitted_at', 'payload_sha256', 'supersedes')
|
| 154 |
+
|
| 155 |
+
def __init__(self, path: str) -> None:
|
| 156 |
+
super().__init__()
|
| 157 |
+
self._lock = threading.Lock()
|
| 158 |
+
self._db = sqlite3.connect(path, check_same_thread=False, isolation_level=None)
|
| 159 |
+
self._db.execute('PRAGMA journal_mode=WAL')
|
| 160 |
+
self._db.execute(
|
| 161 |
+
'CREATE TABLE IF NOT EXISTS emissions ('
|
| 162 |
+
'emission_id TEXT PRIMARY KEY, zone_id TEXT, valid_date TEXT, state TEXT, '
|
| 163 |
+
'alert_level TEXT, trigger TEXT, drought_risk REAL, flood_risk REAL, '
|
| 164 |
+
'emitted_at TEXT, payload_sha256 TEXT, supersedes TEXT)')
|
| 165 |
+
|
| 166 |
+
def get(self, emission_id):
|
| 167 |
+
with self._lock:
|
| 168 |
+
row = self._db.execute(
|
| 169 |
+
'SELECT ' + ', '.join(self._COLS) + ' FROM emissions WHERE emission_id = ?',
|
| 170 |
+
(emission_id,)).fetchone()
|
| 171 |
+
if row is None:
|
| 172 |
+
return None
|
| 173 |
+
r = dict(zip(self._COLS, row))
|
| 174 |
+
return AlertEmissionRecord(
|
| 175 |
+
emission_id=r['emission_id'], zone_id=r['zone_id'],
|
| 176 |
+
valid_date=date.fromisoformat(r['valid_date']), state=EmissionState(r['state']),
|
| 177 |
+
alert_level=r['alert_level'], trigger=r['trigger'],
|
| 178 |
+
drought_risk=r['drought_risk'], flood_risk=r['flood_risk'],
|
| 179 |
+
emitted_at=datetime.fromisoformat(r['emitted_at']),
|
| 180 |
+
payload_sha256=r['payload_sha256'], supersedes=r['supersedes'])
|
| 181 |
+
|
| 182 |
+
def put(self, record):
|
| 183 |
+
row = (record.emission_id, record.zone_id, record.valid_date.isoformat(),
|
| 184 |
+
record.state.value, record.alert_level, record.trigger, record.drought_risk,
|
| 185 |
+
record.flood_risk, record.emitted_at.isoformat(), record.payload_sha256,
|
| 186 |
+
record.supersedes)
|
| 187 |
+
with self._lock:
|
| 188 |
+
self._db.execute('INSERT OR REPLACE INTO emissions VALUES (?,?,?,?,?,?,?,?,?,?,?)', row)
|
| 189 |
+
|
| 190 |
+
def clear(self):
|
| 191 |
+
with self._lock:
|
| 192 |
+
self._db.execute('DELETE FROM emissions')
|
| 193 |
+
|
| 194 |
+
def __len__(self):
|
| 195 |
+
with self._lock:
|
| 196 |
+
return self._db.execute('SELECT COUNT(*) FROM emissions').fetchone()[0]
|
| 197 |
+
|
| 198 |
+
|
| 199 |
def make_emission_id(zone_id: str, valid_date: date) -> str:
|
| 200 |
raw = f"{zone_id}|{valid_date.isoformat()}|product_v1"
|
| 201 |
return hashlib.sha256(raw.encode("utf-8")).hexdigest()[:32]
|
|
|
|
| 212 |
ledger: EmissionLedger,
|
| 213 |
gate: ProductGateConfig = DEFAULT_PRODUCT_GATE,
|
| 214 |
valid_time: Optional[datetime] = None,
|
| 215 |
+
tz: tzinfo = timezone.utc,
|
| 216 |
) -> ServiceResult:
|
| 217 |
if not score.zone_id or not str(score.zone_id).strip():
|
| 218 |
return ServiceResult(False, "INVALID_INPUT", {"error": "empty zone_id"})
|
|
|
|
| 227 |
{"error": "valid_time must be timezone-aware"},
|
| 228 |
)
|
| 229 |
|
| 230 |
+
valid_date = valid_time.astimezone(tz).date()
|
| 231 |
emission_id = make_emission_id(score.zone_id, valid_date)
|
| 232 |
|
| 233 |
existing = ledger.get(emission_id)
|
| 234 |
+
supersedes: Optional[str] = None
|
| 235 |
if existing is not None and existing.state == EmissionState.EMITTED:
|
| 236 |
+
if score.alert_level.severity() <= AlertLevel(existing.alert_level).severity():
|
| 237 |
+
return ServiceResult(
|
| 238 |
+
True,
|
| 239 |
+
"ALREADY_EMITTED",
|
| 240 |
+
{
|
| 241 |
+
"emission_id": emission_id,
|
| 242 |
+
"zone_id": score.zone_id,
|
| 243 |
+
"valid_date": valid_date.isoformat(),
|
| 244 |
+
"prior_trigger": existing.trigger,
|
| 245 |
+
},
|
| 246 |
+
)
|
| 247 |
+
supersedes = existing.payload_sha256 # strictly higher level: send again
|
| 248 |
|
| 249 |
if not is_product_actionable(score, gate):
|
| 250 |
return ServiceResult(
|
|
|
|
| 306 |
flood_risk=score.flood_risk,
|
| 307 |
emitted_at=datetime.now(timezone.utc),
|
| 308 |
payload_sha256=payload_hash,
|
| 309 |
+
supersedes=supersedes,
|
| 310 |
)
|
| 311 |
ledger.put(record)
|
| 312 |
|
| 313 |
return ServiceResult(
|
| 314 |
True,
|
| 315 |
+
"EMITTED_ESCALATION" if supersedes else "EMITTED",
|
| 316 |
{
|
| 317 |
"emission_id": emission_id,
|
| 318 |
"zone_id": score.zone_id,
|
|
|
|
| 320 |
"trigger": trigger,
|
| 321 |
"alert_level": score.alert_level.value,
|
| 322 |
"payload_sha256": payload_hash,
|
| 323 |
+
"supersedes": supersedes,
|
| 324 |
},
|
| 325 |
)
|
| 326 |
|
|
|
|
| 332 |
ledger: EmissionLedger,
|
| 333 |
gate: ProductGateConfig = DEFAULT_PRODUCT_GATE,
|
| 334 |
valid_time: Optional[datetime] = None,
|
| 335 |
+
tz: tzinfo = timezone.utc,
|
| 336 |
) -> ServiceResult:
|
| 337 |
return await emit_product_alert(
|
| 338 |
transport,
|
|
|
|
| 340 |
ledger=ledger,
|
| 341 |
gate=gate,
|
| 342 |
valid_time=valid_time,
|
| 343 |
+
tz=tz,
|
| 344 |
)
|
| 345 |
|
| 346 |
|