LordXido commited on
Commit
bfe64fb
·
verified ·
1 Parent(s): 10a6f46

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +59 -109
app.py CHANGED
@@ -1,135 +1,85 @@
1
- import gradio as gr
 
2
  import hashlib
3
  import time
 
 
 
 
 
 
 
4
 
5
  # ===============================
6
- # CodexVault Reflex Memory (Session-Lattice)
7
  # ===============================
8
- REFLEX_MEMORY = []
9
- REFLEX_COUNT = 0
10
 
11
- def classify_state(score: float) -> str:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
  if score >= 0.8:
13
  return "HALT"
14
  elif score >= 0.4:
15
  return "MONITOR"
16
  return "STABLE"
17
 
18
- def reflex_guard(scenario: str):
19
- global REFLEX_COUNT
20
- REFLEX_COUNT += 1
21
-
 
22
  ts = time.strftime("%Y-%m-%d %H:%M:%S")
23
- scenario_norm = scenario.strip().lower()
24
- scenario_hash = hashlib.sha256(scenario_norm.encode()).hexdigest()[:12]
25
 
26
- if any(k in scenario_norm for k in ["surge", "override", "anomaly", "breach", "failure"]):
27
  score = 0.92
28
- elif any(k in scenario_norm for k in ["delay", "degrade", "unstable"]):
29
  score = 0.55
30
  else:
31
  score = 0.18
32
 
33
- state = classify_state(score)
34
-
35
- entry = {
36
- "timestamp": ts,
37
- "hash": scenario_hash,
38
- "state": state,
39
- "score": score
40
- }
41
-
42
- REFLEX_MEMORY.append(entry)
43
-
44
- log = [
45
- f"[CodexVault]: Reflex #{REFLEX_COUNT}",
46
- f"[Timestamp]: {ts}",
47
- f"[Scenario Hash]: {scenario_hash}",
48
- f"[Analyzer]: Anomaly score = {score:.2f}",
49
- f"[Decision]: SYSTEM STATE → {state}",
50
- ]
51
 
52
  if state == "HALT":
53
- log.append("[Action]: HALT_SIGNAL issued (preemptive defense engaged).")
54
  elif state == "MONITOR":
55
- log.append("[Action]: Monitoring escalation pathways.")
56
  else:
57
- log.append("[Action]: System stable. Passive vigilance.")
58
-
59
- log.append(f"[Vault]: Reflex entries stored = {len(REFLEX_MEMORY)}")
60
-
61
- return "\n".join(log)
62
 
 
 
 
 
 
 
 
63
 
64
- # ===============================
65
- # Codex UI (Embedded Frontend)
66
- # ===============================
67
- HTML_UI = """
68
- <div style="background:#0f0f0f;color:#ffffff;padding:32px;
69
- font-family:Segoe UI,Arial;border-radius:14px;max-width:900px;margin:auto">
70
-
71
- <h1 style="text-align:center">🛡️ Codex ReflexGuard</h1>
72
- <p style="text-align:center;opacity:0.8">
73
- CodexVault Eternal Node · Reflex-Level Safety Intelligence
74
- </p>
75
-
76
- <div style="margin-top:20px">
77
- <label style="font-weight:bold">System Scenario</label>
78
- <textarea id="scenario"
79
- style="width:100%;height:100px;margin-top:8px;
80
- background:#1e1e1e;color:#fff;
81
- border-radius:10px;border:none;padding:12px"></textarea>
82
- </div>
83
-
84
- <button onclick="runReflex()"
85
- style="margin-top:18px;padding:14px 22px;
86
- background:#00ffff;color:#000;
87
- font-weight:bold;border:none;
88
- border-radius:10px;cursor:pointer">
89
- 🚀 Launch Reflex Check
90
- </button>
91
-
92
- <pre id="output"
93
- style="margin-top:24px;background:#1e1e1e;
94
- padding:16px;border-radius:10px;
95
- min-height:160px;white-space:pre-wrap">
96
- [CodexVault]: Awaiting scenario input…
97
- </pre>
98
-
99
- <p style="margin-top:20px;font-size:12px;opacity:0.6;text-align:center">
100
- This node demonstrates reflex-level safety intelligence.
101
- It is not predictive analytics. It is pre-emptive system defense.
102
- </p>
103
- </div>
104
-
105
- <script>
106
- async function runReflex(){
107
- const scenario = document.getElementById("scenario").value;
108
- const output = document.getElementById("output");
109
-
110
- if(!scenario){
111
- output.textContent += "\\n[Error]: No scenario provided.";
112
- return;
113
- }
114
-
115
- const res = await fetch("/run/predict", {
116
- method: "POST",
117
- headers: {"Content-Type":"application/json"},
118
- body: JSON.stringify({data:[scenario]})
119
- });
120
-
121
- const data = await res.json();
122
- output.textContent = data.data[0];
123
- }
124
- </script>
125
- """
126
-
127
- # ===============================
128
- # Gradio App (HF-Native)
129
- # ===============================
130
- with gr.Blocks(css="body{background:#0f0f0f}") as demo:
131
- gr.HTML(HTML_UI)
132
- hidden = gr.Textbox(visible=False)
133
- hidden.change(reflex_guard, hidden, hidden, api_name="run")
134
 
