Download app/routers/admin.py from Adisri99/Space-Risk-Intelligence-API: direct link, hf CLI and curl.
- Browser
- Download file 2.25 kB
-
https://huggingface.co/spaces/Adisri99/Space-Risk-Intelligence-API/resolve/main/app/routers/admin.py
- Command line
-
hf download hf://spaces/Adisri99/Space-Risk-Intelligence-API/app/routers/admin.py
-
curl -L -o admin.py https://huggingface.co/spaces/Adisri99/Space-Risk-Intelligence-API/resolve/main/app/routers/admin.py
2.25 kB
| import json | |
| from fastapi import APIRouter, Depends | |
| from app.database import get_db | |
| from app.feature_engineering import normalize_object | |
| from app.ingestion import fetch_celestrak_json | |
| from app.repository import list_high_risk_pairs, latest_runs, get_run | |
| from app.services import scoring_cycle, demo_objects | |
| from app.scripts.train_baseline import run_training | |
| router=APIRouter(prefix="/api/v1", tags=["admin"]) | |
| def ingest_celestrak(db=Depends(get_db)): return {"status":"ok", **scoring_cycle(db, [normalize_object(x) for x in fetch_celestrak_json()], source="celestrak")} | |
| def score_demo_cycle(db=Depends(get_db)): return {"status":"ok", **scoring_cycle(db, demo_objects(db), source="demo")} | |
| def train_baseline(): | |
| model_path,rows,metrics=run_training(); return {"status":"ok","model_path":model_path,"rows_used":rows,"metrics":metrics} | |
| def alerts_live(limit:int=25, db=Depends(get_db)): | |
| rows=list_high_risk_pairs(db, limit); return [{"pair_id":r.pair_id,"final_score":r.final_score,"risk_label":r.risk_label,"top_factors":json.loads(r.top_factors_json)} for r in rows] | |
| def runs(db=Depends(get_db)): return [{"run_id":r.run_id,"source":r.source,"object_count":r.object_count,"candidate_pair_count":r.candidate_pair_count,"scored_pair_count":r.scored_pair_count,"completed":r.completed,"created_at":r.created_at} for r in latest_runs(db, 10)] | |
| def run_detail(run_id:str, db=Depends(get_db)): | |
| r=get_run(db, run_id) | |
| if not r: return {"detail":"Run not found"} | |
| rows=[x for x in list_high_risk_pairs(db,200) if x.latest_run_id==run_id]; labels={"low":0,"medium":0,"high":0,"critical":0} | |
| for x in rows: labels[x.risk_label]=labels.get(x.risk_label,0)+1 | |
| return {"run_id":r.run_id,"source":r.source,"object_count":r.object_count,"candidate_pair_count":r.candidate_pair_count,"scored_pair_count":r.scored_pair_count,"completed":r.completed,"created_at":r.created_at,"label_distribution":labels,"top_pairs":[{"pair_id":x.pair_id,"final_score":x.final_score,"risk_label":x.risk_label} for x in sorted(rows, key=lambda z:z.final_score, reverse=True)[:20]]} | |