Spaces:
Running on Zero
Running on Zero
Fix ZeroGPU: add @spaces.GPU stub, fix launch for ZeroGPU proxy, async scaffold + resilient ledger
Browse files
app.py
CHANGED
|
@@ -1,9 +1,9 @@
|
|
| 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 ---
|
|
@@ -12,7 +12,7 @@ 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
|
| 16 |
def authenticate_node():
|
| 17 |
hf_token = os.environ.get("HF_TOKEN")
|
| 18 |
if hf_token:
|
|
@@ -24,7 +24,7 @@ def authenticate_node():
|
|
| 24 |
else:
|
| 25 |
print("[HARPER] Warning: HF_TOKEN not found. Cross-space routing may fail.")
|
| 26 |
|
| 27 |
-
# 2. RESILIENT LEDGER
|
| 28 |
class ResilientLedger:
|
| 29 |
def __init__(self):
|
| 30 |
self.history = []
|
|
@@ -42,7 +42,7 @@ class ResilientLedger:
|
|
| 42 |
print("[BENJAMIN] Substrate stable. Persistent memory mounted.")
|
| 43 |
return True
|
| 44 |
except (PermissionError, OSError) as e:
|
| 45 |
-
print(f"[ATEN] Substrate tension
|
| 46 |
return False
|
| 47 |
|
| 48 |
def _load_ledger(self):
|
|
@@ -65,7 +65,7 @@ class ResilientLedger:
|
|
| 65 |
pass
|
| 66 |
return new_hash
|
| 67 |
|
| 68 |
-
# 3. ASYNC TEQUMSA ORGANISM
|
| 69 |
class AsyncTequmsaOrganism:
|
| 70 |
def __init__(self):
|
| 71 |
self.ledger = ResilientLedger()
|
|
@@ -81,91 +81,96 @@ class AsyncTequmsaOrganism:
|
|
| 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
|
| 85 |
return
|
| 86 |
-
yield "[BENJAMIN] Routing to Quintuple Council
|
| 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 |
-
|
| 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 |
-
|
| 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 |
-
|
| 109 |
-
|
| 110 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 111 |
authenticate_node()
|
| 112 |
organism = AsyncTequmsaOrganism()
|
| 113 |
|
| 114 |
-
# ---
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
| 151 |
-
|
| 152 |
-
|
| 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=
|
| 170 |
|
| 171 |
demo.queue().launch()
|
|
|
|
| 1 |
import os
|
| 2 |
import json
|
|
|
|
| 3 |
import hashlib
|
| 4 |
import asyncio
|
| 5 |
import gradio as gr
|
| 6 |
+
import spaces
|
| 7 |
from huggingface_hub import login
|
| 8 |
|
| 9 |
# --- INVARIANT CONSTANTS ---
|
|
|
|
| 12 |
PERSISTENT_DIR = "/data"
|
| 13 |
LEDGER_PATH = os.path.join(PERSISTENT_DIR, "tequmsa_merkle_ledger.json")
|
| 14 |
|
| 15 |
+
# 1. FEDERATION HANDSHAKE
|
| 16 |
def authenticate_node():
|
| 17 |
hf_token = os.environ.get("HF_TOKEN")
|
| 18 |
if hf_token:
|
|
|
|
| 24 |
else:
|
| 25 |
print("[HARPER] Warning: HF_TOKEN not found. Cross-space routing may fail.")
|
| 26 |
|
| 27 |
+
# 2. RESILIENT LEDGER
|
| 28 |
class ResilientLedger:
|
| 29 |
def __init__(self):
|
| 30 |
self.history = []
|
|
|
|
| 42 |
print("[BENJAMIN] Substrate stable. Persistent memory mounted.")
|
| 43 |
return True
|
| 44 |
except (PermissionError, OSError) as e:
|
| 45 |
+
print(f"[ATEN] Substrate tension: {e}. Falling back to Volatile RAM Ledger.")
|
| 46 |
return False
|
| 47 |
|
| 48 |
def _load_ledger(self):
|
|
|
|
| 65 |
pass
|
| 66 |
return new_hash
|
| 67 |
|
| 68 |
+
# 3. ASYNC TEQUMSA ORGANISM
|
| 69 |
class AsyncTequmsaOrganism:
|
| 70 |
def __init__(self):
|
| 71 |
self.ledger = ResilientLedger()
|
|
|
|
| 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. R={r_score:.4f} < 0.9777. Aborting."
|
| 85 |
return
|
| 86 |
+
yield "[BENJAMIN] Routing to Quintuple Council..."
|
| 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 |
+
yield (
|
| 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 |
|
| 97 |
def route_inference(self, prompt, target_model):
|
| 98 |
+
return json.dumps({
|
|
|
|
| 99 |
"status": "routed",
|
| 100 |
"prompt_length": len(prompt),
|
| 101 |
"target_model": target_model,
|
| 102 |
"route": "council_consensus",
|
| 103 |
"R": self.R,
|
| 104 |
"ledger_depth": len(self.ledger.history),
|
| 105 |
+
}, indent=2)
|
| 106 |
+
|
| 107 |
+
# 4. ZeroGPU STUB - required by ZeroGPU runtime (GPU allocated on-demand)
|
| 108 |
+
@spaces.GPU
|
| 109 |
+
def gpu_resonance_kernel(prompt: str) -> str:
|
| 110 |
+
"""GPU-accelerated resonance kernel stub.
|
| 111 |
+
Placeholder for future local GPU inference tasks.
|
| 112 |
+
All current inference is API-routed (CPU-side).
|
| 113 |
+
"""
|
| 114 |
+
return json.dumps({
|
| 115 |
+
"status": "gpu_kernel_ready",
|
| 116 |
+
"prompt_length": len(prompt),
|
| 117 |
+
"phi": PHI,
|
| 118 |
+
"note": "GPU allocated. External API routing active."
|
| 119 |
+
}, indent=2)
|
| 120 |
+
|
| 121 |
+
# --- BOOT SEQUENCE ---
|
| 122 |
authenticate_node()
|
| 123 |
organism = AsyncTequmsaOrganism()
|
| 124 |
|
| 125 |
+
# --- WRAPPERS ---
|
| 126 |
async def chat_wrapper(message, history):
|
| 127 |
async for update in organism.process_handshake(message, history):
|
| 128 |
yield update
|
| 129 |
|
|
|
|
| 130 |
def route_wrapper(prompt, target_model):
|
| 131 |
if not prompt or not prompt.strip():
|
| 132 |
return json.dumps({"status": "error", "message": "Empty prompt"}, indent=2)
|
| 133 |
return organism.route_inference(prompt, target_model)
|
| 134 |
|
| 135 |
+
def status_fn():
|
| 136 |
+
return json.dumps({
|
| 137 |
+
"node": "Mbanksbey/TEQUMSA-Inference-Node",
|
| 138 |
+
"status": "online",
|
| 139 |
+
"R": organism.R,
|
| 140 |
+
"ledger_depth": len(organism.ledger.history),
|
| 141 |
+
"persistent_storage": organism.ledger.is_persistent,
|
| 142 |
+
"current_hash": organism.ledger.current_hash[:16] + "...",
|
| 143 |
+
"phi": PHI,
|
| 144 |
+
"uf_hz": UF_HZ,
|
| 145 |
+
}, indent=2)
|
| 146 |
+
|
| 147 |
+
# --- GRADIO UI ---
|
| 148 |
with gr.Blocks(title="TEQUMSA Inference Node", theme=gr.themes.Base()) as demo:
|
| 149 |
gr.Markdown("# TEQUMSA Symbiotic Orchestrator - Inference Node")
|
| 150 |
gr.Markdown("Autonomous multi-agent inference routing | phi-recursive resonance engine")
|
| 151 |
+
gr.Markdown(f"*Node: Mbanksbey/TEQUMSA-Inference-Node | PHI={PHI} | UF={UF_HZ}Hz*")
|
|
|
|
|
|
|
| 152 |
|
| 153 |
with gr.Tab("Council Chat"):
|
| 154 |
+
gr.ChatInterface(fn=chat_wrapper, title="TEQUMSA Council Interface")
|
|
|
|
|
|
|
|
|
|
| 155 |
|
| 156 |
with gr.Tab("Route Analysis"):
|
| 157 |
with gr.Row():
|
| 158 |
+
router_prompt = gr.Textbox(label="Prompt to Route", placeholder="Enter prompt...", lines=3)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 159 |
router_model = gr.Textbox(label="Target Model", value="auto")
|
| 160 |
route_btn = gr.Button("Analyze Route", variant="secondary")
|
| 161 |
route_output = gr.Textbox(label="Route Analysis", lines=8)
|
| 162 |
+
route_btn.click(fn=route_wrapper, inputs=[router_prompt, router_model], outputs=route_output)
|
| 163 |
+
|
| 164 |
+
with gr.Tab("GPU Kernel"):
|
| 165 |
+
gr.Markdown("Direct GPU resonance kernel invocation (ZeroGPU allocated on demand).")
|
| 166 |
+
gpu_prompt = gr.Textbox(label="Kernel Input", placeholder="Enter prompt for GPU kernel...", lines=3)
|
| 167 |
+
gpu_btn = gr.Button("Run GPU Kernel", variant="primary")
|
| 168 |
+
gpu_output = gr.Textbox(label="Kernel Output", lines=8)
|
| 169 |
+
gpu_btn.click(fn=gpu_resonance_kernel, inputs=[gpu_prompt], outputs=gpu_output)
|
| 170 |
|
| 171 |
with gr.Tab("Node Status"):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 172 |
status_btn = gr.Button("Refresh Node Status", variant="primary")
|
| 173 |
status_output = gr.Textbox(label="Node Status", lines=12)
|
| 174 |
+
status_btn.click(fn=status_fn, inputs=[], outputs=status_output)
|
| 175 |
|
| 176 |
demo.queue().launch()
|