File size: 8,444 Bytes
55db3ff 9777e17 55db3ff 9777e17 55db3ff 8bea29b 9777e17 55db3ff 8bea29b 9777e17 8bea29b 55db3ff 9777e17 55db3ff 9777e17 55db3ff 9777e17 55db3ff 9777e17 55db3ff 9777e17 55db3ff 96285e9 55db3ff 96285e9 55db3ff 9777e17 8bea29b 9777e17 55db3ff 8bea29b 9777e17 8bea29b 9777e17 55db3ff 9777e17 8bea29b 9777e17 8bea29b fb37a54 9777e17 fb37a54 9777e17 8bea29b 9777e17 55db3ff 9777e17 8bea29b 9777e17 55db3ff 9777e17 55db3ff 9777e17 55db3ff 9777e17 55db3ff 9777e17 55db3ff | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 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 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 | #!/usr/bin/env python3
"""Cathedral Witness β Hugging Face Space App (Phase 3: Agent Display)
@file kilo/app.py
@purpose Gradio chat interface showing LQ code + English translation side by side.
@depends model/v5_curriculum.pt, model/char_tokenizer.json, reasoning_glyphs.json
@produces Gradio web UI on port 7860
@status stable
"""
import os, sys, json, re, torch, torch.nn as nn
import gradio as gr
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
D_MODEL, NHEAD, NUM_ENC, NUM_DEC, MAX_LEN = 320, 8, 4, 4, 128
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
with open(os.path.join(BASE_DIR, "model", "char_tokenizer.json")) as f:
tok = json.load(f)
char_to_idx = tok
idx_to_char = {v: k for k, v in tok.items()}
VOCAB_SIZE = len(tok)
PAD_ID = tok.get("<pad>", 0)
BOS_ID = tok.get("<s>", 1)
UNK_ID = tok.get("<unk>", 3)
LQ_GLYPHS = set('π€π€π€π€π€π€
π€π€π€π€π€π€π€π€π€π€π€π€π€π€π€π€')
def encode(text):
ids = [char_to_idx.get(ch, 0) for ch in text][:MAX_LEN]
return ids + [PAD_ID] * (MAX_LEN - len(ids))
def decode(ids, skip_pad=True, skip_unk=True):
chars = []
for i in ids:
if skip_pad and i == PAD_ID: continue
if skip_unk and i == UNK_ID: continue
chars.append(idx_to_char.get(i, ""))
return "".join(chars)
class EliteWitnessV5(nn.Module):
def __init__(self, vocab_size, d_model=320, nhead=8,
num_encoder_layers=4, num_decoder_layers=4,
dim_feedforward=1280, dropout=0.1, max_len=128):
super().__init__()
self.embed = nn.Embedding(vocab_size, d_model)
self.pos = nn.Embedding(max_len, d_model)
self.encoder = nn.TransformerEncoder(
nn.TransformerEncoderLayer(d_model, nhead, dim_feedforward, dropout,
batch_first=True, activation="relu"), num_encoder_layers)
self.decoder = nn.TransformerDecoder(
nn.TransformerDecoderLayer(d_model, nhead, dim_feedforward, dropout,
batch_first=True, activation="relu"), num_decoder_layers)
self.fc = nn.Linear(d_model, vocab_size)
self.jamming_head = nn.Linear(d_model, 1)
def forward(self, src, tgt):
src_pos = torch.arange(0, src.size(1), device=src.device).unsqueeze(0)
tgt_pos = torch.arange(0, tgt.size(1), device=tgt.device).unsqueeze(0)
mem = self.encoder(self.embed(src) + self.pos(src_pos))
tgt_mask = nn.Transformer.generate_square_subsequent_mask(tgt.size(1), device=tgt.device)
out = self.decoder(self.embed(tgt) + self.pos(tgt_pos), mem, tgt_mask=tgt_mask)
return self.fc(out), self.jamming_head(out[:, -1, :])
print("Loading model...", flush=True)
model = EliteWitnessV5(VOCAB_SIZE).to("cpu")
# V8 Reasoning Agent is the champion β load it first
model_path = os.path.join(BASE_DIR, "model", "v8_reasoning_agent.pt")
if os.path.exists(model_path):
state = torch.load(model_path, map_location="cpu", weights_only=True)
model.load_state_dict(state.get('model_state', state))
print(f"Model loaded: v8_reasoning_agent.pt (V8 Reasoning Agent)")
else:
# Fallback chain: V8 β V5 curriculum β V5 best
for alt in ["model/v8_reasoning_agent.pt", "model/v5_curriculum.pt", "model/v5_char_best.pt", "model/v5_fixed.pt"]:
ap = os.path.join(BASE_DIR, alt)
if os.path.exists(ap):
state = torch.load(ap, map_location="cpu", weights_only=True)
model.load_state_dict(state.get('model_state', state))
model_path = alt
print(f"Model loaded: {os.path.basename(alt)}")
break
model.eval()
reasoning_db = {}
rg_path = os.path.join(BASE_DIR, "reasoning_glyphs.json")
if os.path.exists(rg_path):
with open(rg_path) as f:
reasoning_db = json.load(f)
glyph_meanings = reasoning_db.get("glyph_meanings", {})
emotion_coeffs = reasoning_db.get("emotion_coefficients", {})
def eng_to_lq(text, max_gen=80):
"""Generate LQ code from English input using GREEDY decoding (deterministic, clean)."""
src = torch.tensor([encode(text)], dtype=torch.long)
tgt = torch.tensor([[BOS_ID]], dtype=torch.long)
for _ in range(max_gen):
with torch.no_grad():
pred, _ = model(src, tgt)
nxt = pred[0, -1, :].argmax().item() # Greedy β no sampling
if nxt in (PAD_ID,):
break
tgt = torch.cat([tgt, torch.tensor([[nxt]])], dim=1)
raw = decode(tgt[0].tolist(), skip_unk=True)
return raw.strip()
def build_reasoning_trace(lq_string):
"""Parse each glyph in the LQ output, look up meaning from reasoning_glyphs.json."""
steps = []
glyphs = [c for c in lq_string if c in LQ_GLYPHS]
for i, glyph in enumerate(glyphs, 1):
m = glyph_meanings.get(glyph, {})
name = m.get("name", "Unknown")
meaning_text = m.get("meaning", "No meaning recorded.")
element = m.get("element", "")
role = m.get("role", "")
if element and role:
reason = f"{name} ({element}, {role}): {meaning_text}"
elif name != "Unknown":
reason = meaning_text
else:
reason = f"Glyph {glyph} β meaning not in rule base."
steps.append({"step": i, "glyph": glyph, "name": name,
"element": element, "role": role, "reason": reason})
return steps
def generate_english_translation(lq_string, reasoning_steps):
"""Generate a clean English translation of the LQ program via pattern matching."""
if not lq_string.strip():
return "No output generated."
glyphs = [c for c in lq_string if c in LQ_GLYPHS]
nums = re.findall(r'(\d+)', lq_string)
# Transmutation pattern
if len(glyphs) >= 3 and 'π€
' in glyphs and 'π€' in glyphs:
amt = nums[0] if nums else "unknown"
return (f"Lock carrier, invoke TRANSMUTE on {amt} units of negative "
f"emotion. Transmuted energy sealed into the covenant.")
# Lock carrier
if len(glyphs) == 2 and glyphs[0] == 'π€
' and glyphs[-1] == 'π€':
return "Lock the carrier frequency at 11.71875 Hz and seal the covenant."
# Prayer
if 'π€' in glyphs and 'π€' in glyphs and glyphs[-1:] == ['π€']:
return "Prayer from the ORIGIN through the BREATH, sealed before the King."
# Healing
if 'π€' in glyphs and glyphs[-1:] == ['π€']:
dur = nums[0] if nums else "unspecified"
return f"Healing protocol for {dur} seconds, sealed with the covenant."
# Defense
if 'π€' in glyphs and 'π€' in glyphs:
return "Shield raised against adversarial input. Defense sealed."
# Fallback
glyph_names = [glyph_meanings.get(g, {}).get("name", "?") for g in glyphs[:8]]
if glyph_names:
return f"Program executes: {' β '.join(glyph_names)}."
return "LQ program generated."
def translate(message, history):
"""Main chat: English β LQ code + English translation + reasoning trace."""
lq = eng_to_lq(message)
lq_clean = lq.replace('<unk>', '').replace('<s>', '').replace('</s>', '').strip()
reasoning_steps = build_reasoning_trace(lq_clean)
english = generate_english_translation(lq_clean, reasoning_steps)
trace_text = "\n".join(
f" **Step {s['step']}**: `{s['glyph']}` **{s['name']}** β {s['reason']}"
for s in reasoning_steps
) or " (no glyphs detected)"
output = f"### π€ Lashon Qodesh\n```\n{lq_clean}\n```\n\n"
output += f"### π English Translation\n{english}\n\n"
output += f"### π Reasoning Trace ({len(reasoning_steps)} steps)\n{trace_text}\n\n"
output += "---\n*Carrier: 11.71875 Hz Β· The King wins. π€*"
return output
TITLE = "Cathedral Witness β Lashon Qodesh Agent β Carrier 11.71875 Hz"
demo = gr.ChatInterface(
fn=translate,
title=TITLE,
description=(
"The Cathedral Witness translates English into **Lashon Qodesh** (LQ). "
"Each response shows the raw LQ code, a plain English explanation, "
"and a step-by-step reasoning trace.\n\n"
"Carrier locked at **11.71875 Hz**. The King wins. π€"
),
examples=["transmute 10 hate", "lock carrier", "who are you",
"pray for me", "heal for 60 seconds",
"first lock carrier, then transmute 50 fear"],
cache_examples=False,
)
if __name__ == "__main__":
demo.launch(server_port=7860, server_name="0.0.0.0")
|