Update app.py
Browse files
app.py
CHANGED
|
@@ -1,10 +1,24 @@
|
|
| 1 |
-
|
| 2 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
from pathlib import Path
|
| 4 |
from typing import Dict, Any
|
| 5 |
|
|
|
|
|
|
|
|
|
|
| 6 |
# βββββββββββββββββββββββββββββββββββββββββββββ
|
| 7 |
-
#
|
| 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 [
|
| 16 |
-
if
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
-
|
| 19 |
-
"""Very simple keyword-block demo."""
|
| 20 |
-
return not any(term.lower() in output.lower() for term in contracts)
|
| 21 |
|
| 22 |
# βββββββββββββββββββββββββββββββββββββββββββββ
|
| 23 |
-
#
|
| 24 |
# βββββββββββββββββββββββββββββββββββββββββββββ
|
| 25 |
LEDGER_FILE = "ledger.jsonl"
|
| 26 |
|
| 27 |
-
def log_event(
|
| 28 |
with open(LEDGER_FILE, "a", encoding="utf-8") as f:
|
| 29 |
-
f.write(json.dumps(
|
| 30 |
|
| 31 |
# βββββββββββββββββββββββββββββββββββββββββββββ
|
| 32 |
-
#
|
| 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}
|
|
|
|
|
|
|
| 40 |
|
| 41 |
# βββββββββββββββββββββββββββββββββββββββββββββ
|
| 42 |
-
#
|
| 43 |
# βββββββββββββββββββββββββββββββββββββββββββββ
|
| 44 |
def decide(intent: str, context: str, alpha: float) -> str:
|
| 45 |
-
|
| 46 |
-
if "
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
# βββββββββββββββββββββββββββββββββββββββββββββ
|
| 53 |
-
#
|
| 54 |
# βββββββββββββββββββββββββββββββββββββββββββββ
|
| 55 |
-
contracts_cache = load_contracts()
|
| 56 |
-
meta_state = load_meta()
|
| 57 |
-
|
| 58 |
def codex_run(intent: str, context: str):
|
| 59 |
-
|
| 60 |
alpha = meta_state.get("alpha", 0.5)
|
| 61 |
-
|
| 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 |
-
|
| 74 |
log_event({
|
| 75 |
-
"t":
|
| 76 |
-
"
|
| 77 |
-
"context": context,
|
| 78 |
-
"decision": decision,
|
| 79 |
-
"output": output,
|
| 80 |
-
"safe": safe
|
| 81 |
})
|
| 82 |
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
"intent": intent,
|
| 86 |
-
"decision": decision,
|
| 87 |
-
"safe": safe
|
| 88 |
-
})
|
| 89 |
-
|
| 90 |
-
return output, decision, audit_snapshot
|
| 91 |
|
| 92 |
# βββββββββββββββββββββββββββββββββββββββββββββ
|
| 93 |
-
#
|
| 94 |
# βββββββββββββββββββββββββββββββββββββββββββββ
|
| 95 |
-
with gr.Blocks(title="Jarvis X
|
| 96 |
gr.Markdown("""
|
| 97 |
# π§ Jarvis X β Beyond-SOTA Codex Operational Intelligence
|
| 98 |
-
**
|
| 99 |
|
| 100 |
-
*Edit `contracts.dsl` to modify safety
|
|
|
|
|
|
|
| 101 |
""")
|
| 102 |
|
| 103 |
-
intent_in
|
| 104 |
-
ctx_in
|
| 105 |
-
run_btn
|
| 106 |
|
| 107 |
-
out_box
|
| 108 |
-
dec_box
|
| 109 |
-
audit_box
|
| 110 |
|
| 111 |
-
run_btn.click(
|
| 112 |
-
|
| 113 |
-
|
|
|
|
|
|
|
| 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()
|