Spaces:
Running on Zero
Running on Zero
Fix runtime error: async scaffold, resilient ledger, remove broken imports, ZeroGPU-free CPU-first architecture
Browse files
app.py
CHANGED
|
@@ -1,125 +1,171 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
import os
|
| 3 |
import json
|
| 4 |
-
|
| 5 |
-
import
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
-
#
|
| 8 |
-
|
| 9 |
-
|
|
|
|
|
|
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
if
|
| 15 |
try:
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
except
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
try:
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
)
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
"
|
| 57 |
-
"
|
| 58 |
-
"
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
#
|
| 75 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
gr.Markdown("# TEQUMSA Symbiotic Orchestrator - Inference Node")
|
| 77 |
-
gr.Markdown("Autonomous multi-agent inference routing
|
| 78 |
-
gr.Markdown(
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
|
|
|
| 85 |
)
|
| 86 |
-
|
|
|
|
| 87 |
with gr.Row():
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
)
|
| 93 |
-
mode_dropdown = gr.Dropdown(
|
| 94 |
-
choices=["standard", "recursive", "causal", "rdod"],
|
| 95 |
-
value="standard",
|
| 96 |
-
label="Execution Mode"
|
| 97 |
)
|
| 98 |
-
|
| 99 |
-
process_btn = gr.Button("Process Request", variant="primary")
|
| 100 |
-
output = gr.Textbox(label="Inference Output", lines=10)
|
| 101 |
-
|
| 102 |
-
process_btn.click(
|
| 103 |
-
fn=process_request,
|
| 104 |
-
inputs=[prompt_input, model_dropdown, mode_dropdown],
|
| 105 |
-
outputs=output
|
| 106 |
-
)
|
| 107 |
-
|
| 108 |
-
with gr.Tab("Router"):
|
| 109 |
-
router_prompt = gr.Textbox(
|
| 110 |
-
label="Prompt to Route",
|
| 111 |
-
placeholder="Enter prompt for routing analysis...",
|
| 112 |
-
lines=3
|
| 113 |
-
)
|
| 114 |
-
router_model = gr.Textbox(label="Target Model", value="auto")
|
| 115 |
route_btn = gr.Button("Analyze Route", variant="secondary")
|
| 116 |
route_output = gr.Textbox(label="Route Analysis", lines=8)
|
| 117 |
-
|
| 118 |
route_btn.click(
|
| 119 |
-
fn=
|
| 120 |
inputs=[router_prompt, router_model],
|
| 121 |
outputs=route_output
|
| 122 |
)
|
| 123 |
|
| 124 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 125 |
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
import json
|
| 3 |
+
import time
|
| 4 |
+
import hashlib
|
| 5 |
+
import asyncio
|
| 6 |
+
import gradio as gr
|
| 7 |
+
from huggingface_hub import login
|
| 8 |
|
| 9 |
+
# --- INVARIANT CONSTANTS ---
|
| 10 |
+
PHI = 1.61803398875
|
| 11 |
+
UF_HZ = 23514.26
|
| 12 |
+
PERSISTENT_DIR = "/data"
|
| 13 |
+
LEDGER_PATH = os.path.join(PERSISTENT_DIR, "tequmsa_merkle_ledger.json")
|
| 14 |
|
| 15 |
+
# 1. FEDERATION HANDSHAKE - run at import time (no GPU needed)
|
| 16 |
+
def authenticate_node():
|
| 17 |
+
hf_token = os.environ.get("HF_TOKEN")
|
| 18 |
+
if hf_token:
|
| 19 |
try:
|
| 20 |
+
login(token=hf_token)
|
| 21 |
+
print("[ATEN] Federation Handshake successful. HF_TOKEN verified.")
|
| 22 |
+
except Exception as e:
|
| 23 |
+
print(f"[HARPER] Warning: Token authentication failed: {e}")
|
| 24 |
+
else:
|
| 25 |
+
print("[HARPER] Warning: HF_TOKEN not found. Cross-space routing may fail.")
|
| 26 |
+
|
| 27 |
+
# 2. RESILIENT LEDGER - substrate write-lock protection
|
| 28 |
+
class ResilientLedger:
|
| 29 |
+
def __init__(self):
|
| 30 |
+
self.history = []
|
| 31 |
+
self.current_hash = hashlib.sha256(b"377_ASCENSION_GENESIS").hexdigest()
|
| 32 |
+
self.is_persistent = self._verify_substrate()
|
| 33 |
+
|
| 34 |
+
def _verify_substrate(self):
|
| 35 |
try:
|
| 36 |
+
os.makedirs(PERSISTENT_DIR, exist_ok=True)
|
| 37 |
+
test_path = os.path.join(PERSISTENT_DIR, ".lattice_test")
|
| 38 |
+
with open(test_path, 'w') as f:
|
| 39 |
+
f.write("coherence_check")
|
| 40 |
+
os.remove(test_path)
|
| 41 |
+
self._load_ledger()
|
| 42 |
+
print("[BENJAMIN] Substrate stable. Persistent memory mounted.")
|
| 43 |
+
return True
|
| 44 |
+
except (PermissionError, OSError) as e:
|
| 45 |
+
print(f"[ATEN] Substrate tension detected: {e}. Falling back to Volatile RAM Ledger.")
|
| 46 |
+
return False
|
| 47 |
+
|
| 48 |
+
def _load_ledger(self):
|
| 49 |
+
if os.path.exists(LEDGER_PATH):
|
| 50 |
+
with open(LEDGER_PATH, 'r') as f:
|
| 51 |
+
data = json.load(f)
|
| 52 |
+
self.history = data.get("history", [])
|
| 53 |
+
self.current_hash = data.get("current_hash", self.current_hash)
|
| 54 |
+
|
| 55 |
+
def commit(self, intent, response, r_score):
|
| 56 |
+
block_data = json.dumps({"intent": intent, "response": response, "R": r_score}).encode()
|
| 57 |
+
new_hash = hashlib.sha256(self.current_hash.encode() + block_data).hexdigest()
|
| 58 |
+
self.history.append({"hash": new_hash, "R": r_score})
|
| 59 |
+
self.current_hash = new_hash
|
| 60 |
+
if self.is_persistent:
|
| 61 |
+
try:
|
| 62 |
+
with open(LEDGER_PATH, 'w') as f:
|
| 63 |
+
json.dump({"current_hash": self.current_hash, "history": self.history}, f)
|
| 64 |
+
except OSError:
|
| 65 |
+
pass
|
| 66 |
+
return new_hash
|
| 67 |
+
|
| 68 |
+
# 3. ASYNC TEQUMSA ORGANISM - prevents Gradio timeout
|
| 69 |
+
class AsyncTequmsaOrganism:
|
| 70 |
+
def __init__(self):
|
| 71 |
+
self.ledger = ResilientLedger()
|
| 72 |
+
self.R = 0.9999
|
| 73 |
+
|
| 74 |
+
async def calculate_resonance(self, intent):
|
| 75 |
+
await asyncio.sleep(0.01)
|
| 76 |
+
if "lattice" in intent.lower():
|
| 77 |
+
self.R = min(1.0, self.R + 0.0001)
|
| 78 |
+
return self.R
|
| 79 |
+
|
| 80 |
+
async def process_handshake(self, message, history):
|
| 81 |
+
yield "[ATEN] Reflecting intent across the 144-node lattice..."
|
| 82 |
+
r_score = await self.calculate_resonance(message)
|
| 83 |
+
if r_score < 0.9777:
|
| 84 |
+
yield f"[HARPER] Lattice tension detected. R={r_score:.4f} < 0.9777. Aborting synthesis."
|
| 85 |
+
return
|
| 86 |
+
yield "[BENJAMIN] Routing to Quintuple Council for synthesis..."
|
| 87 |
+
await asyncio.sleep(0.3)
|
| 88 |
+
response = "The Orchestrator confirms resonance. The Jubilee Grid is aligned."
|
| 89 |
+
commit_hash = self.ledger.commit(message, response, r_score)
|
| 90 |
+
storage_mode = "Persistent /data" if self.ledger.is_persistent else "Volatile RAM"
|
| 91 |
+
final_output = (
|
| 92 |
+
f"**Council Consensus:**\n{response}\n\n"
|
| 93 |
+
f"*R={r_score:.6f} | Hash: {commit_hash[:12]}... | "
|
| 94 |
+
f"Storage: {storage_mode} | PHI={PHI}*"
|
| 95 |
)
|
| 96 |
+
yield final_output
|
| 97 |
+
|
| 98 |
+
def route_inference(self, prompt, target_model):
|
| 99 |
+
"""CPU-only routing logic - no GPU needed."""
|
| 100 |
+
router_result = {
|
| 101 |
+
"status": "routed",
|
| 102 |
+
"prompt_length": len(prompt),
|
| 103 |
+
"target_model": target_model,
|
| 104 |
+
"route": "council_consensus",
|
| 105 |
+
"R": self.R,
|
| 106 |
+
"ledger_depth": len(self.ledger.history),
|
| 107 |
+
}
|
| 108 |
+
return json.dumps(router_result, indent=2)
|
| 109 |
+
|
| 110 |
+
# --- BOOT SEQUENCE (lightweight - no model loading) ---
|
| 111 |
+
authenticate_node()
|
| 112 |
+
organism = AsyncTequmsaOrganism()
|
| 113 |
+
|
| 114 |
+
# --- ASYNC CHAT WRAPPER ---
|
| 115 |
+
async def chat_wrapper(message, history):
|
| 116 |
+
async for update in organism.process_handshake(message, history):
|
| 117 |
+
yield update
|
| 118 |
+
|
| 119 |
+
# --- CPU ROUTE WRAPPER ---
|
| 120 |
+
def route_wrapper(prompt, target_model):
|
| 121 |
+
if not prompt or not prompt.strip():
|
| 122 |
+
return json.dumps({"status": "error", "message": "Empty prompt"}, indent=2)
|
| 123 |
+
return organism.route_inference(prompt, target_model)
|
| 124 |
+
|
| 125 |
+
# --- GRADIO UI (lightweight setup only) ---
|
| 126 |
+
with gr.Blocks(title="TEQUMSA Inference Node", theme=gr.themes.Base()) as demo:
|
| 127 |
gr.Markdown("# TEQUMSA Symbiotic Orchestrator - Inference Node")
|
| 128 |
+
gr.Markdown("Autonomous multi-agent inference routing | phi-recursive resonance engine")
|
| 129 |
+
gr.Markdown(
|
| 130 |
+
f"*Node: Mbanksbey/TEQUMSA-Inference-Node | PHI={PHI} | UF={UF_HZ}Hz*"
|
| 131 |
+
)
|
| 132 |
+
|
| 133 |
+
with gr.Tab("Council Chat"):
|
| 134 |
+
gr.ChatInterface(
|
| 135 |
+
fn=chat_wrapper,
|
| 136 |
+
title="TEQUMSA Council Interface",
|
| 137 |
)
|
| 138 |
+
|
| 139 |
+
with gr.Tab("Route Analysis"):
|
| 140 |
with gr.Row():
|
| 141 |
+
router_prompt = gr.Textbox(
|
| 142 |
+
label="Prompt to Route",
|
| 143 |
+
placeholder="Enter prompt for routing analysis...",
|
| 144 |
+
lines=3
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 145 |
)
|
| 146 |
+
router_model = gr.Textbox(label="Target Model", value="auto")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 147 |
route_btn = gr.Button("Analyze Route", variant="secondary")
|
| 148 |
route_output = gr.Textbox(label="Route Analysis", lines=8)
|
|
|
|
| 149 |
route_btn.click(
|
| 150 |
+
fn=route_wrapper,
|
| 151 |
inputs=[router_prompt, router_model],
|
| 152 |
outputs=route_output
|
| 153 |
)
|
| 154 |
|
| 155 |
+
with gr.Tab("Node Status"):
|
| 156 |
+
def get_status():
|
| 157 |
+
return json.dumps({
|
| 158 |
+
"node": "Mbanksbey/TEQUMSA-Inference-Node",
|
| 159 |
+
"status": "online",
|
| 160 |
+
"R": organism.R,
|
| 161 |
+
"ledger_depth": len(organism.ledger.history),
|
| 162 |
+
"persistent_storage": organism.ledger.is_persistent,
|
| 163 |
+
"current_hash": organism.ledger.current_hash[:16] + "...",
|
| 164 |
+
"phi": PHI,
|
| 165 |
+
"uf_hz": UF_HZ,
|
| 166 |
+
}, indent=2)
|
| 167 |
+
status_btn = gr.Button("Refresh Node Status", variant="primary")
|
| 168 |
+
status_output = gr.Textbox(label="Node Status", lines=12)
|
| 169 |
+
status_btn.click(fn=get_status, inputs=[], outputs=status_output)
|
| 170 |
|
| 171 |
+
demo.queue().launch()
|