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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
)
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
|
|
|
|
|
|
| 33 |
)
|
| 34 |
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
| 40 |
)
|
| 41 |
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
-
|
| 45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
outputs=[output, proof]
|
| 51 |
)
|
| 52 |
|
| 53 |
-
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import requests
|
| 3 |
+
import time
|
| 4 |
+
import hashlib
|
| 5 |
+
import json
|
| 6 |
+
import random
|
| 7 |
+
|
| 8 |
+
# ======================================================
|
| 9 |
+
# PUBLIC MACRO DATA (SAFE, LEGAL, OPEN)
|
| 10 |
+
# ======================================================
|
| 11 |
+
|
| 12 |
+
WORLD_BANK_BASE = "https://api.worldbank.org/v2"
|
| 13 |
+
|
| 14 |
+
INDICATORS = {
|
| 15 |
+
"GDP": "NY.GDP.MKTP.CD",
|
| 16 |
+
"INFLATION": "FP.CPI.TOTL.ZG",
|
| 17 |
+
"POPULATION": "SP.POP.TOTL",
|
| 18 |
+
}
|
| 19 |
+
|
| 20 |
+
def fetch_indicator(indicator, country="WLD", year="2022"):
|
| 21 |
+
try:
|
| 22 |
+
url = (
|
| 23 |
+
f"{WORLD_BANK_BASE}/country/{country}/indicator/"
|
| 24 |
+
f"{indicator}?format=json&per_page=1&date={year}"
|
| 25 |
+
)
|
| 26 |
+
r = requests.get(url, timeout=8)
|
| 27 |
+
return r.json()[1][0]["value"]
|
| 28 |
+
except Exception:
|
| 29 |
+
return None
|
| 30 |
+
|
| 31 |
+
def fetch_macro_anchor():
|
| 32 |
+
return {
|
| 33 |
+
"global_gdp": fetch_indicator(INDICATORS["GDP"]),
|
| 34 |
+
"global_inflation": fetch_indicator(INDICATORS["INFLATION"]),
|
| 35 |
+
"population": fetch_indicator(INDICATORS["POPULATION"]),
|
| 36 |
+
"timestamp": time.time(),
|
| 37 |
+
}
|
| 38 |
+
|
| 39 |
+
# ======================================================
|
| 40 |
+
# ECONOMIC WORLD ENGINE (CORE)
|
| 41 |
+
# ======================================================
|
| 42 |
+
|
| 43 |
+
def economic_kernel(commodity, physical_anchor, macro):
|
| 44 |
+
gdp_scale = 1.0
|
| 45 |
+
if macro["global_gdp"]:
|
| 46 |
+
gdp_scale = macro["global_gdp"] / 1e14
|
| 47 |
+
|
| 48 |
+
supply = physical_anchor * gdp_scale
|
| 49 |
+
demand = supply * 0.95
|
| 50 |
+
price_index = round((supply / 10) * gdp_scale, 2)
|
| 51 |
+
|
| 52 |
+
return {
|
| 53 |
+
"commodity": commodity,
|
| 54 |
+
"supply": round(supply, 2),
|
| 55 |
+
"demand": round(demand, 2),
|
| 56 |
+
"price_index": price_index,
|
| 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 {
|
| 63 |
+
"throughput": round(econ["supply"] * (1 - friction), 2),
|
| 64 |
+
"friction": round(friction, 4),
|
| 65 |
+
}
|
| 66 |
+
|
| 67 |
+
def energy_engine(econ):
|
| 68 |
+
return {
|
| 69 |
+
"energy_cost_index": round(econ["price_index"] * 0.4, 2),
|
| 70 |
+
"energy_dependency": (
|
| 71 |
+
"high" if econ["commodity"].lower() in ["oil", "gas"] else "moderate"
|
| 72 |
+
),
|
| 73 |
+
}
|
| 74 |
+
|
| 75 |
+
def sentiment_engine(commodity):
|
| 76 |
+
seed = sum(ord(c) for c in commodity)
|
| 77 |
+
random.seed(seed)
|
| 78 |
+
confidence = random.uniform(0.6, 0.9)
|
| 79 |
+
return {
|
| 80 |
+
"market_confidence": round(confidence, 3),
|
| 81 |
+
"risk_bias": "neutral" if confidence > 0.7 else "risk_off",
|
| 82 |
+
}
|
| 83 |
+
|
| 84 |
+
def analytics_engine(econ, logistics, energy, sentiment, lag_days):
|
| 85 |
+
projected_price = econ["price_index"] * (
|
| 86 |
+
1
|
| 87 |
+
+ (1 - sentiment["market_confidence"]) * 0.08
|
| 88 |
+
- logistics["friction"] * 0.1
|
| 89 |
)
|
| 90 |
+
return {
|
| 91 |
+
"projected_price": round(projected_price, 2),
|
| 92 |
+
"lag_days": lag_days,
|
| 93 |
+
"volatility_index": round(0.015 * lag_days, 4),
|
| 94 |
+
}
|
| 95 |
|
| 96 |
+
# ======================================================
|
| 97 |
+
# Ω-PROOF (DETERMINISTIC TRACE)
|
| 98 |
+
# ======================================================
|
| 99 |
|
| 100 |
+
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(commodity, physical_anchor, lag_days, use_live_data):
|
| 109 |
+
macro = fetch_macro_anchor() if use_live_data else {
|
| 110 |
+
"global_gdp": None,
|
| 111 |
+
"global_inflation": None,
|
| 112 |
+
"population": None,
|
| 113 |
+
"timestamp": time.time(),
|
| 114 |
+
}
|
| 115 |
|
| 116 |
+
econ = economic_kernel(commodity, physical_anchor, macro)
|
| 117 |
+
logistics = logistics_engine(econ)
|
| 118 |
+
energy = energy_engine(econ)
|
| 119 |
+
sentiment = sentiment_engine(commodity)
|
| 120 |
+
projection = analytics_engine(
|
| 121 |
+
econ, logistics, energy, sentiment, lag_days
|
| 122 |
+
)
|
| 123 |
+
|
| 124 |
+
state = {
|
| 125 |
+
"macro_anchor": macro,
|
| 126 |
+
"economic_state": econ,
|
| 127 |
+
"logistics": logistics,
|
| 128 |
+
"energy": energy,
|
| 129 |
+
"sentiment": sentiment,
|
| 130 |
+
"projection": projection,
|
| 131 |
+
}
|
| 132 |
+
|
| 133 |
+
return state, omega_proof(state)
|
| 134 |
+
|
| 135 |
+
# ======================================================
|
| 136 |
+
# JARVIS X CHATBOT INTERFACE
|
| 137 |
+
# ======================================================
|
| 138 |
+
|
| 139 |
+
def jarvis_x_chat(user_message, history):
|
| 140 |
+
msg = user_message.lower()
|
| 141 |
+
|
| 142 |
+
if "what is this system" in msg:
|
| 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 "price" in msg:
|
| 150 |
+
return (
|
| 151 |
+
"Prices emerge from supply, demand, logistics friction, "
|
| 152 |
+
"energy costs, and macroeconomic anchors."
|
| 153 |
)
|
| 154 |
+
|
| 155 |
+
if "beyond sota" in msg:
|
| 156 |
+
return (
|
| 157 |
+
"Yes. This system executes economic state transitions directly, "
|
| 158 |
+
"rather than only visualizing data. That is beyond traditional SOTA dashboards."
|
| 159 |
)
|
| 160 |
|
| 161 |
+
if "proof" in msg:
|
| 162 |
+
return (
|
| 163 |
+
"Each simulation produces an Ω-proof: a deterministic hash of the full world state."
|
| 164 |
+
)
|
| 165 |
+
|
| 166 |
+
return (
|
| 167 |
+
"Jarvis X acknowledges. "
|
| 168 |
+
"You can ask about system design, economics, projections, or execution logic."
|
| 169 |
)
|
| 170 |
|
| 171 |
+
# ======================================================
|
| 172 |
+
# GRADIO APPLICATION
|
| 173 |
+
# ======================================================
|
| 174 |
+
|
| 175 |
+
with gr.Blocks(title="CodexFlow ΩΞ") as app:
|
| 176 |
+
gr.Markdown("# 🌍 CodexFlow ΩΞ")
|
| 177 |
+
gr.Markdown("**Autonomous Economic World Engine + Jarvis X Interface**")
|
| 178 |
+
|
| 179 |
+
with gr.Row():
|
| 180 |
+
with gr.Column(scale=2):
|
| 181 |
+
commodity = gr.Dropdown(
|
| 182 |
+
["Gold", "Oil", "Gas", "Wheat", "Copper"],
|
| 183 |
+
value="Gold",
|
| 184 |
+
label="Commodity"
|
| 185 |
+
)
|
| 186 |
+
physical_anchor = gr.Number(
|
| 187 |
+
value=950,
|
| 188 |
+
label="Physical Anchor (Base Units)"
|
| 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("Run CodexFlow Simulation")
|
| 197 |
+
|
| 198 |
+
output_state = gr.JSON(label="Economic World State")
|
| 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 |
|
| 207 |
+
with gr.Column(scale=1):
|
| 208 |
+
gr.Markdown("## 🧠 Jarvis X")
|
| 209 |
+
gr.ChatInterface(
|
| 210 |
+
fn=jarvis_x_chat,
|
| 211 |
+
chatbot_name="Jarvis X",
|
| 212 |
+
height=420
|
| 213 |
+
)
|
| 214 |
|
| 215 |
+
gr.Markdown(
|
| 216 |
+
"_CodexFlow ΩΞ is a simulation and intelligence engine, "
|
| 217 |
+
"not a trading platform or enforcement authority._"
|
|
|
|
| 218 |
)
|
| 219 |
|
| 220 |
+
app.launch()
|