Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -1,234 +1,245 @@
|
|
| 1 |
-
"""
|
| 2 |
-
SmartContainer Risk Engine β FastAPI Backend
|
| 3 |
-
=============================================
|
| 4 |
-
Offline-Train, Online-Serve architecture.
|
| 5 |
-
Models are loaded once at startup and kept in memory for fast inference.
|
| 6 |
-
|
| 7 |
-
Workflow:
|
| 8 |
-
1. Run train_offline.py to train and save models to saved_models/
|
| 9 |
-
2. Start this server: uvicorn main:app --reload
|
| 10 |
-
3. POST a CSV to /api/predict-batch β receive final_predictions.csv
|
| 11 |
-
|
| 12 |
-
Expected upload schema (no Clearance_Status column):
|
| 13 |
-
Container_ID, Declaration_Date (YYYY-MM-DD), Declaration_Time,
|
| 14 |
-
Trade_Regime (Import / Export / Transit), Origin_Country,
|
| 15 |
-
Destination_Port, Destination_Country, HS_Code, Importer_ID,
|
| 16 |
-
Exporter_ID, Declared_Value, Declared_Weight, Measured_Weight,
|
| 17 |
-
Shipping_Line, Dwell_Time_Hours
|
| 18 |
-
"""
|
| 19 |
-
|
| 20 |
-
import io
|
| 21 |
-
import os
|
| 22 |
-
import joblib
|
| 23 |
-
import httpx
|
| 24 |
-
import pandas as pd
|
| 25 |
-
from contextlib import asynccontextmanager
|
| 26 |
-
from dotenv import load_dotenv
|
| 27 |
-
|
| 28 |
-
load_dotenv()
|
| 29 |
-
|
| 30 |
-
from fastapi import FastAPI, File, HTTPException, Query, UploadFile
|
| 31 |
-
from fastapi.middleware.cors import CORSMiddleware
|
| 32 |
-
from fastapi.responses import StreamingResponse
|
| 33 |
-
|
| 34 |
-
# ββ News API key (server-side only, never exposed to frontend) ββββββββββββ
|
| 35 |
-
GNEWS_API_KEY = os.environ.get("GNEWS_API_KEY", "")
|
| 36 |
-
|
| 37 |
-
from src.config import TRAIN_PATH
|
| 38 |
-
from src.features import preprocess_and_engineer
|
| 39 |
-
from src.model import prepare_features, inference_predict, explain_and_save
|
| 40 |
-
|
| 41 |
-
# ββ Global model / data store (populated at startup) ββββββββββββββββββββββ
|
| 42 |
-
_store: dict = {}
|
| 43 |
-
SAVED_MODELS_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), "saved_models")
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
@asynccontextmanager
|
| 47 |
-
async def lifespan(app: FastAPI):
|
| 48 |
-
"""Load all artifacts into memory once at startup; release on shutdown."""
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
# ββ
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
#
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
)
|
| 133 |
-
|
| 134 |
-
# ββ
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
_store["
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
"""
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
SmartContainer Risk Engine β FastAPI Backend
|
| 3 |
+
=============================================
|
| 4 |
+
Offline-Train, Online-Serve architecture.
|
| 5 |
+
Models are loaded once at startup and kept in memory for fast inference.
|
| 6 |
+
|
| 7 |
+
Workflow:
|
| 8 |
+
1. Run train_offline.py to train and save models to saved_models/
|
| 9 |
+
2. Start this server: uvicorn main:app --reload
|
| 10 |
+
3. POST a CSV to /api/predict-batch β receive final_predictions.csv
|
| 11 |
+
|
| 12 |
+
Expected upload schema (no Clearance_Status column):
|
| 13 |
+
Container_ID, Declaration_Date (YYYY-MM-DD), Declaration_Time,
|
| 14 |
+
Trade_Regime (Import / Export / Transit), Origin_Country,
|
| 15 |
+
Destination_Port, Destination_Country, HS_Code, Importer_ID,
|
| 16 |
+
Exporter_ID, Declared_Value, Declared_Weight, Measured_Weight,
|
| 17 |
+
Shipping_Line, Dwell_Time_Hours
|
| 18 |
+
"""
|
| 19 |
+
import asyncio
|
| 20 |
+
import io
|
| 21 |
+
import os
|
| 22 |
+
import joblib
|
| 23 |
+
import httpx
|
| 24 |
+
import pandas as pd
|
| 25 |
+
from contextlib import asynccontextmanager
|
| 26 |
+
from dotenv import load_dotenv
|
| 27 |
+
|
| 28 |
+
load_dotenv()
|
| 29 |
+
|
| 30 |
+
from fastapi import FastAPI, File, HTTPException, Query, UploadFile
|
| 31 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 32 |
+
from fastapi.responses import StreamingResponse
|
| 33 |
+
|
| 34 |
+
# ββ News API key (server-side only, never exposed to frontend) ββββββββββββ
|
| 35 |
+
GNEWS_API_KEY = os.environ.get("GNEWS_API_KEY", "")
|
| 36 |
+
|
| 37 |
+
from src.config import TRAIN_PATH
|
| 38 |
+
from src.features import preprocess_and_engineer
|
| 39 |
+
from src.model import prepare_features, inference_predict, explain_and_save
|
| 40 |
+
|
| 41 |
+
# ββ Global model / data store (populated at startup) ββββββββββββββββββββββ
|
| 42 |
+
_store: dict = {}
|
| 43 |
+
SAVED_MODELS_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), "saved_models")
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
@asynccontextmanager
|
| 47 |
+
async def lifespan(app: FastAPI):
|
| 48 |
+
"""Load all artifacts into memory once at startup; release on shutdown."""
|
| 49 |
+
|
| 50 |
+
def load_heavy_artifacts():
|
| 51 |
+
print("[Startup] Loading models from saved_models/ ...")
|
| 52 |
+
_store["xgb"] = joblib.load(os.path.join(SAVED_MODELS_DIR, "xgb_model.pkl"))
|
| 53 |
+
_store["lgb"] = joblib.load(os.path.join(SAVED_MODELS_DIR, "lgb_model.pkl"))
|
| 54 |
+
_store["cat"] = joblib.load(os.path.join(SAVED_MODELS_DIR, "cat_model.pkl"))
|
| 55 |
+
|
| 56 |
+
detector = joblib.load(os.path.join(SAVED_MODELS_DIR, "anomaly_detector.pkl"))
|
| 57 |
+
_store["iso"] = detector["iso"]
|
| 58 |
+
_store["iso_rmin"] = detector["rmin"]
|
| 59 |
+
_store["iso_rmax"] = detector["rmax"]
|
| 60 |
+
|
| 61 |
+
# Check if the training CSV actually exists on the server!
|
| 62 |
+
print(f"[Startup] Looking for training data at: {TRAIN_PATH}")
|
| 63 |
+
if not os.path.exists(TRAIN_PATH):
|
| 64 |
+
print(f"π¨ FATAL ERROR: The file {TRAIN_PATH} does not exist on Hugging Face! Did you upload the CSV?")
|
| 65 |
+
else:
|
| 66 |
+
_store["train_df_raw"] = pd.read_csv(TRAIN_PATH)
|
| 67 |
+
print(f"[Startup] Cached train data: {_store['train_df_raw'].shape}")
|
| 68 |
+
|
| 69 |
+
print("β
[Startup] All models ready!")
|
| 70 |
+
|
| 71 |
+
# Run the heavy loading in a separate thread so Uvicorn doesn't freeze
|
| 72 |
+
await asyncio.to_thread(load_heavy_artifacts)
|
| 73 |
+
|
| 74 |
+
yield
|
| 75 |
+
_store.clear()
|
| 76 |
+
|
| 77 |
+
app = FastAPI(
|
| 78 |
+
title="SmartContainer Risk Engine",
|
| 79 |
+
version="1.0.0",
|
| 80 |
+
lifespan=lifespan,
|
| 81 |
+
)
|
| 82 |
+
|
| 83 |
+
app.add_middleware(
|
| 84 |
+
CORSMiddleware,
|
| 85 |
+
allow_origins=["*"],
|
| 86 |
+
allow_credentials=True,
|
| 87 |
+
allow_methods=["*"],
|
| 88 |
+
allow_headers=["*"],
|
| 89 |
+
)
|
| 90 |
+
|
| 91 |
+
@app.get("/api")
|
| 92 |
+
def server_status():
|
| 93 |
+
return {"status": "ok", "message": "Server is running"}
|
| 94 |
+
|
| 95 |
+
@app.get("/health")
|
| 96 |
+
async def health():
|
| 97 |
+
return {"status": "ok", "artifacts": list(_store.keys())}
|
| 98 |
+
|
| 99 |
+
|
| 100 |
+
@app.post("/api/predict-batch")
|
| 101 |
+
async def predict_batch(file: UploadFile = File(...)):
|
| 102 |
+
"""
|
| 103 |
+
Accept a container manifest CSV (no Clearance_Status column).
|
| 104 |
+
Returns final_predictions.csv as a streaming download.
|
| 105 |
+
|
| 106 |
+
Output columns: Container_ID, Risk_Score, Risk_Level, Explanation_Summary
|
| 107 |
+
"""
|
| 108 |
+
if not file.filename.lower().endswith(".csv"):
|
| 109 |
+
raise HTTPException(status_code=400, detail="Only .csv files are accepted.")
|
| 110 |
+
|
| 111 |
+
# ββ Read uploaded test data βββββββββββββββββββββββββββββββββββββββββββ
|
| 112 |
+
contents = await file.read()
|
| 113 |
+
try:
|
| 114 |
+
test_df = pd.read_csv(io.BytesIO(contents))
|
| 115 |
+
except Exception as exc:
|
| 116 |
+
raise HTTPException(status_code=400, detail=f"Could not parse CSV: {exc}")
|
| 117 |
+
|
| 118 |
+
# ββ Fresh copy of cached train data prevents in-place mutation leaking
|
| 119 |
+
# across concurrent requests. β
|
| 120 |
+
train_df = _store["train_df_raw"].copy()
|
| 121 |
+
|
| 122 |
+
# ββ Feature engineering: stats fitted on train_df, mapped to test_df ββ
|
| 123 |
+
X_train, X_test, y_train, train_ids, test_ids = preprocess_and_engineer(
|
| 124 |
+
train_df, test_df
|
| 125 |
+
)
|
| 126 |
+
|
| 127 |
+
# ββ Drop zero-variance Trade_ columns (same step as offline training) β
|
| 128 |
+
X_train, X_test = prepare_features(X_train, X_test)
|
| 129 |
+
|
| 130 |
+
# ββ Safe index alignment before all downstream ops βββββββββββββββββββββ
|
| 131 |
+
X_test = X_test.reset_index(drop=True)
|
| 132 |
+
test_ids = test_ids.reset_index(drop=True)
|
| 133 |
+
|
| 134 |
+
# ββ Inference: inject anomaly score + weighted ensemble predict ββββββββ
|
| 135 |
+
X_test_enriched, proba, predictions, risk_scores = inference_predict(
|
| 136 |
+
_store["xgb"],
|
| 137 |
+
_store["lgb"],
|
| 138 |
+
_store["cat"],
|
| 139 |
+
_store["iso"],
|
| 140 |
+
_store["iso_rmin"],
|
| 141 |
+
_store["iso_rmax"],
|
| 142 |
+
X_test,
|
| 143 |
+
)
|
| 144 |
+
|
| 145 |
+
# ββ SHAP explanations via XGBoost + build output DataFrame ββββββββββββ
|
| 146 |
+
# X_test_enriched already has Anomaly_Score; test_ids is 0-indexed.
|
| 147 |
+
output = explain_and_save(
|
| 148 |
+
_store["xgb"], X_test_enriched, test_ids, predictions, risk_scores
|
| 149 |
+
)
|
| 150 |
+
|
| 151 |
+
# Integrity guard: lengths must match before streaming
|
| 152 |
+
if len(output) != len(test_ids):
|
| 153 |
+
raise HTTPException(
|
| 154 |
+
status_code=500,
|
| 155 |
+
detail=f"Row count mismatch: output={len(output)}, ids={len(test_ids)}",
|
| 156 |
+
)
|
| 157 |
+
|
| 158 |
+
# ββ Stream result as CSV (index=False β no 'Unnamed: 0' column) ββββββββ
|
| 159 |
+
stream = io.StringIO()
|
| 160 |
+
output.to_csv(stream, index=False)
|
| 161 |
+
stream.seek(0)
|
| 162 |
+
|
| 163 |
+
return StreamingResponse(
|
| 164 |
+
iter([stream.getvalue()]),
|
| 165 |
+
media_type="text/csv",
|
| 166 |
+
headers={"Content-Disposition": "attachment; filename=final_predictions.csv"},
|
| 167 |
+
)
|
| 168 |
+
|
| 169 |
+
|
| 170 |
+
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 171 |
+
# TRADE INTELLIGENCE β News endpoint (GNews upstream)
|
| 172 |
+
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 173 |
+
_CATEGORY_TERMS = {
|
| 174 |
+
"congestion": "congestion",
|
| 175 |
+
"shipping": "shipping",
|
| 176 |
+
"container": "container",
|
| 177 |
+
"trade": "trade",
|
| 178 |
+
"terminal": "terminal",
|
| 179 |
+
}
|
| 180 |
+
|
| 181 |
+
|
| 182 |
+
@app.get("/api/trade/trade-intelligence/news")
|
| 183 |
+
async def trade_intelligence_news(
|
| 184 |
+
keyword: str = Query(..., min_length=1),
|
| 185 |
+
category: str = Query("all"),
|
| 186 |
+
limit: int = Query(10, ge=1, le=50),
|
| 187 |
+
):
|
| 188 |
+
"""
|
| 189 |
+
Proxy to GNews API. Maps upstream response to the article schema
|
| 190 |
+
expected by the React frontend.
|
| 191 |
+
"""
|
| 192 |
+
if not GNEWS_API_KEY:
|
| 193 |
+
raise HTTPException(
|
| 194 |
+
status_code=401,
|
| 195 |
+
detail="News API key is not configured on the server.",
|
| 196 |
+
)
|
| 197 |
+
|
| 198 |
+
# Build search query β use OR to broaden instead of AND-narrowing
|
| 199 |
+
if category != "all" and category in _CATEGORY_TERMS:
|
| 200 |
+
search_q = f"{keyword} OR {_CATEGORY_TERMS[category]}"
|
| 201 |
+
else:
|
| 202 |
+
search_q = keyword
|
| 203 |
+
|
| 204 |
+
params = {
|
| 205 |
+
"q": search_q,
|
| 206 |
+
"language": "en",
|
| 207 |
+
"pageSize": str(limit),
|
| 208 |
+
"apiKey": GNEWS_API_KEY,
|
| 209 |
+
}
|
| 210 |
+
|
| 211 |
+
try:
|
| 212 |
+
async with httpx.AsyncClient(timeout=15.0) as client:
|
| 213 |
+
resp = await client.get("https://newsapi.org/v2/everything", params=params)
|
| 214 |
+
except httpx.TimeoutException:
|
| 215 |
+
raise HTTPException(status_code=504)
|
| 216 |
+
except httpx.RequestError:
|
| 217 |
+
raise HTTPException(status_code=502)
|
| 218 |
+
|
| 219 |
+
# Map upstream status codes to what the frontend expects
|
| 220 |
+
if resp.status_code == 401 or resp.status_code == 403:
|
| 221 |
+
raise HTTPException(status_code=401)
|
| 222 |
+
if resp.status_code == 429:
|
| 223 |
+
raise HTTPException(status_code=429)
|
| 224 |
+
if resp.status_code >= 500:
|
| 225 |
+
raise HTTPException(status_code=502)
|
| 226 |
+
if resp.status_code != 200:
|
| 227 |
+
raise HTTPException(status_code=500)
|
| 228 |
+
|
| 229 |
+
data = resp.json()
|
| 230 |
+
raw_articles = data.get("articles", [])
|
| 231 |
+
|
| 232 |
+
articles = [
|
| 233 |
+
{
|
| 234 |
+
"title": a.get("title", ""),
|
| 235 |
+
"description": a.get("description"),
|
| 236 |
+
"url": a.get("url", ""),
|
| 237 |
+
"image_url": a.get("image"),
|
| 238 |
+
"source_name": (a.get("source") or {}).get("name", "Unknown"),
|
| 239 |
+
"published_at": a.get("publishedAt", ""),
|
| 240 |
+
}
|
| 241 |
+
for a in raw_articles
|
| 242 |
+
]
|
| 243 |
+
|
| 244 |
+
return {"articles": articles}
|
| 245 |
+
|