darkfrostx commited on
Commit
6e87ff8
·
verified ·
1 Parent(s): d5411f6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -34
app.py CHANGED
@@ -1,7 +1,7 @@
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
 
@@ -18,46 +18,54 @@ class AuditResult(BaseModel):
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)
 
 
 
1
  from fastapi import FastAPI
2
+ from pydantic import BaseModel, conlist, confloat
3
  from typing import Dict, Any, List, Literal
4
+ from guardrails import Guard
5
 
6
  app = FastAPI()
7
 
 
18
  decision: Literal['APPROVE','REWORK']
19
  deltas: conlist(str, min_items=0)
20
 
21
+ # Guardrails schema (you can add validators later)
22
+ AUDIT_SCHEMA = {
23
+ "type": "object",
24
+ "properties": {
25
+ "decision": {"type":"string","enum":["APPROVE","REWORK"]},
26
+ "metrics": {"type":"object"},
27
+ "deltas": {"type":"array","items":{"type":"string"}}
28
+ },
29
+ "required": ["decision","metrics","deltas"]
30
+ }
31
+ guard = Guard().configure(output_schema=AUDIT_SCHEMA)
32
+
33
  @app.get("/health")
34
  def health():
35
+ return {"ok": True}
36
 
37
  @app.post("/audit", response_model=AuditResult)
38
  def audit(payload: Dict[str, Any]):
39
+ bundle = payload.get("bundle", {}) or {}
40
+ m_in = payload.get("metrics", {}) or {}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41
 
42
+ m = {
43
+ "TCS": float(m_in.get("TCS", 0.0)),
44
+ "HDI": float(m_in.get("HDI", 0.0)),
45
+ "PDS": float(m_in.get("PDS", 0.0)),
46
+ "EVI": int(m_in.get("EVI", 0)),
47
+ "CBS": float(m_in.get("CBS", 0.0)),
48
+ "LQS": float(m_in.get("LQS", 0.0)),
49
+ }
50
 
51
+ edges = len(bundle.get("network",{}).get("data",[]) or [])
52
+ regions = len((bundle.get("regions",{}).get("data") or {}).get("regions_ranked",[]) or [])
53
+ lit_ok = bool(bundle.get("literature"))
54
+
55
+ 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
56
+ m["HDI"] = 1.2 if regions>8 else (1.0 if regions>4 else 0.8)
57
+ m["EVI"] = (1 if edges>0 else 0) + (1 if regions>0 else 0) + (1 if lit_ok else 0)
58
+ m["CBS"] = max(0.2, m["CBS"])
59
+ m["LQS"] = max(0.6, m["LQS"])
60
 
 
61
  decision: Literal['APPROVE','REWORK'] = 'APPROVE'
62
+ deltas: List[str] = []
63
+ if (m["HDI"] < 1.0) or (m["EVI"] < 3) or (m["CBS"] > 0.6):
64
  decision = 'REWORK'
65
+ if m["HDI"] < 1.0: deltas.append("Increase hypothesis diversity (broaden regions).")
66
+ if m["EVI"] < 3: deltas.append("Add at least one more evidence type (review/trials).")
67
+ if m["CBS"] > 0.6: deltas.append("Surface/address contrary findings explicitly.")
68
 
69
+ out_raw = {"metrics": m, "decision": decision, "deltas": deltas}
70
+ _, validated, *_ = guard.parse(out_raw)
71
+ return AuditResult(**validated)