Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,10 +1,45 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from datetime import datetime
|
| 3 |
-
from typing import Dict
|
|
|
|
|
|
|
| 4 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
# =========================
|
| 7 |
-
# CORE
|
| 8 |
# =========================
|
| 9 |
|
| 10 |
def harmonize_price(
|
|
@@ -12,9 +47,6 @@ def harmonize_price(
|
|
| 12 |
physical_anchor: float,
|
| 13 |
lag_factor: float
|
| 14 |
) -> Dict[str, float]:
|
| 15 |
-
"""
|
| 16 |
-
Harmonize synthetic price signals with physical reality.
|
| 17 |
-
"""
|
| 18 |
correction = (synthetic_price - physical_anchor) * min(1.0, lag_factor)
|
| 19 |
harmonized = synthetic_price - correction
|
| 20 |
confidence = 1.0 - abs(correction) / max(synthetic_price, 1.0)
|
|
@@ -26,59 +58,89 @@ def harmonize_price(
|
|
| 26 |
"confidence": round(max(0.0, min(confidence, 1.0)), 2)
|
| 27 |
}
|
| 28 |
|
| 29 |
-
|
| 30 |
def accelerate_cashflow(
|
| 31 |
amount: float,
|
| 32 |
days_delay: int,
|
| 33 |
confidence: float
|
| 34 |
) -> float:
|
| 35 |
-
"""
|
| 36 |
-
Time-value cashflow acceleration (non-speculative).
|
| 37 |
-
"""
|
| 38 |
base_rate = 0.03
|
| 39 |
risk_penalty = (1.0 - confidence) * 0.05
|
| 40 |
discount_rate = base_rate + risk_penalty
|
| 41 |
-
|
| 42 |
-
accelerated_value = amount / ((1 + discount_rate) ** (days_delay / 365))
|
| 43 |
-
return round(accelerated_value, 2)
|
| 44 |
-
|
| 45 |
|
| 46 |
# =========================
|
| 47 |
-
#
|
| 48 |
# =========================
|
| 49 |
|
| 50 |
-
def
|
| 51 |
commodity: str,
|
| 52 |
synthetic_price: float,
|
| 53 |
physical_price: float,
|
| 54 |
-
reporting_lag_days: int
|
|
|
|
| 55 |
) -> Dict:
|
| 56 |
-
|
| 57 |
-
Full CodexFlow runtime execution.
|
| 58 |
-
"""
|
| 59 |
-
lag_factor = min(reporting_lag_days / 10, 1.0)
|
| 60 |
|
|
|
|
| 61 |
harmonized = harmonize_price(
|
| 62 |
-
synthetic_price
|
| 63 |
-
|
| 64 |
-
lag_factor
|
| 65 |
)
|
| 66 |
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
)
|
| 72 |
|
| 73 |
-
|
| 74 |
-
"status": "ok",
|
| 75 |
-
"engine": "CodexFlow ΩΞ",
|
| 76 |
"timestamp": datetime.utcnow().isoformat(),
|
|
|
|
| 77 |
"commodity": commodity,
|
| 78 |
"harmonized_state": harmonized,
|
| 79 |
-
"accelerated_cashflow_estimate":
|
|
|
|
| 80 |
}
|
| 81 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
|
| 83 |
# =========================
|
| 84 |
# GRADIO UI
|
|
@@ -88,53 +150,84 @@ with gr.Blocks() as demo:
|
|
| 88 |
gr.Markdown(
|
| 89 |
"""
|
| 90 |
# CodexFlow ΩΞ Runtime
|
| 91 |
-
**Reality-Anchored Cashflow Acceleration Engine**
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
"""
|
| 96 |
)
|
| 97 |
|
| 98 |
-
|
|
|
|
|
|
|
| 99 |
commodity = gr.Dropdown(
|
| 100 |
["Gold", "Iron Ore", "Crude Oil"],
|
| 101 |
label="Commodity"
|
| 102 |
)
|
| 103 |
-
synthetic_price = gr.Number(
|
| 104 |
-
|
| 105 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
)
|
| 107 |
|
| 108 |
-
with gr.
|
| 109 |
-
|
| 110 |
-
label="
|
| 111 |
-
value=
|
| 112 |
)
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
)
|
| 120 |
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
codexflow_runtime,
|
| 126 |
-
inputs=[
|
| 127 |
-
commodity,
|
| 128 |
-
synthetic_price,
|
| 129 |
-
physical_price,
|
| 130 |
-
reporting_lag
|
| 131 |
-
],
|
| 132 |
-
outputs=output
|
| 133 |
-
)
|
| 134 |
-
|
| 135 |
|
| 136 |
# =========================
|
| 137 |
-
# HUGGING FACE PATCH
|
| 138 |
# =========================
|
| 139 |
|
| 140 |
if __name__ == "__main__":
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
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 |
+
# UTILITIES
|
| 16 |
+
# =========================
|
| 17 |
+
|
| 18 |
+
def load_history():
|
| 19 |
+
if os.path.exists(HISTORY_FILE):
|
| 20 |
+
with open(HISTORY_FILE, "r") as f:
|
| 21 |
+
return json.load(f)
|
| 22 |
+
return []
|
| 23 |
+
|
| 24 |
+
def save_history(entry: Dict):
|
| 25 |
+
history = load_history()
|
| 26 |
+
history.append(entry)
|
| 27 |
+
with open(HISTORY_FILE, "w") as f:
|
| 28 |
+
json.dump(history[-500:], f, indent=2)
|
| 29 |
+
|
| 30 |
+
def validate_api_key(key: str):
|
| 31 |
+
if key != API_KEY:
|
| 32 |
+
raise ValueError("Invalid API key")
|
| 33 |
+
|
| 34 |
+
def watermark(confidence: float) -> str:
|
| 35 |
+
if confidence >= 0.9:
|
| 36 |
+
return "HIGH CONFIDENCE ✔"
|
| 37 |
+
if confidence >= 0.75:
|
| 38 |
+
return "MEDIUM CONFIDENCE ◼"
|
| 39 |
+
return "LOW CONFIDENCE ⚠"
|
| 40 |
|
| 41 |
# =========================
|
| 42 |
+
# CORE ENGINE
|
| 43 |
# =========================
|
| 44 |
|
| 45 |
def harmonize_price(
|
|
|
|
| 47 |
physical_anchor: float,
|
| 48 |
lag_factor: float
|
| 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)
|
|
|
|
| 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 |
risk_penalty = (1.0 - confidence) * 0.05
|
| 68 |
discount_rate = base_rate + risk_penalty
|
| 69 |
+
return round(amount / ((1 + discount_rate) ** (days_delay / 365)), 2)
|
|
|
|
|
|
|
|
|
|
| 70 |
|
| 71 |
# =========================
|
| 72 |
+
# SINGLE & BATCH RUNTIME
|
| 73 |
# =========================
|
| 74 |
|
| 75 |
+
def codexflow_execute(
|
| 76 |
commodity: str,
|
| 77 |
synthetic_price: float,
|
| 78 |
physical_price: float,
|
| 79 |
+
reporting_lag_days: int,
|
| 80 |
+
api_key: str
|
| 81 |
) -> Dict:
|
| 82 |
+
validate_api_key(api_key)
|
|
|
|
|
|
|
|
|
|
| 83 |
|
| 84 |
+
lag_factor = min(reporting_lag_days / 10, 1.0)
|
| 85 |
harmonized = harmonize_price(
|
| 86 |
+
synthetic_price,
|
| 87 |
+
physical_price,
|
| 88 |
+
lag_factor
|
| 89 |
)
|
| 90 |
|
| 91 |
+
accelerated = accelerate_cashflow(
|
| 92 |
+
harmonized["harmonized_price_index"],
|
| 93 |
+
reporting_lag_days,
|
| 94 |
+
harmonized["confidence"]
|
| 95 |
)
|
| 96 |
|
| 97 |
+
result = {
|
|
|
|
|
|
|
| 98 |
"timestamp": datetime.utcnow().isoformat(),
|
| 99 |
+
"engine": "CodexFlow ΩΞ",
|
| 100 |
"commodity": commodity,
|
| 101 |
"harmonized_state": harmonized,
|
| 102 |
+
"accelerated_cashflow_estimate": accelerated,
|
| 103 |
+
"confidence_watermark": watermark(harmonized["confidence"])
|
| 104 |
}
|
| 105 |
|
| 106 |
+
save_history(result)
|
| 107 |
+
return result
|
| 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, s, p in zip(commodities, synthetic_prices, physical_prices):
|
| 120 |
+
outputs.append(
|
| 121 |
+
codexflow_execute(c, s, p, lag_days, api_key)
|
| 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 |
+
# DASHBOARD HELPERS
|
| 140 |
+
# =========================
|
| 141 |
+
|
| 142 |
+
def get_history():
|
| 143 |
+
return load_history()
|
| 144 |
|
| 145 |
# =========================
|
| 146 |
# GRADIO UI
|
|
|
|
| 150 |
gr.Markdown(
|
| 151 |
"""
|
| 152 |
# CodexFlow ΩΞ Runtime
|
| 153 |
+
**Autonomous Reality-Anchored Cashflow Acceleration Engine**
|
| 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 Execution"):
|
| 167 |
commodity = gr.Dropdown(
|
| 168 |
["Gold", "Iron Ore", "Crude Oil"],
|
| 169 |
label="Commodity"
|
| 170 |
)
|
| 171 |
+
synthetic_price = gr.Number(label="Synthetic Price Index", value=1000)
|
| 172 |
+
physical_price = gr.Number(label="Physical Price Anchor", value=950)
|
| 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 |
+
run_btn.click(
|
| 178 |
+
codexflow_execute,
|
| 179 |
+
inputs=[
|
| 180 |
+
commodity,
|
| 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.Textbox(
|
| 191 |
+
label="Commodities (comma-separated)",
|
| 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 |
+
lambda c, s, p, l, k: codexflow_batch(
|
| 208 |
+
c.split(","),
|
| 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("Historical Trends"):
|
| 225 |
+
hist_btn = gr.Button("Load History")
|
| 226 |
+
hist_out = gr.JSON()
|
| 227 |
+
hist_btn.click(get_history, outputs=hist_out)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 228 |
|
| 229 |
# =========================
|
| 230 |
+
# HUGGING FACE DEPLOY PATCH
|
| 231 |
# =========================
|
| 232 |
|
| 233 |
if __name__ == "__main__":
|