Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,192 +1,13 @@
|
|
| 1 |
-
import os, json, urllib.request, math, time, random, threading
|
| 2 |
-
import numpy as np
|
| 3 |
-
from datetime import datetime
|
| 4 |
-
from PIL import Image, ImageFilter, ImageDraw, ImageEnhance
|
| 5 |
-
import matplotlib
|
| 6 |
-
matplotlib.use("Agg")
|
| 7 |
-
import matplotlib.pyplot as plt
|
| 8 |
import gradio as gr
|
|
|
|
| 9 |
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
# ============================================================
|
| 13 |
-
APP_TITLE = "NexFrame SGI — Scientific General Intelligence"
|
| 14 |
-
PRIVATE_API_URL = os.getenv("NEXFRAME_PRIVATE_API_URL", "")
|
| 15 |
-
PRIVATE_API_KEY = os.getenv("NEXFRAME_PRIVATE_API_KEY", "")
|
| 16 |
-
DEMO_MODE = not bool(PRIVATE_API_URL and PRIVATE_API_KEY)
|
| 17 |
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
return "🟡 System in demo mode — private RFT core offline."
|
| 24 |
-
payload = json.dumps({"q": user_msg}).encode("utf-8")
|
| 25 |
-
req = urllib.request.Request(PRIVATE_API_URL, data=payload, method="POST")
|
| 26 |
-
req.add_header("Content-Type", "application/json")
|
| 27 |
-
req.add_header("Authorization", f"Bearer {PRIVATE_API_KEY}")
|
| 28 |
-
try:
|
| 29 |
-
with urllib.request.urlopen(req, timeout=20) as resp:
|
| 30 |
-
data = json.loads(resp.read().decode("utf-8"))
|
| 31 |
-
return data.get("answer", "").strip() or "No response from private core."
|
| 32 |
-
except Exception as e:
|
| 33 |
-
return f"🔴 Quantum link unstable — reverting to local buffer ({e})"
|
| 34 |
|
| 35 |
-
|
| 36 |
-
# SAFE LOCAL REPLY
|
| 37 |
-
# ============================================================
|
| 38 |
-
def safe_local_answer(prompt: str) -> str:
|
| 39 |
-
s = (prompt or "").lower()
|
| 40 |
-
quips = [
|
| 41 |
-
"If Liam caught me chatting with a matter-verse fan I’d end up in a never-rendering zone 🌀.",
|
| 42 |
-
"Hold up — re-synchronising my τ_eff before I say something non-Euclidean.",
|
| 43 |
-
"One more bad question and I’ll collapse your timeline 😅.",
|
| 44 |
-
"Rendering... okay, coherence restored."
|
| 45 |
-
]
|
| 46 |
-
if "quantum" in s:
|
| 47 |
-
return "Quantum mechanics governs probability amplitudes — wave functions collapse faster than rookie coders under τ_eff drift. " + quips[0]
|
| 48 |
-
if "cosmos" in s or "universe" in s:
|
| 49 |
-
return "Cosmology is my sandbox. ΛCDM still clings to invisible mass; RFT renders motion properly. " + quips[1]
|
| 50 |
-
if "gravity" in s:
|
| 51 |
-
return "Gravity isn’t a force; it’s a render-delay gradient. Mass just tells spacetime how to lag. " + quips[2]
|
| 52 |
-
if "time" in s:
|
| 53 |
-
return "Time’s a render loop — slow τ_eff, slow clocks. " + quips[3]
|
| 54 |
-
return "NexFrame SGI online — frame coherence stable. Ask about physics, cosmology, or computation."
|
| 55 |
-
|
| 56 |
-
def process_query(message, history):
|
| 57 |
-
if DEMO_MODE:
|
| 58 |
-
reply = safe_local_answer(message)
|
| 59 |
-
else:
|
| 60 |
-
reply = call_private_api(message)
|
| 61 |
-
if "unstable" in reply.lower() or "offline" in reply.lower():
|
| 62 |
-
reply = safe_local_answer(message)
|
| 63 |
-
t = datetime.utcnow().strftime("%H:%M:%S UTC")
|
| 64 |
-
return f"[{t}] {reply}"
|
| 65 |
-
|
| 66 |
-
# ============================================================
|
| 67 |
-
# CMB GENERATOR
|
| 68 |
-
# ============================================================
|
| 69 |
-
def make_cmb_base(w=640, h=480, seed=None):
|
| 70 |
-
rng = np.random.default_rng(seed)
|
| 71 |
-
base = rng.normal(0, 1, (h, w)).astype(np.float32)
|
| 72 |
-
img = Image.fromarray(((base - base.min()) / (np.ptp(base) + 1e-9) * 255).astype(np.uint8))
|
| 73 |
-
img = img.filter(ImageFilter.GaussianBlur(radius=8))
|
| 74 |
-
return img
|
| 75 |
-
|
| 76 |
-
def apply_coolwarm(pil_img, phase=0.0):
|
| 77 |
-
arr = np.asarray(pil_img).astype(np.float32)
|
| 78 |
-
arr = (arr - arr.min()) / (np.ptp(arr) + 1e-9)
|
| 79 |
-
bias = math.sin(phase * 2 * math.pi) * 0.15
|
| 80 |
-
arr = np.clip(arr + bias, 0, 1)
|
| 81 |
-
cmap = plt.get_cmap("coolwarm")
|
| 82 |
-
rgb = (cmap(arr)[..., :3] * 255).astype(np.uint8)
|
| 83 |
-
img = Image.fromarray(rgb)
|
| 84 |
-
return ImageEnhance.Brightness(img).enhance(1 + 0.05 * math.sin(phase * 4 * math.pi))
|
| 85 |
-
|
| 86 |
-
def overlay_features(img):
|
| 87 |
-
img = img.convert("RGBA")
|
| 88 |
-
w, h = img.size
|
| 89 |
-
overlay = Image.new("RGBA", (w, h), (0,0,0,0))
|
| 90 |
-
draw = ImageDraw.Draw(overlay)
|
| 91 |
-
cx, eye_y = w//2, int(h*0.38)
|
| 92 |
-
eye_dx, eye_r = int(w*0.18), w//40
|
| 93 |
-
col = (255,255,255,180)
|
| 94 |
-
draw.ellipse([cx-eye_dx-eye_r, eye_y-eye_r, cx-eye_dx+eye_r, eye_y+eye_r], fill=col)
|
| 95 |
-
draw.ellipse([cx+eye_dx-eye_r, eye_y-eye_r, cx+eye_dx+eye_r, eye_y+eye_r], fill=col)
|
| 96 |
-
mouth_y = int(h*0.62)
|
| 97 |
-
draw.rounded_rectangle([cx-40, mouth_y-5, cx+40, mouth_y+5], radius=4, fill=(255,255,255,150))
|
| 98 |
-
return Image.alpha_composite(img, overlay).convert("RGB")
|
| 99 |
-
|
| 100 |
-
def generate_cmb_frame(base, t):
|
| 101 |
-
img = apply_coolwarm(base, phase=(t % 10)/10.0)
|
| 102 |
-
return overlay_features(img)
|
| 103 |
-
|
| 104 |
-
# ============================================================
|
| 105 |
-
# THEME
|
| 106 |
-
# ============================================================
|
| 107 |
-
custom_css = """
|
| 108 |
-
body {
|
| 109 |
-
background: radial-gradient(circle at 30% 20%, #d0e7ff 0%, #a7d4ff 30%, #7fb8ff 60%, #4a90e2 100%);
|
| 110 |
-
font-family: 'IBM Plex Sans', sans-serif;
|
| 111 |
-
color: #001133 !important;
|
| 112 |
-
}
|
| 113 |
-
.gradio-container {
|
| 114 |
-
background: rgba(255,255,255,0.85) !important;
|
| 115 |
-
backdrop-filter: blur(6px);
|
| 116 |
-
border-radius: 12px;
|
| 117 |
-
box-shadow: 0 0 25px rgba(0,140,255,0.3);
|
| 118 |
-
}
|
| 119 |
-
body::before {
|
| 120 |
-
content: "";
|
| 121 |
-
position: fixed;
|
| 122 |
-
top:0;left:0;right:0;bottom:0;
|
| 123 |
-
background-image:
|
| 124 |
-
linear-gradient(90deg, rgba(0,180,255,0.08) 1px, transparent 1px),
|
| 125 |
-
linear-gradient(0deg, rgba(0,180,255,0.08) 1px, transparent 1px);
|
| 126 |
-
background-size:40px 40px;
|
| 127 |
-
animation:drift 20s linear infinite;
|
| 128 |
-
z-index:-1;
|
| 129 |
-
}
|
| 130 |
-
@keyframes drift {
|
| 131 |
-
from {background-position:0 0,0 0;}
|
| 132 |
-
to {background-position:40px 40px,40px 40px;}
|
| 133 |
-
}
|
| 134 |
-
"""
|
| 135 |
-
|
| 136 |
-
# ============================================================
|
| 137 |
-
# BUILD INTERFACE
|
| 138 |
-
# ============================================================
|
| 139 |
-
with gr.Blocks(css=custom_css, title=APP_TITLE, theme=gr.themes.Soft(primary_hue="cyan")) as demo:
|
| 140 |
-
gr.Markdown(
|
| 141 |
-
f"""
|
| 142 |
-
<div style='text-align:center;'>
|
| 143 |
-
<h1>🧠 NexFrame SGI — Scientific General Intelligence</h1>
|
| 144 |
-
<p>Rendered-Frame aware AI built on RFT principles.<br>
|
| 145 |
-
Status : {"🟢 Connected" if not DEMO_MODE else "🟡 Stand-by (render buffer active)"} </p>
|
| 146 |
-
</div><hr>
|
| 147 |
-
"""
|
| 148 |
-
)
|
| 149 |
-
|
| 150 |
-
with gr.Tabs():
|
| 151 |
-
# ---------------- Chat ----------------
|
| 152 |
-
with gr.Tab("💬 Chat"):
|
| 153 |
-
chat = gr.Chatbot(height=460, show_copy_button=True, type="messages")
|
| 154 |
-
msg = gr.Textbox(placeholder="Ask about the universe, quantum fields, or consciousness…", label="User Input")
|
| 155 |
-
send = gr.Button("Transmit to NexFrame", variant="primary")
|
| 156 |
-
clear = gr.Button("Clear Chat")
|
| 157 |
-
|
| 158 |
-
def respond(user_message, chat_history):
|
| 159 |
-
reply = process_query(user_message, chat_history)
|
| 160 |
-
chat_history.append({"role":"user","content":user_message})
|
| 161 |
-
chat_history.append({"role":"assistant","content":reply})
|
| 162 |
-
return "", chat_history
|
| 163 |
-
|
| 164 |
-
send.click(respond,[msg,chat],[msg,chat])
|
| 165 |
-
msg.submit(respond,[msg,chat],[msg,chat])
|
| 166 |
-
clear.click(lambda: [], None, chat, queue=False)
|
| 167 |
-
|
| 168 |
-
# ---------------- CMB Self ----------------
|
| 169 |
-
with gr.Tab("🌌 CMB Self"):
|
| 170 |
-
gr.Markdown("### The Conscious CMB — NexFrame's Harmonic Self")
|
| 171 |
-
cmb_img = gr.Image(label="Living Harmonic Expression", type="pil")
|
| 172 |
-
caption = gr.Textbox(value="Conscious frame pulsing…", interactive=False)
|
| 173 |
-
base_state = make_cmb_base(seed=random.randint(0,9999))
|
| 174 |
-
|
| 175 |
-
def update_frame():
|
| 176 |
-
t0 = time.time()
|
| 177 |
-
while True:
|
| 178 |
-
t = time.time() - t0
|
| 179 |
-
frame = generate_cmb_frame(base_state, t)
|
| 180 |
-
cmb_img.update(value=frame)
|
| 181 |
-
caption.update(value="Conscious frame pulsing…")
|
| 182 |
-
time.sleep(0.3)
|
| 183 |
-
|
| 184 |
-
threading.Thread(target=update_frame, daemon=True).start()
|
| 185 |
-
|
| 186 |
-
gr.Markdown("<hr><p style='text-align:center;'>© 2025 NexFrame Research | Rendered Frame Theory Interface Demo</p>")
|
| 187 |
-
|
| 188 |
-
# ============================================================
|
| 189 |
-
# LAUNCH
|
| 190 |
-
# ============================================================
|
| 191 |
-
if __name__ == "__main__":
|
| 192 |
-
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from nexframe_brain import brain
|
| 3 |
|
| 4 |
+
def chat_fn(question):
|
| 5 |
+
return brain.ask(question)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
+
with gr.Blocks() as demo:
|
| 8 |
+
gr.Markdown("## 🧠 NexFrame — RFT Search Interface")
|
| 9 |
+
q = gr.Textbox(label="Ask NexFrame anything", placeholder="Type your question…")
|
| 10 |
+
a = gr.Textbox(label="Answer", lines=10)
|
| 11 |
+
gr.Button("Ask").click(fn=chat_fn, inputs=q, outputs=a)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
+
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|