135
- demo.launch()
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI
2
+ from pydantic import BaseModel
3
  import hashlib
4
  import time
5
+ from typing import List
6
+
7
+ app = FastAPI(
8
+ title="Codex ReflexGuard Enterprise API",
9
+ version="vΩΞ-E",
10
+ description="Reflex-Level Safety Intelligence API (CodexVault)"
11
+ )
12
 
13
  # ===============================
14
+ # CodexVault (In-Memory Reference)
15
  # ===============================
16
+ CODEX_VAULT: List[dict] = []
 
17
 
18
+ # ===============================
19
+ # Data Contracts
20
+ # ===============================
21
+ class ReflexRequest(BaseModel):
22
+ scenario: str
23
+ source: str | None = "unknown"
24
+
25
+ class ReflexResponse(BaseModel):
26
+ timestamp: str
27
+ scenario_hash: str
28
+ anomaly_score: float
29
+ system_state: str
30
+ action: str
31
+ vault_size: int
32
+
33
+ # ===============================
34
+ # Core Logic
35
+ # ===============================
36
+ def classify(score: float) -> str:
37
  if score >= 0.8:
38
  return "HALT"
39
  elif score >= 0.4:
40
  return "MONITOR"
41
  return "STABLE"
42
 
43
+ # ===============================
44
+ # Enterprise Endpoint
45
+ # ===============================
46
+ @app.post("/v1/reflex/check", response_model=ReflexResponse)
47
+ def reflex_check(req: ReflexRequest):
48
  ts = time.strftime("%Y-%m-%d %H:%M:%S")
49
+ normalized = req.scenario.lower().strip()
50
+ scenario_hash = hashlib.sha256(normalized.encode()).hexdigest()
51
 
52
+ if any(k in normalized for k in ["override", "surge", "breach", "failure"]):
53
  score = 0.92
54
+ elif any(k in normalized for k in ["delay", "unstable", "degrade"]):
55
  score = 0.55
56
  else:
57
  score = 0.18
58
 
59
+ state = classify(score)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60
 
61
  if state == "HALT":
62
+ action = "HALT_SIGNAL"
63
  elif state == "MONITOR":
64
+ action = "ESCALATE_MONITORING"
65
  else:
66
+ action = "CONTINUE_OPERATION"
 
 
 
 
67
 
68
+ entry = {
69
+ "timestamp": ts,
70
+ "scenario_hash": scenario_hash,
71
+ "score": score,
72
+ "state": state,
73
+ "source": req.source
74
+ }
75
 
76
+ CODEX_VAULT.append(entry)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
77
 
78
+ return ReflexResponse(
79
+ timestamp=ts,
80
+ scenario_hash=scenario_hash,
81
+ anomaly_score=score,
82
+ system_state=state,
83
+ action=action,
84
+ vault_size=len(CODEX_VAULT)
85
+ )