Spaces:
Sleeping
Sleeping
Update api.py
Browse files
api.py
CHANGED
|
@@ -233,10 +233,11 @@ def get_feature_schema(disaster_type: str):
|
|
| 233 |
|
| 234 |
@app.post("/predict/flood")
|
| 235 |
def predict_flood(req: FloodPredictionRequest):
|
| 236 |
-
|
|
|
|
| 237 |
if errors:
|
| 238 |
raise HTTPException(422, {"validation_errors": errors})
|
| 239 |
-
result = flood_predictor.predict(
|
| 240 |
return {"disaster_type": "flood", **prediction_result_to_dict(result)}
|
| 241 |
|
| 242 |
|
|
@@ -319,31 +320,22 @@ import numpy as np # needed for explain endpoint
|
|
| 319 |
|
| 320 |
@app.get("/predict/cyclone/live")
|
| 321 |
def predict_cyclone_live():
|
| 322 |
-
"""
|
| 323 |
-
Fetch live IMD cyclone bulletin,
|
| 324 |
-
extract parameters,
|
| 325 |
-
engineer features,
|
| 326 |
-
run cyclone predictor.
|
| 327 |
-
"""
|
| 328 |
bulletin = imd_fetcher.fetch_hourly_bulletin()
|
| 329 |
-
|
| 330 |
if bulletin["status"] != "success":
|
| 331 |
raise HTTPException(502, f"IMD fetch failed: {bulletin.get('message')}")
|
| 332 |
|
| 333 |
raw_params = imd_fetcher.parse_cyclone_parameters(bulletin["content"])
|
| 334 |
-
|
| 335 |
if not raw_params:
|
| 336 |
raise HTTPException(404, "No cyclone parameters detected in IMD bulletin")
|
| 337 |
|
| 338 |
-
|
|
|
|
| 339 |
|
| 340 |
-
# Validate against model schema
|
| 341 |
errors = cyclone_predictor.validate_input(features)
|
| 342 |
if errors:
|
| 343 |
raise HTTPException(422, {"validation_errors": errors})
|
| 344 |
|
| 345 |
result = cyclone_predictor.predict(features, 50)
|
| 346 |
-
|
| 347 |
return {
|
| 348 |
"source": "IMD RSMC live bulletin",
|
| 349 |
"raw_parameters": raw_params,
|
|
|
|
| 233 |
|
| 234 |
@app.post("/predict/flood")
|
| 235 |
def predict_flood(req: FloodPredictionRequest):
|
| 236 |
+
features = req.features.model_dump() # or .dict() for Pydantic v1
|
| 237 |
+
errors = flood_predictor.validate_input(features)
|
| 238 |
if errors:
|
| 239 |
raise HTTPException(422, {"validation_errors": errors})
|
| 240 |
+
result = flood_predictor.predict(features, req.n_mc_samples)
|
| 241 |
return {"disaster_type": "flood", **prediction_result_to_dict(result)}
|
| 242 |
|
| 243 |
|
|
|
|
| 320 |
|
| 321 |
@app.get("/predict/cyclone/live")
|
| 322 |
def predict_cyclone_live():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 323 |
bulletin = imd_fetcher.fetch_hourly_bulletin()
|
|
|
|
| 324 |
if bulletin["status"] != "success":
|
| 325 |
raise HTTPException(502, f"IMD fetch failed: {bulletin.get('message')}")
|
| 326 |
|
| 327 |
raw_params = imd_fetcher.parse_cyclone_parameters(bulletin["content"])
|
|
|
|
| 328 |
if not raw_params:
|
| 329 |
raise HTTPException(404, "No cyclone parameters detected in IMD bulletin")
|
| 330 |
|
| 331 |
+
engineered = cyclone_engineer.engineer_features(raw_params)
|
| 332 |
+
features = cyclone_engineer.to_model_features(engineered) # ← new step
|
| 333 |
|
|
|
|
| 334 |
errors = cyclone_predictor.validate_input(features)
|
| 335 |
if errors:
|
| 336 |
raise HTTPException(422, {"validation_errors": errors})
|
| 337 |
|
| 338 |
result = cyclone_predictor.predict(features, 50)
|
|
|
|
| 339 |
return {
|
| 340 |
"source": "IMD RSMC live bulletin",
|
| 341 |
"raw_parameters": raw_params,
|