Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,17 +4,16 @@ import time
|
|
| 4 |
import hashlib
|
| 5 |
import json
|
| 6 |
import random
|
| 7 |
-
from collections import defaultdict
|
| 8 |
|
| 9 |
# ======================================================
|
| 10 |
-
# GLOBAL MEMORY
|
| 11 |
# ======================================================
|
| 12 |
|
| 13 |
CHAT_MEMORY = []
|
| 14 |
LAST_STATE = {}
|
| 15 |
|
| 16 |
# ======================================================
|
| 17 |
-
# PUBLIC MACRO DATA (WORLD BANK
|
| 18 |
# ======================================================
|
| 19 |
|
| 20 |
WORLD_BANK_BASE = "https://api.worldbank.org/v2"
|
|
@@ -46,15 +45,12 @@ def fetch_macro_anchor(country="WLD"):
|
|
| 46 |
}
|
| 47 |
|
| 48 |
# ======================================================
|
| 49 |
-
# ECONOMIC
|
| 50 |
# ======================================================
|
| 51 |
|
| 52 |
-
def economic_kernel(commodity,
|
| 53 |
-
gdp_scale = 1.0
|
| 54 |
-
|
| 55 |
-
gdp_scale = macro["global_gdp"] / 1e14
|
| 56 |
-
|
| 57 |
-
supply = physical_anchor * gdp_scale
|
| 58 |
demand = supply * 0.95
|
| 59 |
price_index = round((supply / 10) * gdp_scale, 2)
|
| 60 |
|
|
@@ -66,10 +62,6 @@ def economic_kernel(commodity, physical_anchor, macro):
|
|
| 66 |
"currency_flow": round(demand * price_index, 2),
|
| 67 |
}
|
| 68 |
|
| 69 |
-
# ======================================================
|
| 70 |
-
# AUXILIARY ENGINES
|
| 71 |
-
# ======================================================
|
| 72 |
-
|
| 73 |
def logistics_engine(econ):
|
| 74 |
friction = abs(econ["supply"] - econ["demand"]) / max(econ["supply"], 1)
|
| 75 |
return {
|
|
@@ -86,75 +78,35 @@ def energy_engine(econ):
|
|
| 86 |
}
|
| 87 |
|
| 88 |
def sentiment_engine(commodity):
|
| 89 |
-
seed
|
| 90 |
-
random.seed(seed)
|
| 91 |
confidence = random.uniform(0.6, 0.9)
|
| 92 |
return {
|
| 93 |
"market_confidence": round(confidence, 3),
|
| 94 |
"risk_bias": "neutral" if confidence > 0.7 else "risk_off",
|
| 95 |
}
|
| 96 |
|
| 97 |
-
def analytics_engine(econ, logistics,
|
| 98 |
projected_price = econ["price_index"] * (
|
| 99 |
-
1
|
| 100 |
-
+ (1 - sentiment["market_confidence"]) * 0.08
|
| 101 |
-
- logistics["friction"] * 0.1
|
| 102 |
)
|
| 103 |
return {
|
| 104 |
"projected_price": round(projected_price, 2),
|
| 105 |
-
"lag_days":
|
| 106 |
-
"volatility_index": round(0.015 * lag_days, 4),
|
| 107 |
-
}
|
| 108 |
-
|
| 109 |
-
# ======================================================
|
| 110 |
-
# COMMODITY NETWORK GRAPH (LIGHTWEIGHT)
|
| 111 |
-
# ======================================================
|
| 112 |
-
|
| 113 |
-
def commodity_graph(commodity):
|
| 114 |
-
graph = {
|
| 115 |
-
"Gold": ["USD", "Inflation", "Energy"],
|
| 116 |
-
"Oil": ["Transport", "USD", "Energy"],
|
| 117 |
-
"Gas": ["Electricity", "Industry"],
|
| 118 |
-
"Wheat": ["Food", "Transport"],
|
| 119 |
-
"Copper": ["Construction", "Energy"],
|
| 120 |
-
}
|
| 121 |
-
return {
|
| 122 |
-
"node": commodity,
|
| 123 |
-
"connections": graph.get(commodity, [])
|
| 124 |
}
|
| 125 |
|
| 126 |
# ======================================================
|
| 127 |
-
# Ω-PROOF
|
| 128 |
# ======================================================
|
| 129 |
|
| 130 |
def omega_proof(state):
|
| 131 |
-
|
| 132 |
-
return hashlib.sha256(canonical).hexdigest()
|
| 133 |
|
| 134 |
# ======================================================
|
| 135 |
-
#
|
| 136 |
# ======================================================
|
| 137 |
|
| 138 |
-
def
|
| 139 |
-
|
| 140 |
-
"node": "CodexFlow_ΩΞ_Node",
|
| 141 |
-
"version": "ΩΞ-1.0",
|
| 142 |
-
"state": state,
|
| 143 |
-
"proof": proof,
|
| 144 |
-
}
|
| 145 |
-
|
| 146 |
-
# ======================================================
|
| 147 |
-
# MASTER ORCHESTRATION
|
| 148 |
-
# ======================================================
|
| 149 |
-
|
| 150 |
-
def run_codexflow(
|
| 151 |
-
commodity,
|
| 152 |
-
physical_anchor,
|
| 153 |
-
lag_days,
|
| 154 |
-
country,
|
| 155 |
-
use_live_data
|
| 156 |
-
):
|
| 157 |
-
macro = fetch_macro_anchor(country) if use_live_data else {
|
| 158 |
"country": country,
|
| 159 |
"global_gdp": None,
|
| 160 |
"inflation": None,
|
|
@@ -162,74 +114,54 @@ def run_codexflow(
|
|
| 162 |
"timestamp": time.time(),
|
| 163 |
}
|
| 164 |
|
| 165 |
-
econ = economic_kernel(commodity,
|
| 166 |
logistics = logistics_engine(econ)
|
| 167 |
energy = energy_engine(econ)
|
| 168 |
sentiment = sentiment_engine(commodity)
|
| 169 |
-
projection = analytics_engine(
|
| 170 |
-
econ, logistics, energy, sentiment, lag_days
|
| 171 |
-
)
|
| 172 |
-
network = commodity_graph(commodity)
|
| 173 |
|
| 174 |
state = {
|
| 175 |
-
"
|
| 176 |
-
"
|
| 177 |
"logistics": logistics,
|
| 178 |
"energy": energy,
|
| 179 |
"sentiment": sentiment,
|
| 180 |
"projection": projection,
|
| 181 |
-
"commodity_network": network,
|
| 182 |
}
|
| 183 |
|
| 184 |
proof = omega_proof(state)
|
| 185 |
-
federated = federate_node(state, proof)
|
| 186 |
|
| 187 |
global LAST_STATE
|
| 188 |
-
LAST_STATE =
|
| 189 |
|
| 190 |
-
return
|
| 191 |
|
| 192 |
# ======================================================
|
| 193 |
-
# JARVIS X
|
| 194 |
# ======================================================
|
| 195 |
|
| 196 |
-
def jarvis_x_chat(
|
| 197 |
-
CHAT_MEMORY.append(
|
| 198 |
|
| 199 |
-
|
|
|
|
| 200 |
return json.dumps(LAST_STATE, indent=2) if LAST_STATE else "No state yet."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 201 |
|
| 202 |
-
|
| 203 |
-
return (
|
| 204 |
-
"CodexFlow ΩΞ is a world-economic execution engine. "
|
| 205 |
-
"It simulates supply, demand, cashflow, logistics, energy, "
|
| 206 |
-
"and macro anchors as one auditable system."
|
| 207 |
-
)
|
| 208 |
-
|
| 209 |
-
if "proof" in user_message.lower():
|
| 210 |
-
return "Each run emits an Ω-proof: a deterministic hash of the full world state."
|
| 211 |
-
|
| 212 |
-
if "beyond sota" in user_message.lower():
|
| 213 |
-
return (
|
| 214 |
-
"Yes. This system executes economic state transitions directly, "
|
| 215 |
-
"rather than only visualizing data."
|
| 216 |
-
)
|
| 217 |
-
|
| 218 |
-
if "memory" in user_message.lower():
|
| 219 |
-
return f"I currently retain {len(CHAT_MEMORY)} interaction steps."
|
| 220 |
-
|
| 221 |
-
return (
|
| 222 |
-
"Jarvis X acknowledged. "
|
| 223 |
-
"You may query system state, projections, architecture, or proofs."
|
| 224 |
-
)
|
| 225 |
|
| 226 |
# ======================================================
|
| 227 |
-
# GRADIO
|
| 228 |
# ======================================================
|
| 229 |
|
| 230 |
-
with gr.Blocks(
|
| 231 |
gr.Markdown("# 🌍 CodexFlow ΩΞ")
|
| 232 |
-
gr.Markdown("**
|
| 233 |
|
| 234 |
with gr.Row():
|
| 235 |
with gr.Column(scale=2):
|
|
@@ -238,42 +170,28 @@ with gr.Blocks(title="CodexFlow ΩΞ") as app:
|
|
| 238 |
value="Gold",
|
| 239 |
label="Commodity"
|
| 240 |
)
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
)
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
)
|
| 248 |
-
country = gr.Textbox(
|
| 249 |
-
value="WLD",
|
| 250 |
-
label="Country Code (WLD, USA, CHN, ZAF, etc.)"
|
| 251 |
-
)
|
| 252 |
-
use_live = gr.Checkbox(
|
| 253 |
-
value=True, label="Use Live Public Macro Data"
|
| 254 |
-
)
|
| 255 |
-
run_btn = gr.Button("Execute CodexFlow")
|
| 256 |
|
| 257 |
-
|
| 258 |
-
|
| 259 |
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
inputs=[commodity,
|
| 263 |
-
outputs=[
|
| 264 |
)
|
| 265 |
|
| 266 |
with gr.Column(scale=1):
|
| 267 |
gr.Markdown("## 🧠 Jarvis X")
|
| 268 |
-
gr.ChatInterface(
|
| 269 |
-
fn=jarvis_x_chat,
|
| 270 |
-
chatbot_name="Jarvis X",
|
| 271 |
-
height=420
|
| 272 |
-
)
|
| 273 |
|
| 274 |
gr.Markdown(
|
| 275 |
-
"
|
| 276 |
-
"It does not execute trades, enforce contracts, or access private systems._"
|
| 277 |
)
|
| 278 |
|
| 279 |
app.launch()
|
|
|
|
| 4 |
import hashlib
|
| 5 |
import json
|
| 6 |
import random
|
|
|
|
| 7 |
|
| 8 |
# ======================================================
|
| 9 |
+
# GLOBAL MEMORY
|
| 10 |
# ======================================================
|
| 11 |
|
| 12 |
CHAT_MEMORY = []
|
| 13 |
LAST_STATE = {}
|
| 14 |
|
| 15 |
# ======================================================
|
| 16 |
+
# PUBLIC MACRO DATA (WORLD BANK)
|
| 17 |
# ======================================================
|
| 18 |
|
| 19 |
WORLD_BANK_BASE = "https://api.worldbank.org/v2"
|
|
|
|
| 45 |
}
|
| 46 |
|
| 47 |
# ======================================================
|
| 48 |
+
# ECONOMIC ENGINE
|
| 49 |
# ======================================================
|
| 50 |
|
| 51 |
+
def economic_kernel(commodity, anchor, macro):
|
| 52 |
+
gdp_scale = macro["global_gdp"] / 1e14 if macro["global_gdp"] else 1.0
|
| 53 |
+
supply = anchor * gdp_scale
|
|
|
|
|
|
|
|
|
|
| 54 |
demand = supply * 0.95
|
| 55 |
price_index = round((supply / 10) * gdp_scale, 2)
|
| 56 |
|
|
|
|
| 62 |
"currency_flow": round(demand * price_index, 2),
|
| 63 |
}
|
| 64 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
def logistics_engine(econ):
|
| 66 |
friction = abs(econ["supply"] - econ["demand"]) / max(econ["supply"], 1)
|
| 67 |
return {
|
|
|
|
| 78 |
}
|
| 79 |
|
| 80 |
def sentiment_engine(commodity):
|
| 81 |
+
random.seed(sum(ord(c) for c in commodity))
|
|
|
|
| 82 |
confidence = random.uniform(0.6, 0.9)
|
| 83 |
return {
|
| 84 |
"market_confidence": round(confidence, 3),
|
| 85 |
"risk_bias": "neutral" if confidence > 0.7 else "risk_off",
|
| 86 |
}
|
| 87 |
|
| 88 |
+
def analytics_engine(econ, logistics, sentiment, lag):
|
| 89 |
projected_price = econ["price_index"] * (
|
| 90 |
+
1 + (1 - sentiment["market_confidence"]) * 0.08 - logistics["friction"] * 0.1
|
|
|
|
|
|
|
| 91 |
)
|
| 92 |
return {
|
| 93 |
"projected_price": round(projected_price, 2),
|
| 94 |
+
"lag_days": lag,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
}
|
| 96 |
|
| 97 |
# ======================================================
|
| 98 |
+
# Ω-PROOF
|
| 99 |
# ======================================================
|
| 100 |
|
| 101 |
def omega_proof(state):
|
| 102 |
+
return hashlib.sha256(json.dumps(state, sort_keys=True).encode()).hexdigest()
|
|
|
|
| 103 |
|
| 104 |
# ======================================================
|
| 105 |
+
# MASTER RUN
|
| 106 |
# ======================================================
|
| 107 |
|
| 108 |
+
def run_codexflow(commodity, anchor, lag, country, live):
|
| 109 |
+
macro = fetch_macro_anchor(country) if live else {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
"country": country,
|
| 111 |
"global_gdp": None,
|
| 112 |
"inflation": None,
|
|
|
|
| 114 |
"timestamp": time.time(),
|
| 115 |
}
|
| 116 |
|
| 117 |
+
econ = economic_kernel(commodity, anchor, macro)
|
| 118 |
logistics = logistics_engine(econ)
|
| 119 |
energy = energy_engine(econ)
|
| 120 |
sentiment = sentiment_engine(commodity)
|
| 121 |
+
projection = analytics_engine(econ, logistics, sentiment, lag)
|
|
|
|
|
|
|
|
|
|
| 122 |
|
| 123 |
state = {
|
| 124 |
+
"macro": macro,
|
| 125 |
+
"economy": econ,
|
| 126 |
"logistics": logistics,
|
| 127 |
"energy": energy,
|
| 128 |
"sentiment": sentiment,
|
| 129 |
"projection": projection,
|
|
|
|
| 130 |
}
|
| 131 |
|
| 132 |
proof = omega_proof(state)
|
|
|
|
| 133 |
|
| 134 |
global LAST_STATE
|
| 135 |
+
LAST_STATE = state
|
| 136 |
|
| 137 |
+
return state, proof
|
| 138 |
|
| 139 |
# ======================================================
|
| 140 |
+
# JARVIS X CHAT
|
| 141 |
# ======================================================
|
| 142 |
|
| 143 |
+
def jarvis_x_chat(message, history):
|
| 144 |
+
CHAT_MEMORY.append(message)
|
| 145 |
|
| 146 |
+
m = message.lower()
|
| 147 |
+
if "state" in m:
|
| 148 |
return json.dumps(LAST_STATE, indent=2) if LAST_STATE else "No state yet."
|
| 149 |
+
if "proof" in m:
|
| 150 |
+
return "Ω-Proof secures the current economic state deterministically."
|
| 151 |
+
if "what is this" in m:
|
| 152 |
+
return "CodexFlow ΩΞ is an executable economic world simulation engine."
|
| 153 |
+
if "memory" in m:
|
| 154 |
+
return f"I retain {len(CHAT_MEMORY)} interaction steps."
|
| 155 |
|
| 156 |
+
return "Jarvis X acknowledged. Ask about system state, projections, or design."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 157 |
|
| 158 |
# ======================================================
|
| 159 |
+
# GRADIO UI (HF SAFE)
|
| 160 |
# ======================================================
|
| 161 |
|
| 162 |
+
with gr.Blocks() as app:
|
| 163 |
gr.Markdown("# 🌍 CodexFlow ΩΞ")
|
| 164 |
+
gr.Markdown("**Economic World Engine + Jarvis X Interface**")
|
| 165 |
|
| 166 |
with gr.Row():
|
| 167 |
with gr.Column(scale=2):
|
|
|
|
| 170 |
value="Gold",
|
| 171 |
label="Commodity"
|
| 172 |
)
|
| 173 |
+
anchor = gr.Number(value=950, label="Physical Anchor")
|
| 174 |
+
lag = gr.Slider(1, 365, value=7, step=1, label="Reporting Lag (days)")
|
| 175 |
+
country = gr.Textbox(value="WLD", label="Country Code")
|
| 176 |
+
live = gr.Checkbox(value=True, label="Use Live Public Data")
|
| 177 |
+
|
| 178 |
+
run = gr.Button("Execute CodexFlow")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 179 |
|
| 180 |
+
state_out = gr.JSON(label="Economic State")
|
| 181 |
+
proof_out = gr.Textbox(label="Ω-Proof")
|
| 182 |
|
| 183 |
+
run.click(
|
| 184 |
+
run_codexflow,
|
| 185 |
+
inputs=[commodity, anchor, lag, country, live],
|
| 186 |
+
outputs=[state_out, proof_out],
|
| 187 |
)
|
| 188 |
|
| 189 |
with gr.Column(scale=1):
|
| 190 |
gr.Markdown("## 🧠 Jarvis X")
|
| 191 |
+
gr.ChatInterface(fn=jarvis_x_chat, height=420)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 192 |
|
| 193 |
gr.Markdown(
|
| 194 |
+
"_Simulation only. No trading, enforcement, or private systems._"
|
|
|
|
| 195 |
)
|
| 196 |
|
| 197 |
app.launch()
|