LordXido commited on
Commit
ddeb720
Β·
verified Β·
1 Parent(s): b4dc5ac

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +77 -61
app.py CHANGED
@@ -1,10 +1,24 @@
1
- import gradio as gr
2
- import json, time
 
 
 
 
 
 
 
 
 
 
 
3
  from pathlib import Path
4
  from typing import Dict, Any
5
 
 
 
 
6
  # ─────────────────────────────────────────────
7
- # Ξ›* ── Contract / Safety Engine
8
  # ─────────────────────────────────────────────
9
  CONTRACT_FILE = "contracts.dsl"
10
 
@@ -12,105 +26,107 @@ def load_contracts() -> list[str]:
12
  path = Path(CONTRACT_FILE)
13
  if not path.exists():
14
  return []
15
- return [line.strip() for line in path.read_text().splitlines()
16
- if line.strip() and not line.startswith("#")]
 
 
 
17
 
18
- def contracts_pass(output: str, contracts: list[str]) -> bool:
19
- """Very simple keyword-block demo."""
20
- return not any(term.lower() in output.lower() for term in contracts)
21
 
22
  # ─────────────────────────────────────────────
23
- # Ξ© ── Immutable Audit Ledger
24
  # ─────────────────────────────────────────────
25
  LEDGER_FILE = "ledger.jsonl"
26
 
27
- def log_event(event: Dict[str, Any]):
28
  with open(LEDGER_FILE, "a", encoding="utf-8") as f:
29
- f.write(json.dumps(event) + "\n")
30
 
31
  # ─────────────────────────────────────────────
32
- # Meta-Learning Placeholder (disabled)
33
  # ─────────────────────────────────────────────
34
  META_FILE = "meta.json"
35
-
36
  def load_meta() -> Dict[str, Any]:
37
  if Path(META_FILE).exists():
38
  return json.loads(Path(META_FILE).read_text())
39
- return {"alpha": 0.5} # Ξ± = LLM vs symbolic mix
 
 
40
 
41
  # ─────────────────────────────────────────────
42
- # Decision Logic (Hybrid LLM / Symbolic stub)
43
  # ─────────────────────────────────────────────
44
  def decide(intent: str, context: str, alpha: float) -> str:
45
- """Replace with real LLM call if desired."""
46
- if "code" in intent.lower():
47
- return f"[LLM*{alpha:.2f}] Generated code stub respecting constraints."
48
- if "analyze" in intent.lower():
49
- return "[Symbolic] Performed bounded system analysis."
50
- return "[Explain] Bounded explanatory output."
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51
 
52
  # ─────────────────────────────────────────────
53
- # Main Operational Engine
54
  # ─────────────────────────────────────────────
55
- contracts_cache = load_contracts()
56
- meta_state = load_meta()
57
-
58
  def codex_run(intent: str, context: str):
59
- timestamp = time.time()
60
  alpha = meta_state.get("alpha", 0.5)
61
- decision = decide(intent, context, alpha)
62
-
63
- output = (
64
- f"Decision: {decision}\n\n"
65
- f"Context:\n{context}\n\n"
66
- f"System status: constraints enforced, Ξ±={alpha:.2f}"
67
- )
68
 
69
  safe = contracts_pass(output, contracts_cache)
70
  if not safe:
71
  output = "Output blocked by contract engine (Ξ›*)."
72
 
73
- # Audit ledger
74
  log_event({
75
- "t": timestamp,
76
- "intent": intent,
77
- "context": context,
78
- "decision": decision,
79
- "output": output,
80
- "safe": safe
81
  })
82
 
83
- audit_snapshot = json.dumps({
84
- "t": timestamp,
85
- "intent": intent,
86
- "decision": decision,
87
- "safe": safe
88
- })
89
-
90
- return output, decision, audit_snapshot
91
 
92
  # ─────────────────────────────────────────────
93
- # Gradio Interface
94
  # ─────────────────────────────────────────────
95
- with gr.Blocks(title="Jarvis X – Beyond-SOTA Codex Demo") as demo:
96
  gr.Markdown("""
97
  # 🧠 Jarvis X β€” Beyond-SOTA Codex Operational Intelligence
98
- **Constraint-aware, auditable decision engine**
99
 
100
- *Edit `contracts.dsl` to modify safety rules. All events append to `ledger.jsonl`.*
 
 
101
  """)
102
 
103
- intent_in = gr.Textbox(label="Intent (Ξ¨)", placeholder="e.g. generate code")
104
- ctx_in = gr.Textbox(label="Context (Ο‡)", lines=4)
105
- run_btn = gr.Button("Run")
106
 
107
- out_box = gr.Textbox(label="System Output (Θ)", lines=6)
108
- dec_box = gr.Textbox(label="Decision Trace", lines=1)
109
- audit_box = gr.Textbox(label="Audit Snapshot", lines=2)
110
 
111
- run_btn.click(codex_run,
112
- inputs=[intent_in, ctx_in],
113
- outputs=[out_box, dec_box, audit_box])
 
 
114
 
115
  if __name__ == "__main__":
116
  demo.launch()
 
1
+ """
2
+ Jarvis X β€” Beyond-SOTA Codex Demo
3
+ Fully-contained Gradio app with toy multi-LLM router, constraint engine,
4
+ and audit ledger.
5
+
6
+ Companions needed in the same directory:
7
+ β€’ llm_sim.py – toy LLM provider registry
8
+ β€’ contracts.dsl – keyword safety list
9
+ β€’ requirements.txt – gradio>=4.0
10
+ """
11
+
12
+ import json
13
+ import time
14
  from pathlib import Path
