Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,9 +4,17 @@ import time
|
|
| 4 |
import hashlib
|
| 5 |
import json
|
| 6 |
import random
|
|
|
|
| 7 |
|
| 8 |
# ======================================================
|
| 9 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
# ======================================================
|
| 11 |
|
| 12 |
WORLD_BANK_BASE = "https://api.worldbank.org/v2"
|
|
@@ -28,16 +36,17 @@ def fetch_indicator(indicator, country="WLD", year="2022"):
|
|
| 28 |
except Exception:
|
| 29 |
return None
|
| 30 |
|
| 31 |
-
def fetch_macro_anchor():
|
| 32 |
return {
|
| 33 |
-
"
|
| 34 |
-
"
|
| 35 |
-
"
|
|
|
|
| 36 |
"timestamp": time.time(),
|
| 37 |
}
|
| 38 |
|
| 39 |
# ======================================================
|
| 40 |
-
# ECONOMIC
|
| 41 |
# ======================================================
|
| 42 |
|
| 43 |
def economic_kernel(commodity, physical_anchor, macro):
|
|
@@ -57,6 +66,10 @@ def economic_kernel(commodity, physical_anchor, macro):
|
|
| 57 |
"currency_flow": round(demand * price_index, 2),
|
| 58 |
}
|
| 59 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
def logistics_engine(econ):
|
| 61 |
friction = abs(econ["supply"] - econ["demand"]) / max(econ["supply"], 1)
|
| 62 |
return {
|
|
@@ -93,6 +106,23 @@ def analytics_engine(econ, logistics, energy, sentiment, lag_days):
|
|
| 93 |
"volatility_index": round(0.015 * lag_days, 4),
|
| 94 |
}
|
| 95 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
# ======================================================
|
| 97 |
# Ω-PROOF (DETERMINISTIC TRACE)
|
| 98 |
# ======================================================
|
|
@@ -101,14 +131,33 @@ def omega_proof(state):
|
|
| 101 |
canonical = json.dumps(state, sort_keys=True).encode()
|
| 102 |
return hashlib.sha256(canonical).hexdigest()
|
| 103 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
# ======================================================
|
| 105 |
# MASTER ORCHESTRATION
|
| 106 |
# ======================================================
|
| 107 |
|
| 108 |
-
def run_codexflow(
|
| 109 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
"global_gdp": None,
|
| 111 |
-
"
|
| 112 |
"population": None,
|
| 113 |
"timestamp": time.time(),
|
| 114 |
}
|
|
@@ -120,6 +169,7 @@ def run_codexflow(commodity, physical_anchor, lag_days, use_live_data):
|
|
| 120 |
projection = analytics_engine(
|
| 121 |
econ, logistics, energy, sentiment, lag_days
|
| 122 |
)
|
|
|
|
| 123 |
|
| 124 |
state = {
|
| 125 |
"macro_anchor": macro,
|
|
@@ -128,44 +178,49 @@ def run_codexflow(commodity, physical_anchor, lag_days, use_live_data):
|
|
| 128 |
"energy": energy,
|
| 129 |
"sentiment": sentiment,
|
| 130 |
"projection": projection,
|
|
|
|
| 131 |
}
|
| 132 |
|
| 133 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 134 |
|
| 135 |
# ======================================================
|
| 136 |
-
# JARVIS X CHATBOT INTERFACE
|
| 137 |
# ======================================================
|
| 138 |
|
| 139 |
def jarvis_x_chat(user_message, history):
|
| 140 |
-
|
| 141 |
|
| 142 |
-
if "
|
| 143 |
-
return (
|
| 144 |
-
"CodexFlow ΩΞ is a simulated economic world engine. "
|
| 145 |
-
"It models supply, demand, cashflow, logistics, energy, and sentiment "
|
| 146 |
-
"as a unified, auditable system."
|
| 147 |
-
)
|
| 148 |
|
| 149 |
-
if "
|
| 150 |
return (
|
| 151 |
-
"
|
| 152 |
-
"
|
|
|
|
| 153 |
)
|
| 154 |
|
| 155 |
-
if "
|
|
|
|
|
|
|
|
|
|
| 156 |
return (
|
| 157 |
"Yes. This system executes economic state transitions directly, "
|
| 158 |
-
"rather than only visualizing data.
|
| 159 |
)
|
| 160 |
|
| 161 |
-
if "
|
| 162 |
-
return (
|
| 163 |
-
"Each simulation produces an Ω-proof: a deterministic hash of the full world state."
|
| 164 |
-
)
|
| 165 |
|
| 166 |
return (
|
| 167 |
-
"Jarvis X
|
| 168 |
-
"You
|
| 169 |
)
|
| 170 |
|
| 171 |
# ======================================================
|
|
@@ -174,7 +229,7 @@ def jarvis_x_chat(user_message, history):
|
|
| 174 |
|
| 175 |
with gr.Blocks(title="CodexFlow ΩΞ") as app:
|
| 176 |
gr.Markdown("# 🌍 CodexFlow ΩΞ")
|
| 177 |
-
gr.Markdown("**Autonomous Economic World Engine + Jarvis X
|
| 178 |
|
| 179 |
with gr.Row():
|
| 180 |
with gr.Column(scale=2):
|
|
@@ -185,22 +240,26 @@ with gr.Blocks(title="CodexFlow ΩΞ") as app:
|
|
| 185 |
)
|
| 186 |
physical_anchor = gr.Number(
|
| 187 |
value=950,
|
| 188 |
-
label="Physical Anchor
|
| 189 |
)
|
| 190 |
lag_days = gr.Slider(
|
| 191 |
1, 365, value=7, step=1, label="Reporting Lag (days)"
|
| 192 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 193 |
use_live = gr.Checkbox(
|
| 194 |
value=True, label="Use Live Public Macro Data"
|
| 195 |
)
|
| 196 |
-
run_btn = gr.Button("
|
| 197 |
|
| 198 |
-
output_state = gr.JSON(label="Economic
|
| 199 |
output_proof = gr.Textbox(label="Ω-State Proof")
|
| 200 |
|
| 201 |
run_btn.click(
|
| 202 |
fn=run_codexflow,
|
| 203 |
-
inputs=[commodity, physical_anchor, lag_days, use_live],
|
| 204 |
outputs=[output_state, output_proof],
|
| 205 |
)
|
| 206 |
|
|
@@ -213,8 +272,8 @@ with gr.Blocks(title="CodexFlow ΩΞ") as app:
|
|
| 213 |
)
|
| 214 |
|
| 215 |
gr.Markdown(
|
| 216 |
-
"_CodexFlow ΩΞ is a simulation and intelligence engine
|
| 217 |
-
"not
|
| 218 |
)
|
| 219 |
|
| 220 |
app.launch()
|
|
|
|
| 4 |
import hashlib
|
| 5 |
import json
|
| 6 |
import random
|
| 7 |
+
from collections import defaultdict
|
| 8 |
|
| 9 |
# ======================================================
|
| 10 |
+
# GLOBAL MEMORY (CONVERSATION + STATE)
|
| 11 |
+
# ======================================================
|
| 12 |
+
|
| 13 |
+
CHAT_MEMORY = []
|
| 14 |
+
LAST_STATE = {}
|
| 15 |
+
|
| 16 |
+
# ======================================================
|
| 17 |
+
# PUBLIC MACRO DATA (WORLD BANK — SAFE)
|
| 18 |
# ======================================================
|
| 19 |
|
| 20 |
WORLD_BANK_BASE = "https://api.worldbank.org/v2"
|
|
|
|
| 36 |
except Exception:
|
| 37 |
return None
|
| 38 |
|
| 39 |
+
def fetch_macro_anchor(country="WLD"):
|
| 40 |
return {
|
| 41 |
+
"country": country,
|
| 42 |
+
"global_gdp": fetch_indicator(INDICATORS["GDP"], country),
|
| 43 |
+
"inflation": fetch_indicator(INDICATORS["INFLATION"], country),
|
| 44 |
+
"population": fetch_indicator(INDICATORS["POPULATION"], country),
|
| 45 |
"timestamp": time.time(),
|
| 46 |
}
|
| 47 |
|
| 48 |
# ======================================================
|
| 49 |
+
# ECONOMIC KERNEL
|
| 50 |
# ======================================================
|
| 51 |
|
| 52 |
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 {
|
|
|
|
| 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 (DETERMINISTIC TRACE)
|
| 128 |
# ======================================================
|
|
|
|
| 131 |
canonical = json.dumps(state, sort_keys=True).encode()
|
| 132 |
return hashlib.sha256(canonical).hexdigest()
|
| 133 |
|
| 134 |
+
# ======================================================
|
| 135 |
+
# FEDERATION ENVELOPE
|
| 136 |
+
# ======================================================
|
| 137 |
+
|
| 138 |
+
def federate_node(state, proof):
|
| 139 |
+
return {
|
| 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,
|
| 161 |
"population": None,
|
| 162 |
"timestamp": time.time(),
|
| 163 |
}
|
|
|
|
| 169 |
projection = analytics_engine(
|
| 170 |
econ, logistics, energy, sentiment, lag_days
|
| 171 |
)
|
| 172 |
+
network = commodity_graph(commodity)
|
| 173 |
|
| 174 |
state = {
|
| 175 |
"macro_anchor": macro,
|
|
|
|
| 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 = federated
|
| 189 |
+
|
| 190 |
+
return federated, proof
|
| 191 |
|
| 192 |
# ======================================================
|
| 193 |
+
# JARVIS X CHATBOT (SYSTEM INTERFACE)
|
| 194 |
# ======================================================
|
| 195 |
|
| 196 |
def jarvis_x_chat(user_message, history):
|
| 197 |
+
CHAT_MEMORY.append(user_message.lower())
|
| 198 |
|
| 199 |
+
if "state" in user_message.lower():
|
| 200 |
+
return json.dumps(LAST_STATE, indent=2) if LAST_STATE else "No state yet."
|
|
|
|
|
|
|
|
|
|
|
|
|
| 201 |
|
| 202 |
+
if "what is this" in user_message.lower():
|
| 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 |
# ======================================================
|
|
|
|
| 229 |
|
| 230 |
with gr.Blocks(title="CodexFlow ΩΞ") as app:
|
| 231 |
gr.Markdown("# 🌍 CodexFlow ΩΞ")
|
| 232 |
+
gr.Markdown("**Autonomous Economic World Engine + Jarvis X Master Orchestrator**")
|
| 233 |
|
| 234 |
with gr.Row():
|
| 235 |
with gr.Column(scale=2):
|
|
|
|
| 240 |
)
|
| 241 |
physical_anchor = gr.Number(
|
| 242 |
value=950,
|
| 243 |
+
label="Physical Anchor"
|
| 244 |
)
|
| 245 |
lag_days = gr.Slider(
|
| 246 |
1, 365, value=7, step=1, label="Reporting Lag (days)"
|
| 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 |
+
output_state = gr.JSON(label="Federated Economic State")
|
| 258 |
output_proof = gr.Textbox(label="Ω-State Proof")
|
| 259 |
|
| 260 |
run_btn.click(
|
| 261 |
fn=run_codexflow,
|
| 262 |
+
inputs=[commodity, physical_anchor, lag_days, country, use_live],
|
| 263 |
outputs=[output_state, output_proof],
|
| 264 |
)
|
| 265 |
|
|
|
|
| 272 |
)
|
| 273 |
|
| 274 |
gr.Markdown(
|
| 275 |
+
"_CodexFlow ΩΞ is a simulation and intelligence engine. "
|
| 276 |
+
"It does not execute trades, enforce contracts, or access private systems._"
|
| 277 |
)
|
| 278 |
|
| 279 |
app.launch()
|