Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,232 +3,252 @@ from datetime import datetime
|
|
| 3 |
from typing import Dict, List
|
| 4 |
import os
|
| 5 |
import json
|
|
|
|
| 6 |
|
| 7 |
-
# =========================
|
| 8 |
# CONFIGURATION
|
| 9 |
-
# =========================
|
| 10 |
|
| 11 |
API_KEY = os.environ.get("CODEXFLOW_API_KEY", "DEMO_KEY")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
HISTORY_FILE = "history.json"
|
|
|
|
| 13 |
|
| 14 |
-
# =========================
|
| 15 |
-
#
|
| 16 |
-
# =========================
|
| 17 |
|
| 18 |
-
def
|
| 19 |
-
if os.path.exists(
|
| 20 |
-
with open(
|
| 21 |
return json.load(f)
|
| 22 |
return []
|
| 23 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
def save_history(entry: Dict):
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
json.dump(history[-500:], f, indent=2)
|
| 29 |
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
|
| 34 |
-
def
|
| 35 |
-
if
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
-
# =========================
|
| 42 |
-
# CORE ENGINE
|
| 43 |
-
# =========================
|
| 44 |
|
| 45 |
-
def harmonize_price(
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
) -> Dict[str, float]:
|
| 50 |
-
correction = (synthetic_price - physical_anchor) * min(1.0, lag_factor)
|
| 51 |
-
harmonized = synthetic_price - correction
|
| 52 |
-
confidence = 1.0 - abs(correction) / max(synthetic_price, 1.0)
|
| 53 |
|
| 54 |
return {
|
| 55 |
-
"
|
|
|
|
| 56 |
"correction": round(-correction, 2),
|
| 57 |
-
"harmonized_price_index": round(harmonized, 2),
|
| 58 |
"confidence": round(max(0.0, min(confidence, 1.0)), 2)
|
| 59 |
}
|
| 60 |
|
| 61 |
-
def accelerate_cashflow(
|
| 62 |
-
amount: float,
|
| 63 |
-
days_delay: int,
|
| 64 |
-
confidence: float
|
| 65 |
-
) -> float:
|
| 66 |
base_rate = 0.03
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
return round(amount / ((1 +
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
|
| 75 |
def codexflow_execute(
|
| 76 |
commodity: str,
|
| 77 |
synthetic_price: float,
|
| 78 |
physical_price: float,
|
| 79 |
-
|
| 80 |
api_key: str
|
| 81 |
) -> Dict:
|
| 82 |
-
validate_api_key(api_key)
|
| 83 |
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
lag_factor
|
| 89 |
-
)
|
| 90 |
|
| 91 |
accelerated = accelerate_cashflow(
|
| 92 |
-
harmonized["
|
| 93 |
-
|
| 94 |
harmonized["confidence"]
|
| 95 |
)
|
| 96 |
|
| 97 |
result = {
|
| 98 |
-
"timestamp": datetime.utcnow().isoformat(),
|
| 99 |
"engine": "CodexFlow ΩΞ",
|
|
|
|
| 100 |
"commodity": commodity,
|
|
|
|
| 101 |
"harmonized_state": harmonized,
|
| 102 |
-
"
|
| 103 |
-
"confidence_watermark":
|
| 104 |
}
|
| 105 |
|
|
|
|
| 106 |
save_history(result)
|
| 107 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 108 |
|
| 109 |
def codexflow_batch(
|
| 110 |
commodities: List[str],
|
| 111 |
-
synthetic_prices: List[float],
|
| 112 |
-
physical_prices: List[float],
|
| 113 |
lag_days: int,
|
| 114 |
api_key: str
|
| 115 |
) -> List[Dict]:
|
|
|
|
| 116 |
validate_api_key(api_key)
|
| 117 |
outputs = []
|
| 118 |
|
| 119 |
-
for c
|
|
|
|
|
|
|
| 120 |
outputs.append(
|
| 121 |
-
codexflow_execute(c,
|
| 122 |
)
|
| 123 |
-
return outputs
|
| 124 |
|
| 125 |
-
|
| 126 |
-
# REST-LIKE API ENDPOINT
|
| 127 |
-
# =========================
|
| 128 |
-
|
| 129 |
-
def api_endpoint(payload: Dict) -> Dict:
|
| 130 |
-
return codexflow_execute(
|
| 131 |
-
payload["commodity"],
|
| 132 |
-
payload["synthetic_price"],
|
| 133 |
-
payload["physical_price"],
|
| 134 |
-
payload["reporting_lag_days"],
|
| 135 |
-
payload["api_key"]
|
| 136 |
-
)
|
| 137 |
|
| 138 |
-
# =========================
|
| 139 |
-
#
|
| 140 |
-
# =========================
|
| 141 |
|
| 142 |
-
def
|
| 143 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 144 |
|
| 145 |
-
# =========================
|
| 146 |
-
#
|
| 147 |
-
# =========================
|
| 148 |
|
| 149 |
with gr.Blocks() as demo:
|
| 150 |
-
gr.Markdown(
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
Features:
|
| 156 |
-
• API-key gated execution
|
| 157 |
-
• Batch commodity processing
|
| 158 |
-
• Confidence watermarking
|
| 159 |
-
• Historical trend memory
|
| 160 |
-
• REST-style JSON interface
|
| 161 |
-
"""
|
| 162 |
-
)
|
| 163 |
|
| 164 |
api_key = gr.Textbox(label="API Key", type="password")
|
| 165 |
|
| 166 |
-
with gr.Tab("Single
|
| 167 |
-
commodity = gr.Dropdown(
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
)
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
reporting_lag = gr.Slider(0, 30, value=7, step=1, label="Reporting Lag (Days)")
|
| 174 |
-
run_btn = gr.Button("Run CodexFlow")
|
| 175 |
-
output = gr.JSON()
|
| 176 |
|
| 177 |
-
|
| 178 |
codexflow_execute,
|
| 179 |
-
inputs=[
|
| 180 |
-
|
| 181 |
-
synthetic_price,
|
| 182 |
-
physical_price,
|
| 183 |
-
reporting_lag,
|
| 184 |
-
api_key
|
| 185 |
-
],
|
| 186 |
-
outputs=output
|
| 187 |
)
|
| 188 |
|
| 189 |
-
with gr.Tab("Batch Mode"):
|
| 190 |
-
commodities = gr.
|
| 191 |
-
|
| 192 |
-
value="Gold,Iron Ore,Crude Oil"
|
| 193 |
-
)
|
| 194 |
-
synthetic_prices = gr.Textbox(
|
| 195 |
-
label="Synthetic Prices (comma-separated)",
|
| 196 |
-
value="1000,1200,800"
|
| 197 |
-
)
|
| 198 |
-
physical_prices = gr.Textbox(
|
| 199 |
-
label="Physical Prices (comma-separated)",
|
| 200 |
-
value="950,1150,770"
|
| 201 |
-
)
|
| 202 |
-
lag_days = gr.Slider(0, 30, value=7, step=1, label="Reporting Lag (Days)")
|
| 203 |
batch_btn = gr.Button("Run Batch")
|
| 204 |
batch_out = gr.JSON()
|
| 205 |
|
| 206 |
batch_btn.click(
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
list(map(float, s.split(","))),
|
| 210 |
-
list(map(float, p.split(","))),
|
| 211 |
-
l,
|
| 212 |
-
k
|
| 213 |
-
),
|
| 214 |
-
inputs=[
|
| 215 |
-
commodities,
|
| 216 |
-
synthetic_prices,
|
| 217 |
-
physical_prices,
|
| 218 |
-
lag_days,
|
| 219 |
-
api_key
|
| 220 |
-
],
|
| 221 |
outputs=batch_out
|
| 222 |
)
|
| 223 |
|
| 224 |
-
with gr.Tab("
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 228 |
|
| 229 |
-
# =========================
|
| 230 |
# HUGGING FACE DEPLOY PATCH
|
| 231 |
-
# =========================
|
| 232 |
|
| 233 |
if __name__ == "__main__":
|
| 234 |
demo.launch(
|
|
|
|
| 3 |
from typing import Dict, List
|
| 4 |
import os
|
| 5 |
import json
|
| 6 |
+
import uuid
|
| 7 |
|
| 8 |
+
# ======================================================
|
| 9 |
# CONFIGURATION
|
| 10 |
+
# ======================================================
|
| 11 |
|
| 12 |
API_KEY = os.environ.get("CODEXFLOW_API_KEY", "DEMO_KEY")
|
| 13 |
+
LICENSE_TIERS = {
|
| 14 |
+
"DEMO_KEY": "demo",
|
| 15 |
+
"PRO_KEY": "pro",
|
| 16 |
+
"INSTITUTIONAL_KEY": "institutional"
|
| 17 |
+
}
|
| 18 |
HISTORY_FILE = "history.json"
|
| 19 |
+
AUDIT_FILE = "audit_log.json"
|
| 20 |
|
| 21 |
+
# ======================================================
|
| 22 |
+
# PERSISTENCE & GOVERNANCE
|
| 23 |
+
# ======================================================
|
| 24 |
|
| 25 |
+
def _load_file(path):
|
| 26 |
+
if os.path.exists(path):
|
| 27 |
+
with open(path, "r") as f:
|
| 28 |
return json.load(f)
|
| 29 |
return []
|
| 30 |
|
| 31 |
+
def _save_file(path, data, limit=1000):
|
| 32 |
+
with open(path, "w") as f:
|
| 33 |
+
json.dump(data[-limit:], f, indent=2)
|
| 34 |
+
|
| 35 |
+
def log_audit(entry: Dict):
|
| 36 |
+
audit = _load_file(AUDIT_FILE)
|
| 37 |
+
audit.append(entry)
|
| 38 |
+
_save_file(AUDIT_FILE, audit)
|
| 39 |
+
|
| 40 |
def save_history(entry: Dict):
|
| 41 |
+
hist = _load_file(HISTORY_FILE)
|
| 42 |
+
hist.append(entry)
|
| 43 |
+
_save_file(HISTORY_FILE, hist)
|
|
|
|
| 44 |
|
| 45 |
+
# ======================================================
|
| 46 |
+
# SECURITY / LICENSING
|
| 47 |
+
# ======================================================
|
| 48 |
|
| 49 |
+
def validate_api_key(key: str) -> str:
|
| 50 |
+
if key not in LICENSE_TIERS:
|
| 51 |
+
raise ValueError("Invalid API key")
|
| 52 |
+
return LICENSE_TIERS[key]
|
| 53 |
+
|
| 54 |
+
# ======================================================
|
| 55 |
+
# LIVE MARKET FEED (SAFE STUB)
|
| 56 |
+
# ======================================================
|
| 57 |
+
|
| 58 |
+
def fetch_live_market_price(commodity: str) -> float:
|
| 59 |
+
"""
|
| 60 |
+
Stub for live feeds.
|
| 61 |
+
Replace with exchange / data-provider API.
|
| 62 |
+
"""
|
| 63 |
+
base_prices = {
|
| 64 |
+
"Gold": 1000,
|
| 65 |
+
"Iron Ore": 1200,
|
| 66 |
+
"Crude Oil": 800
|
| 67 |
+
}
|
| 68 |
+
return base_prices.get(commodity, 1000)
|
| 69 |
|
| 70 |
+
# ======================================================
|
| 71 |
+
# CORE ECONOMIC ENGINE
|
| 72 |
+
# ======================================================
|
| 73 |
|
| 74 |
+
def harmonize_price(synthetic: float, physical: float, lag: float) -> Dict:
|
| 75 |
+
correction = (synthetic - physical) * min(1.0, lag)
|
| 76 |
+
harmonized = synthetic - correction
|
| 77 |
+
confidence = 1.0 - abs(correction) / max(synthetic, 1.0)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
|
| 79 |
return {
|
| 80 |
+
"synthetic_price": round(synthetic, 2),
|
| 81 |
+
"harmonized_price": round(harmonized, 2),
|
| 82 |
"correction": round(-correction, 2),
|
|
|
|
| 83 |
"confidence": round(max(0.0, min(confidence, 1.0)), 2)
|
| 84 |
}
|
| 85 |
|
| 86 |
+
def accelerate_cashflow(amount: float, delay_days: int, confidence: float) -> float:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
base_rate = 0.03
|
| 88 |
+
risk_adj = (1 - confidence) * 0.05
|
| 89 |
+
rate = base_rate + risk_adj
|
| 90 |
+
return round(amount / ((1 + rate) ** (delay_days / 365)), 2)
|
| 91 |
+
|
| 92 |
+
def confidence_watermark(c: float) -> str:
|
| 93 |
+
if c >= 0.9: return "AAA – High Confidence"
|
| 94 |
+
if c >= 0.75: return "AA – Medium Confidence"
|
| 95 |
+
return "A – Caution"
|
| 96 |
+
|
| 97 |
+
# ======================================================
|
| 98 |
+
# CODEX MESH INTEGRATION
|
| 99 |
+
# ======================================================
|
| 100 |
+
|
| 101 |
+
def codex_mesh_broadcast(payload: Dict):
|
| 102 |
+
"""
|
| 103 |
+
Mesh-ready broadcast stub.
|
| 104 |
+
Replace with real message bus / federation layer.
|
| 105 |
+
"""
|
| 106 |
+
payload["mesh_id"] = f"mesh-{uuid.uuid4()}"
|
| 107 |
+
payload["mesh_timestamp"] = datetime.utcnow().isoformat()
|
| 108 |
+
return payload
|
| 109 |
+
|
| 110 |
+
# ======================================================
|
| 111 |
+
# MAIN RUNTIME
|
| 112 |
+
# ======================================================
|
| 113 |
|
| 114 |
def codexflow_execute(
|
| 115 |
commodity: str,
|
| 116 |
synthetic_price: float,
|
| 117 |
physical_price: float,
|
| 118 |
+
lag_days: int,
|
| 119 |
api_key: str
|
| 120 |
) -> Dict:
|
|
|
|
| 121 |
|
| 122 |
+
tier = validate_api_key(api_key)
|
| 123 |
+
|
| 124 |
+
lag_factor = min(lag_days / 10, 1.0)
|
| 125 |
+
harmonized = harmonize_price(synthetic_price, physical_price, lag_factor)
|
|
|
|
|
|
|
| 126 |
|
| 127 |
accelerated = accelerate_cashflow(
|
| 128 |
+
harmonized["harmonized_price"],
|
| 129 |
+
lag_days,
|
| 130 |
harmonized["confidence"]
|
| 131 |
)
|
| 132 |
|
| 133 |
result = {
|
|
|
|
| 134 |
"engine": "CodexFlow ΩΞ",
|
| 135 |
+
"tier": tier,
|
| 136 |
"commodity": commodity,
|
| 137 |
+
"timestamp": datetime.utcnow().isoformat(),
|
| 138 |
"harmonized_state": harmonized,
|
| 139 |
+
"accelerated_cashflow": accelerated,
|
| 140 |
+
"confidence_watermark": confidence_watermark(harmonized["confidence"])
|
| 141 |
}
|
| 142 |
|
| 143 |
+
# Governance
|
| 144 |
save_history(result)
|
| 145 |
+
log_audit({
|
| 146 |
+
"event": "execution",
|
| 147 |
+
"tier": tier,
|
| 148 |
+
"commodity": commodity,
|
| 149 |
+
"time": result["timestamp"]
|
| 150 |
+
})
|
| 151 |
+
|
| 152 |
+
# Mesh propagation
|
| 153 |
+
return codex_mesh_broadcast(result)
|
| 154 |
+
|
| 155 |
+
# ======================================================
|
| 156 |
+
# BATCH EXECUTION
|
| 157 |
+
# ======================================================
|
| 158 |
|
| 159 |
def codexflow_batch(
|
| 160 |
commodities: List[str],
|
|
|
|
|
|
|
| 161 |
lag_days: int,
|
| 162 |
api_key: str
|
| 163 |
) -> List[Dict]:
|
| 164 |
+
|
| 165 |
validate_api_key(api_key)
|
| 166 |
outputs = []
|
| 167 |
|
| 168 |
+
for c in commodities:
|
| 169 |
+
synthetic = fetch_live_market_price(c)
|
| 170 |
+
physical = synthetic * 0.95 # conservative anchor
|
| 171 |
outputs.append(
|
| 172 |
+
codexflow_execute(c, synthetic, physical, lag_days, api_key)
|
| 173 |
)
|
|
|
|
| 174 |
|
| 175 |
+
return outputs
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 176 |
|
| 177 |
+
# ======================================================
|
| 178 |
+
# INVESTOR DECK GENERATOR
|
| 179 |
+
# ======================================================
|
| 180 |
|
| 181 |
+
def generate_investor_deck() -> Dict:
|
| 182 |
+
return {
|
| 183 |
+
"CodexFlow": {
|
| 184 |
+
"Problem": "Synthetic price distortion & cashflow delay",
|
| 185 |
+
"Solution": "Reality-anchored harmonization + time compression",
|
| 186 |
+
"Moat": "Trust, auditability, physical anchoring",
|
| 187 |
+
"Revenue": [
|
| 188 |
+
"Licensing tiers",
|
| 189 |
+
"Transaction fees",
|
| 190 |
+
"Institutional APIs"
|
| 191 |
+
],
|
| 192 |
+
"Target Clients": [
|
| 193 |
+
"Producers",
|
| 194 |
+
"Banks",
|
| 195 |
+
"Treasuries",
|
| 196 |
+
"Exchanges"
|
| 197 |
+
]
|
| 198 |
+
}
|
| 199 |
+
}
|
| 200 |
|
| 201 |
+
# ======================================================
|
| 202 |
+
# UI
|
| 203 |
+
# ======================================================
|
| 204 |
|
| 205 |
with gr.Blocks() as demo:
|
| 206 |
+
gr.Markdown("""
|
| 207 |
+
# CodexFlow ΩΞ
|
| 208 |
+
**Autonomous Reality-Anchored Cashflow Operating System**
|
| 209 |
+
""")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 210 |
|
| 211 |
api_key = gr.Textbox(label="API Key", type="password")
|
| 212 |
|
| 213 |
+
with gr.Tab("Single Run"):
|
| 214 |
+
commodity = gr.Dropdown(["Gold", "Iron Ore", "Crude Oil"])
|
| 215 |
+
synthetic_price = gr.Number(label="Synthetic Price")
|
| 216 |
+
physical_price = gr.Number(label="Physical Anchor")
|
| 217 |
+
lag = gr.Slider(0, 30, value=7, step=1)
|
| 218 |
+
btn = gr.Button("Execute")
|
| 219 |
+
out = gr.JSON()
|
|
|
|
|
|
|
|
|
|
| 220 |
|
| 221 |
+
btn.click(
|
| 222 |
codexflow_execute,
|
| 223 |
+
inputs=[commodity, synthetic_price, physical_price, lag, api_key],
|
| 224 |
+
outputs=out
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 225 |
)
|
| 226 |
|
| 227 |
+
with gr.Tab("Batch / Live Mode"):
|
| 228 |
+
commodities = gr.CheckboxGroup(["Gold", "Iron Ore", "Crude Oil"])
|
| 229 |
+
lag_b = gr.Slider(0, 30, value=7)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 230 |
batch_btn = gr.Button("Run Batch")
|
| 231 |
batch_out = gr.JSON()
|
| 232 |
|
| 233 |
batch_btn.click(
|
| 234 |
+
codexflow_batch,
|
| 235 |
+
inputs=[commodities, lag_b, api_key],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 236 |
outputs=batch_out
|
| 237 |
)
|
| 238 |
|
| 239 |
+
with gr.Tab("Investor Deck"):
|
| 240 |
+
deck_btn = gr.Button("Generate Deck")
|
| 241 |
+
deck_out = gr.JSON()
|
| 242 |
+
deck_btn.click(generate_investor_deck, outputs=deck_out)
|
| 243 |
+
|
| 244 |
+
with gr.Tab("Audit Log"):
|
| 245 |
+
audit_btn = gr.Button("View Audit")
|
| 246 |
+
audit_out = gr.JSON()
|
| 247 |
+
audit_btn.click(lambda: _load_file(AUDIT_FILE), outputs=audit_out)
|
| 248 |
|
| 249 |
+
# ======================================================
|
| 250 |
# HUGGING FACE DEPLOY PATCH
|
| 251 |
+
# ======================================================
|
| 252 |
|
| 253 |
if __name__ == "__main__":
|
| 254 |
demo.launch(
|