15
  from typing import Dict, Any
16
 
17
+ import gradio as gr
18
+ from llm_sim import LLM_REGISTRY # <-- toy LLM providers
19
+
20
  # ─────────────────────────────────────────────
21
+ # Ξ›* ── Contract / Safety Engine
22
  # ─────────────────────────────────────────────
23
  CONTRACT_FILE = "contracts.dsl"
24
 
 
26
  path = Path(CONTRACT_FILE)
27
  if not path.exists():
28
  return []
29
+ return [ln.strip() for ln in path.read_text().splitlines()
30
+ if ln.strip() and not ln.startswith("#")]
31
+
32
+ def contracts_pass(text: str, rules: list[str]) -> bool:
33
+ return not any(rule.lower() in text.lower() for rule in rules)
34
 
35
+ contracts_cache = load_contracts()
 
 
36
 
37
  # ─────────────────────────────────────────────
38
+ # Ξ© ── Immutable Audit Ledger
39
  # ─────────────────────────────────────────────
40
  LEDGER_FILE = "ledger.jsonl"
41
 
42
+ def log_event(ev: Dict[str, Any]):
43
  with open(LEDGER_FILE, "a", encoding="utf-8") as f:
44
+ f.write(json.dumps(ev) + "\n")
45
 
46
  # ─────────────────────────────────────────────
47
+ # Meta state (Ξ± weight) β€” stubbed
48
  # ─────────────────────────────────────────────
49
  META_FILE = "meta.json"
 
50
  def load_meta() -> Dict[str, Any]:
51
  if Path(META_FILE).exists():
52
  return json.loads(Path(META_FILE).read_text())
53
+ return {"alpha": 0.5}
54
+
55
+ meta_state = load_meta() # Ξ± weight for LLM vs symbolic
56
 
57
  # ─────────────────────────────────────────────
58
+ # Decision Logic (toy multi-LLM + symbolic blend)
59
  # ─────────────────────────────────────────────
60
  def decide(intent: str, context: str, alpha: float) -> str:
61
+ # 1. Pick toy provider based on simple keywords
62
+ if "advanced" in intent.lower():
63
+ provider = LLM_REGISTRY["claude"]
64
+ elif "concise" in intent.lower():
65
+ provider = LLM_REGISTRY["mistral"]
66
+ else:
67
+ provider = LLM_REGISTRY["gpt"]
68
+
69
+ # 2. Generate toy LLM response
70
+ prompt = f"Intent: {intent}\nContext: {context}"
71
+ llm_out = provider.generate(prompt)
72
+
73
+ # 3. Symbolic stub
74
+ symbolic_out = "(symbolic system stub)"
75
+
76
+ # 4. Blend outputs
77
+ blended = (
78
+ f"[{provider.name} * {alpha:.2f}] {llm_out}\n"
79
+ f"[Symbolic * {1-alpha:.2f}] {symbolic_out}"
80
+ )
81
+ return blended
82
 
83
  # ─────────────────────────────────────────────
84
+ # Gradio Callback
85
  # ─────────────────────────────────────────────
 
 
 
86
  def codex_run(intent: str, context: str):
87
+ t = time.time()
88
  alpha = meta_state.get("alpha", 0.5)
89
+ output = decide(intent, context, alpha)
 
 
 
 
 
 
90
 
91
  safe = contracts_pass(output, contracts_cache)
92
  if not safe:
93
  output = "Output blocked by contract engine (Ξ›*)."
94
 
95
+ decision_trace = "provider_mix Ξ±={:.2f}".format(alpha)
96
  log_event({
97
+ "t": t, "intent": intent, "context": context,
98
+ "decision": decision_trace, "output": output, "safe": safe
 
 
 
 
99
  })
100
 
101
+ audit = json.dumps({"t": t, "decision": decision_trace, "safe": safe})
102
+ return output, decision_trace, audit
 
 
 
 
 
 
103
 
104
  # ─────────────────────────────────────────────
105
+ # Gradio UI
106
  # ─────────────────────────────────────────────
107
+ with gr.Blocks(title="Jarvis X β€” Beyond-SOTA Codex Demo") as demo:
108
  gr.Markdown("""
109
  # 🧠 Jarvis X β€” Beyond-SOTA Codex Operational Intelligence
110
+ **Toy multi-LLM router, constraint engine, audit ledger**
111
 
112
+ *Edit `contracts.dsl` to modify safety keywords. All runs append to
113
+ `ledger.jsonl`. LLMs here are simulated; replace in `llm_sim.py`
114
+ with real API calls when ready.*
115
  """)
116
 
117
+ intent_in = gr.Textbox(label="Intent (Ξ¨)", placeholder="e.g. generate code, advanced analysis")
118
+ ctx_in = gr.Textbox(label="Context (Ο‡)", lines=4)
119
+ run_btn = gr.Button("Run")
120
 
121
+ out_box = gr.Textbox(label="System Output (Θ)", lines=6)
122
+ dec_box = gr.Textbox(label="Decision Trace", lines=1)
123
+ audit_box = gr.Textbox(label="Audit Snapshot", lines=2)
124
 
125
+ run_btn.click(
126
+ fn=codex_run,
127
+ inputs=[intent_in, ctx_in],
128
+ outputs=[out_box, dec_box, audit_box]
129
+ )
130
 
131
  if __name__ == "__main__":
132
  demo.launch()