Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,19 +1,63 @@
|
|
| 1 |
from fastapi import FastAPI
|
| 2 |
-
from
|
|
|
|
|
|
|
| 3 |
|
| 4 |
app = FastAPI()
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from fastapi import FastAPI
|
| 2 |
+
from pydantic import BaseModel, Field, conlist, confloat
|
| 3 |
+
from typing import Dict, Any, List, Literal
|
| 4 |
+
from datetime import datetime
|
| 5 |
|
| 6 |
app = FastAPI()
|
| 7 |
|
| 8 |
+
class Metrics(BaseModel):
|
| 9 |
+
TCS: confloat(ge=0, le=1) = 0.0
|
| 10 |
+
HDI: float = 0.0
|
| 11 |
+
PDS: confloat(ge=0, le=1) = 0.0
|
| 12 |
+
EVI: int = 0
|
| 13 |
+
CBS: confloat(ge=0, le=1) = 0.0
|
| 14 |
+
LQS: confloat(ge=0, le=1) = 0.0
|
| 15 |
+
|
| 16 |
+
class AuditResult(BaseModel):
|
| 17 |
+
metrics: Metrics
|
| 18 |
+
decision: Literal['APPROVE','REWORK']
|
| 19 |
+
deltas: conlist(str, min_items=0)
|
| 20 |
+
|
| 21 |
+
@app.get("/health")
|
| 22 |
+
def health():
|
| 23 |
+
return {"ok": True, "service": "auditor", "ts": datetime.utcnow().isoformat()}
|
| 24 |
+
|
| 25 |
+
@app.post("/audit", response_model=AuditResult)
|
| 26 |
+
def audit(payload: Dict[str, Any]):
|
| 27 |
+
bundle = payload.get("bundle", {})
|
| 28 |
+
m_in = payload.get("metrics", {})
|
| 29 |
+
|
| 30 |
+
# Use incoming metrics as a starting point
|
| 31 |
+
m = Metrics(**{
|
| 32 |
+
"TCS": float(m_in.get("TCS", 0.0)),
|
| 33 |
+
"HDI": float(m_in.get("HDI", 0.0)),
|
| 34 |
+
"PDS": float(m_in.get("PDS", 0.0)),
|
| 35 |
+
"EVI": int(m_in.get("EVI", 0)),
|
| 36 |
+
"CBS": float(m_in.get("CBS", 0.0)),
|
| 37 |
+
"LQS": float(m_in.get("LQS", 0.0)),
|
| 38 |
+
})
|
| 39 |
+
|
| 40 |
+
# Simple structural checks (you can expand later)
|
| 41 |
+
edges = len(bundle.get("network",{}).get("data",[]) or [])
|
| 42 |
+
regions = len((bundle.get("regions",{}).get("data") or {}).get("regions_ranked",[]) or [])
|
| 43 |
+
lit_ok = bool(bundle.get("literature"))
|
| 44 |
+
|
| 45 |
+
# Patch metrics heuristically
|
| 46 |
+
m.TCS = min(1.0, (1 if edges>0 else 0) + (1 if regions>0 else 0) + (1 if lit_ok else 0)) * 0.9
|
| 47 |
+
if regions > 8: m.HDI = max(m.HDI, 1.2)
|
| 48 |
+
elif regions > 4: m.HDI = max(m.HDI, 1.0)
|
| 49 |
+
else: m.HDI = max(m.HDI, 0.8)
|
| 50 |
+
m.EVI = (1 if edges>0 else 0) + (1 if regions>0 else 0) + (1 if lit_ok else 0)
|
| 51 |
+
m.CBS = max(0.2, float(m.CBS)) # pretend we saw a little contrary evidence
|
| 52 |
+
|
| 53 |
+
deltas: List[str] = []
|
| 54 |
+
|
| 55 |
+
# Gate: if hypotheses thin or contrary evidence high -> REWORK
|
| 56 |
+
decision: Literal['APPROVE','REWORK'] = 'APPROVE'
|
| 57 |
+
if (m.HDI < 1.0) or (m.EVI < 3) or (m.CBS > 0.6):
|
| 58 |
+
decision = 'REWORK'
|
| 59 |
+
if m.HDI < 1.0: deltas.append("Increase hypothesis diversity: broaden regions search.")
|
| 60 |
+
if m.EVI < 3: deltas.append("Add at least one more evidence type (e.g., trials or review).")
|
| 61 |
+
if m.CBS > 0.6: deltas.append("Surface and address contrary findings explicitly.")
|
| 62 |
+
|
| 63 |
+
return AuditResult(metrics=m, decision=decision, deltas=deltas)
|